---
title: "Expand first and contract later · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/migrations/expand-and-contract
updated: 2026-10-20
licence: "All rights reserved"
---

# Expand first and contract later

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Migrations](https://agentwranglers.se/playbook/data/migrations)
- Rule: Make every schema change work with both the old and the new code, so deploys and rollbacks never break.
- How: To rename a column, add the new one and write to both, from the app or a trigger. Copy old rows across in batches, move reads to the new column, and drop the old one later. Add columns as nullable or with a constant default, and add `NOT NULL` once every row is filled. Never drop or rename in the same release that stops using a column.
- Check: Deploy the migration, then run the previous release's tests against the new schema. They pass.

Example:

```sql
-- Release 1: expand
ALTER TABLE customers ADD COLUMN full_name text;
UPDATE customers SET full_name = name WHERE full_name IS NULL AND id BETWEEN $1 AND $2;
-- Release 2: code reads and writes full_name only
-- Release 3: contract
ALTER TABLE customers DROP COLUMN name;
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/migrations/expand-and-contract`.

- Previous: [Never edit an applied migration](https://agentwranglers.se/playbook/data/migrations/immutable-migrations)
- Next: [Keep migrations from locking tables](https://agentwranglers.se/playbook/data/migrations/lock-safe-migrations)
- [Migrations](https://agentwranglers.se/playbook/data/migrations)
