◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
Articles/Claude Code
⟐ Claude Code/2026-05-02Advanced

Designing Zero-Downtime Database Migrations with Claude Code: A Production Operations Guide

A production guide to designing zero-downtime database migrations with Claude Code. Covers Expand-Contract, NOT NULL additions, renames, backfills, and subagent reviews — practical patterns that survive real production traffic.

claude-code132databasemigration9production111postgresqladvanced11

✦ Premium Article

Database migrations are the quiet kind of work most days, but on the rare day they break in production, they break everything. I once shipped an ALTER TABLE ... ADD COLUMN ... NOT NULL at 2 AM on a PostgreSQL instance that hadn't yet upgraded past version 10, and the full-table rewrite locked out our API for thirty minutes. Application retries amplified the lock contention, and by the time I rolled back, dashboards were lit up red. I've never forgotten the sweat of that night.

The hard part is that the danger of a migration isn't visible from reading the SQL alone. Whether one line of DDL takes a millisecond or scans every row depends entirely on the database engine, the version, and the table size. Claude Code is, in my experience, exceptionally good at spotting these invisible landmines and rewriting migrations into safer multi-step shapes. This guide brings together the patterns I now use to evolve schemas without taking the system offline, paired with how I drive Claude Code through each step.

Why Migrations Take Down Production

In my own incident reviews, the migrations that take a system down almost always fall into one of three categories. The first is running long-locking DDL directly against production. Statements like ALTER TABLE ... ADD COLUMN ... NOT NULL DEFAULT 'foo' are instant on PostgreSQL 11 and later, but they trigger a full table rewrite on older versions and on certain managed services. On a hundred-million-row table, that translates to minutes of locked tables and a flood of timeouts.

The second category is shipping the application code and the schema in the wrong order. If the new schema reaches production while old replicas still serve traffic, the old code starts referencing columns that no longer exist and returns 500s. Reverse the order and the new code can't find columns it expects. There's no path through this except careful sequencing.

The third category is launching a migration without a rollback plan. The moment a DROP COLUMN lands, the data is gone. By the time you realize something downstream broke, restoring from backup means losing every write that happened in between. The Expand-Contract pattern exists precisely to make these three categories impossible.

Three Principles That Make Zero Downtime Possible

Expand-Contract works because it enforces three principles. The first principle is that you always pass through a state where both the old code and the new code can run. You create a window — sometimes hours, sometimes weeks — where the application can read and write either shape, and you advance the deploys through that window step by step.

The second principle is that you only run DDL that completes in a fraction of a second on production. Anything longer gets decomposed into smaller DDL, or routed through online migration tools like pg_repack or gh-ost. If a single statement might lock a hot table for more than about a second, it doesn't go to production as-is.

The third principle is that every migration has a real reverse direction, and the reverse direction doesn't lose data. You write down migrations that you'd actually run if production caught fire, not toy ones that satisfy a linter.

I make these explicit to Claude Code by keeping a section in my project's CLAUDE.md like this:

# Migration Operations Rules
 
## Mandatory
- DDL applied to production must complete in under 1 second
- New columns are added as NULLable, then promoted to NOT NULL in a later migration
- Column drops follow: remove all code references → deploy → DROP COLUMN
- Renames follow: add new column → dual-write → switch reads → drop old column
- Every migration ships with a real, tested down migration
 
## Required Review Items
- Estimated affected row count (verified with EXPLAIN or COUNT)
- Predicted lock time (measured against a realistic test database)
- Compatibility with the previous deployment (does old code keep running?)

Once that's in place, Claude Code naturally generates migrations that respect the rules instead of taking the dangerous shortcut.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦If you've ever broken up a sweat watching a production migration stall mid-deploy, you'll learn the Expand-Contract patterns to ship without downtime starting today
✦You'll get concrete SQL and code for the riskiest changes — NOT NULL additions, column renames, type changes — broken into safe five-step deploys
✦You'll learn how to delegate migration reviews to a Claude Code subagent so production landmines get caught before they reach a real database
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⟐ Claude Code2026-04-30
Composing Claude Code Skills — Building Automation a Single Skill Can't Reach
A practical guide to going beyond one-skill-at-a-time and composing multiple Claude Code Skills into real workflows. Pipeline, branching, and meta-skill patterns with full implementation examples and the failure modes you should design against.
⟐ Claude Code2026-04-29
Observability for Claude Code with OpenTelemetry — A Production-Grade Tracing Guide for Agentic Workflows
Trace Claude Code agent runs end to end with OpenTelemetry. Hook integration, per-tool spans, MCP propagation, cost attribution, and sampling patterns that survive thousands of runs per day.
⟐ Claude Code2026-04-24
Debugging Claude Code Hooks in Production — Where to Start When Logs Are Missing
Your Claude Code hooks work locally but go silent in production. Here is the three-layer observability pattern I use on real projects, plus a 60-second minimal reproduction recipe for isolating failures.