db-migrate / node-db-migrate

Database migration framework for node
Other
2.32k stars 360 forks source link

Best practice when needing to modify an existing migration that has already been run #746

Closed nodesocket closed 3 years ago

nodesocket commented 3 years ago

I'm submitting a...

Question

If I modify an existing migration .sql and then run npx db-migrate up does not know that the migration changed. Only when running npx db-migrate down which destroys everything in the table and then npx db-migrate up does it re-create the table with the modified schema.

What is the best practice when needing to modify an existing migration? Should a new migration be created (i.e. treat migrations as immutable)? Obviously dropping the table and re-creating it is not possible when in production.

Environment


db-migrate version: 0.11.12
db-migrate driver with versions: `db-migrate-pg 1.2.2`, using `sql-file`

Additional information:
- Node version: v14.17.5
- Platform:  Mac
wzrdtales commented 3 years ago

simple, you don't ever.

create a new migration and make the adjustment. if your table is completely unused and you're making a big change, just drop the table and recreate it. never ever touch migrations that have been committed to the mainline or have run in production