简体   繁体   中英

TypeORM: "No migrations pending" when attempting to run migrations manually

I have a new web app and I've written a migrator to create a user table. However, no matter what I try, typeorm does not appear to find this migrator and hence, does not run it.

My file structure (other files/folders not shown):

├── Server
│   ├── dist
|   |   ├── Migrations
|   |   |   ├── 1234567891234567890-AddUserTable.js
|   |   |   ├── 1234567891234567890-AddUserTable.js.map
|   |   |   ├── 1234567891234567890-AddUserTable.d.ts
│   ├── src
|   |   ├── Migrations
|   |   |   ├── 1234567891234567890-AddUserTable.ts
|   |   ├── app.module.ts

app.module.ts

@Module({
    imports: [
        ConfigModule.forRoot({ envFilePath: '.env' }),
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                type: 'mysql',
                host: configService.get('TYPEORM_HOST'),
                port: +configService.get<number>('TYPEORM_PORT'),
                username: configService.get('TYPEORM_USERNAME'),
                password: configService.get('TYPEORM_PASSWORD'),
                database: configService.get('TYPEORM_DATABASE'),
                synchronize: configService.get('TYPEORM_SYNCHRONIZE'),
                entities: [__dirname + '/**/*.entity{.ts,.js}'],
                migrations: [__dirname + '/Migrations/**/*.js'],
                migrationsRun: false,
                cli: {
                    migrationsDir: './Migrations',
                },
            }),
            inject: [ConfigService],
        }),
    ],
    controllers: [],
    providers: [],
})
export class AppModule {
    constructor(private connection: Connection) {}
}

In order to run this, in my console window, I type: nest start in order get my Server started.

Then, I run npx typeorm migration:run which I get:

query: SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'myDB' AND `TABLE_NAME` = 'migrations'
query: SELECT * FROM `myDB`.`migrations` `migrations` ORDER BY `id` DESC
No migrations are pending

If I look in my DB, I see a migrations table with no entries inside.

I have tried to delete my migrator file and create it again with a more recent timestamp and that does not work either.

npx typeorm migration:create -n "MyMigratorName"

Any help would be greatly appreciated.

For anyone coming here for help:

The config above mentions migrations to be found in js files:

migrations: [__dirname + '/Migrations/**/*.js'],

However from the folder structure its clear that migrations are written in ts files not js.

To run migrations from ts follow the officially recommended way described here:

https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md#if-entities-files-are-in-typescript

Also in that case don't forget to update the migrations blob to be ts:

migrations: [__dirname + '/Migrations/**/*.ts'],

If you want to run the migrations from js files you will have to provide the location to be from dist folder after the ts files have been compiled to js form.

Typeorm only loads migration from files with js extension,

In the ormconfig file where you set up your connection;

{
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "test",
    "password": "test",
    "database": "test",
    "entities": ["entity/*.js"],
    "migrationsTableName": "custom_migration_table",
    "migrations": ["migration/*.js"],
    "cli": {
        "migrationsDir": "migration"
    }
}

"migrations": ["migration/*.js"] - indicates that typeorm will load migrations from the given "migration" directory.

if your migration folder is stored in a dist or src folder,

"migrations": ["migration/*.js"] will now be "migrations": ["dist/migration/*.js"] or "migrations": ["src/migration/*.js"] , the * means any migration filename.

"migrationsDir": "migration" - indicates that the CLI must create new migrations in the "migration" directory.

Please note that if your migration files have typescript extensions(.ts), you have to compile your migrations files into javascript files if not typeorm will not find them.

In addition to answer given by @Upka Uchechi

If you're using typescript, you need to run the following commands

$ npm run build   # This will compile the typescript files into JavaScript files 
$ npm run typeorm -- migration:run

To give a bit more context. If your configuration files are lookated at <PROJECT ROOT>/db-migrations , the build command above should move the compiled JavaScript files to <PROJECT ROOT>/dist/db-migrations . For that, your configuration file would look like this

  ... 
  "migrations": ["dist/db-migrations/*.js"],
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM