The nightly rehearsal: v1 to v2, every night
Owner: @zuki · Written by the Backend Engineer · Last reviewed: 2026-11-24 · Conforms to the decisions through D90
What the nightly migration rehearsal is for, how a night runs on the local stack and then on staging, what makes a night clean, and where each run is logged. The Backend Engineer keeps this guide; the decisions and the rules outrank it (how we work). The steps themselves are runbook steps, linked below; this guide doesn't repeat their blocks.
It moved here on 2026-11-05 from the Google doc [Guide] Nightly migration rehearsal, which zuki has archived (#56, D28). On the way, the job's runner became GitHub Actions (D36), staging's only writers became its rebuild and the rehearsal (D65), and the night became one command, tools/migration/night.py.
What a rehearsal is for
Section titled “What a rehearsal is for”The migration from v1 runs once for real, on cutover night (D2). The rehearsal runs that same script every night before then, on v1's live data, so cutover night holds no surprise:
- It proves the schema applies from nothing. Staging is rebuilt from main each night: every file in supabase/migrations on an empty database, then the seed (D11). Until cutover the migration files are edited in place, so this is the only history staging has.
- It proves every step still places v1's rows. v1 keeps moving while v2 is built (D75): a new value nobody mapped, a row a rule refuses or a table v1 adds shows up the night it appears, not on cutover night.
- It measures the night. Each step's time and row count, the photo copy's top-up, and the database's size on staging's compute.
- It counts toward the gate. Cutover waits for 10 clean nights in a row, among other things, never for a date (D20).
The data phase built the steps for this first, before any module page, so the schema could still change cheaply (D17).
What a night does
Section titled “What a night does”One night, in order, wherever it runs:
- A run (
migration.begin_run()), and the snapshot:legacy.*becomes a copy of v1's public schema, from one moment. - Unmapped values: any v1 value with no
value_maprow stops the night before a step runs (R-MIG-02). On staging it runs again after the logins load (item 3). A discovery (night.py staging --discover) lists them and goes on to item 3. - v1's logins into the night's login tables, which every end empties (R-MIG-09).
night.py stagingthen runs TL-DP-04's block 5 and printsunmapped <n> · unsigned dropped denies <n>, and stops before the steps if either isn't 0. Unmapped lines go tonight-unmapped.txt; for the denies (R-MIG-11) only the count shows. A discovery doesn't stop here: it writes every unmapped line tonight-unmapped.txtand every unsigned deny tonight-denies.txt, then goes straight to the end (item 7) withfailed. - The seven steps, each in its own transaction: master_data, access, vehicles, drivers, rentals, operations, money.
- The photo copy:
photo-copy.py mirrortops upv1-holdwith v1's objects,migration.photo_manifest()classifies them, andplacewrites each file at its owning row's path (D14). - The reconcile: every check, minus the rows signed in
migration.accepted_differences(D18). - The end, every time, even after a stop:
finish_run()withsucceededorfailed, the login tables emptied, and on staging the foreign server to v1 and v2_reader's user mapping dropped.
What each step loads and checks is on the backend runbook's data phase tab; how the script is shaped is in the database guide.
Local first: a night on the v1 copy
Section titled “Local first: a night on the v1 copy”Every step is first run on zuki's Mac, on the local stack, against a restored copy of v1 (v1copy), never against v1 itself. Each step's runbook has a section, "The night in one paste", that runs all its blocks with one command and compares its counts with the .expect file beside it:
python3 tools/migration/night.py local be-dp-40The command is tools/migration/night.py; its docstring says what each of its three commands (local, staging, staging-end) and the flags of staging do and which environment it reads. A changed or new count isn't a stop: it goes on the step's issue, and once accepted, the night's counts file in ../opleet-private replaces the .expect.
Then staging: L0, L1, then the job
Section titled “Then staging: L0, L1, then the job”A night on staging reads v1 itself, through the read-only user v2_reader, and writes only to staging. It climbs the automation ladder one level at a time, and each level waits for zuki's clean run of the one below (D36):
| Level | What runs | Done when |
|---|---|---|
| L0 | TL-DP-04, block by block, by zuki, twice on two different days | zuki has logged two clean runs on #187 |
| L1 | python3 tools/migration/night.py staging, TL-DP-04's blocks 2 to 10 with no prompts, from a clean tree at origin/main |
zuki has logged one clean run on #187, which closes data phase row 9 |
| L2 | The GitHub Actions job, in the staging environment only main can use, with an always-run step that runs night.py staging-end |
zuki turns it on; its first run is row 10, the first full night |
What holds at every level:
- v1 is only read. v2_reader reads v1 production for the snapshot and the logins; nothing writes to v1. Writing to v1 production stays L0 for good (the automation ladder).
- The job holds staging's and the read-only credentials only, as secrets of the GitHub environment, never production's (D36). v2_reader's password reaches staging only as a user mapping for the length of the night, and the end drops it.
- The keys are the new ones: the photo copy uses staging's secret key, never a service role key (D13).
night.py staging --no-photosskips the photo copy and its step, and needs no photo reader's login (V1_PUBLISHABLE_KEY, PHOTO_EMAIL, PHOTO_PASSWORD). It prints that at the start and the end, and such a night doesn't close data phase row 9: L1's clean run includes the photos.night.py staging --discoverlists what would stop a night, and runs no step. Use it when v1 has new values or denies and a normal night stops at the first check: one run then shows every unmapped value and unsigned dropped deny. It goes on past the snapshot's unmapped values, loads the logins, and writes both lists to../opleet-private(night-unmapped.txtandnight-denies.txt, mode 600, replaced each time); the terminal shows counts and paths only, since a deny line holds a login id. It rebuilds staging like any night, leaves out the photos, the reconcile and the grants, sostaging_readercan't read again until the next clean night, and needs no photo reader's login. Its run always endsfailed, and it's never a clean L1 run. It exits 0 when both lists are written and staging is cleared. zuki then maps the values (R-MIG-02) and signs each deny per login (R-MIG-06, R-MIG-11), and a normal night follows. A denied name listed only as unmapped still needs its signature once it's mapped onto none, so run--discoveragain (dropped_denies()lists only names whosevalue_maprow is empty). It doesn't combine with--no-photos.- Staging and production are separate projects, with no branching (D5). The rehearsal never touches production, which stays empty until cutover week (D11).
- Nobody else writes to staging (D65). The backend team reads it through
staging_reader, the read-only role that every rebuild keeps; TL-DP-04's block 10 makes it, and its Check proves it can't write. - Output is counts only (D15): block names, counts, statuses, check names and error keys. Every block's full output goes to
../opleet-private, never to a log, an issue or the report.
After its counts, night.py staging prints the night's differences from night-staging.expect, staging's baseline beside the runbooks: TL-DP-04's L0 run 1 of 2026-11-10, which zuki accepted as the money baseline (#569). It lists names that are new, names that are gone and values that changed, as old → new; a reconcile check with no line is 0. It's a report for zuki to read, not a check: v1 moves (D75), so a difference never fails the night, and the counts are still written to ../opleet-private/night-staging-counts.txt, in the same format. The file holds names and integers only (D15). It isn't be-dp-40.expect, which stays the local night's check on the v1 copy: six steps, with no access and no photos.
against night-staging.expect: 3 differences (a report, not a check) new rows.access.profiles = 12 gone rows.photos.contracts (was 4) changed snapshot.tables: 63 → 64What a clean night means
Section titled “What a clean night means”A night is clean when all of these hold:
- Every block ran with no error, and the run ends
succeeded. - The reconcile lists nothing: every check is 0 once the signed rows are skipped (R-MIG-05, D18). A difference nobody has signed counts, money included (D34).
- On staging, the end left no foreign server, no v2_reader password and no v1 login row, and
staging_readerstill reads and can't write. TL-DP-04's Check prints each of these. - Locally, no count changed against the step's
.expect;night.pyexits non-zero otherwise. On staging, a difference fromnight-staging.expectis reported, never failed.
A signed row carries its check, its row key, the reason, who signed and when (D18). Until the single report on #392, the lines are collected in the reconcile ledger, not signed.
By hand first
Section titled “By hand first”zuki runs every operational step by hand before anything runs it unattended. Each local night, both L0 runs of TL-DP-04 and the L1 run are zuki's, from zuki's Mac; agents never run anything against staging, v1 or the copy. A step is done when zuki has logged a clean run, not when it is written. The job's secrets, the report channel and turning the job on are zuki's too.
After any stop, the end runs first: TL-DP-04's block 9 with failed, or night.py staging-end failed, so the logins and v2_reader's password leave staging.
Where runs are logged
Section titled “Where runs are logged”- A local night: a comment on the step's issue, with what
night.pyprints under "To post on the issue" (rentals on #302, operations on #304, money on #306). - L0 and L1 on staging: a comment on #187, as TL-DP-04's Runs says: the date, main's commit, each block's time, and the lines of Expect and Check as counts.
- L2: the job's report goes to a Slack incoming webhook if that costs nothing extra, otherwise email (D36).
- In the database: each night is a row in
migration.runs, with its status, and each step writes onemigratedrow per table topublic.audit_log. - The reconcile's lines, by step, in the reconcile ledger.
Never a line of a CSV or of the reconcile file, a password or a key (D15).