TL-DP-04 · A full night on staging, by hand
Why: every night so far ran on your Mac, against a copy of v1. This step runs one on staging, against v1 itself: it rebuilds staging from main, snapshots v1 through v2_reader, runs row 5's seven steps and the photo copy, reconciles, and leaves staging holding the night. It's the L0 run of the nightly rehearsal (D36). The script that wraps it (L1) and the GitHub Actions job (L2) climb from it, one level at a time (the automation ladder), so there's no workflow in the repository yet.
Three things differ from a local night:
- v1 has no
v1_export.export.sqlbuilds that schema on the copy, and v2_reader can't build it on v1, because it only reads. So block 3 reads v1's catalog through v2_reader and makesv1_live's foreign tables on staging, with v1's own types as text, asexport.sqldoes. It writes nothing to v1, and a table v1 adds is read from the night it appears (D75). Tried on 2026-11-05 between the local containers: 55 relations, every column read. The copy has no column of a v1 type, so the text branch is untested until this run. - Staging is hosted. It reads through the session pooler, and Supabase's default statement timeout would kill the snapshot, so every long block turns it off for its own session.
- Errors stay short. Each psql here runs with
VERBOSITY=terse, because an error's DETAIL can hold one driver's amounts (D15).
v2_reader's password can read v1's password hashes. It reaches staging only as a user mapping for the length of the night, and block 9 drops it, so it never rests in staging's catalog.
The night also makes D65's read-only role, which each rebuild must keep.
ID TL-DP-04 · Level L0 · Run order data phase row 9 · Issue #187
Staging holds a full night from main: every step run on v1's live data, the files placed, a reconcile with the same five money checks at 0 as the local night, and a run marked succeeded. No foreign server, no v2_reader password and no v1 login hash is left on staging. staging_reader can read the night and can't write.
Run the whole step twice, on two different days. The second run is what proves the rebuild: it starts from a staging that holds real logins and files, as every later night will.
Before
Section titled “Before”- Main holds the night.
integrationis merged into main (D81), with CI green, so the money step and BE-M0-33's rebuild come from the same commit. - Your own clone, not a team clone. You are at the root of the clone that holds
.env.stagingand has the CLI linked to staging (BE-M0-28), on main, aftergit pull. A team clone holds no staging values and is never linked (D77). - Staging is the new Pro project, with 5 buckets,
v1-holdincluded. - TL-DP-01, TL-DP-02 and TL-DP-03 are done. From the password manager: "Opleet v1 · v2_reader", "Opleet v1 · photo reader", "Opleet v2 staging", and v1's publishable key.
- Staging's session pooler host: in staging's dashboard, Connect › Session pooler, the host only. Below it is <STAGING_POOLER_HOST>.
- Docker is running, for psql 17.
../opleet-privateexists. - About four hours the first time, on a Mac that doesn't sleep:
caffeinate -din another tab.mirrortook 92 minutes for a full copy, and it's a top-up on later runs ifv1-holdsurvives the rebuild. Pick a quiet time on v1: the snapshot and the file copy read production. - On the first run only, staging has no real login: BE-M0-33's block 2 prints
0 other logins.
Run every block in the same terminal: block 1's helpers and passwords live only in that shell.
1 · The passwords and two helpers
Section titled “1 · The passwords and two helpers”Paste each password at its prompt. They stay out of your history, and docker run -e NAME passes them by name, so they aren't on any command line. Each helper is one line, with no \ to continue it: continuations broke when pasted into zsh (#571). Every command on this page is one line for the same reason.
printf 'Staging database password: '; read -rs STAGING_PW; echoprintf 'v2_reader password: '; read -rs V1_READONLY_PASSWORD; echoexport STAGING_PW V1_READONLY_PASSWORD
stg() { PGPASSWORD="$STAGING_PW" docker run --rm -i -e PGPASSWORD -e V1_READONLY_PASSWORD postgres:17 psql "host=<STAGING_POOLER_HOST> port=5432 dbname=postgres user=postgres.<STAGING_PROJECT_REF> sslmode=require" -qX -v ON_ERROR_STOP=1 -v VERBOSITY=terse "$@"; }v1r() { PGPASSWORD="$V1_READONLY_PASSWORD" docker run --rm -i -e PGPASSWORD -v "$PWD/tools/migration/v1-copy:/sql:ro" -v "$PWD/../opleet-private":/out postgres:17 psql "host=<V1_POOLER_HOST> port=5432 dbname=postgres user=v2_reader.<V1_PROJECT_REF> sslmode=require" -qX -v ON_ERROR_STOP=1 -v VERBOSITY=terse "$@"; }
stg -Atc "select 'staging ' || current_database()"v1r -Atc "select 'v1 read only: ' || current_setting('transaction_read_only')"2 · Rebuild staging from main
Section titled “2 · Rebuild staging from main”Run BE-M0-33, blocks 1 and 3 to 6, with these changes:
- Block 1: go on even if the
git logline prints nothing. A night always rebuilds. - Block 2: on the first run it must print
0 other logins. On later runs, the others are last night's migrated logins. A migrated login keeps v1's id and its date, so check them by id against v1, which v2_reader reads throughv2_export.auth_users, neverauth. Run the line below. It must printnot from v1 0; then write down the count and go on.
ids=$(v1r -Atc "select string_agg(quote_literal(id::text), ',') from v2_export.auth_users"); stg -Atc "select 'from v1 ' || count(*) filter (where id::text in ($ids)) || ', not from v1 ' || count(*) filter (where id::text not in ($ids)) from auth.users where coalesce(email,'') not like '%@e2e.opleet.test'"- Block 4: if the reset kept any login, don't delete them by hand. Run the block below, which deletes every login on staging, then BE-M0-33's block 2 again: it must print
0 E2E logins, 0 other logins.
stg <<'SQL'delete from auth.users;SQL3 · v1's tables on staging, through v2_reader
Section titled “3 · v1's tables on staging, through v2_reader”The first command reads v1's catalog through tools/migration/v1-copy/foreign-tables.sql and writes a file of table and column names, with no row in it; night.py staging runs the same file (#551). The second makes the foreign server for tonight, and takes v2_reader's password from the environment, never from a file.
v1r -At -f /sql/foreign-tables.sql > ../opleet-private/v1-foreign-tables.sql
{ cat <<'SQL'create extension if not exists postgres_fdw with schema extensions;drop server if exists v1 cascade;create server v1 foreign data wrapper postgres_fdw options (host '<V1_POOLER_HOST>', port '5432', dbname 'postgres', sslmode 'require');\getenv v1_password V1_READONLY_PASSWORDcreate user mapping for postgres server v1 options (user 'v2_reader.<V1_PROJECT_REF>', password :'v1_password');drop schema if exists v1_live cascade;create schema v1_live;revoke all on schema v1_live from public;SQLcat ../opleet-private/v1-foreign-tables.sqlecho "select count(*) as foreign_tables from information_schema.tables where table_schema = 'v1_live';"} | stg4 · A run, its snapshot, and what's unmapped
Section titled “4 · A run, its snapshot, and what's unmapped”stg <<'SQL'set statement_timeout = 0;select migration.begin_run('TL-DP-04, staging, L0') as run;select migration.snapshot() as tables_copied;select domain, field, v1_value, rows from migration.unmapped_values() order by 1, 2, 3;SQLIf it prints a line, stop. Go to block 9 with failed, and post the lines on #187 after checking they hold no name. Mapping a new value is BE's, and the fix lands on main.
5 · v1's logins into the night
Section titled “5 · v1's logins into the night”The two files hold emails and password hashes. Block 9 deletes them.
v1r -f /sql/logins-export.sqlchmod 600 ../opleet-private/v1-logins.csv ../opleet-private/v1-identities.csvstg -c "truncate migration.v1_auth_users, migration.v1_auth_identities"stg -c "\copy migration.v1_auth_users from pstdin with (format csv, header)" < ../opleet-private/v1-logins.csvstg -c "\copy migration.v1_auth_identities from pstdin with (format csv, header)" < ../opleet-private/v1-identities.csvNow that the logins are in, check what block 4 couldn't see yet: the access fields' values (R-MIG-02) and the denies that map onto no v2 code and aren't signed (R-MIG-11).
stg -At <<'SQL'select 'unmapped ' || count(*) from migration.unmapped_values();select 'unsigned dropped denies ' || count(*) from migration.dropped_denies() d where not exists (select 1 from migration.accepted_differences a where a.check_name = 'access dropped denies' and a.row_key = d.row_key);SQLIf either isn't 0, stop. Go to block 9 with failed. For unmapped lines, run block 4's unmapped_values() line and post them on #187 after checking they hold no name. For dropped denies, post the count only: their lines hold login ids.
6 · The seven steps
Section titled “6 · The seven steps”stg <<'SQL'set statement_timeout = 0;\timing onselect migration.run('master_data') as master_rows;select migration.run('access') as access_rows;select migration.run('vehicles') as vehicle_rows;select migration.run('drivers') as driver_rows;select migration.run('rentals') as rental_rows;select migration.run('operations') as operation_rows;select migration.run('money') as money_rows;SQL7 · The photo copy
Section titled “7 · The photo copy”The same work as BE-DP-23's blocks 2 to 5, against staging. mirror asks for the photo reader's password. The subshell keeps staging's secret key out of your terminal's environment; never print it.
v1r -f /sql/photo-objects.sqlchmod 600 ../opleet-private/mop-files-copy-objects.csv( set -a; . ./.env.staging; set +a export V2_URL="$SUPABASE_URL" V2_SERVICE_KEY="$SUPABASE_SECRET_KEY" export V1_REF=<V1_PROJECT_REF> V1_KEY=<V1_PUBLISHABLE_KEY> PHOTO_EMAIL=<PHOTO_READER_EMAIL> python3 tools/migration/photo-copy.py mirror ../opleet-private/mop-files-copy-objects.csv ../opleet-private/photo-copied.csv)chmod 600 ../opleet-private/photo-copied.csv
stg -c "truncate migration.v1_objects, migration.photo_copied"stg -c "\copy migration.v1_objects from pstdin with (format csv, header)" < ../opleet-private/mop-files-copy-objects.csvstg -c "\copy migration.photo_copied (object_id, status, detail, size) from pstdin with (format csv, header)" < ../opleet-private/photo-copied.csvstg <<'SQL'set statement_timeout = 0;select * from migration.photo_manifest();SQL
stg -c "\copy (select * from migration.photo_plan()) to pstdout with (format csv, header)" > ../opleet-private/photo-plan.csv: > ../opleet-private/photo-placed.csvchmod 600 ../opleet-private/photo-plan.csv ../opleet-private/photo-placed.csv( set -a; . ./.env.staging; set +a export V2_URL="$SUPABASE_URL" V2_SERVICE_KEY="$SUPABASE_SECRET_KEY" python3 tools/migration/photo-copy.py place ../opleet-private/photo-plan.csv ../opleet-private/photo-placed.csv) &&stg -c "\copy migration.photo_placed (object_id, v2_path, status, detail) from pstdin with (format csv, header)" < ../opleet-private/photo-placed.csv &&stg <<'SQL'set statement_timeout = 0;select count(*) as reclassified from migration.photo_manifest();select migration.run('photos') as rows_written;SQLIf mirror stops, run its lines again: it skips what's held. If place reports failures, nothing loads. Run the lines from the plan's \copy down again, as they are: a file already at its v2 path is written as placed, already there with no download, so only the failed files are fetched. The same rerun rebuilds photo-placed.csv if it is lost or cut off. Never load a partial photo-placed.csv by hand.
8 · The reconcile
Section titled “8 · The reconcile”The first command prints counts per check. The file holds v1 ids and one driver's amounts, so it stays on your Mac.
stg <<'SQL'set statement_timeout = 0;select * from migration.photo_counts();select check_name, count(*) as lines from migration.reconcile() group by 1 order by 1;SQLstg -c "select * from migration.reconcile()" > ../opleet-private/reconcile-tl-dp-04.txtchmod 600 ../opleet-private/reconcile-tl-dp-04.txt9 · The end of the night, every time
Section titled “9 · The end of the night, every time”Run this block even after a stop, with failed in place of succeeded. It drops the foreign server and the password with it first, so a stop before block 4, with no run to end, still clears them. Then it ends the run, which empties the login tables (R-MIG-09), and deletes the login export.
stg <<'SQL'drop schema if exists v1_live cascade;drop server if exists v1 cascade;select migration.finish_run('succeeded');SQLrm -f ../opleet-private/v1-logins.csv ../opleet-private/v1-identities.csv ../opleet-private/v1-foreign-tables.sqlThen prove the block ran: once, it was pasted and didn't. This reads only counts and the run's status.
stg -At <<'SQL'select 'run ' || id || ' ' || status from migration.runs order by id desc limit 1;select 'foreign servers ' || count(*) from pg_foreign_server;select 'login rows ' || (select count(*) from migration.v1_auth_users) + (select count(*) from migration.v1_auth_identities);SQLecho "files left $(ls ../opleet-private/v1-logins.csv ../opleet-private/v1-identities.csv ../opleet-private/v1-foreign-tables.sql 2>/dev/null | wc -l | tr -d ' ')"10 · The read-only role
Section titled “10 · The read-only role”First run only: make the role. Generate a password of 32 letters and digits in the password manager entry "Opleet v2 staging · read-only". \password hashes it on your Mac, so staging never sees it in plain text. Type the lines at the prompt, one by one, pasting the password twice, then \q.
PGPASSWORD="$STAGING_PW" docker run --rm -it -e PGPASSWORD postgres:17 psql "host=<STAGING_POOLER_HOST> port=5432 dbname=postgres user=postgres.<STAGING_PROJECT_REF> sslmode=require"create role staging_reader login nosuperuser nocreatedb nocreaterole noinherit bypassrls;alter role staging_reader set default_transaction_read_only = on;\password staging_readerEvery run: a rebuild drops the tables, and their grants go with them. The role reads the app's tables, what the night recorded, and tonight's copy of v1. The login tables and auth stay out.
stg <<'SQL'grant usage on schema public, migration, legacy to staging_reader;grant select on all tables in schema public, migration, legacy to staging_reader;revoke select on migration.v1_auth_users, migration.v1_auth_identities from staging_reader;SQLReaching staging with it, through the Supabase CLI (D65):
printf 'staging_reader password: '; read -rs SR; echopnpm supabase db query --db-url "postgresql://staging_reader.<STAGING_PROJECT_REF>:$SR@<STAGING_POOLER_HOST>:5432/postgres" "select count(*) as drivers from public.drivers"unset SRExpect
Section titled “Expect”Counts are from BE-DP-40's run 2 on the copy of 2026-11-05. v1 moves (D75), so a small rise is normal.
- Block 1:
staging postgres, thenv1 read only: on. - Block 2: what BE-M0-33 says (every file under supabase/migrations, and 5 buckets), with
0 other loginsbefore block 5, and 11 E2E logins made. - Block 3:
foreign_tables55 or more. - Block 4: a run id,
tables_copiedthe same number, and no unmapped line. - Block 5: the
\copycommands print nothing, which means nothing went wrong under-q; block 8'saccesschecks show the counts. Thenunmapped 0andunsigned dropped denies 0. - Block 6: master data, vehicles, drivers, rentals and operations as in BE-DP-40, and
money_rowsabout 174,516. Note each step's time: BE-DP-40's six steps took 7 minutes on the Mac, and staging's Micro compute will be slower. Post the times. - Block 7:
objectsabout 75,000.mirrorends withcopiedand nofailedline.place74,587 files on the first run (theattachedandv1_filerows of the plan, 4,801 of them cars' files from v1), counting theplaced, already thereline with theplacedline. A failure line isHTTP 0/<type>after 3 tries. Thenrows_written. - Block 8: the five money checks print no line, so each is 0:
money balance per driver,money credit per driver,money payments per day and account,money paid bills per dayandmoney allocations. The other lists stay open on #544. v1's bills changed on 2026-11-05 and 11-06 (#569), so the money counts may differ from BE-DP-40's run 2. The next night zuki accepts sets the money baseline. Until that night is logged,be-dp-40.expectand the counts above stay as they are. - Block 9: no error, then
run <id> succeeded(orfailed),foreign servers 0,login rows 0andfiles left 0. - Block 10:
driversabove 0.
stg -At <<'SQL'select 'status ' || status from migration.runs order by id desc limit 1;select 'foreign servers ' || count(*) from pg_foreign_server;select 'login rows held ' || (select count(*) from migration.v1_auth_users) + (select count(*) from migration.v1_auth_identities);select 'database ' || pg_size_pretty(pg_database_size(current_database()));SQLprintf 'staging_reader password: '; read -rs SR; echopnpm supabase db query --db-url "postgresql://staging_reader.<STAGING_PROJECT_REF>:$SR@<STAGING_POOLER_HOST>:5432/postgres" "delete from public.drivers where false"unset SRIt prints status succeeded, foreign servers 0, login rows held 0 and the database's size, then an error, cannot execute DELETE in a read-only transaction. Close the terminal afterwards: it holds the passwords from block 1.
The smoke test: open the staging deployment and sign in as the E2E super_admin from .env.e2e. The menu opens on the landing page.
If it fails
Section titled “If it fails”After any stop, run block 9 with failed before anything else, so v2_reader's password and the logins leave staging.
- Block 1 prints
could not translate host nameor a timeout: check the two pooler hosts. Staging's direct host is IPv6 only, so use the session pooler's. - Block 3:
permission denied for foreign-data wrapper postgres_fdw: hostedpostgrescan't use the wrapper as this step assumes. Post the line on #187; the CTO changes the step. - Block 3:
permission denied for tableorschema, or block 4 stops on a v1 relation: post the table's name on #183. TL-DP-01's default privileges may not cover a table v1's own role created. - Block 3 or 4:
type "…" does not exist: a v1 type the case in block 3 doesn't cover. Post the line; never add the type to v2. canceling statement due to statement timeout: a block lost its first line. Run it again as written.- Block 4 or 6 stops with
could not write, a dropped connection orserver closed the connection: staging's Micro compute may be too small. Post the block and its time on #187, with the database's size. Don't change the compute size without a word from the CTO: it costs money. - Block 6 stops: post the
ERRORline. Read it first: drop anything with a name or an amount. mirrororplaceHTTP 400on every file:.env.stagingneeds the secret key, not the service role key. Then read BE-DP-23's If it fails.- On the second run,
mirrorcopies everything again: the rebuild emptiedv1-hold. Note it on #187: every night then takes the full 92 minutes, and the job needs a different plan. - On the second run,
staging_readeris missing: the rebuild dropped the role. Post it on #187, and make it again. pnpm supabase db queryrefuses--db-url, or can't sign in as staging_reader: post the line on #187. The role is fine if psql reaches it; only the way in changes.- Block 2's
delete from auth.usersis refused: post the line on #187, and don't delete the logins by hand. - Anything else in block 2: BE-M0-33's If it fails.
Nothing here writes to v1. Staging is rebuilt by the next run of this step, or by BE-M0-33. To remove the read-only role: stg -c "drop owned by staging_reader; drop role staging_reader", then clear its password manager entry. The CSVs in ../opleet-private go with BE-DP-08's Undo.
Then: L1 and L2
Section titled “Then: L1 and L2”- L1 is one script that wraps blocks 2 to 10 with no prompts, and it prints counts only. BE writes it (#524's script, extended to staging), and it needs
photo-copy.pyto read the photo reader's password from the environment. zuki logs one clean run of it on #187, which closes this row. - L2 is the GitHub Actions job, in the
stagingenvironment that only main can use. It holds v2_reader's, the photo reader's and staging's secrets, and an always-run step does block 9's work. Its report goes to a Slack webhook if that costs nothing extra, otherwise email (D36). It's written after L1, from what these runs measured, and zuki turns it on.
Row 10, the first full night, is the first run of that job.
Log each run as a comment on #187: the date, main's commit, each block's time, the lines of Expect and Check as counts, and for the second run what block 2 found. Counts, statuses and check names only (D15): never a line of a CSV or of the reconcile file, a password or a key.