by MattGaiser · 6y ago · 0 comments
> The order of the scripts had to run in a proscribed order. They had to be listed on the deployment instructions. So the migrations would run, and then fail during a prod deployment for various reasons. Most notably a dependent script hadn't run in prod yet. They had run in the Dev/QA environments and everything had run just fine. Or Dev/QA had been hand tweaked and that change hadn't made it to the script during prod deployment because they were testing it for a month to see performance before moving to prod, etc... etc...
How do companies get this right? My organization has all these problems as well and I lose 10-15% of my time chasing misplaced SQL scripts or SQL scripts with improper tweaks.
Run them in a predefined order with a migration tool (something as simple as file name order is fine).
Save metadata in the db to ensure migrations are not run twice and keep a record (again migration tools can do this).
Test migrations multiple times prior to deploy using a sanitized copy of production.
Any of these steps would help, all of them would eliminate your problems.
That's what the article explores.
So if you merge in separate scripts you will need to linearize by hand.
On top of this, there's a table within the database tracking which migrations are run, so you don't accidentally rerun these for example.
I prefer this approach very much and even wrote about it here https://vsevolod.net/migrations/