Massimiliano Oreto
Massimiliano Oreto

Reputation: 67

Nestjs with Typeorm No migrations are pending

i have a fresh database, i generate migration and then i try to run it but this is the result:

query: SELECT VERSION() AS version query: SELECT VERSION() AS version Data Source has been initialized! query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'laps5' AND TABLE_NAME = 'migrations' query: SELECT * FROM testdb.migrations migrations ORDER BY id DESC No migrations are pending

It work correctly until i have updated my package.json and now it can't work.

This is my config:

export const datasourceOptions: DataSourceOptions = {
type: 'mysql',
host: 'localhost',
username: 'xxxx',
password: 'xxxxxx',
synchronize: false,
entities: [Token, UserAnag, User],
migrations: ['/migrations/*.js'],
database: process.env.DATABASE,
};

My migration folder is on root of project. I also must change entities option from:

  entities: [__dirname + '/src/entities/*.js'],

to

 entities: [Token, UserAnag, User]

because the generate command return to me "No changes in database schema were found"

So actually i can create migration but if i try to run it doesn't work. I search and try a lot of solution, like change my migration folder position into dist and build solution before launch migration but without it work

The command i use to run migration is:

npm run typeorm migration:run -- -d ./db.datasource.ts

where typeorm is defined as

cross-env NODE_ENV=development typeorm-ts-node-commonjs

In my Production system, it seem's work correctly. Locally none.

Upvotes: 0

Views: 1341

Answers (2)

Ahmed El-Tabarani
Ahmed El-Tabarani

Reputation: 763

In my case, there was a bug in TypeORM 0.3.12

Check this:
https://github.com/typeorm/typeorm/issues/9860
https://github.com/typeorm/typeorm/issues/9766

The solution, as mentioned there,
is to downgrade to 0.3.11 and wait for the problem to be fixed.

npm i [email protected]

Upvotes: 0

Dmitrii Tkachenko
Dmitrii Tkachenko

Reputation: 586

You can actually fix it the same way you did with the entities, although it becomes harder to manage once your project grows.

For the entities I would suggest to create a barrel index file to export all your entities, so you can:

import * as entitiesMap from '@project-name/entities';

const entities = Object.values(entitiesMap);

I have entities defined in a lib, read more here NestJS docs.

As for the migrations: TypeOrm can't locate them. Long story short: it tries to resolve them starting from the root and finds the folder with .ts migrations. To get to the .js you will need to amend the path __dirname + '/**/migrations/*.js' or __dirname + '*/**/migrations/*.js'.

Try adding console log of the resulting path in your config.

Upvotes: 0

Related Questions