B2B Second Inventory Reports Deploy Notes
Deploy order, migration 0069, the locks it takes, the required permissions:sync, the manual grant step, why the API and worker deploy together, and rollback limits for bb phase 6. Phase 6 ships as its own release.
B2B Second Inventory (bb) — Reports Deploy Notes
These notes cover migration 0069_bb_report_exports. The earlier steps in Deploy Notes (phase 1), Stock Deploy Notes (phase 3), Orders Deploy Notes (phase 4) and Returns and Refunds Deploy Notes (phase 5) still apply. This page adds what phase 6 changes.
Commands marked not run were read against the repo but were not executed against a database when this page was written.
1. Deploy order
Phase 6 is a separate release, after the phase 4 and 5 release has settled.
- Back up with
pg_dump. - Check the precondition: the production migration ledger head is below
0069(production was at0059). Afterpnpm db:migratein step 3, the ledger head is0069(70 rows). On production this release took the ledger from0059to0069. If the head is not0069after the migrate, stop. - Migrate in a quiet window with no catalog or inventory job queued or running:
pnpm db:migrate. See section 2. - Deploy the API and the worker together. They are one process (section 3).
- Sync permissions:
pnpm --filter @nano-store/api permissions:sync. Required. - Deploy Admin #2.
- Grant: a superadmin opens Admin #2 Roles and gives each bb role that should see reports
BB_Reports_READ. The grant shows buyer names and what each buyer spent, even withoutBB_Customers_READ, so give it on purpose. - Admin #1 is not deployed.
permissions:sync is required
The migration inserts no permission row. permissions:sync creates five BB_Reports_ codes (CREATE, READ, UPDATE, DELETE, RESTORE) and grants them to nobody: superadmin takes every code except BB_* codes, and admin takes only what isGrantableToAdmin allows, which excludes every BB_* code (apps/api/scripts/sync-permissions.ts). Without the sync the Roles screen has no Reports module, and bb staff get 403 while a superadmin still passes every check.
Only BB_Reports_READ is checked by any route. The other four exist and do nothing.
2. What migration 0069 locks
0069_bb_report_exports.sql changes the bb_catalog_job CHECK constraints so a report job is allowed, and builds three read indexes. drizzle-kit migrate applies every pending migration in one transaction, so every lock lasts to the end of the batch. The file starts with SET LOCAL lock_timeout = '5s'.
| Order | Lock | Tables | Why |
|---|---|---|---|
| 1 | SHARE | bb_customers, bb_inventory_movement, bb_refund, in that order | The three CREATE INDEX IF NOT EXISTS statements run under it |
| 2 | ACCESS EXCLUSIVE | bb_catalog_job | Dropping and re-adding the CHECKs |
- Movement is locked before refund because order cancel and return inspect write them in that order, so the file cannot deadlock against them.
- While the
SHARElock is held, every action that writes stock or a refund waits until the batch commits: orders, cancels, returns, refund decisions, customer edits, stock adjustments and imports. Reads of those three tables do not block. - The
ACCESS EXCLUSIVElock comes only after the index builds, so job enqueue, claim and sweep block only for the short constraint change. Reads and writes ofbb_catalog_jobboth block during it. - A lock that cannot be had within 5 s aborts the whole batch with nothing applied and the ledger unchanged. Run it again.
- An import writes movements for its whole run, so it holds the migration off.
- The inventory reconcile runs hourly (
bb-inventory-reconcile.scheduler.ts); avoid the top of the hour.
Large tables
With 100,000 or more rows in a table, build its index first, out of band, with CREATE INDEX CONCURRENTLY under the same name and definition. IF NOT EXISTS then skips it in the migration. IF NOT EXISTS also skips the invalid leftover of a failed build, so first check pg_index.indisvalid for bb_customers_created_idx, bb_inventory_movement_created_idx and bb_refund_settled_idx. For any that is false, drop that index and build it again. Not run.
The file's SHA-256 at the time of writing is bc9fce2d6c53a0074d34d770c8e3e1e550195604a2d7b39b8582c62b0c13115f (checked with sha256sum). The migration ledger matches a row by this hash, so an edited 0069 will not match.
3. API and worker deploy together
The worker is not a separate process. BbModule imports BbCatalogWorkerModule, which provides the queue processor, the job runner and the export handler, and the only entrypoint is apps/api/src/main.ts ("start": "node dist/src/main.js"; package.json has no worker script).
"Together" therefore means no mixed-version replicas. An old replica that claims a report job has no branch for it:
- for a non-superadmin's job, the claim is refused;
- for a superadmin's job, the handler throws before it opens a file, retries, and ends the job
failedwith the generic sentence.
This is read from the code and was not run against two versions. In a rolling deploy, drain or stop old replicas before anyone can queue a report. Report exports cannot be queued until Admin #2 ships, so deploying the API first is safe.
4. Intermediate states
| State | Effect |
|---|---|
| New backend, old Admin #2 | Supported for the existing screens. No reports screen exists |
| Old backend, new Admin #2 | Not supported. The Reports routes answer 404 |
| Admin #1 and the storefront | No effect |
5. Rollback
Redeploy the old images first. They run on the new schema, because the CHECKs only widen for what they write. To stop report requests before rolling an image back, remove BB_Reports_READ from the roles.
The schema is reversible without data loss only until the first report row exists in bb_catalog_job. After that, the down file's guard raises and refuses, and 0069 is forward-fix only. Getting past the guard means deleting a record of who exported what. Only the owner (Nav), in writing, may authorise that.
The down file, packages/db/src/migrations/down/0069_bb_report_exports.down.sql:
- sets
lock_timeoutto 5 s and takesSHAREon the three tables, thenACCESS EXCLUSIVEonbb_catalog_job; - refuses if any
reportrow exists, naming the count; - restores both CHECKs, drops
chk_bb_catalog_job_report_is_export, and drops the three indexes.
Reads of the three tables block while it runs.
migrate-down.ts refuses a production database URL, so a production rollback is by hand, with a backup first. migrate-down.ts itself runs the down file and deletes the ledger row by hash in one transaction; a by-hand rollback has to do the same two things.
Run from packages/db. Verified on a scratch database on 2026-11-06:
psql -1 -v ON_ERROR_STOP=1 \
-f packages/db/src/migrations/down/0069_bb_report_exports.down.sql \
-c "DELETE FROM drizzle.__drizzle_migrations WHERE hash = 'bc9fce2d6c53a0074d34d770c8e3e1e550195604a2d7b39b8582c62b0c13115f'"-1 runs the file and the delete in one transaction, and ON_ERROR_STOP aborts it on the first error. psql -f without -1 fails on the LOCK statements outside a transaction. A ledger row left behind stops 0069 from applying again.
What the check covered: a fresh database named *_test, migrated to head (70 ledger rows, the three indexes and chk_bb_catalog_job_report_is_export present). After the command above: the three indexes and the CHECK were gone, the 0069 ledger row was deleted (69 rows), and the command exited 0. pnpm db:migrate then re-applied 0069 cleanly (70 rows, objects back). The check ran with no report rows, so the guard's refusal path was not exercised here. The sha256 above matched 0069_bb_report_exports.sql on that day; re-hash the file at the release commit before running this on production.
6. See Also
- Reports API reference
- Reports backend
- Returns and Refunds Deploy Notes (phase 5)
- Deploy Notes (phase 1)