Skip to content
MSO CloudDocumentation

ADR 0028 — Brand-scoped ingest: shop ownership registry + one ingest-brand binding

On this page
  • Status: Accepted (2026-08-06; adversarial review, operator ruling)
  • Owners: platform
  • Supersedes: nothing; extends §82 (org → brand tenancy) to the INGEST plane.

Operator ruling (binding)#

  1. R1 — Ingest (CSV/Excel upload, connectors) operates under ONE specific active brand. Data entered while brand X is active belongs to brand X. Always.
  2. R2 — Ingest under brand A colliding with, overwriting, or deleting brand B's data must be IMPOSSIBLE BY CONSTRUCTION (constraint / hard reject), never a convention an operator must remember.
  3. R3 — Connectors follow brand; the marketplace-shop ↔ brand relationship is an explicit, owned mechanism.
  4. R4 — No brand selected: read surfaces may show the all-brands/total view; ingest surfaces are disabled with a stated reason.
  5. R5 — General architecture. A new client with N brands onboards with configuration only; zero per-customer code (§81 spirit).

Why the old shape failed#

Marketplace exports carry NO brand or shop identifier — identity came from a free-typed template constant (platform_shop_id), remappable again by the org-scoped alias table, while the DB's order identity UNIQUE(org, platform, platform_shop_id, platform_order_id) has no brand and the upsert's SET rewrites money but never brand_id. One wrong string = brand B's rows land in brand A's shop namespace; a colliding order id rewrites A's money under A's label. The system trusted a hand-typed string to hold the tenancy boundary.

Decisions#

D1 — platform_shops ownership registry (the R2/R3 anchor)

(org_id, platform, canonical_shop_id) → brand_id NOT NULL, with UNIQUE(org_id, platform, canonical_shop_id) (one owner per physical shop) and a superkey UNIQUE(org_id, platform, canonical_shop_id, brand_id) that exists solely as the composite-FK target for fact tables. Brand FK is ON DELETE NO ACTION (0146 rule).

  • RLS class: org_only — deliberately NOT brand-predicated. The ingest reject must SEE that a shop is owned by a different brand; under the brand predicate the foreign row is invisible and "owned by other brand" is indistinguishable from "unowned" (the guard would silently self-defeat). This arbitration overrides the connector panel's draft.
  • Registration: (a) marketplace connector create claims the shop under the connector's brand; (b) upload first-ingest claims an unregistered shop for the ingest brand — in a SHORT standalone transaction with an audit log, not inside the batch tx; (c) admin CRUD. Claiming a shop owned by another brand is a hard error (SHOP_BRAND_MISMATCH), and a mismatch at ingest is a PERMANENT failure (DLQ once, §56).
  • Synthetic shop ids are brand-unique. Shopless exports mint ${platform}-default--<brandId> (was ${platform}-default): a synthetic id is a bucket, not a physical object — two brands' shopless uploads legitimately coexist, so the one-owner rule applies to PHYSICAL platform ids only.
  • platform_shop_aliases stays brand-FREE (pure raw→canonical identity resolution; per-alias brand could self-contradict). Guards: an alias may not bridge two registry rows with different owners; the alias auto-seed lookup is brand-scoped. The UI shows the OWNER brand via JOIN to the registry, read-only.
  • Ecosystem: the table joins ARCHIVE_PORTABLE_TABLES, the org-data category registry, and the runtime-ACL grant lists in the same change; org-archive restore synthesizes registry rows from staged orders (SELECT DISTINCT org, platform, shop, brand) BEFORE loading orders, so pre-migration archives stay restorable. Sample/demo/seed writers claim shops through the same shared helper.
  • Backfill winner is deterministic (most orders, then latest placed_at).
  • Shop reassignment (brand restructure) is a deliberate maintenance script that re-stamps facts and the registry row in one transaction — the composite FK blocks casual reassignment by design.

D2 — orders_natural_key stays PHYSICAL; brand correctness comes from the registry

Brand inside the natural key would let the same physical order exist under two brands — org GMV would exceed the client's own export (double-count, worse than overwrite). Instead:

  • Composite FK orders(org_id, platform, platform_shop_id, brand_id) → platform_shops superkey (NOT VALID first; VALIDATE in a follow-up migration to keep the lock window short).
  • The upsert's conflict-SET heals brand_id from the registry, and heals order_items.brand_id in the same statement. Recon asserts order_items.brand_id == parent orders.brand_id.
  • Key rule (extends §82): brand_id belongs in a unique key ONLY when the same key-tuple can truthfully exist for two brands in the physical world (true for synthetic/bucket ids, false for physical platform objects). The verify:rls check that enforces "brand-stamped fact ⇒ registry FK or parent FK" ships with a baseline exception list (only shrinks) covering not-yet-migrated tables; child-of-owned-fact via parent FK is a first-class satisfying shape.

D3 — source_split_daily rekeys to (org, brand, platform, canonical_shop_id, day, source)

NULLS NOT DISTINCT (§68). The vendor feed is per-shop; the old org-wide key made two brands' connectors overwrite each other. Transition is the dangerous part: legacy rows keyed ('', NULL) never conflict with new-key rows, so the writer DELETEs the legacy row for the same (org, platform, day, source) in the same tx as each upsert, and a TS backfill script (decrypts credentials — SQL cannot — §28 org-iterating) stamps shops before the index swap. Recon asserts no ''-shop row coexists with a real-shop row for the same day.

D4 — ONE ingest-brand binding: resolveIngestBrand, wrapped by ingestBrandProcedure

One helper owns the rule; the procedure tier is wiring only (no second predicate — two parallel mechanisms was itself a D4-shaped bug):

  1. Explicit input.brandId must equal the resolved context brand (client echo; mismatch = CONFLICT, kills the stale-tab race).
  2. No active brand + org has EXACTLY ONE brand → auto-pin it (single-brand orgs notice nothing).
  3. No active brand + multi-brand org → PRECONDITION_FAILED (ingest_requires_brand) — R4.
  4. Resolved brand must pass canViewBrand.

Applies to every mutation that stamps fact/staging rows: templated + untemplated upload, connector create, backfill trigger. Template AUTHORING is asset management, not ingest — org-wide templates (brand_id NULL) remain authorable by all-brands admins; an upload THROUGH an org-wide template still resolves a concrete ingest brand from context. NOT NULL on manual_upload_jobs.brand_id / import_staging_rows.brand_id ships only in phase 3 (below), after every staging writer carries a brand.

D5 — Connectors: brand enforced at BINDING, not at each trigger

  • Creating a 1st-party (marketplace/eCom + marketing) connector in a multi-brand org requires the resolved ingest brand; the connector row stores it; sync stamps every row from connector.brandId and assertShopBrand cross-checks the registry (visible thanks to org_only RLS).
  • Manual Sync Now / backfill of an already-bound connector is allowed under the all-brands view — the destination is fixed on the connector row and shown as a brand chip in the confirm dialog. (Arbitration: UX panel's rule wins; the per-trigger equality gate broke real triage workflow with zero safety gain.)
  • Webhooks resolve shop → registry → brand; resolution failure or mismatch is PERMANENT (DLQ once), never a retry storm. Postgres 23502/23503 on ingest tables join the permanent classification.
  • Re-parenting a connector's brand once it has synced history is blocked; the sanctioned path is the D1 reassignment script.

D6 — Metrics: brand joins the grain; physical dedupe stays org-wide

  • mergedCreatorDailySql grain becomes (org, brand, handle, platform, day); the api_content / line_months suppression sets become brand-scoped (a brand's API data may only suppress THAT brand's CSV).
  • csv_dedup stays org-wide last-wins: one physical order line = one surviving row, whichever brand uploaded it. Brand-scoped dedupe would let the same physical line count under two brands (double-count). Recon asserts no (order, sku, content) is promoted under two brands.
  • rebuildCreatorPerformanceDaily + refreshOrgMaterializations gain a brand-scoped mode: delete/rebuild only the (org, brand) partition the ingest touched (§82); org-wide remains for §28 maintenance.
  • Transition gate: before/after org-total equality for a single-brand org (numbers must not move), wired into verify:data.

D7 — UX contract

Brand context is tri-state: brand X (full) / all (read-only totals) / unresolved. Ingest surfaces under all: visible but disabled with a terse Vietnamese reason (§62). Every ingest confirm shows the receiving brand as a chip BEFORE the action (the receipt). Single-brand orgs are auto-pinned — their UX does not change. Alias management stays org-scoped (an identity repair, not brand data), guarded by the D1 cross-bridge check.

Deploy order (safety-critical — red-team finding: unordered ships break live orgs)#

  1. 0147: platform_shops (+ RLS policy, ACL grants, archive inventory, category registry) + deterministic backfill + brand-unique synthetic shop ids + shared claim helper adopted by promoters, sync, seed/demo/sample writers, and org-archive restore synthesis.
  2. Connector brand backfill: every 1st-party connector in a multi-brand org gets a brand (sole-brand orgs auto-stamp); settle sheets/social cron writers.
  3. Staging/job brand backfill → NOT NULL + restore-side normalization + 23502/23503 permanent DLQ classification.
  4. orders composite FK NOT VALID → separate VALIDATE migration.
  5. source_split_daily rekey + credential-decrypting backfill script (runs before the index swap).
  6. Metrics brand grain + brand-scoped rebuild (+ cube regenerate if projections change, §9).
  7. UI: ingest-brand binding surfaces, disabled states, brand chips.
  8. New reconciliation assertions + the verify:rls key-rule check (with baseline).

Each step is independently shippable and gate-verified; a single-brand org must notice nothing at every step.