Database Deployment Automation: Shipping Schema Changes Without the Quiet-Hours Ritual

Database deployment automation in practice: versioned migrations, expand and contract, lock-safe changes, a migration job in the pipeline, and a review step that reads every schema change before it runs.

DoneDeploy engineering29 November 20267 min read
Database Deployment Automation: Shipping Schema Changes Without the Quiet-Hours Ritual

Most teams automated their application deploys years ago. The database is where the automation stops. Code goes out on merge, but the schema change that came with it waits for someone senior to run it by hand, late in the evening, with a backup taken “just in case” and a chat window open in case it goes wrong.

That ritual exists for a good reason: a bad schema change can lock a busy table, break the version of the app that is still running, or lose data in a way no rollback undoes. Automating database deployment does not remove that risk. It moves the care from the person running the change to the change itself, and to the review it gets before it runs.

This post is about how we set that up: the rules a migration must follow so it is safe to run automatically, where it runs in the pipeline, and the review step that catches the dangerous ones before they reach production.

What “automated” should mean for a database

An automated database deployment has four properties:

  1. Every change is a versioned file in Git. No one types ALTER TABLE into a production console. The schema in production is the result of the migration files in the repository, applied in order.
  2. The same tool applies them everywhere. Developer laptops, CI, staging and production all run the same migration tool against the same files. Flyway, Liquibase, Alembic, Rails or Django migrations, golang-migrate: the choice matters less than using one consistently.
  3. Migrations run as their own step. Not hidden inside application start-up, where five replicas race to apply the same change, but as a single job that runs once, before or after the new code depending on the change.
  4. A person approves the change. Automation decides how a migration runs. A reviewer decides whether it should.

If you have the first three and not the fourth, you have automated the speed at which a bad migration reaches production.

The rule that makes it possible: old and new code must both work

During any rolling deploy, the old version of your application and the new one run at the same time against the same database. A migration that only the new code understands breaks the old code for those minutes. Rename a column, and every old pod that still selects the old name starts failing.

The fix is a pattern usually called expand and contract:

Step Change Old code New code
1. Expand Add the new column, table or index. Nothing is removed. Works, ignores it Works
2. Migrate Deploy code that writes to both, backfill existing rows in batches Works Works
3. Switch Deploy code that reads only from the new structure Gone Works
4. Contract Drop the old column in a later release Gone Works

It is slower than one RENAME COLUMN, and it is the reason the change can go out on a Tuesday afternoon. It also gives you a real rollback: until step 4, rolling back the application is enough, because nothing it needs has been removed.

Changes that look harmless and are not

Most migration incidents we see come from a handful of patterns. On PostgreSQL, the common ones are:

  • Creating an index on a large table without CONCURRENTLY. A plain CREATE INDEX blocks writes to the table until it finishes. CREATE INDEX CONCURRENTLY does not, but it cannot run inside a transaction, so the migration tool has to be told not to wrap it in one.
  • Adding NOT NULL to an existing column. PostgreSQL scans the whole table to check it while holding a lock that blocks reads and writes. The safer route is a CHECK (col IS NOT NULL) NOT VALID constraint, then VALIDATE CONSTRAINT as a separate step.
  • Changing a column’s type. Many type changes rewrite the whole table under an exclusive lock.
  • Waiting in the lock queue. Even a fast ALTER TABLE needs a brief exclusive lock. If a long query holds the table, the migration waits, and every query behind it waits too. Setting lock_timeout in the migration makes it fail fast and retry instead of stalling the application.
  • Dropping something the running code still reads. This is the expand and contract rule again, broken in one line.
  • Backfilling a large table in one statement. One huge UPDATE holds locks and generates a large burst of write-ahead log. Batches of a few thousand rows, committed one by one, are slower and much kinder.

MySQL and other engines have their own list. The point is the same: whether a migration is safe depends on the table it touches, not just on the SQL.

Where migrations run in the pipeline

We wire migrations into the pipeline as a separate job with a clear order:

  1. In CI, every migration runs against a real database in a container, at the version you run in production, starting from the current schema. Our CI process post covers this stage; it is where a migration that only works on an empty database gets caught.
  2. The migration job runs once per release, with its own credentials that can change the schema and nothing else. The application’s database user cannot run DROP TABLE. How those credentials get to the job without living in the repository is covered in our secrets management post.
  3. Expand migrations run before the new code, contract migrations run in a later release, after the old code is gone.
  4. The job records what it applied and fails the release if a migration fails, so code never goes out against a schema it does not expect.
  5. A restore you have tested sits behind all of it. Some changes cannot be undone by a down migration. Dropped data comes back only from a backup, and only if that backup has actually been restored before.

The review step: reading the change, not just the SQL

All of the above is mechanics. The step that decides whether a release is calm is the review, and it is also the hardest one to do well. A reviewer looking at a two-line migration in a pull request sees CREATE INDEX ON orders (customer_id);. They do not see that orders is the largest table in the database and takes writes all day.

This is the part of our Databases service we want to describe here: we use AI to review schema changes and slow query logs, and engineers check what it suggests before anything changes. In practice, for a migration pull request, that review reads the migration together with the context a busy human reviewer rarely has open: the current schema, the size of the tables involved, and the queries that use them. It then comments on the pull request in plain language.

Take the index above as an example. The review would point out that a plain CREATE INDEX on a large, write-heavy table blocks writes while it builds, suggest CREATE INDEX CONCURRENTLY in a migration that does not run inside a transaction, and suggest a lock_timeout so the job fails fast instead of queuing behind a long report query. An engineer reads that, agrees or disagrees, and changes the file. The same review flags a dropped column that the current code still reads, a NOT NULL added in one step, or an index that duplicates one you already have.

Three things it does not do:

  • It does not approve or merge. It comments. A person on your team or ours approves, the same rule we described in what we let AI agents touch.
  • It does not need your customer data. It works from schemas, query patterns and plans. What it may access is agreed with you up front.
  • It does not replace the CI run against a real database. The review catches risk that tests on a small dataset cannot show; CI catches the errors a reader can miss. You want both.

Where to start

You do not need to change everything at once. In the order we usually take it:

  1. Get every schema change into versioned files, even if a person still runs them. Nothing else works until the repository is the truth.
  2. Run migrations in CI against a real database at your production version.
  3. Agree the unsafe patterns for your engine and write them down, so reviewers check against a list rather than memory.
  4. Move the run into a pipeline job with its own credentials and lock_timeout, and keep a person approving the release.
  5. Restore a backup on a schedule, so the last line of defence is one you have tried.

DoneDeploy sets up, migrates and operates PostgreSQL, MySQL, MongoDB and Redis, with backups that are actually restored and schema changes reviewed before they run. Our packages are fixed-price, and engineers approve every change that reaches production. If your schema changes still wait for quiet hours, book a free call and we will look at your setup with you.