Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions frontend/content/blog/migration-check-blind-spot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: The migration check that couldn't see a one-line migration
description: After a dropped column took VisualCI down for 20 seconds, we added a CI check for unsafe migrations, with unit tests. Its tests passed, and it couldn't see the simplest migration there is. How we found the gap by planting a bad migration, and what we changed in the tests.
date: 2026-12-06T12:00:00+13:00
author: Nathan Broadbent
draft: true
---

On 4 December the first build of a new VisualCI project failed with a 500. The same build, run again 90 seconds later, worked. The web pod's log said `no column found for name: setup_dismissed_at`.

Six minutes earlier we had shipped an organization settings page. Its migration replaced `organizations.setup_dismissed_at` with a new column, copied the data across, and dropped the old one. The new code didn't read it any more, so the change was correct. But our deploys run migrations before the rollout, so for about 20 seconds the previous release was still serving requests against the new schema, and it still selected `setup_dismissed_at`. Every request that loaded an organization failed until the new pods were ready. The first build happened to land in that window.

The fix for the process is well known: expand, then contract. Add the new column and copy the data in one release, stop reading the old one in the same release, and drop it in a later one. We wrote that rule into the repository's agent instructions. Rules get forgotten, though, so we also added a check.

## The check

`scripts/ci/migration_safety.py` runs on every pull request. It looks at the migrations the change adds, finds every column or table they drop, rename or retype, and searches the code on the base commit (the release that will still be serving during the rollout) for each name. If the old code still reads it, CI fails:

```
drops or changes column `setup_dismissed_at`, which app/src still reads
```

It also fails a migration that adds a `NOT NULL` column with no default, since the old release's inserts would leave it out. A migration can let a name through on purpose with a `-- contract: <name>: <why>` line.

It came with five unit tests, and before merging we replayed the commit that caused the outage: the check failed on it, as it should. We replayed the 17 other commits on main that added migrations, and they all passed. That looked like a well-tested check.

## Planting a bad migration

The next day we ported the check to another Laravel Rust app. Before trusting it there, the agent doing the port planted a migration that should obviously fail: one line, dropping a column the code still reads.

```rust
pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
tx.query("ALTER TABLE planes DROP COLUMN address").unprepared().await?;
Ok(())
}
```

The check passed.

## Why it couldn't see it

Laravel Rust migrations are Rust files, and the SQL lives in string literals: a raw string constant (`const SCHEMA: &str = r#"…"#`) or a plain string passed straight to `tx.query("…")`. The check read the whole `.rs` file, split it on `;`, and matched `ALTER TABLE` at the start of each piece.

Split that way, the first piece of every file starts with Rust code: `use laravel::db::AnyTransaction;` ends at its own semicolon, and the next piece begins `pub async fn migrate(…) { tx.query("ALTER TABLE …`. A match anchored at the start of the piece never sees the `ALTER TABLE` buried in it. The same happens to the first statement of a raw string constant, which follows `const SCHEMA: &str = r#"`.

Statements after the first one in a raw string came out as clean SQL, and those were checked. That's why the replay of the outage worked: its `DROP COLUMN setup_dismissed_at` was the third statement in its string. So the check caught the migration it was written for, and missed:

- a removal that is the first statement in its string;
- a removal in a plain `tx.query("…")` string, the usual way to write a one-line migration.

We replayed every past migration in both repositories against its parent with the fixed check, and none had slipped through the gap.

## Why the tests passed

Every unit test called the parser with bare SQL:

```python
removed_names("ALTER TABLE organizations DROP COLUMN setup_dismissed_at;")
```

That's the input the parser was written for, and it handled it correctly. But the check never receives bare SQL. It receives Rust source files, and no test gave it one. The tests checked the parser, and nothing checked the whole script on the files it actually runs on.

## The fix

The check now pulls the SQL out of the file's string literals first, both raw strings and plain ones, and parses only that. Plain strings needed one more detail: a Rust string can continue onto the next line with a trailing `\`, which drops the newline and the next line's indentation. The check joins those the same way Rust does.

The tests changed more than the code:

- **Real migration files.** The parser tests now take Rust source: a raw string constant, a plain string in the call, and a plain string with a line continuation.
- **The old behaviour, pinned.** One test runs the unextracted file through the parser and asserts it finds nothing. If someone removes the extraction step, that test says exactly what goes wrong.
- **The whole script, end to end.** A test builds a throwaway git repository shaped like ours: a base commit whose code reads `setup_dismissed_at`, then a commit adding the migration. It runs the script as CI does and checks the exit code and message for three migrations: the 4 December file as it merged, which must fail; a one-line drop, which must fail; and the same drop let through by a `-- contract:` line, which must pass.

Every new test had to fail before the fix, and one of them didn't. The first line-continuation test broke the string in the middle of the SQL, and it passed without the fix, because the parser collapses whitespace and the leftover `\` and indentation didn't change what it matched. Only a break between `COLUMN` and the column's name fails without the join: unjoined, the parser reads the column as `COLUMN`. That's the test we kept.

## What we'd do earlier next time

- **Test a check on the input it really gets.** Unit tests on cleaned-up input are useful, but at least one test should run the whole check on a file shaped like the ones in the repository.
- **Plant a known-bad change before you trust a gate.** It takes a minute. Here the plant was the simplest migration there is, and it found the gap at once. A check that never fires looks exactly like a codebase that never trips it.
- **Make every new test fail first.** If you haven't seen a test fail without the fix, you don't know what it tests. One of ours tested nothing until we moved the line break.
- **Replay history in both directions.** Replaying the outage showed the check could catch it. Replaying every past migration showed nothing had slipped through. Neither replay would have found this gap: only a new, deliberately bad input did.

The check and its tests are in VisualCI's repository, in `scripts/ci/migration_safety.py` and `scripts/ci/test_migration_safety.py`.
Loading