r/Database • • 5d ago

How does your team get database migrations from a code change to production - and what still goes wrong?

Post image

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.

0 Upvotes

11 comments sorted by

2

u/TomaRavelli 5d 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 WAIT clause keeps a metadata lock from stalling live traffic, and it also means a failure tells you exactly what ran and what didn't.

1

u/vlavrynovych 5d ago

Thanks for the concrete example. Testing against a scrubbed production clone and using expand-and-contract are useful details. When a DDL migration fails, how do you verify what committed before deciding whether it’s safe to rerun or needs a repair migration?

BTW I faced similar issues with Firebase Realtime DB. But we typically had in our migrations a verification steps and as a separate a rollback script. So, the rollback script was a bit smarter that for SQL, it was able to identify what exatly were already updated to revert only where needed. And storing data which you are changing in tmp file or even an new table or field in object/document of you database were very helping.

Are you writing migration scripts as javascript/typescript files?

1

u/TomaRavelli 5d ago

Before touching anything I compare SHOW CREATE TABLE against the migration history table to see what actually landed. With one statement per file, the schema either changed or it didn't. MariaDB also supports IF NOT EXISTS and IF EXISTS on a lot of ALTER TABLE clauses, so reruns are mostly safe without custom rollback logic.

I roll forward instead of reverting, since down-migrations on live data tend to cause more damage than they fix. The files are plain SQL, and I only use TypeScript scripts for heavy data backfills in batches.

1

u/Weekly-Werewolf4007 4d ago

The schema and migration-history check makes sense for DDL. For the TypeScript backfills, what do you use as the restart point if a job stops halfway through?

1

u/TomaRavelli 2d ago

I chunk by primary key (ORDER BY id LIMIT N) and write the last processed ID to a checkpoint table after each batch. If the job dies, the script reads that cursor and picks up with WHERE id > last_id instead of rescanning. Since the checkpoint is written after the batch, one batch can get replayed, which is why I keep the update idempotent with AND new_col IS NULL.

That only works because the new column is never NULL once it's filled. Rows inserted behind the cursor are covered by the app writing the column itself, so nothing gets skipped.

1

u/basic_retention 5d 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 5d 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 4d 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 4d 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

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.