#Migration fails

1 messages · Page 1 of 1 (latest)

lofty flame
#

Hi, I'm trying to use migration of typorm 0.3.12, but its not working.
my app.module

TypeOrmModule.forRootAsync({
      name: DB_NAMES.MIA,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService): TypeOrmModuleOptions => {
        return {
          type: 'mssql',
          host: configService.get<string>('DATABASE_HOST'),
          username: configService.get<string>('DATABASE_USER'),
          password: configService.get<string>('DATABASE_PASSWORD'),
          database: configService.get<string>('DATABASE_NAME'),
          entities: [SecurityUser, Session, SecurityGroup, Dictionary],
          migrations: [SecurityUserDefault],
          synchronize: true,
          options: {
            encrypt: false,
          },
          logging: true,
        };
      },
      dataSourceFactory: async (options) => {
        const dataSource = await new DataSource(options).initialize();
        return dataSource;
      },
    }),

migration file

export class SecurityUserDefault implements MigrationInterface {
  name: 'SecurityUserDefault';
  async up(queryRunner) {
    await queryRunner.query(
      `INSERT INTO ...`,
    );
  }

  async down(queryRunner) {
    await queryRunner.query(
      `DELETE FROM ...`,
    );
  }
};

when it try to run:
npx typeorm migration:run

i get Missing required argument: dataSource

i don't have a separate file for ds config, does that mean I can't use migration importing typeorm this way?

lofty flame
dapper mantle
lofty flame
dapper mantle
lofty flame
#

i get this error as soon as i run the app

#

npm run start:dev

dapper mantle
#

If you set it this way the configuration without Datasource

TypeOrmModule.forRootAsync({
      name: DB_NAMES.MIA,
      imports: [ConfigModule],
      inject: [configService],
      useFactory: (configService: ConfigService): TypeOrmModuleOptions => {
        return {
          type: 'mssql',
          host: configService.get<string>('DATABASE_HOST'),
          username: configService.get<string>('DATABASE_USER'),
          password: configService.get<string>('DATABASE_PASSWORD'),
          database: configService.get<string>('DATABASE_NAME'),
          entities: [SecurityUser, Session, SecurityGroup, Dictionary],
          migrations: [SecurityUserDefault],
          synchronization: true,
          options: {
            encrypt: false,
          },
          logging: true,
        };
      },
    }),```Do you always have the error?
lofty flame
#

i only get the error when i import

 NestTypeOrmCommandsModule.forRoot({
      migrationsDir: 'src/migrations',
    }),
dapper mantle
#

try setting it like this:

TypeOrmModule.forRootAsync({
      name: DB_NAMES.MIA,
      imports: [ConfigModule],
      inject: [configService],
      useFactory: (configService: ConfigService) => ({
        type: 'mssql',
        host: configService.get<string>('DATABASE_HOST'),
        port: configService.get<string>('DATABASE_PORT'),
        username: configService.get<string>('DATABASE_USER'),
        password: configService.get<string>('DATABASE_PASSWORD'),
        database: configService.get<string>('DATABASE_NAME'),
        entity: [__dirname + '/**/*.entity.{ts,js}'],
        migrations: ['dist/migrations/**/*.js'],
        synchronization: true,
        logging: true,
        cli: {
          migrationsDir: 'src/migrations',
        }
      }),
    }),

without using the NestTypeOrmCommandsModule

lofty flame
#

seems like im still missing the datasource
when i run npm run migration:generate test

current context)```
dapper mantle
#

very strange, if you can, create a minimal reproduction so I can take a look at it.

lofty flame
#

minimal project for test

#

im using sql express so change the .env and the typeorm config

dapper mantle
#

I managed to start the app, as a database I'm using mysql , but I still can't get the migrations to work, if I can solve it I'll write to you here. It should be a sequence of commands, i just need to remember exactly which ones to do before and after.

dapper mantle
lofty flame
# dapper mantle There seems to be a bug on the cli of typeorm with version 0.3.12: https://githu...

ok so i got migration working by reverting to 0.3.11
using this npm run typeorm -- -d ./typeorm.config.ts migration:generate ./src/migrations/%npm_config_name%
thanks.

nest command, however, seem to still not able to find the DataSource :

[Error]: Nest could not find DataSource element (this provider does not exist in the 
current context)

perhaps the typeorm-cli.config.ts needs to be registered somewhere?

dapper mantle
#

Try adding this in typeorm-cli.config.ts:
migrations: [], // add migrations path

lofty flame
#

sadly im still getting the same error.
i can just work with typeorm cli, so its all good