Skip to content

BE-DP-23 · The photo copy on the local stack: every v1 file held, then attached where its step has run

ID BE-DP-23 · Level L0 · Pack Pack R6 · The photo copy · Run order row 6, after BE-DP-22; in a night, after row 5's steps and before the reconcile · Issue #262

Every file in v1's bucket mop-files is copied into the local stack's private bucket v1-hold, under its object id, and migration.file_map says where each one stands (R-MIG-07): attached to its record, a file from v1 on its driver or car, held because its owner can't be told, waiting for the step that loads its owner or record, or not copied (R-STO-20). A file goes to its record's D14 path only when that record has been loaded tonight; until then it waits, and is never kept as a file from v1 just because its step hasn't run (R-STO-23, the CTO on #262).

Until row 5's domain steps have merged, none of them runs, so every file waits: this run copies the bytes and counts them per folder. When a step merges, the next run of this step attaches that step's folders.

It reads v1 two ways, both read-only (D68): the object list through v2_reader (TL-DP-02), and the files through the photo reader login (TL-DP-03). It writes only to the local stack. The CSVs it writes hold file names and paths, so they stay in ../opleet-private, mode 600; you post counts only (D15).

  • BE-DP-22 is done, and you are at the root of your clone on main once pack R6 has merged, or on its branch be/262-photo-copy before that. The local stack is running on that schema (pnpm supabase db reset after switching).
  • docker ps --filter name=v1copy lists the copy as Up (BE-DP-06).
  • At least 15 GB free on the disk Docker uses: v1 holds about 5.6 GB of files and grows every day, and storage held about 12 GB after placing (v1-hold plus the placed copies). df -h ~ shows it.
  • From the password manager: "Opleet v1 · v2_reader" (the pooler host and project ref), "Opleet v1 · photo reader" (its email and password), and v1's publishable key (TL-DP-03's <V1_PUBLISHABLE_KEY>).
  • About two hours: block 3 took 92 minutes for 71,830 files, photo_manifest() 2.5 minutes and block 5's place 8 minutes. Block 3 is a top-up: if it stops, run it again and it carries on.
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select migration.begin_run('BE-DP-23, the photo copy, local') as run;
select migration.snapshot() as tables_copied;
select coalesce(string_agg(proname, ', ' order by proname), '(none)') as domain_steps
from pg_proc where pronamespace = 'migration'::regnamespace and proname like 'step\_%' and proname <> 'step_photos';
SQL

If domain_steps lists any step (row 5's packs have merged), run them now, in the timeline's order, before block 2, each as select migration.run('<name>'); with <name> the step without step_: master_data, access, vehicles, drivers, rentals, operations, money, whichever are listed. The access step needs v1's logins loaded first, as in BE-DP-29's block 2; skip it if you haven't loaded them tonight. If it prints (none), go on to block 2.

One password prompt: paste <V1_READONLY_PASSWORD>.

Terminal window
docker run --rm -it -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" -v ON_ERROR_STOP=1 -f /sql/photo-objects.sql
chmod 600 ../opleet-private/mop-files-copy-objects.csv

The first line reads the local stack's URL and secret key into this shell only; never print or paste them. Storage answers HTTP 400 to the service role JWT, so the copy uses SECRET_KEY. The script asks for the photo reader's password.

Terminal window
eval "$(pnpm supabase status -o env | grep -E '^(API_URL|SECRET_KEY)=')"
export V2_URL="$API_URL" V2_SERVICE_KEY="$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

4 · Load both lists, and classify every file

Section titled “4 · Load both lists, and classify every file”
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "truncate migration.v1_objects, migration.photo_copied"
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "\copy migration.v1_objects from pstdin with (format csv, header)" < ../opleet-private/mop-files-copy-objects.csv
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "\copy migration.photo_copied (object_id, status, detail, size) from pstdin with (format csv, header)" < ../opleet-private/photo-copied.csv
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "select * from migration.photo_manifest()"
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -c "select folder, reason, count(*) as files from migration.file_map where status in ('waiting', 'held', 'not_copied') group by 1, 2 order by 1, 2"

5 · Only if block 4 lists attached or v1_file: place those files, and write their rows

Section titled “5 · Only if block 4 lists attached or v1_file: place those files, and write their rows”

Skip to block 6 when block 4 lists no attached and no v1_file line, which is the case until a domain step has run in block 1.

place needs V2_URL and V2_SERVICE_KEY from block 3's first two lines. Run this block in the same shell as block 3, or run those two lines again first. It stops at a failed place, so nothing loads a failure. The plan marks each file already at its path in its bucket, and place skips it without reading v1-hold (R-STO-24); it never overwrites or deletes a file.

Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "\copy (select * from migration.photo_plan()) to pstdout with (format csv, header)" > ../opleet-private/photo-plan.csv
touch ../opleet-private/photo-placed.csv
chmod 600 ../opleet-private/photo-plan.csv ../opleet-private/photo-placed.csv
python3 tools/migration/photo-copy.py place ../opleet-private/photo-plan.csv ../opleet-private/photo-placed.csv &&
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "\copy migration.photo_placed (object_id, v2_path, status, detail) from pstdin with (format csv, header)" < ../opleet-private/photo-placed.csv &&
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select count(*) as reclassified from migration.photo_manifest();
select migration.run('photos') as rows_written;
SQL

If photo_placed refuses a row as a duplicate on a second night, run truncate migration.photo_placed first and load the file again: a file already at its path is reported as placed again.

6 · The counts, the reconcile, and the end of the run

Section titled “6 · The counts, the reconcile, and the end of the run”
Terminal window
docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
select * from migration.photo_counts();
select check_name, count(*) as rows from migration.reconcile() group by 1 order by 1;
select migration.finish_run('succeeded');
SQL
  • Block 1: a run id, tables_copied 35 (or more if v1 has grown), and domain_steps (none) until row 5's packs merge.
  • Block 2: COPY and a number near 71,800 (v1 listed 71,830 objects on the last run and grows), then objects, folder_markers 1 and size about 5,578 MB.
  • Block 3: objects listed: N · already held: 0 on the first run (on a later run, how many it skips), then a progress line every 1,000 files, then mirror: N files in … s (5,532 s for a full copy) and its counts: copied: N, and failed (…) lines only if some files couldn't be read. With any failure it ends mirror: N of M files failed and exits non-zero: see If it fails before block 4.
  • Block 4: photo_manifest() takes about 2.5 minutes at this size, then a table of folder, status, files. With no domain step run, every folder's files are waiting, apart from any not_copied. Then the reasons table: one waiting for the vehicles step line for each car folder (checkpoint, vehicle, asuransi, etle) and one waiting for the drivers step line for each driver folder (bc, driver, payment, rebate). With the vehicles and drivers steps run but not operations, the checkpoint, asuransi and etle files wait for operations, and the vehicle folder's photos are already v1_file with its STNKs attached.
  • Block 5: skipped when block 4 lists no attached and no v1_file. On a night with domain steps: place: N files in … s with placed: N, where N is the attached and v1_file files not yet placed (69,731 files in 475 s on the last run), and placed, already there: N for the files already at their path, which it doesn't read again. Then reclassified and rows_written; the photos step took 6.5 s. With any failure it ends place: N of M files failed, exits non-zero and loads nothing. A dropped connection or a 5xx is tried three times before it counts as failed.
  • Block 6: one line per folder (asuransi, bc, checkpoint, driver, etle, payment, rebate, vehicle) with attached, v1_file, held, waiting, not_copied and total. Every total is that folder's files; with no domain step, waiting is the total minus not_copied. The reconcile lists files not copied with the same number as the sum of not_copied, or nothing when it is 0, then succeeded.

Post on #262: block 2's three numbers, block 3's counts, block 4's two tables, and block 6's table and reconcile lines. Folder words, statuses, reasons and counts only: never a line of the CSVs, which hold names.

Block 3:

  • failed (v1 HTTP 400/404): v1 deleted those files between block 2 and block 3 (v1 is live). Run blocks 2 and 3 again once: the second run only fetches what's missing. What still fails goes on #262 with its count.
  • failed (v1 HTTP 400/403) or 401, or sign in to v1 … HTTP 400: the photo reader can't read. Stop, and post the line on #240.
  • listing v1-hold on v2: HTTP 400: first check that V2_SERVICE_KEY holds SECRET_KEY: Storage answers 400 to the service role JWT, whose ES256 signature it doesn't accept. Run block 3's first two lines again, then block 3. Still 400 with SECRET_KEY: the local stack is on a schema without pack R6. Switch to main or the branch, pnpm supabase db reset, and run block 3 again.
  • failed (v2 …): the local stack refused the upload. Post the line on #262.
  • It stops halfway (the Mac slept, the network dropped): run block 3 again. It skips every file already held at its size.
  • V2_SERVICE_KEY is not set: your CLI names the key differently. pnpm supabase status -o env | cut -d= -f1 lists the names only; use the one ending in SECRET_KEY in the first line instead, and run block 3 again.

Block 4:

  • A held line with a v1 folder the photo copy has no rule for: v1 has started a new top folder. Post the folder word and its count on #262; those files stay held, never dropped, until the Backend Engineer adds the folder.
  • An ERROR naming a legacy. table: block 1's snapshot didn't run in this database. Run block 1 again, then block 4.

Block 5:

  • failed (hold HTTP 0/…) or failed (v2 HTTP 0/…), or a 5xx: the connection dropped three times in a row (the word after HTTP 0/ is the error's type, such as TimeoutError). Run block 5 again as it is: the plan lists the same files, every file already at its path is skipped without a download, and only the failed ones are read from v1-hold again. The same rerun rebuilds a lost photo-placed.csv, because every placed file is skipped and written to it again. If the same files fail again, post the line on #262.
  • failed (hold HTTP 400/404) on every file: V2_SERVICE_KEY is the service role key, and Storage answers Bucket not found to it. Run block 3's first two lines, which read SECRET_KEY, then run truncate migration.photo_placed if an earlier run loaded the failures, and run block 5 again.
  • failed (v2 HTTP 400/413): a file is larger than its bucket allows (the payments bucket takes 5 MB). It stays counted as not copied. Post the count on #262. The block stopped before loading, so run its last two commands by hand: they load the placed files and write their rows.
  • Before reloading after a failed run: docker exec -i supabase_db_opleet psql -qX -U postgres -d postgres -v ON_ERROR_STOP=1 -c "truncate migration.photo_placed", then run block 5 again. A file already at its path is reported as placed again.

Block 6: a files not copied count above the sum of block 3's failures means some planned files weren't placed: run block 5 again.

Nothing here writes to v1. On the local stack, pnpm supabase db reset empties migration and the v2 rows; the bytes in v1-hold stay on Docker's volume until pnpm supabase stop --no-backup, which deletes all local data. ../opleet-private keeps the three CSVs with the dump; they go with BE-DP-08's Undo.

Log each run as a comment on #262: date · who · result · notes, with the counts above.