Search code examples
database-migrationtypeorm

Using TypeORM migrations, how do I specify a particular connection?


I'm using TypeORM and trying to run a migration on a test connection. In my ormconfig.json, I specify two separate connections as follows:

[{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "username",
  "password": "",
  "database": "database",
  "entities": [
    "build/entity/**/*.js"
  ],
  "migrations": [
    "build/migration/**/*.js"
  ],
  "synchronize": false,
  "autoSchemaSync": true,
  "logging": false,
  "cli": {
    "migrationsDir": "src/migration",
    "entitiesDir": "src/entity",
    "subscribersDir": "src/subscriber"
  }
},
{
  "name": "test",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "username",
  "password": "",
  "database": "database-test",
  "entities": [
    "build/entity/**/*.js"
  ],
  "migrations": [
    "build/migration/**/*.js"
  ],
  "synchronize": false,
  "autoSchemaSync": true,
  "logging": false,
  "cli": {
    "migrationsDir": "src/migration",
    "entitiesDir": "src/entity",
    "subscribersDir": "src/subscriber"
  }
}]

How do I specify the connection with name test from the TypeORM CLI? I'm trying things like:

typeorm migrations:run -c test

but I'm not having any luck. Is there a better way to do this?


Solution

  • Though you said you weren't having luck with that, that's exactly what I do. Perhaps some supporting information will help. My exact migration command looks like this (I'm using TypeScript and so I'm running things through ts-node first):

    $(npm bin)/ts-node $(npm bin)/typeorm migration:run -c test
    

    in my ormconfig.json, I've specified that there's a default and test connection, just like yours.

    Perhaps it's as simple as saying "migration" rather than "migrations" like you have? When I use "migrations" I just get the help information printed out. Is that what you're seeing?