The safest schema change never touches the column in use
Split a breaking migration into small deploys, each reversible until the contract phase, and do not walk away before the final cleanup.
In brief
- Do not modify a column in use. Add the new structure alongside it, write to both, and only then switch the read path.
- Each step is a separate deploy, backward compatible with the previous one, so it can be rolled back right up to the contract phase.
- Skip the contract phase and the system can end up worse than before you started.
One customers table, one name column, and three readers: the main app, a nightly reporting job and an ETL pipeline owned by another team. Say the client now needs to push customer data into a new system that wants first and last names in separate fields.
The first plan that comes to mind is a single migration: split the column, rename it, update the code, deploy once and be done. But during a rolling deploy, old instances of the app are still running and still looking for name. Tonight’s reporting job knows nothing about the change either.
This situation is especially hard for an FDE. You are working on someone else’s database, where you do not know everyone who reads what, and you cannot ask the whole company to stop and wait for you. The technique built for this is called expand–contract.
The idea: never change what is running
Martin Fowler calls it parallel change: a way to introduce a backward-incompatible change to an interface safely. A database schema is an interface too, and every running SQL query is one of its clients.
The pattern has three phases. In the expand phase, you extend the interface so it supports the old and new versions at the same time. In the migrate phase, you move each client from the old version to the new one. In the contract phase, you remove the old version.
Pete Hodgson puts the benefit plainly: instead of one risky deploy, you get a series of independent deploys, and every change in the series stays compatible.
Because each stage is backward compatible with the previous one, different parts of the system can sit at different stages. Old and new versions of the app run side by side without breaking each other.
Splitting the name column, one deploy at a time
Prisma’s guide to expand–contract uses this exact example: when you need to split name into first_name and last_name, add both new columns rather than modifying name. Stripe, describing how it runs online migrations at scale, breaks the work into four steps.
It dual-writes to the old and new structures, moves all reads to the new structure, writes only to the new structure, and then removes data that still depends on the old model.
Applied to the client’s table, the first step touches only the schema:
-- Step 1: expand. Only add; do not modify or drop anything.
ALTER TABLE customers ADD COLUMN first_name text NULL;
ALTER TABLE customers ADD COLUMN last_name text NULL;
The new columns allow NULL, so the old app can insert rows as usual. If anything goes wrong, you simply drop the two columns you added, and nobody is affected.
The second step changes the write code. As Prisma describes it, in this phase the client writes to both the original and the new structure, while the read path still uses the original:
def save_customer(conn, cid, full_name, first, last):
conn.execute(
"""UPDATE customers
SET name = %s, first_name = %s, last_name = %s
WHERE id = %s""",
(full_name, first, last, cid),
)
Dual writes only keep new records in sync. Millions of existing rows still have an empty first_name. Stripe handles this with a backfill: filling in what is missing in the new store so both sides hold identical information.
-- Step 3 (a separate deploy or a separate job): backfill in batches, repeat until no rows remain
UPDATE customers
SET first_name = split_part(name, ' ', 1),
last_name = substr(name, length(split_part(name, ' ', 1)) + 2)
WHERE id IN (
SELECT id FROM customers
WHERE first_name IS NULL
ORDER BY id LIMIT 5000
);
The SQL above assumes the first word is the given name and the rest is the family name. For a Vietnamese name such as “Nguyen Van An”, where the family name comes first, that rule gets it completely wrong. This is a business decision rather than a technical bug, and the client, not you, has to make it.
Check before switching the read path
The read path is where mistakes are easiest to make, because errors there show up directly in front of users. Stripe used GitHub’s Scientist library to verify read paths before switching them. You do not need that particular library, only the idea: read from both sides, compare, log any mismatch, but keep returning the result from the old side.
def display_name(row):
old = row["name"]
if row["first_name"] is None:
return old # row not backfilled yet: nothing to compare, avoid false mismatch logs
new = f'{row["first_name"]} {row["last_name"] or ""}'.strip()
if old != new:
log.warning("name_mismatch", extra={"id": row["id"]})
return old # keep trusting the old structure until the logs are clean
The limits of this function need stating clearly. Joining first_name and last_name almost always rebuilds the original string, so it cannot detect “Nguyen” being put in the given-name field by mistake. It only catches technical faults such as lost characters, wrong whitespace or rows the dual write missed.
To catch business errors, compare against something the client has approved: a sample of a few hundred rows whose names the client’s own staff have split, or a signed-off rule for splitting family names. A mismatch against that sample is the real signal that the backfill is getting the meaning wrong.
Run this for a few days on the client’s real traffic. Only when the logs show no more mismatches, or every mismatch has been explained, do you deploy the version that reads from first_name and last_name. Then one more deploy switches writes to the new columns only.
The table below belongs in the handover document for the client. It shows, for each step, how to roll back if you have to:
| Step | Writes to | Reads from | How to roll back |
|---|---|---|---|
| 1. Expand | name |
name |
Drop the two new columns |
| 2. Dual write | both | name |
Revert to code that writes only name |
| 3. Batched backfill | both | name |
Stop the job; existing data is untouched |
| 4. Comparison reads | both | name, logging mismatches |
Turn off the comparison |
| 5. Switch reads, then write only new columns | both, then new columns | new columns | Point reads back to name while dual writes continue |
| 6. Contract | new columns | new columns | No cheap way back |
The further down the steps you go, the more expensive a rollback becomes, and by the contract step there is almost no way back. That is why contract must be the very last step and needs its own sign-off.
The contract phase: the step most often forgotten
Once everything is running smoothly, the biggest temptation is to stop there. Fowler warns that if you skip the contract phase, you can end up in a worse state than when you started: two parallel columns, code that writes twice, and a successor who does not know which column to trust.
-- Final step: contract. Run only when no client reads name any more.
ALTER TABLE customers DROP COLUMN name;
“No client” is something you must prove, not guess. In the client’s environment, check the database query logs, ask the team that owns the ETL, and look at the dashboards. The nightly reporting job is the first thing to break the next morning.
In a client’s environment, everything is slower
Hodgson estimates that a team able to deploy to production at any time can, once practiced, finish an expand–contract cycle in an hour or two. Client environments are rarely like that: there may be a fixed release schedule, a change board, and another team owning half the clients. The whole process then takes far longer.
So in the very first working session, ask the client how often they can deploy and who approves it. The answer determines how you bundle the steps. With only one release window a month, you may have to combine expand and dual write in the same release, but never combine contract with any other step.
Common mistakes
The most common mistake is running ALTER COLUMN or RENAME directly on a column in use, which skips the expand phase entirely. The second is backfilling with a single UPDATE across the whole table, which holds locks for a long time and slows down the very system you are trying not to disturb. The third is switching the read path before the backfill is finished, so users see empty names.
Two more tend to surface later. One is forgetting the clients that do not live in your repo. The other is abandoning the contract phase and leaving the project, handing the client a schema harder to understand than when you arrived.
For your career, this is a skill worth spelling out on a CV. Instead of “performed database migrations”, write that you changed the schema of a client’s production table through a series of backward-compatible deploys, with comparison reads and a rollback plan for every step.
When an FDE interview asks about incidents, do not only tell firefighting stories. Prepare one like this as well: how you planned a change to go through without stopping the system, and how each step could be rolled back.
This week’s exercise: pick a real column in your current project, write a plan following the six steps in the table above, and give it to a colleague to read. If they cannot point to a step where the system breaks, your plan is ready to take to a client.
Was this article useful?
Thanks for the feedback!