The system I look after now reads every store from Postgres, and SQLite is a copy that still gets written. The next step stops writing it. Before that, I went looking for anything that still depended on the SQLite files rather than on the data. I found three kinds of code that would break quietly, and one guard worth adding.

"Does the file exist?"

Twenty-four places in seventeen modules checked whether a store's file existed before reading it. That's sensible when the file is the database. Once it isn't, the answer means nothing.

The dangerous one was a lookup that links a job to its contact in the accounting system. With no file it returned "no link", so the caller would search for the contact by name. That could create the duplicate the link exists to prevent.

All twenty-four now ask the database layer, which knows each store's mode. Once SQLite is off, the store's schema in Postgres is what counts.

"How fresh is this?"

Staff can ask the system questions about jobs. Under each answer it lists where the data came from and when that source was last updated. It flags a source as stale if it should refresh every fifteen minutes and hasn't.

"Last updated" was the file's modification time. Once SQLite stops being written, every answer would call the board copy stale within forty-five minutes. Nobody would trust the label after the first day.

Now each store answers with its own newest timestamp from Postgres, such as its last sync or its newest record. If Postgres can't answer, the label is left off rather than showing the frozen file's time. On production the new times matched the old ones to the minute for twelve of thirteen sources, which is what you'd expect while both are still written. The thirteenth now shows its last full rebuild instead of its last small write, which is what its label always claimed.

Retention that pruned the wrong copy

A weekly job deletes old history entries after eighteen months. It did this with the SQLite command-line tool, directly on the file. Since the read switch, that deleted from the copy only. Once SQLite is off, it would delete nothing at all, and the retention rule would stop applying without a single error.

Nothing was old enough yet, so nothing had drifted. The job now goes through the same database layer as everything else, and a test runs the real script against temporary stores.

The guard

The production configuration is generated from a template. If someone re-rendered an old template after the switch, SQLite would come back as a copy that had missed every write since. The render now refuses any change that takes a store out of the SQLite-off mode. The only way back is the rollback script, which rebuilds SQLite from Postgres first.

On staging, the switch took 3 seconds. One write reached Postgres while the SQLite file stayed identical byte for byte. The rollback took 5 seconds and copied back exactly that one row.

What I took from it

  • Search for files being used as signals (existence, modification time, a command-line tool), not just for queries.
  • A quiet failure needs a test that runs the real script. A unit test of a function it calls won't catch it.
  • Make the dangerous direction refuse, and give the safe direction one obvious command.

The switch is planned for this weekend: twenty-eight stores in one window. The two that moved on Friday get their own week first.