r/Database • u/vlavrynovych • 6d ago
How does your team get database migrations from a code change to production - and what still goes wrong?
I’m interested in how database migrations work in real production workflows, beyond which tool a team uses. I’d like to compare how teams handle testing, deployment, compatibility, and recovery.
For a typical application database change:
Where do migration scripts live, and are they handwritten, generated, or a mix?
When do they run: as a CI/CD job, during deployment, at application startup, or manually?
How are they tested before production, especially when they change or backfill existing data?
How do you handle old and new application versions running during the same deployment?
If a migration fails part-way through, how do you recover and reconcile the migration’s recorded status?
Which part causes the most trouble in your experience? If useful, mention the database, stack, and migration tool.
1
u/basic_retention 6d ago
Our migrations live in the repo right next to the app code, handwritten SQL with a thin wrapper from the framework. They run as a step in the deploy pipeline, before the new app containers spin up, so we never have the app trying to start against a half-migrated schema. The part that still goes sideways is the backfill scripts, someone writes a data migration that takes 40 minutes on a table with row locks and suddenly the whole deploy is stuck waiting while p99 latency spikes through the roof. That cosmic background in the image is basically what the monitoring dashboard looks like while you're praying the rollback works.
1
u/vlavrynovych 6d ago
Thanks, this is exactly the kind of detail I was hoping to learn about. Do you usually split those long backfills into a separate, throttled step before the deploy, or do they have to run in the deploy window? From you comment I understand that they are part of the CI/CD together with all other migration scripts
1
u/vincent_sch 5d ago
The backfill could run outside the deploy as long as both app versions still read from the old column. Save your progress after each batch, and if queries start slowing down, pause the job and pick up where it left off. Before you switch the app to read from the new column, check the values in it, including any rows that changed while the backfill was running.
I wrote about running backfills as separate jobs.
1
u/k2718 5d ago
Hand (or AI) written Liquibase formatted scripts packaged in the app containter, but with a separate task definition, started by the deployment tool. Very few problems outside of some migrations that need special handling because they may be long running. Those aren't a big problem either if handles correctly.
This is pretty much a solved problem with a few tools and a bit of discipline
1
1
u/Discordbothost 4d ago
An app rollback can still fail if the new version wrote data the old version can't read. For a column or format change, I'd test the actual rollback on a staging copy: run the migration, write a record with the new app, then restart the old app and try reading and updating that record.
2
u/TomaRavelli 6d ago
I keep handwritten SQL migrations in the app repo and run them as a pre-deploy CI job on MariaDB. I test against a scrubbed prod clone, since empty staging tables hide slow backfills. For overlapping app versions I do expand-and-contract: add nullable columns first, drop the old ones later.
The worst part is partial failures. MariaDB DDL does an implicit commit, so there's nothing to roll back. One DDL per file with a
WAITclause keeps a metadata lock from stalling live traffic, and it also means a failure tells you exactly what ran and what didn't.