You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migration performance correlated to number of migration files #4136
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
We've got a pretty large TS project with 50 migration files and have been experiencing a gradual slowdown in running any migration.
For example yarn typeorm -f .ormconfig migration:run used to take just under 10 seconds a few months ago, but now its takin 120 seconds even if there are no migrations to run.
Similarly, generating migrations has been slowing down and is also now taking 120 seconds.
Some of the slowdown is obviously because of increase TS compile time of the project, but there is a distinct correlation between the performance of the migrator and the number of migration files in the project.
Deleting all but the latest migration files will decrease the 120-second migration time to 15 seconds. The TS compile time for these migrations is just a few seconds, so it seems that TypeORM is doing some unnecessary work for each added migration file.
Reacted by Alex, Doron Tohar, Jack Maris, Kevin van der Burgt, Asarew, Sammy Teillet, Miha, Jori Lallo, Adam Bar, Daniel Meechan and 23 more
I was noticing this same problem and tried to dig into what was causing the issue.
Ultimately each migration script was getting require()'d sequentially within TypeORM code. The require was taking about a half second per script, which makes sense if it was having to resolve and compile each from scratch.
I also noticed that in my tsconfig.json I wasn't including the migration directory. I ended up adding "migration/**/*.ts" to the include directive, and now it's loading fast. Hopefully that resolves what you're seeing as well.
Reacted by Aleksander Barszczewski and Ivan Rubinson
I am having this problem too, and it's painful continue to work like this. Our project has 110+ migrations, using ts-node and the migration folder on tsconfig.json. The startup on the project is affected as well, mainly when starting the conections with createConnection(). I don't have a clue how to procede.
I've managed to get a CPU profile of it happening, but then if I subsequently call migration:run again in a separate process it's fast. I suspect because some caching is going on either at the node / v8 level.
I'm having this issue too, we currently have ~70 migrations and it takes times to finish (up to 2-3 minutes on slow cpu).
I'v managed to fix this problem using the builded javascript (*.js), it now takes few seconds.
createConnection is faster with javascript files (< 100ms) vs typescript (~ 20 secs)
Reacted by James Judd, Jack Maris, Aleksander Barszczewski, Miha, Yevgen Brizha, Léo Eichhorn and Andy
Something what I don't understand is why createConnection() reads migration files - the method itself does not need them AFAIK. They are only needed by migration commands.
My current workaround is:
// ormconfig.tsmodule.exports={name: 'default',type: 'postgres',uuidExtension: 'pgcrypto',synchronize: false,migrations: process.env.SKIP_MIGRATIONS ? [] : ['src/migrations/*.js','src/migrations/*.ts'],}// package.json{"scripts": {"start": "SKIP_MIGRATIONS=1 yarn nodemon --exec node -r module-alias/register -r ts-node/register ./src/containers/api.ts",// used to start app locally for development}}
Now createConnection() takes 5 sec instead of 55 sec (there are 74 migrations in the project) when starting app locally.
Would be nice if only migration commands read migration files out of the box.
Have the same problem with createConnection() taking about 50 seconds -- I have 42 migrations. If I set TYPEORM_MIGRATIONS=[] it's essentially instant (less than a second).
We actually solved these problems by adding the --transpile-only ts-node flag to the npm script that runs the migrations. Reduced our run time ten-fold and is pretty safe as you don't really need to check your typescript syntax when you run migrations. Then we also squashed all of our migrations, which gave us even more performance improvements. All in all, these two changes got us from 120 second runs down to 4 seconds.
Reacted by Donald Hruska, Vincent François, ALT, Niccolò Fanton, Daniel Cox, José Joaquín Bocanegra, Fabio, Phongsakorn, Rost, Jakob Murko and 5 more
Reacted by strowk
Reacted by ALT, Mark Piper, Niccolò Fanton, Daniel Cox, José Joaquín Bocanegra, lucasvieceli, Nicholas Mota Ferreira and Maksim
Reacted by Niccolò Fanton, José Joaquín Bocanegra, Fabio, Léo Eichhorn, lucasvieceli, Nicholas Mota Ferreira and Bruno Damacena
Unrelated to this issue, but when I try --transpile-only with ts-node, I get an error with the Postgres driver:
TypeError: Cannot read property 'connect' of undefined
File "src/driver/postgres/PostgresDriver.ts", line 774, col 25, in null.<anonymous>
?, in new Promise
File "src/driver/postgres/PostgresDriver.ts", line 773, col 16, in PostgresDriver.obtainMasterConnection
File "src/driver/postgres/PostgresQueryRunner.ts", line 90, col 58, in PostgresQueryRunner.connect
File "src/driver/postgres/PostgresQueryRunner.ts", line 163, col 55, in PostgresQueryRunner.<anonymous>
File "node_modules/tslib/tslib.js", line 133, col 27, in step
op = body.call(thisArg, _);
File "node_modules/tslib/tslib.js", line 114, col 57, in Object.next
function verb(n) { return function (v) { return step([n, v]); }; }
File "node_modules/tslib/tslib.js", line 107, col 75, in null.<anonymous>
step((generator = generator.apply(thisArg, _arguments || [])).next());
?, in new Promise
File "node_modules/tslib/tslib.js", line 103, col 16, in Object.__awaiter
return new (P || (P = Promise))(function (resolve, reject) {
Something what I don't understand is why createConnection() reads migration files - the method itself does not need them AFAIK. They are only needed by migration commands.
My current workaround is:
// ormconfig.tsmodule.exports={name: 'default',type: 'postgres',uuidExtension: 'pgcrypto',synchronize: false,migrations: process.env.SKIP_MIGRATIONS ? [] : ['src/migrations/*.js','src/migrations/*.ts'],}// package.json{"scripts": {"start": "SKIP_MIGRATIONS=1 yarn nodemon --exec node -r module-alias/register -r ts-node/register ./src/containers/api.ts",// used to start app locally for development}}
Now createConnection() takes 5 sec instead of 55 sec (there are 74 migrations in the project) when starting app locally.
Would be nice if only migration commands read migration files out of the box.
The migrations can be set to run when connecting:
migrationsRun - Indicates if migrations should be auto run on every application launch. As an alternative, you can use CLI and run migration:run command.
Based on the comments in this thread and the root cause of the issue, the workaround I did was to build the project first (transpile to js) and then run the migrations using the js files.
Run time on an already migrated database went down from minutes to seconds.
I've pointed the migrations config setting to the JS output files from our typescript build but still waiting 3+ minutes for 43 migrations to be checked (even when none of them need to be applied). Are there any other steps folks have taken for this to work?
Update: using the transpile-only option mentioned above had much more significant impact for us (down to a few seconds).
Activity
ideodora commentedon May 15, 2019
same here
pwwolf commentedon Jul 10, 2019
I was noticing this same problem and tried to dig into what was causing the issue.
Ultimately each migration script was getting
require()'d sequentially within TypeORM code. The require was taking about a half second per script, which makes sense if it was having to resolve and compile each from scratch.I also noticed that in my tsconfig.json I wasn't including the migration directory. I ended up adding "migration/**/*.ts" to the include directive, and now it's loading fast. Hopefully that resolves what you're seeing as well.
ygorlazaro commentedon Jul 13, 2019
I am having this problem too, and it's painful continue to work like this. Our project has 110+ migrations, using ts-node and the migration folder on tsconfig.json. The startup on the project is affected as well, mainly when starting the conections with
createConnection(). I don't have a clue how to procede.Juddling commentedon Sep 12, 2019
Can anyone consistently reproduce this issue?
I've managed to get a CPU profile of it happening, but then if I subsequently call
migration:runagain in a separate process it's fast. I suspect because some caching is going on either at the node / v8 level.alermant commentedon Sep 13, 2019
I'm having this issue too, we currently have ~70 migrations and it takes times to finish (up to 2-3 minutes on slow cpu).
I'v managed to fix this problem using the builded javascript (*.js), it now takes few seconds.
createConnectionis faster with javascript files (< 100ms) vs typescript (~ 20 secs)alekbarszczewski commentedon Dec 12, 2019
Something what I don't understand is why
createConnection()reads migration files - the method itself does not need them AFAIK. They are only needed by migration commands.My current workaround is:
Now
createConnection()takes 5 sec instead of 55 sec (there are 74 migrations in the project) when starting app locally.Would be nice if only migration commands read migration files out of the box.
pbn4 commentedon Apr 6, 2020
@pleerock Are there any updates to this?
mtdewulf commentedon Apr 20, 2020
Have the same problem with
createConnection()taking about 50 seconds -- I have 42 migrations. If I setTYPEORM_MIGRATIONS=[]it's essentially instant (less than a second).artman commentedon Apr 20, 2020
We actually solved these problems by adding the
--transpile-onlyts-node flag to the npm script that runs the migrations. Reduced our run time ten-fold and is pretty safe as you don't really need to check your typescript syntax when you run migrations. Then we also squashed all of our migrations, which gave us even more performance improvements. All in all, these two changes got us from 120 second runs down to 4 seconds.mtdewulf commentedon Apr 20, 2020
Unrelated to this issue, but when I try
--transpile-onlywith ts-node, I get an error with the Postgres driver:Juddling commentedon Apr 21, 2020
The migrations can be set to run when connecting:
https://typeorm.io/#/connection-options/common-connection-options
anthonylau commentedon Apr 29, 2020
@artman
Would you mind sharing how you squash migrations? Was that done manually?
tylergets commentedon Aug 13, 2020
For us the simple workaround is currently
if (!ormOptions.migrationsRun) { ormOptions.migrations = []; }daramasala commentedon Sep 30, 2020
Based on the comments in this thread and the root cause of the issue, the workaround I did was to build the project first (transpile to js) and then run the migrations using the js files.
Run time on an already migrated database went down from minutes to seconds.
Performance improvement to dev xp (#9294)
jamescrowley commentedon Feb 20, 2025
I've pointed the migrations config setting to the JS output files from our typescript build but still waiting 3+ minutes for 43 migrations to be checked (even when none of them need to be applied). Are there any other steps folks have taken for this to work?
Update: using the transpile-only option mentioned above had much more significant impact for us (down to a few seconds).
clintonb commentedon Feb 20, 2025
@jamescrowley using SWC for compilation drastically decreased waiting time for us.
https://swc.rs/
dsfcoll commentedon May 26, 2025
I found the problem.
For every migration file, TypeORM tries to find the nearest package.json:
https://github.com/typeorm/typeorm/blob/master/src/util/ImportUtils.ts#L29
This is an expensive operation, and performance degrades linearly with the number of migration files.
Suggested Solutions:
getNearestPackageJsonfunction can be rewritten to use a synchronous approach.For example, in this case:
We can cache the result for
/apps/db/src/migrationsinstead of looking it up for every individual file.vKolerts commentedon Jun 18, 2025
up
perf: Cache package.json location between invocation
perf: Cache package.json location between getNearestPackageJson invoc…
perf: Cache package.json location between getNearestPackageJson invoc…
livanov commentedon Mar 30, 2026
No change in performance with version
0.3.28. Is this released, actually?