BE-M0-33 · Rebuild staging from main, by hand
ID BE-M0-33 · Level L0, and stays L0 until the nightly rehearsal job replaces it · Pack Pack F · Staging · Run order row 32, then whenever main's migrations or seed change · Issue #22
Staging is wiped and rebuilt from main: every migration on an empty database, then the seed, then the E2E logins. It holds exactly what main holds again.
Until cutover, migration files are edited in place (D11). An edited file keeps its version, so db push sees nothing new and staging silently keeps the old SQL. A rebuild is the only way an edit reaches staging. The nightly rehearsal job will do this every night once it exists. Until then, zuki runs this step by hand.
Not reversible. It deletes everything on staging. Today that is only the seed and the test logins. Never on production: block 3 refuses to run unless the linked project is the one .env.staging names.
When to run it
Section titled “When to run it”- A pull request merged into main changed a file under supabase/migrations or supabase/seed.sql since staging was last built. Block 1 shows this.
- BE-M0-29 failed partway, and the fix has merged.
Before
Section titled “Before”- BE-M0-28 is Done: the CLI is linked to staging.
- The checks on main's latest commit are green, the database job included (TL-M0-18).
- You know the commit staging was last built from. It's in the newest run comment of BE-M0-29 or of this step. Below it is <LAST_BUILT_COMMIT>.
- The password manager is open on the "Opleet v2 staging" entry.
1 · From main, clean, linked to staging, and what changed since the last build
Section titled “1 · From main, clean, linked to staging, and what changed since the last build”cd ~/opleet-v2/opleetgit switch main && git pullgit status --shortref="$(cat supabase/.temp/project-ref)"; want="$(sed -n 's/^STAGING_PROJECT_REF=//p' .env.staging)"; [ -n "$ref" ] && [ "$ref" = "$want" ] && echo "linked to staging" || echo "STOP: not linked to staging"git log --oneline <LAST_BUILT_COMMIT>..HEAD -- supabase/migrations supabase/seed.sqlIf the last command prints nothing, staging already matches main. Stop here; there is nothing to rebuild.
2 · Who has a login on staging
Section titled “2 · Who has a login on staging”This prints counts only, never an address.
( set -a; . ./.env.staging; set +a curl -s "$SUPABASE_URL/auth/v1/admin/users?per_page=1000" -H "apikey: $SUPABASE_SECRET_KEY" -H "Authorization: Bearer $SUPABASE_SECRET_KEY" | python3 -c 'import json, sys; u = json.load(sys.stdin)["users"]; e = sum(x.get("email", "").endswith("@e2e.opleet.test") for x in u); print(e, "E2E logins,", len(u) - e, "other logins")')Go on only if it prints "0 other logins". A rebuild removes every profile, so any other person would lose their access.
3 · Wipe staging and rebuild it from main
Section titled “3 · Wipe staging and rebuild it from main”The test runs first, and the reset runs only if it passes: the linked ref must be non-empty and equal the one .env.staging names, so two missing files don't pass. The CLI asks before it resets: answer y. If it asks for the database password, paste <STAGING_DB_PASSWORD>.
ref="$(cat supabase/.temp/project-ref)"; want="$(sed -n 's/^STAGING_PROJECT_REF=//p' .env.staging)"; [ -n "$ref" ] && [ "$ref" = "$want" ] && pnpm supabase db reset --linked4 · Clear the old E2E logins
Section titled “4 · Clear the old E2E logins”Run block 2 again.
- If it prints "0 E2E logins, 0 other logins", the reset removed them. Go on to block 5.
- If it prints "11 E2E logins", the reset kept the logins but removed their profiles. Delete them before block 5, because the script can't attach a role to a login without a profile. In the dashboard, open Authentication › Users, search for e2e.opleet.test, select all eleven, and delete them. Then run block 2 once more: it must print "0 E2E logins, 0 other logins".
5 · Make the E2E logins again
Section titled “5 · Make the E2E logins again”pnpm db:e2e-users --staging6 · Note the commit staging was built from
Section titled “6 · Note the commit staging was built from”git rev-parse --short HEADThis is <LAST_BUILT_COMMIT> for the next run.
Expect
Section titled “Expect”- Block 1:
git status --shortprints nothing; then "linked to staging"; then one or more commits that changed a migration or the seed. - Block 2: "11 E2E logins, 0 other logins".
- Block 3: a question asking whether to reset the remote database. After y, it resets, applies every file under supabase/migrations in order (20 today;
ls supabase/migrations | wc -lcounts them), seeds from supabase/seed.sql, and ends with "Finished supabase db reset". - Block 4: "0 E2E logins, 0 other logins", straight away or after the deletes.
- Block 5: eleven lines starting with "made", then "11 logins in the E2E organization, all pools. .env.e2e updated (password not shown)."
- Block 6: main's newest commit, in seven characters.
Run the Check blocks of BE-M0-29 and BE-M0-30. They must print what follows. BE-M0-29's page says 13 migrations and four buckets, which was true at its first push; main has more of both now.
- one matching row per file under supabase/migrations (20 today;
ls supabase/migrations | wc -lcounts them), each with the same version under Local and Remote; [{"slug":"e2e"}], 11 system roles and five buckets:['documents', 'inspections', 'payments', 'screening', 'v1-hold'];- the eleven roles with their counts;
- anon 42501.
If it fails
Section titled “If it fails”- Block 1 doesn't print "linked to staging": stop. Don't reset anything. Follow BE-M0-28's If it fails.
- Block 2 prints other logins: stop. Don't reset. Paste the counts into the issue you log on, and ask zuki. Once v1's data is on staging, the rehearsal job takes over, and it handles real logins.
- Block 3 prints nothing and returns to the prompt: the test failed, so the reset didn't run. The linked project isn't the one .env.staging names, or one of the two files is missing or empty. Stop, and follow BE-M0-28's If it fails.
- Block 3 stops at a migration with an error: staging is left half built. Paste the error with its file name. The fix lands on main through a pull request; then run this step again from block 1.
- Block 3 prints "Usage", or doesn't know --linked: the CLI isn't the pinned one. Run
pnpm install, thenpnpm supabase --version, and paste both outputs. - Block 5 fails with a 409 or "violates foreign key constraint": a login without a profile was left behind. Do block 4's deletes, then block 5 again.
- A check differs from its page: follow that page's If it fails.
Not reversible. What was on staging is gone; running this step again rebuilds it from main. Never run any part of it against production. If block 1's test fails, nothing has happened yet.
Log each run as a comment on the pull request whose merge made the rebuild necessary, or on #22 for a run inside pack F. Include the date, the commit from block 6, block 2's counts before and after, the last line of block 3, and the checks' output. Never paste the database password, the secret key or a login's address or password.