Migrations been added to root folder not migration folder

Viewed 10367

I am trying to set up migrations for Nestjs TypeORM, in my TypeOrmModule.forRoot() i have added the desired folder for the migrations, but it keeps adding the migrations to the root folder.

TypeOrmModule.forRoot({
  type: 'mssql',
  host: 'test',
  port: 1,
  username: 'test',
  password: 'test',
  database: 'test',
  entities: [__dirname + '/**/entities/*{.ts,.js}'],
  synchronize: false,
  options: {
    useUTC: true,
  },
  migrations: [__dirname + '/**/migration/*.ts'],
  cli: {
    migrationsDir: __dirname + '/**/migration',
  },
})
6 Answers

I had the same issue, I just added the -d option in the CLI command to specify the directory , like this :

ts-node ./node_modules/typeorm/cli.js migration:generate -n migration -d src/infrastructure/migrations

I think this might be because of when you create a migration you're probably using the typeorm package right? (like so typeorm migration:create -n PostRefactoring). Which will use an entirely different config to that which you've specified in your nest application. The easiest way I suppose would be to create an env file and use TYPEORM_MIGRATIONS_DIR to define your migration directory.

See here for available env options http://typeorm.io/#/using-ormconfig/using-environment-variables. You can then link up your envs with your application so they're defined in one place.

I don't want to be that person that goes around advertising their own packages, you could easily achieve your own setup if you wish. I built a config module which you can use for typeorm configs like so

https://github.com/nestjs-community/nestjs-config#typeorm

import {Module} from '@nestjs/common';
import {ConfigModule, ConfigService} from 'nestjs-config';
import {TypeOrmModule} from '@nestjs/typeorm';
import * as path from 'path';

@Module({
    imports: [
        ConfigModule.load(path.resolve(__dirname, 'config/**/*.{ts,js}')),
        TypeOrmModule.forRootAsync({
            useFactory: (config: ConfigService) => config.get('database'),
            inject: [ConfigService],
        }),
    ],
})
export class AppModule {}

This would allow you to define your configs in a file like so

//src/config/database.ts
export default {
    type: 'mssql',
    host: process.env.TYPEORM_HOST,
    port: process.env.TYPEORM_PORT,
    username: process.env.TYPEORM_USERNAME,
    password: process.env.TYPEORM_PASSWORD,
    database: process.env.TYPEORM_DATABASE,
    entities: [process.env.TYPEORM_ENTITIES],
    synchronize: process.env.TYPEORM_SYNCHRONIZE == 'true',
    migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR
};

Then your .env

TYPEORM_HOST=test
TYPEORM_USERNAME=test
TYPEORM_PASSWORD=test
TYPEORM_PORT=1
TYPEORM_MIGRATIONS_DIR=src/migrations

Now you'll be able to use the typeorm command and you'll still have your database configs defined in one place. Hope this helps!

I had the same problem and, for me, the issue was the connection name. My connection config was not using "default" as name, so we needed to pass its name to typeorm cli, like this:

typeorm migration:create -n MyMigration -c my-connection-name

Just share my experience. I have typeorm 0.2.12

  1. I've configured typeorm script as described here

  2. Created ormconfig.ts that exported config: export = config; (exactly)

  3. Set migrationsDir without __dirname just src/migrations

And it work for me.

In typeorm 0.3, I run:

npm run typeorm migration:create destination_path\name_migration 

My package.json looks like this:

"scripts": {    
  "typeorm": "typeorm-ts-node-commonjs"
},

CLI command doesn't see (and doesn't know anything about exported name typeOrmConfig, so, it used default exported config if there is any). So, to make it work, the config should be exported by default. But because export default is a bad pattern, I rarely use it.

My example:

  // typeorm.config.ts file

  // Old code
  export const typeOrmConfig: TypeOrmModuleOptions = {
    type: 'postgres',
    host: 'localhost' || process.env.DB_HOST,
    ...
  }

  // New code, make config visible for CLI commands
  module.exports = typeOrmConfig;
Related