Skip to content
groundworkv0.2

Schema & migrations

How to change a database's structure safely when real data already lives in it.

7 min read · Last reviewed November 20, 2026

Schema & migrations

A is a versioned, repeatable script that changes your database's structure. It's version control for your schema.

You'll be able to#

  • Understand how migrations flow from dev to production
  • Spot migrations that would tables or lose data
  • Ask for zero-downtime changes

The mental model#

Your schema evolves in numbered steps (0001_create_users, 0002_add_avatar…). Every environment (dev, staging, prod) applies the same steps in the same order, and a table in the database records which steps have already run.

Key concepts#

  • Migration file: SQL (or code) describing one change. It gets committed to git.
  • Generate vs push: Tools like Drizzle and Prisma can either generate migration files (use this for production) or push the schema directly (fine for local prototyping only).
  • Up / down: Apply / revert. Many teams only roll forward, fixing mistakes with a new migration.
  • Destructive change: Dropping or renaming a column or table, or narrowing a type. These can lose data.
  • Expand → migrate → contract: The zero-downtime pattern. Add the new column, deploy code that writes to both, backfill old rows, switch reads over, then drop the old column in a later release.
  • Locks: Some operations (adding a non-null column with a volatile default on old Postgres versions, creating a non-concurrent on a big table) can block writes. Use CREATE INDEX CONCURRENTLY on large tables.
  • Seed data: A script that fills dev and staging with realistic fake data.

Decisions you make (not the agent)#

Your call, not the agent's

DecisionSensible defaultChoose otherwise when…
WorkflowGenerated migration files, reviewed in PR, applied in CI/CDThrowaway prototype → push
RenamesExpand/contract (add new, backfill, drop old later)Empty table / pre-launch
When migrations runDeploy pipeline step before new code goes live—

What good output looks like#

  • Each migration is small and does one thing, and is reviewed like code
  • Every statement in it has a known lock and a known cost on a big table
  • Indexes on large tables are built without blocking writes
  • A destructive change is separated from the code change, over several releases
  • There is a plan for what happens if it fails halfway

Red flags#

  • DROP COLUMN or a rename in the same release as the code change, on a live app
  • Migrations edited after they've already been applied somewhere
  • db push used against production
  • A big backfill run inside a single transaction on a large table
  • Common mistakes: a plain CREATE INDEX on a large live table, which blocks inserts, updates and deletes until it finishes; CREATE INDEX CONCURRENTLY inside a transaction block, which PostgreSQL doesn't allow; adding a foreign key or CHECK constraint with a full-table check on a large table, when NOT VALID followed by VALIDATE CONSTRAINT is available; an interrupted concurrent index build that leaves an invalid index behind

Worked example#

Task: Speed up "bookings for a business in a date range" with an index, on a large production table.

You prompt: "Add an index for bookings by business and date." The agent adds a normal CREATE INDEX, tests it on a small development database, and ships it.

What a good result contains: CREATE INDEX CONCURRENTLY, run outside a transaction block, a note that it takes longer and costs more work than a normal build, and a check afterwards that the index is valid.

The wrong turn: on the production table, a normal index build blocks inserts, updates and deletes until it finishes. Reads still work, but every new booking waits. It was fine on the small development table, so nothing warned you.

How review catches it: read the generated SQL and ask, for every statement, what lock does this take and for how long on a big table? A bare CREATE INDEX should be a red flag. If a concurrent build fails, PostgreSQL leaves an that queries ignore but writes still pay for. Drop it or rebuild it, and check how your migration tool runs a statement outside a transaction.

Sample prompts#

Prompt
I need to rename users.name to users.full_name in production with no downtime.
Write the expand/contract plan as separate migrations and code changes across
releases, and tell me exactly what to deploy in which order.

Why it works: It asks for the sequence, not just the SQL, which is where renames go wrong in production.

Prompt
Review the pending migrations in drizzle/ for: data loss, long locks on large
tables, missing indexes for new foreign keys, and irreversibility. Rate each risk
and suggest safer alternatives.

Why it works: A fixed list of risks gets a verdict on each one, which is easier to check than "is this safe?".

Plan a safe change to a big table#

Prompt
I need to <add an index / add a foreign key / add a NOT NULL constraint> on the large table <table> in production, without blocking writes. Write the migration plan: for each statement, the lock it takes and whether it scans or rewrites the table; use CREATE INDEX CONCURRENTLY for indexes, and NOT VALID followed by VALIDATE CONSTRAINT for constraints where they apply; say how to run statements that can't be in a transaction; what to check afterwards (for example, that the index is valid); and how to roll back.

Why it works: It asks for the lock and the cost of each statement up front, which is the information a bad migration hides.

Explain each statement's lock#

Prompt
For each statement in this migration, explain: what lock it takes, whether it scans or rewrites the table, whether it can run inside a transaction, how long it will take on a table of <size> rows, and whether it can be undone. Mark anything you aren't sure of, and what I should test on a copy of production data.

Why it works: ALTER TABLE statements take strong locks by default, so asking about each one surfaces the risky ones. Treat the answer as a lead and verify on a copy.

Rehearse on a copy#

Prompt
Don't touch production. Describe how to run this migration against a copy of production-sized data in staging, what to measure (duration, locks held, blocked queries), and what result would make you stop and rewrite it.

Why it works: Migrations that look fine on a small development database often fail at production size.

Check your understanding#

Check your understanding

1/3Why use CREATE INDEX CONCURRENTLY on a large live table?
2/3Can CREATE INDEX CONCURRENTLY run inside a transaction block?
3/3In expand/contract, what comes last?

Checklist#

  • Each migration is small, reviewed and committed
  • I know the lock each statement takes and what it costs on a large table
  • Large-table indexes use CONCURRENTLY, outside a transaction block
  • New constraints on large tables use NOT VALID and VALIDATE CONSTRAINT where possible
  • Destructive changes ship separately from the code that stops using the old structure
  • The migration was rehearsed on production-sized data

Go deeper#

Saved in this browser only.