Nano Store - Ecommerce Docs
Developer Resourcesb2b-second-inventory

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.

  1. Create and restore the replay database:

    createdb nanostore_prodreplay
    pg_restore --no-owner -d nanostore_prodreplay <dump>
  2. Run the migrations with DIRECT_DATABASE_URL pointing at the replay database, as the same DB role the app connects as.

  3. Run permissions:sync, then the two SQL counts.

  4. Record whether the customer role exists. This measures the exposure for Follow-up 1 in the plan:

    SELECT name FROM role WHERE name IN ('customer','admin','superadmin');
  5. 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:sync

This runs ts-node --project tsconfig.json scripts/sync-permissions.ts (apps/api/package.json).

Replay rules

RuleReason
The replay DB name must not match \bprod(uction)?\bmigrate-down.ts refuses URLs that look like production
Never set TEST_DATABASE_URL to the replay DBPlan rule, citing learning a-restore-revives-every-credential-revoked-after-the-dump
Never connect a live app to the replay DBSame 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)

  1. Back up: pg_dump -Fc "$DIRECT_DATABASE_URL" -f nanostore-pre-bb-$(date +%F).dump, then run pg_restore --list on the file to prove it is readable.
  2. In a quiet window, run pnpm db:migrate.
  3. Deploy the code. Wait until every instance runs it. No old instance may still serve.
  4. Run permissions:sync, then the two SQL counts.
  5. Smoke test:
    • Superadmin on the bb routes.
    • A retail admin on GET /roles, /team and /permissions.
    • Superadmin's GET /auth/permissions holds no BB_ 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:

  1. The body of 0060_bb_foundation.down.sql.
  2. DELETE FROM drizzle.__drizzle_migrations WHERE hash = '<0060 hash>'. Read the hash from the ledger row first. This is the same pair of statements migrate-down.ts runs.
  3. 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.sql

The 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).

  1. 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.

  2. 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'
  3. Delete the bb permission codes:

    DELETE FROM permission WHERE module LIKE 'BB\_%' ESCAPE '\'

    This destroys every bb grant (role_permission cascades). The file from step 2 is the only copy.

  4. Roll back the code.

Exposed while old code runs:

  • bb roles list in /roles and can be renamed.
  • A staff-less bb role can be deleted, cascading its bb_role row.
  • Banned bb staff list in /team and 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:

  1. Deploy the new code to every instance.
  2. Run permissions:sync.
  3. Re-insert the grants from the step 2 file, joined by (role.public_id, module, action).
  4. Unban exactly the ids in the step 1 file.
  5. 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.sh

Migration 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).

VariableRequiredMeaning
BB_CHECK_SUPERADMIN_EMAILYesSuperadmin login
BB_CHECK_SUPERADMIN_PASSWORDYesSuperadmin password
BB_CHECK_RETAIL_ADMIN_EMAILYesLogin holding the seeded admin role
BB_CHECK_RETAIL_ADMIN_PASSWORDYesPassword for that login
BB_CHECK_DATABASE_URLNopsql 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.sh and verify-api.sh only against the prod-dump replay or a staging database. Never run them against production: they boot a local API process and write zz-bbcheck-* fixtures.