user3182518
user3182518

Reputation: 227

Migrations been added to root folder not migration folder

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',
  },
})

Upvotes: 4

Views: 14942

Answers (7)

swaap
swaap

Reputation: 71

The solution for me was to add filename prefix. say i want to generate migrations with names migration-xxxxx in migrations folder.

then instead of

npm run typeorm -- --dataSource=src/database/data-source.ts migration:generate src/database/migrations

i will have to run

npm run typeorm -- --dataSource=src/database/data-source.ts migration:generate src/database/migrations/migration

Upvotes: 0

Diego Guedes
Diego Guedes

Reputation: 31

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"
},

Upvotes: 2

PedroMiotti
PedroMiotti

Reputation: 1124

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

Upvotes: 5

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;

Upvotes: -1

bashleigh
bashleigh

Reputation: 9324

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!

Upvotes: 4

Aliaksei Litsvin
Aliaksei Litsvin

Reputation: 1261

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.

Upvotes: 2

viniciusjssouza
viniciusjssouza

Reputation: 1275

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

Upvotes: 3

Related Questions