On Tuesday the system I look after stopped reading from SQLite. Twenty-eight of its thirty stores now answer from Postgres, and SQLite became the copy. This is what the switch looked like, what the first half hour found, and how I decided it had held.
The window
I had rehearsed the switch and its rollback on a staging machine, so the production run was the same script. It does four things:
- A preflight compare of every store, with nothing stopped. If anything differs, it stops there.
- It stops the timers and services, then waits for running jobs to finish.
- It sets every generated id to start past the highest one in its table. Nineteen of twenty-five were behind, because the copy had been given the ids rather than making its own.
- It flips the setting and starts everything again, the webhook receiver first.
Writers were stopped for about a second. Health checks came back green, and the first minute showed no failed writes and no mismatched reads.
The first half hour
Two small jobs started failing on every run. Both run in a tight sandbox that can't write to the data directory. While SQLite was the main database, their history entries had failed to save without anyone noticing: the jobs ran, but the history never recorded them. Once Postgres was the main database, the entry reached Postgres, and the SQLite copy failed instead, loudly.
So the switch didn't break anything. It exposed a gap that had been there all along. The fix leaves those two jobs unrecorded until SQLite is switched off, which is what they were before, and records them properly once it is.
When it has held
My rule was three working days of clean nightly compares, including a Monday or a Friday. Those two days carry the weekly reports and the busiest batches, so they exercise the most code.
One detail matters here: each compare runs early in the morning and covers the previous day's writes. Thursday's compare proves Wednesday. So "three days" is three working days of writes, and the third is proved by the compare on Saturday morning.
Both compares so far have come back clean on all thirty stores, with nothing new in the failure log.
The last two stores
Two stores waited. One was built after the migration started, so it needed its own copy, its own soak and its own switch. The other is the set of small JSON files that record what has already been sent, plus two access tokens. Those first needed rows in Postgres that could be the truth, not just a mirror of the files.
Both moved on Friday morning, in one window:
- the preflight matched 163 rows in the first store and all fourteen files against their rows;
- no generated id would have collided;
- services were back with nothing logged as a warning.
All thirty stores now read from Postgres.
What I took from it
- Rehearse the exact script, not a description of it. The production window was boring because staging had already been interesting.
- A switch can expose failures that were already happening. Read the first errors as information before treating them as a regression.
- Count the days that were proved, not the days that passed.
Next is the step that can't be eased back so lightly: SQLite stops being written at all.
