# The Migration That Passed Every Test

> The migration was three lines, tests passed on a fresh database, and code review took two minutes. In production it failed, and the rollback made it worse. Migration assurance in TicketResolve checks each step against the deployed code, scores the risk with its reasons, validates the data read-only and keeps the approvals, while your own pipeline does the work.

Source: https://www.ticketresolve.ai/blog/the-migration-that-passed-every-test · Published 2026-11-16 · TicketResolve AI

The migration was three lines. It added a `currency` column, dropped `legacy_ref` and built an index on `created_at`. Tests passed on a fresh database. Code review took two minutes.

In production it failed. Then the rollback made things worse.

- **The new column was `NOT NULL` with no default**, and the orders table had 12,840 rows.
- **The version still running during the rollout read `legacy_ref`** on every order lookup.
- **The index build locked the table against writes** while it ran.

None of this shows up on an empty test database. All of it was in the files.

Here's how the evening went at the online retailer where it happened. At 19:00 the deploy started. The `NOT NULL` step failed halfway through. The team rolled back the code, but the column drop had already run in the same batch, so the rolled-back code, which read `legacy_ref`, now failed on every order lookup. Meanwhile the index build, retried by the pipeline, held a write lock. Checkout was down for 40 minutes on a Thursday evening.

## First, the basics

- **A database migration** changes the database's structure: adding or dropping tables, columns, indexes.
- **A migration tool or framework** (Flyway, Liquibase, Prisma, Alembic, Django, Entity Framework, Rails, Knex, TypeORM) runs migrations in order.
- **`NOT NULL`** means a column must have a value. **A default** fills it for existing rows.
- **An index** speeds up lookups. Building one can **lock** a table, blocking writes; **`CONCURRENTLY`** (PostgreSQL) builds it without blocking.
- **A rolling deployment** runs old and new code side by side for a while, so both must work with the database.
- **Rollback** undoes a change. **A backfill** fills in values for existing rows.
- **The code index** is TicketResolve's index of your repositories. **Deployment events** say which commit is running.
- **A CI gate** stops a pipeline until a condition is met.
- **A read-only transaction** can read data but not change it.

**Why does this matter?** Migrations are among the riskiest changes a team makes, because they change data that can't simply be redeployed. Most failures come from the running code and the real data, neither of which an empty test database has.

## The problem, in plain terms

1. **Tests run on empty databases.**
2. **Running code still uses** what the migration removes.
3. **Locks and data problems** only show in production.
4. **Reviews are quick** because migrations look small.
5. **Approvals don't mean much** when the author can approve.

## Read the migration the way production will

**Migration assurance reads the files your team already writes.** That covers **plain SQL for PostgreSQL, MySQL and SQL Server, Flyway, Liquibase, Prisma, Alembic, Django, Entity Framework, Rails, Knex and TypeORM**. **Each statement or call becomes an operation with a file and line.** Each operation is then classified:

- **Destructive**: drops a table or column, truncates, or deletes rows. The data is gone.
- **Breaking**: code that is still running will fail. Renames do this, and so does a `NOT NULL` column without a default.
- **Locking**: blocks writes while it runs. Examples are index builds without `CONCURRENTLY`, foreign keys that check every row, and type changes.
- **Additive** steps are safe for the code that's already deployed.

**Every risky step also gets a safer way to do it**, such as building the index concurrently, or adding the column as nullable, backfilling it, then making it `NOT NULL`.

## Check it against the code that's actually deployed

Linting a migration on its own misses the most common failure: code that still uses what you're removing. **TicketResolve looks up the commit your deployment events say is running and searches the code index at that commit.** In our example it reports ***Drop column orders.legacy_ref — still used in src/orders/repo.ts:42***. **If the deployed commit wasn't indexed, the page says it used the latest index instead.**

## A risk score you can argue with

**The score runs from 0 to 100, and every point comes with its reason and line:**

- +30 for dropping a column (data loss)
- +25 because the code still uses it
- +25 for a `NOT NULL` column without a default
- +10 for an index build that blocks writes
- +15 for having no rollback that works
- +10 for running in production

**This migration scores 100: critical. Critical risk needs two approvals from people other than the author.** If you disagree with a factor, you can see exactly which one it is.

## Ask the data, read-only

**Most migration failures come from the data, not the syntax.** A NOT NULL change fails if there are NULLs. A unique index fails on duplicates. A foreign key fails on orphan rows. **TicketResolve writes the questions as queries that each return a single number.** For example:

```sql
SELECT COUNT(*) AS value FROM "orders" WHERE "legacy_ref" IS NOT NULL
```

**Your CI runs them with a small script in a read-only transaction, as a user that can only read. Only the numbers come back.** **Every query must be one aggregate `SELECT`: no strings, no writes, no sleeps, no file access. The server and the script both check this before anything runs.** **A blocking check that finds rows stops approval.** **After the migration, the same row counts are compared with the numbers from before.**

## Approvals that mean something

**The workflow has ten steps**, from detected and analysed through validated, in review and approved, then scheduled, running and ran, to checked and completed. **Each step is recorded with who did it and why.**

- **The person who submitted a migration can't approve it.**
- **A failing check after approval withdraws the approvals.**
- **`tr-migrations.mjs gate` stops your pipeline until the migration is approved.**

**If someone runs it anyway, the run is still recorded, and the owner gets a critical alert.**

**TicketResolve never runs the migration and never sees your database credentials. Your pipeline does the work.** We help it know what it's about to do.

## A worked example, start to finish

Here's the same three-line migration at the same retailer, a month later, with migration assurance.

**Monday 10:00, pull request.** The developer, Wen, opens a pull request with the migration. **Detected.** It's **analysed**:

1. `ADD COLUMN currency TEXT NOT NULL` (line 3): **breaking**, no default on a table with rows. Safer way: add as nullable, backfill, then set `NOT NULL`.
2. `DROP COLUMN legacy_ref` (line 4): **destructive**, and **still used in src/orders/repo.ts:42** at the deployed commit.
3. `CREATE INDEX ... ON orders(created_at)` (line 5): **locking**. Safer way: `CREATE INDEX CONCURRENTLY`.

**Risk score: 100, critical**, each point with its reason and line.

**10:05, data checks.** Their CI runs the read-only queries with the script, as a read-only user:

- *Rows in orders*: 12,840.
- *Rows where legacy_ref IS NOT NULL*: 12,402. **Blocking check finds rows**: dropping it loses data that's still populated. Approval is stopped.

**10:30, rewrite.** Wen splits it into two migrations:

- **Migration A, this release:** add `currency` as nullable, backfill with `'GBP'`, then set `NOT NULL`; build the index **concurrently**. Additive and non-locking.
- **Migration B, next release:** drop `legacy_ref`, after the code that reads it is removed.

**Migration A** re-analysed: **risk 10** (production only). Data checks pass. **Validated → in review.** Two colleagues approve; Wen can't approve her own.

**Wednesday 19:00, deploy.** The pipeline's **`tr-migrations.mjs gate`** passes because A is approved. The pipeline runs it. **Ran → checked**: row counts after match the numbers from before (12,840). **Completed.**

**Two weeks later, Migration B.** The code no longer reads `legacy_ref` at the deployed commit. Still destructive (+30), but no code uses it. Before approving, the reviewers check that the column's data was archived; two people approve.

**Meanwhile, a bypass.** Someone runs a different migration by hand on a Friday, without approval. **The run is still recorded, and the owner gets a critical alert.**

## Five more real-world scenarios

### 1. The deployed commit isn't indexed

**Situation.** The code index hasn't indexed the live commit yet.

**How TR handles it.** The page says it used the latest index instead.

### 2. A data check fails after approval

**Situation.** New duplicates appear before the unique index runs.

**How TR handles it.** A failing check after approval withdraws the approvals.

### 3. A query that tries to write

**Situation.** Someone edits a check to update rows.

**How TR handles it.** Every query must be one aggregate SELECT; the server and the script both refuse anything else.

### 4. A foreign key on a big table

**Situation.** A migration adds a foreign key that checks every row.

**How TR handles it.** It's classified as locking, with a safer way.

### 5. Credentials

**Situation.** Security asks whether TicketResolve needs database access.

**How TR handles it.** No. It never runs the migration or sees your credentials; your CI runs the read-only checks.

## Before and after

**Before.** Three lines, a green CI on an empty database, a two-minute review, and 40 minutes of checkout down on a Thursday evening.

**After.** Each step classified with a file and line, the drop checked against the deployed code, a score of 100 with every reason, a blocking data check, a split migration approved by two people, a CI gate, and row counts compared after.

## How to tell it's working

- **Critical migrations are rewritten** before they run.
- **Data checks run in CI** for every migration.
- **No migration runs without approval**, and bypasses raise alerts.

## Who it's for

- **Developers** who write migrations.
- **DBAs and platform teams** who review them.
- **Engineering leaders** who want risky changes visible.

## How it fits with the rest of TicketResolve

Migration assurance is part of **Reliability Intelligence**. It uses the **code index** and **deployment events**, and its metrics (assessed, high-risk, incompatible, rollback-ready, post-validated) appear in **reliability metrics**. Migrations show on the **service timeline** and in the **release check**.

## Mistakes to avoid

- **Dropping columns in the same release** that stops reading them.
- **Building indexes without `CONCURRENTLY`** on busy tables.
- **Skipping the gate** "just this once".

## Do it in TicketResolve, step by step

**Where:** Reliability Intelligence → Migrations (`/dashboard/reliability/migrations`). **You'll need:** Reliability intelligence; the code index and deployment events; a read-only database user for CI; two approvers for critical risk. **Full guide:** [Migration assurance](https://www.ticketresolve.ai/docs/analytics/migration-assurance).

1. **Connect your repositories** so migration files are detected.
2. **Read the analysis**: each operation with its file, line and class, and a safer way for risky steps.
3. **Check the deployed-code findings**: what still uses what you're removing.
4. **Read the risk score** and every point's reason.
5. **Run the data checks in CI** with the script, in a read-only transaction, as a read-only user.
6. **Submit for review.** The author can't approve; critical risk needs two approvers.
7. **Add the gate.** `tr-migrations.mjs gate` stops your pipeline until the migration is approved.
8. **After it runs**, the same row counts are compared with the numbers from before.

**How to check it worked:** a migration shows its ten-step history with who did each step and why, and its row counts before and after.

Read the [migration assurance guide](https://www.ticketresolve.ai/docs/analytics/migration-assurance) to try it on your next schema change.

## Frequently asked questions

**Which tools are supported?**
Plain SQL for PostgreSQL, MySQL and SQL Server, and Flyway, Liquibase, Prisma, Alembic, Django, Entity Framework, Rails, Knex and TypeORM.

**How does it know the code still uses a column?**
It searches the code index at the commit your deployment events say is running.

**Can I see why a migration scored high?**
Yes. Every point comes with its reason and line.

**Does TicketResolve query our database?**
No. Your CI runs read-only queries and only the numbers come back.

**What queries are allowed?**
One aggregate SELECT each: no strings, no writes, no sleeps, no file access.

**Who can approve?**
Not the person who submitted it. Critical risk needs two approvals.

**What if someone runs it without approval?**
The run is recorded and the owner gets a critical alert.

**Does TicketResolve run the migration?**
No. Your pipeline does.
