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

Duplicate tables #779

Closed vanpana closed 3 years ago

vanpana commented 3 years ago

I encountered an issue while using the programatic API where the migrations are already performed and the API tries to migrate the DB again (thus failing, since there are already the tables created).

The code is:

await runner({
        count: Date.now(),
        client,
        dir: 'migrations',
        direction: 'up',
        migrationsTable: 'version',
        timestamp: true,
 });

What I'm trying to achieve is migrate to the latest version.

Is there something I'm doing wrong?

vanpana commented 3 years ago

Follow-up: I see that when this happens, 'version' table has no entries actually. Doesn't sound like an error in my code, since I'm not wiping any table. Worth investigating though.

littlewhywhat commented 3 years ago

hey @vanpana, thank you for submitting the issue. Could you provide more context please. Exact steps to reproduce would be helpful :pray:

I see that when this happens, 'version' table has no entries actually

Yes, if migration table is empty then node-pg-migrate will think that it needs to start from the first migration.

I guess that the migrations were run successfully prior the error and if so, then they must have been recorded in the some table (as each migration and it's recording is run in a corresponding transaction by default - so if smth fails, the whole migration should have being rolled back). Could you confirm that you ran the runner before with the same migrationsTable name? Could you check if you don't have some suspicious other table in the DB that was created. (if you used prior cli then the default one is pgmigrations).

Also client option in the above code is not correct if you are using the latest version - it should be dbClient. Please see https://salsita.github.io/node-pg-migrate/#/api

Btw it's also possible to use 'fake' option CAUTIOUSLY to recreate entries in the migrationTable without running the migrations (those will be created with new timestamps).

Thank you!

littlewhywhat commented 3 years ago

closing after no answer for a week. Please reopen if you have more questions. Thank you