Database Schema Migrations in CI/CD Without Downtime
During a rolling deploy, old and new versions of your app use the same database at once. Here is how to change the schema so both keep working, and where migrations belong in the pipeline.
Published December 1, 2026 · HostingX Solutions LLC
Short answer
Make every migration backward compatible with the app version already running. Use expand and contract: add the new structure, move the app to it, and remove the old structure in a later release.
Avoid long locks. In PostgreSQL, a normal
CREATE INDEXlocks out writes until it finishes;CREATE INDEX CONCURRENTLYdoesn't, but can't run inside a transaction block.Lint migrations in pull requests. Tools such as
atlas migrate lintflag destructive changes and table locks or rewrites before they ship.Run migrations once, as their own pipeline step, before the new app version rolls out. On Kubernetes with Argo CD, that's a Job with a
PreSynchook, written so it's safe to run again.For database engine upgrades, use a blue/green approach. Amazon RDS Blue/Green Deployments switch over in typically under a minute, with no data loss.
Why Schema Changes Break Zero-Downtime Deploys
A rolling update replaces app instances a few at a time, so for minutes the old and new versions serve traffic side by side, against the same database. If the new version's migration renames a column, every old instance starts failing the moment the migration commits. If you roll the app back, the old code meets a schema it doesn't understand. And if a migration holds a lock on a busy table, requests queue up behind it whichever version they come from.
So zero-downtime migrations rest on two rules: each schema state must work with both the current and the next app version, and no migration may hold a lock that blocks traffic for long. The rest of this guide applies those rules. For the app side of zero-downtime releases, see our zero-downtime Kubernetes upgrades guide.
Expand and Contract, With a Worked Example
Martin Fowler describes parallel change, also called expand and contract, as a way to make “backward-incompatible changes to an interface in a safe manner” in three phases: expand to “support both the old and the new versions,” migrate all clients, then contract to “remove the old version.” Applied to a database, renaming users.fullname to display_name takes three releases:
Release 1: expand (migration V12__add_display_name.sql)
-- New column, nullable, so existing inserts keep working
ALTER TABLE users ADD COLUMN display_name text;
-- App release 1 writes BOTH columns and reads display_name,
-- falling back to fullname when display_name is NULL.Between releases: backfill in batches (a job, not a migration)
UPDATE users
SET display_name = fullname
WHERE id IN (
SELECT id FROM users
WHERE display_name IS NULL
LIMIT 5000
);
-- Repeat until no rows are updated. Small batches keep
-- row locks short and replication lag low.Release 3: contract (migration V14__drop_fullname.sql)
-- Only after no deployed version reads or writes fullname
ALTER TABLE users DROP COLUMN fullname;Release 2 is app-only: it stops writing fullname and drops the fallback. Each step leaves a schema that works for the version before and after it, so any single release can be rolled back. The cost is time: a rename becomes three deploys. That trade is worth it for any table that live traffic touches.
Safe and Unsafe DDL, at a Glance
The specifics vary by database and version; this table uses PostgreSQL behavior as the reference:
| Change | Risk if done directly | Safer approach |
|---|---|---|
| Add a nullable column | Usually safe | Add it nullable, backfill in batches, add constraints later |
| Add an index | Blocks writes for the whole build | CREATE INDEX CONCURRENTLY, in its own migration, outside a transaction |
| Rename a column or table | Breaks the running app version | Expand and contract: add new, write both, switch reads, drop old later |
| Drop a column | Breaks any version still reading it | Only in a contract migration, after no deployed version uses it |
| Change a column type | Can rewrite the table and break readers | Add a new column of the new type and migrate with expand and contract |
| Add NOT NULL or a foreign key | Can scan or lock large tables | Backfill first; check the database’s options for adding constraints without long locks |
The index case is worth spelling out. PostgreSQL's documentation says that with CONCURRENTLY, it builds the index “without taking any locks that prevent concurrent inserts, updates, or deletes,” whereas a standard build “locks out writes (but not reads).” It also notes that CREATE INDEX CONCURRENTLY cannot be performed within a transaction block, and that a failed build leaves behind an “invalid” index you need to drop and retry. Many migration tools wrap each migration in a transaction by default, so put concurrent index builds in their own migration and configure that one to run outside a transaction.
V13__index_users_display_name.sql (runs outside a transaction)
SET lock_timeout = '5s'; -- fail fast instead of queueing traffic
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_display_name
ON users (display_name);Migration Tools and Linting
| Tool | Model | Notes |
|---|---|---|
| Flyway | Versioned SQL files, each with a version, description and checksum | Records applied migrations in flyway_schema_history; checksums “detect accidental changes” to files already applied |
| Liquibase | Changelogs of changesets in SQL, XML, YAML or JSON | Liquibase Community moved to the Functional Source License from version 5.0; each release reverts to Apache 2.0 two years after launch, and earlier versions stay Apache 2.0 |
| Atlas | Versioned migrations or a declared desired schema | atlas migrate lint flags destructive operations, breaking schema changes, and table locks or rewrites that could cause downtime |
| Framework migrations | Rails, Django, Alembic, Prisma and similar | Same principles apply; most lack built-in lock or compatibility checks, so add review rules or a linter |
Whichever tool you use, treat applied migrations as immutable. Flyway's documentation advises against editing migrations already applied to permanent environments; write a new migration instead, which its checksums will otherwise catch. Then add a check in pull requests. With Atlas, --dev-url points at a disposable “dev database that will be used to simulate the changes,” and --git-base limits the check to migrations changed on the branch:
atlas migrate lint \
--dev-url "$DEV_DATABASE_URL" \
--git-base mainIf you don't use a linter, encode the rules in review: no DROP or rename outside a contract migration, no index without CONCURRENTLY on large tables, and every migration tested against a copy of production-sized data.
Where Migrations Belong in the Pipeline
Pull request: lint the migration and run it against a fresh database, then run the test suite against the migrated schema.
Before the rollout: run pending migrations once, from a single job with its own database credentials, then deploy the new app version.
After the rollout: run backfills as separate, resumable jobs, and schedule the contract migration for a later release.
Avoid running migrations from the app's startup code. With several replicas starting at once, you depend on the tool's locking to stop them racing, and a slow migration delays every pod's readiness. A single job is easier to observe and to stop.
On Kubernetes with Argo CD: a PreSync hook
Argo CD runs resources annotated with argocd.argoproj.io/hook: PreSync “prior to the application of the manifests,” and only continues to the main sync if they succeed. Its documentation includes a PreSync Job for exactly this purpose, and notes that hooks run on every sync, so the migration command must be safe to run more than once. Migration tools that record applied versions, like the ones above, already are.
migrate-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: db-migrate
annotations:
argocd.argoproj.io/hook: PreSync
argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
spec:
backoffLimit: 0 # don't retry a half-applied migration blindly
activeDeadlineSeconds: 900
template:
spec:
restartPolicy: Never
serviceAccountName: db-migrate
containers:
- name: migrate
image: registry.example.com/app-migrations:1.4.0
command: ["/app/migrate"] # your Flyway, Liquibase or Atlas command
envFrom:
- secretRef:
name: db-migrate-credentialsBeforeHookCreation is Argo CD's default delete policy: it deletes the previous hook Job before creating the next one, so the last run stays inspectable. Give the migration Job its own database user with DDL rights, and keep the app's user limited to data access. For more on GitOps controls around this, see GitOps compliance with Argo CD.
Rollbacks: Roll Forward
Down migrations look reassuring but rarely survive contact with real data: once new rows use a new column, dropping it loses data. With expand and contract, you seldom need them. Rolling the app back is safe because the previous schema elements still exist until the contract release, and a bad migration is fixed with a new forward migration. Keep a tested backup and point-in-time recovery for the rare case that isn't enough.
Zero-Downtime Database Engine Upgrades
Major engine upgrades are a different problem from schema changes. On AWS, Amazon RDS Blue/Green Deployments create a staging (green) copy of production that stays in sync, where you can upgrade the major or minor engine version and test. Switchover “typically takes under a minute with no data loss and no need for application changes,” because the green environment takes over the production names and endpoints. RDS supports it for MariaDB, MySQL and PostgreSQL, with a separate Aurora version. The green environment is read-only by default, and RDS Proxy or smart drivers reduce downtime during switchover. If you run databases on Kubernetes, follow your operator's documented upgrade path and rehearse it on a copy first.
Frequently Asked Questions
Should database migrations run in the CI/CD pipeline?
Yes, as a dedicated step that runs once per deploy, before the new app version rolls out, and only after the migration passed linting and tests in the pull request. Avoid running them from application startup.
How do you rename a column without downtime?
Use expand and contract: add the new column, have the app write both and read the new one with a fallback, backfill in batches, stop using the old column, then drop it in a later release.
How do I run migrations with Argo CD?
Package them as a Kubernetes Job annotated argocd.argoproj.io/hook: PreSync. Argo CD runs it before applying the rest of the manifests and stops the sync if it fails. Make the command idempotent, because hooks run on every sync.
Is Liquibase still open source?
From version 5.0, Liquibase Community uses the Functional Source License, which Liquibase describes as source-available. You can still run and modify it; each release reverts to Apache 2.0 two years after launch, and earlier versions remain Apache 2.0.
How do you upgrade a PostgreSQL major version without downtime on AWS?
Use Amazon RDS Blue/Green Deployments: create a green copy on the new version, test it, then switch over. AWS says switchover typically takes under a minute with no data loss.
Do schema changes still mean a maintenance window?
We help US businesses build pipelines that ship schema changes safely: migration linting, PreSync jobs, expand-and-contract practices and blue/green engine upgrades. The first 30-minute consultation is free.
Sources
Related Articles
Zero-Downtime Kubernetes Upgrades
The cluster and app side of zero downtime.
CI/CD Pipeline Automation Guide
The stages of a modern pipeline, from lint to deploy.
GitOps Compliance with Argo CD
Auditable deploys, including hooks and approvals.
Monolith to Microservices Migration
Splitting a shared database step by step.
See also: DevOps automation services · CI/CD migration playbook · Disaster recovery as code
Services
Subscribe to our newsletter
Get monthly email updates about improvements.
© 2026 HostingX Solutions LLC. All Rights Reserved.
LLC No. 0008072296 | Est. 2026 | New Mexico, USA
Terms of Service
Privacy Policy
Acceptable Use Policy