B2B Second Inventory Deploy Notes
Pre-deploy prod-dump replay, rollback runbooks A and B for migration 0060, and post-deploy verification for bb phase 1.
B2B Second Inventory (bb) — Deploy Notes
These notes cover migration 0060_bb_foundation (the bb_role, bb_customers and bb_audit tables plus the bb_audit_reject_mutation() function). They restate the plan's runbooks. Where the plan is vague, this page says so instead of filling the gap.
1. Pre-deploy: prod-dump replay
The replay is manual. No script performs it. It needs a production dump supplied by the owner.
-
Create and restore the replay database:
createdb nanostore_prodreplay pg_restore --no-owner -d nanostore_prodreplay <dump> -
Run the migrations with
DIRECT_DATABASE_URLpointing at the replay database, as the same DB role the app connects as. -
Run
permissions:sync, then the two SQL counts. -
Record whether the
customerrole exists. This measures the exposure for Follow-up 1 in the plan:SELECT name FROM role WHERE name IN ('customer','admin','superadmin'); -
Run the down migration (
--to=0060), then up, once on the replay.
Expected post-sync counts: 20 / 0.
The first number is the count of BB_* permission rows. 4 modules (BB_Roles, BB_Staff, BB_Audit, BB_Customers; packages/db/src/authorization/permission-catalog.ts) times 5 actions gives 20:
SELECT count(*) FROM permission WHERE module LIKE 'BB\_%' ESCAPE '\';The second number is the count of BB_* permissions held by the superadmin role. It must be 0, because the superadmin row holds no BB_* code.
SELECT count(*) FROM role_permission rp
JOIN role r ON r.id = rp.role_id
JOIN permission p ON p.id = rp.permission_id
WHERE r.name = 'superadmin' AND p.module LIKE 'BB\_%' ESCAPE '\';This is the owner-confirmed post-sync check ("deploy check 2" in the plan): the count of role_permission rows linking the superadmin role to a BB_* permission must be 0. The query joins role_permission, role and permission, as defined in packages/db/src/schema/roles.ts.
Run the sync from the repo root:
pnpm --filter @nano-store/api permissions:syncThis runs ts-node --project tsconfig.json scripts/sync-permissions.ts (apps/api/package.json).
Replay rules
| Rule | Reason |
|---|---|
The replay DB name must not match \bprod(uction)?\b | migrate-down.ts refuses URLs that look like production |
Never set TEST_DATABASE_URL to the replay DB | Plan rule, citing learning a-restore-revives-every-credential-revoked-after-the-dump |
| Never connect a live app to the replay DB | Same learning: a restore revives credentials revoked after the dump |
The refusal in packages/db/src/scripts/migrate-down.ts reads:
Refusing to run: the database URL looks like production. Reverse it by hand, with a backup taken first.
Production deploy order (from the plan)
- Back up:
pg_dump -Fc "$DIRECT_DATABASE_URL" -f nanostore-pre-bb-$(date +%F).dump, then runpg_restore --liston the file to prove it is readable. - In a quiet window, run
pnpm db:migrate. - Deploy the code. Wait until every instance runs it. No old instance may still serve.
- Run
permissions:sync, then the two SQL counts. - Smoke test:
- Superadmin on the bb routes.
- A retail admin on
GET /roles,/teamand/permissions. - Superadmin's
GET /auth/permissionsholds noBB_code.
2. Rollback
migrate-down.ts refuses production URLs, so every production rollback step is by hand, with the backup already taken.
Down-file guard
0060_bb_foundation.down.sql refuses to run, naming the row counts, while any of bb_role, bb_customers or bb_audit holds a row. Dropping bb_role would turn every bb role into a retail role and show every bb staff member in retail /team.
Every bb write also writes bb_audit, and its append-only trigger refuses DELETE. After the first bb write the guard therefore always fires: 0060 is forward-fix only after first use. Getting past the guard means truncating bb_audit or dropping its trigger, which destroys the audit trail. Only the owner, in writing, may authorise that. No agent and no runbook step does it.
Runbook A: before first bb use (all three bb tables empty)
In one transaction:
- The body of
0060_bb_foundation.down.sql. DELETE FROM drizzle.__drizzle_migrations WHERE hash = '<0060 hash>'. Read the hash from the ledger row first. This is the same pair of statementsmigrate-down.tsruns.DELETE FROM permission WHERE module LIKE 'BB\_%' ESCAPE '\'.
Then roll back the code.
Read the ledger, newest first, then compute the file hash and match it:
SELECT id, hash FROM drizzle.__drizzle_migrations ORDER BY id DESC;sha256sum packages/db/src/migrations/0060_bb_foundation.sqlThe ledger hash column equals that SHA-256 hex digest (packages/db/src/scripts/migrate-down.ts). Use the digest in the DELETE ... WHERE hash = '<0060 hash>' above.
Runbook B: after first bb use (code-only rollback, schema stays)
Requires the owner's written sign-off for the leak window (AR-14 in the plan).
-
Ban bb staff first, recording who.
\copy (SELECT id FROM admin_users WHERE role_id IN (SELECT role_id FROM bb_role) AND banned IS NOT TRUE) TO 'bb-banned-<date>.csv' UPDATE admin_users SET banned = true, ban_reason = 'bb code rollback <date>' WHERE id IN (<those ids>)A ban is enforced on the next request.
-
Snapshot bb grants.
\copy (SELECT r.public_id, p.module, p.action FROM role_permission rp JOIN bb_role b ON b.role_id = rp.role_id JOIN role r ON r.id = rp.role_id JOIN permission p ON p.id = rp.permission_id) TO 'bb-grants-<date>.csv' -
Delete the bb permission codes:
DELETE FROM permission WHERE module LIKE 'BB\_%' ESCAPE '\'This destroys every bb grant (
role_permissioncascades). The file from step 2 is the only copy. -
Roll back the code.
Exposed while old code runs:
- bb roles list in
/rolesand can be renamed. - A staff-less bb role can be deleted, cascading its
bb_rolerow. - Banned bb staff list in
/teamand a retail admin could unban or re-role them. - Deleting a bb role that still has (banned) staff stays blocked by
ROLE_IN_USE.
Roll-forward:
- Deploy the new code to every instance.
- Run
permissions:sync. - Re-insert the grants from the step 2 file, joined by
(role.public_id, module, action). - Unban exactly the ids in the step 1 file.
- Run the deploy checks.
Schema rollback after first use
Not a runbook step. It needs the owner's written decision to destroy bb_audit.
3. Verification after deploy
Run the live checks through the boot wrapper. scripts/verify-api.sh boots the compiled API, runs the checks script with BASE_URL set, and always tears down:
scripts/verify-api.sh scripts/checks/bb-phase1.shMigration 0060 must already be applied to the database the booted API uses. The script writes idempotent fixtures named zz-bbcheck-* (one bb role, one bb staff row).
| Variable | Required | Meaning |
|---|---|---|
BB_CHECK_SUPERADMIN_EMAIL | Yes | Superadmin login |
BB_CHECK_SUPERADMIN_PASSWORD | Yes | Superadmin password |
BB_CHECK_RETAIL_ADMIN_EMAIL | Yes | Login holding the seeded admin role |
BB_CHECK_RETAIL_ADMIN_PASSWORD | Yes | Password for that login |
BB_CHECK_DATABASE_URL | No | psql URL of the DB the booted API uses. Defaults to DATABASE_URL from apps/api/.env |
Login is rate limited (5 per 15 minutes per IP), so the script caches tokens in a private temp directory and reuses them.
Run
bb-phase1.shandverify-api.shonly against the prod-dump replay or a staging database. Never run them against production: they boot a local API process and writezz-bbcheck-*fixtures.
B2B Second Inventory API Reference
Endpoint reference for the bb phase 1 admin API — roles, permissions, staff, customers and audit — with contracts and error codes.
B2B Second Inventory Catalog Feature Overview
Phase 2 of the bb (B2B second inventory) area — a catalog of categories, brands, series, tags, products, variants and option axes, with bulk actions and import/export jobs, kept apart from the retail catalog.