Skip to content

Migration performance correlated to number of migration files #4136

Closed

Description

@artman

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[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.

Activity

  1. ideodora commented on May 15, 2019

    @ideodora

    same here

  2. pwwolf commented on Jul 10, 2019

    @pwwolf

    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.

  3. ygorlazaro commented on Jul 13, 2019

    @ygorlazaro

    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.

  4. Juddling commented on Sep 12, 2019

    @Juddling
    Contributor

    Can anyone consistently reproduce this issue?

    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.

  5. alermant commented on Sep 13, 2019

    @alermant

    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)

  6. alekbarszczewski commented on Dec 12, 2019

    @alekbarszczewski

    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.ts
    
    module.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.

  7. pbn4 commented on Apr 6, 2020

    @pbn4

    @pleerock Are there any updates to this?

  8. mtdewulf commented on Apr 20, 2020

    @mtdewulf

    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).

  9. artman commented on Apr 20, 2020

    @artman
    Author

    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.

  10. mtdewulf commented on Apr 20, 2020

    @mtdewulf

    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) {
    
  11. Juddling commented on Apr 21, 2020

    @Juddling
    Contributor

    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.ts
    
    module.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.

    https://typeorm.io/#/connection-options/common-connection-options

  12. anthonylau commented on Apr 29, 2020

    @anthonylau

    @artman

    squashed all of our migrations

    Would you mind sharing how you squash migrations? Was that done manually?

  13. tylergets commented on Aug 13, 2020

    @tylergets

    For us the simple workaround is currently
    if (!ormOptions.migrationsRun) { ormOptions.migrations = []; }

  14. daramasala commented on Sep 30, 2020

    @daramasala

    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.

  15. jamescrowley commented on Feb 20, 2025

    @jamescrowley

    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).

  16. clintonb commented on Feb 20, 2025

    @clintonb
    Contributor

    @jamescrowley using SWC for compilation drastically decreased waiting time for us.

    https://swc.rs/

  17. dsfcoll commented on May 26, 2025

    @dsfcoll

    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:

    • The getNearestPackageJson function can be rewritten to use a synchronous approach.
    • We can add caching by parent directory.
      For example, in this case:
    { migrations: ['/apps/db/src/migrations/**.js'] }

    We can cache the result for /apps/db/src/migrations instead of looking it up for every individual file.

  18. vKolerts commented on Jun 18, 2025

    @vKolerts

    up

  19. added a commit that references this issue on Jul 25, 2025
    6022bca
  20. added a commit that references this issue on Aug 18, 2025
    b6ffd46
  21. added a commit that references this issue on Dec 2, 2025
    64dec14
  22. livanov commented on Mar 30, 2026

    @livanov

    No change in performance with version 0.3.28. Is this released, actually?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions