One test kept failing in the CI job that runs every store's tests against Postgres. Then it would pass on a re-run, then fail on someone else's branch. I wrote last week that flaky tests are findings, not noise. This one took three attempts to find, and the answer was a column order.
The symptom
The test covers a half-hourly sync. It creates one job, schedules two people on it, and checks the calls it would make. Its own assertions always passed. What failed was the check that runs after every test: the read verifier. During the test, it runs sampled queries against both databases and compares the answers. It reported that one simple query, "this job's row", had come back differently from SQLite and from Postgres.
Locally it failed four runs in ten when run on its own.
The wrong suspect
My first find was a real bug, just not this one. When Postgres can't be reached, a process-wide flag skips the copy's writes for sixty seconds, so a short outage doesn't stall every job. In tests, one test's simulated outage could leave the flag set for the next test, which then silently missed writes. I made each test start with the flag clear, and fail loudly if it got set during the test.
The test kept failing.
Make the failure talk
The mismatch report only said "values differ". I changed it to name the row and the columns: 10, 11, 21 and 22. Then I printed both rows.
The values were identical. The order wasn't. In Postgres, the two timestamp columns sat in positions 10 and 11. In SQLite they were last.
Why
The table had grown. Its first version ended with the two timestamps, and eleven columns were added after that. In production that's still the order on disk: an added column goes on the end. The Postgres table was generated from the production file, so it matched.
When those columns were added, they also went into the code's own CREATE TABLE, but before the timestamps rather than after them. That only matters for a brand-new file, and production never makes one. Every test does.
So in tests, SELECT * returned the same row in two orders, and the verifier compares by position. The code reads columns by name, so nothing in production was wrong. The test was only intermittent because the verifier skips a comparison while a write is in flight, and whether the read landed in a quiet moment came down to timing.
The fix
I declared the columns in production order and added a test that compares a fresh file's layout with the migration, column by column. It fails without the fix. Afterwards the test passed ten runs out of ten, along with the other 139 tests in its area in all three database modes.
I didn't change the verifier to compare by name. Code that unpacks a row by position would break on this difference, and the check is right to flag it.
What I took from it
- When a check says "different", make it say where before guessing why.
- A fix that finds a real bug isn't proof it found this bug. Keep the failure open until it stops.
- On a table that has grown, add new columns last, everywhere, including the schema that only tests ever run.
