salsita / node-pg-migrate

Node.js database migration management for PostgreSQL
https://salsita.github.io/node-pg-migrate
MIT License
1.29k stars 177 forks source link

Database not unlocked when External DB Client is used #885

Closed seanadkinson closed 2 months ago

seanadkinson commented 2 years ago

Hi there,

I am running pgMigrate programmatically on app startup via something similar to the following:

   await pgMigrate({
        dbClient: dbClient,
        migrationsTable: 'migrations',
        dir: path.join(__dirname, '..', '..', 'migrations'),
        direction: 'up',
    });

You can see I am providing an external dbClient.

When running my app with multiple nodes, the first node successfully runs the migrations, but every subsequent node gets "Error: Another migration is already running"

I believe this is happening because when you check that the database is connected before unlocking here, it is always false, since you don't set the connectionStatus for external clients here. This means that the database remains locked forever.

I think if you set connectionStatus = ConnectionStatus.CONNECTED for external clients, this should solve the issue. Thoughts?

AHBruns commented 2 years ago

I'm running into this as well.

matei-radu commented 2 years ago

I'm having this issue with the same setup: running the migration function programmatically and with an external dbClient. Although my scenario is a bit different, as I first encountered the problem running tests (thus running pgMigrate more than once because of some test cases).

I think @seanadkinson explanation on the connection status when using an external client makes sense: with the current setup, the status will remain ConnectionStatus.DISCONNECTED when using an external client, then unlock is never called.

I tried running pgMigrate with the option noLock: true and indeed I no longer get the error.