postgrator
postgrator copied to clipboard
Cannot run skipped migration without rolling back
Describe the bug It doesn't seem like there's a way to force postgrator to run a script that has been skipped/missed without first rolling back (assuming you have an undo for every script in between) and then re-running.
To Reproduce
- Create a handful of scripts that make small changes to the database.
- Number them sequentially
- Skip a number and create one more (e.g. 001, 002, 003, 005)
- Run Postgrator
- Create a script for the skipped number (004)
- Run Postgrator again
- Note that because the script is less than the current "version" it does not run or get added to the schema_versions table
Expected behavior Since the script has not yet been run and is not in the schema_versions table, it should be run (or at least make a command/option to "catch up" and run any missed scripts/numbers)
Additional context This is useful in cases where you have a shared repository and multiple developers. It is also necessary if you have a "dev" and "prod" setup and need to commit a quick fix to the "prod" version while working on the "dev" version (e.g. script 039 is run in dev as part of a feature, but a bug in prod requires script 040 to fix it first. When 039 is pulled to prod, it will not run)
The flaw is in the way the getRunnableMigrations is calculated: https://github.com/rickbergfalk/postgrator/blob/master/postgrator.js#L211-L234
It only checks for the versions higher than the latest applied. If should also check for files that have not been applied which are lower than the latest migration.
Is it best practice? Not sure. Is is unexpected behaviour? Not sure