Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
paths: - "packages/jobs/src/backups/" - "packages/jobs/src/inngest/functions/tasks/company-backup.ts" - "packages/jobs/src/inngest/functions/tasks/company-export.ts" - "packages/jobs/src/inngest/functions/tasks/company-import.ts" - "packages/jobs/src/inngest/functions/tasks/company-restore.ts" - "apps/erp/app/modules/settings/backups.service.ts" - "apps/erp/app/modules/settings/settings.service.ts" - "apps/erp/app/modules/settings/ui/Backups/" - "apps/erp/app/routes/x+/settings+/backups.tsx" - "apps/erp/app/routes/api+/settings.backup-summary.ts" - "apps/erp/app/routes/api+/settings.backup-restore-status.$restoreRunId.ts" - "apps/erp/app/services/onboarding.server.ts" - "packages/jobs/src/scripts/check-backups.ts" - "packages/jobs/manifests/**"
Company Backup / Restore / Onboarding Seed
Per-company logical backup, in-place restore (with revert), and onboarding-seed
from a committed demo template. Inngest tasks, NOT edge functions — the old
import-company / finalize-import / revert-import edge functions and the
company-revert / publish-demo / refresh-demo-catalog jobs were deleted; this
is the replacement. Reader-facing docs: docs/content/docs/platform/backups.mdx
(kept deliberately impl-free — keep internals here, not there).
User-facing rules of the feature: backups require settings update permission
(no owner gate — the old group.ownerId === userId check was removed; the route
sends carbon/company-export itself via exportCompanyBackup in
settings.service.ts), exclude secrets, and a restore is
reversible via an auto-snapshot.
Backups are a Business/Enterprise feature (BACKUPS in FEATURE_PLANS), with
an internal-staff / local-dev escape hatch. The authoritative gate is
canManageBackups(client, companyId, email)
(apps/erp/app/modules/settings/settings.service.ts) =
canAccessBackups(email) || companyHasFeature(client, companyId, { feature: "BACKUPS" }).
canAccessBackups(email) (apps/erp/app/utils/backups.ts) = IS_LOCAL_DEV || isInternalEmail(email) stays a pure browser-safe predicate — the escape hatch, and the
whole gate for Demo Data (x+/settings+/demo-data), which stays internal-only. Used
by the backups.tsx loader/action and every api+/settings.backup-* loader/action (404
on deny); the nav gates it in useSettingsSubmodules.tsx via usePlanGate({ feature: "BACKUPS" }) OR isInternal/isLocalDev (Demo Data still uses localOrInternalRoutes).
Internal = @carbon.ms / @carbon.us.org.
canManageBackups is the route-level UX gate (a deletable open-code if). The
un-strippable commercial LOCK is requireBackupsEntitlement(companyId)
(packages/ee/src/backups.server.ts → @carbon/ee/backups.server), embedded at the
top of every START-action durable function — companyExportFunction,
companyRestoreFunction, companyImportFunction (finalize/revert are NOT gated —
they only resolve an already-started restore, and gating them would strand a
pending restore if entitlement lapsed). It is
IS_LOCAL_DEV-exempt (the service-role job path carries no email, so the
isInternalEmail half of the hatch cannot apply there) and otherwise
requireEntitlement("BACKUPS") — Community/Starter throw. Onboarding demo-template
apply/revert call buildCompanyBackup/wipeAndLoad DIRECTLY, and the import fn
exempts referencedTemplate, so templating stays free on every edition. Do NOT push
the check into buildCompanyBackup/wipeAndLoad — that breaks onboarding.
The multi-tenant caveats below are real but are NOT a cross-tenant leak in the backup
feature — every entry point is scoped to the caller's companyId. They were the reason
this stayed internal-only, and exposing it to customers is a deliberate product decision:
(1) restore is a full in-company wipe-and-reload (large destructive surface, snapshot-
reversible); (2) the export HARD-REFUSES when a company's live data already holds
cross-tenant references left by an upstream bug (tables like jobOperationDependency
still lacking companyId in their PK — see .ai/specs/2026-08-25-backup-durability.md),
offering a destructive "remove corrupted data" path. Closing that upstream schema hole
is the remaining hardening.
Shared engine — company-backup.ts
The catalog is schema-introspected, not a hand-maintained list. The
catalog + compatibility layer lives in packages/jobs/src/backups/schema.ts
(no runtime @carbon/* imports, so bare-tsx scripts and the ERP server can
both use it; company-backup.ts re-exports it), exported to app code as
@carbon/jobs/backups:
getCompanyTableCatalog(db)readsinformation_schemafor every public BASE TABLE carrying acompanyIdorcompanyGroupIdcolumn, buildsTableInfo(columns, FK edges,scopeColumn,hasId/id type), and topologically sorts (referenced-first).companyIdwins when a table has both columns.scopeColumn: "companyId" | "companyGroupId".companyId= the company's own data;companyGroupId= config shared across a company group (chart of accounts, currencies, dimensions).- Skip/scope sets:
SECRET_TABLES(apiKey,apiKeyRateLimit,companyIntegration,employeePin,webhook,oauthClient,oauthCode,oauthToken,ssoConnection,ssoDomain,pushSubscription— never travel;pushSubscriptionis a browser's push endpoint and its encryption keys, so a restore must neither bring back signed-out browsers nor wipe the current ones; SSO rows carry values unique across all companies (providerId, a verifieddomain), so a cross-company copy collided;employeePinholds console-PIN bcrypt hashes, a credential for a 4-digit PIN, and stays in place on restore alongsideemployee(IN_PLACE_SKIPPED_TABLES), which is never wiped, so its (employee, company) FK has nothing to orphan it from;apiKeyRateLimitis here because its NOT-NULLapiKeyIdreferences the stripped secretapiKey, so exporting it alone would dangle every row on restore — and it's UNLOGGED operational counters, not user data),STRUCTURAL_TABLES(company— excluded from catalog),TRANSIENT_TABLES(demandForecastSource,demandActual,supplyForecast,supplyActual— MRP planning output that MRP regenerates wholesale every run; excluded from the catalog entirely alongsideSTRUCTURAL_TABLES, so they're never exported/wiped/loaded and the next MRP run rebuilds them.demandForecastSource's discriminator CHECK (sourceType↔ which ofjobId/salesOrderLineId/demandProjectionIdis non-null) made a remapped restore crash — the FK-nulling dangling-ref policy inbuildRowTransformsnulls a set FK and violates the CHECK.demandForecastis deliberately kept: it has a user-forecast write path and no such CHECK.planningActionis kept too —DismissedandassigneeOverriddenare the planner's own state, not regenerable — even though it has a comparable CHECK (planningAction_change_target_chk,20261006130000: a change action keeps itsjobIdorpurchaseOrderLineId). Its target FKs are nullable with ON DELETE CASCADE, so a consistent snapshot never carries a dangling target; a remapped restore that did would null the FK and fail that CHECK. The dataset wipe (wipe.tsTRANSIENT_MRP_TABLES) deletes it, which is a different path. The two excluded sets are unioned intoCATALOG_EXCLUDED_TABLES, whichassertBackupImportablealso skips — an OLDER backup that still carries an excluded table is not schema drift, its rows are just ignored on load),RESEED_SKIPPED_TABLES(memberships/invites/employee/externalIntegrationMapping — skipped on onboarding reseed),IN_PLACE_SKIPPED_TABLES(access/identity tables a restore must keep so the user isn't locked out). - Format: a backup is a FOLDER, not a file —
exports/<name>/holdingmanifest.json, onetables/<table>.ndjson.gzper NON-EMPTY table, andassets/<path>files. Paths come frombackupDir/backupManifestPath/backupTablePath/backupTablesDir/backupAssetsDir; each table is written and read a line at a time (serializeTable/deserializeTable), so a huge table never materializes as one>512MBstring (V8's max). Tables are dumped and loaded in parallel (mapWithConcurrency,TABLE_CONCURRENCY = 6). The whole folder bundles into one.carbon.tar.gzonly for cross-environment download and upload. There is no single.carbon.json.gzartifact and noBACKUP_GZ_SUFFIX— that was the retired one-file format, whose whole-file{ manifest, data }JSON broke at ~512MB of row data. manifest.jsonis written LAST, bywriteBackupManifest, after every table file and asset is in place. Its presence IS the "backup is complete" marker: the UI lists a manifest-less folder asIncomplete, and restore/import refuse to read one.- Versioning:
BACKUP_VERSION(currently 1 — single supported format, no legacy branch) in the manifest;assertBackupImportablerejects a file whose version no longer matches or that's missing a now-required column. - Reseed re-inclusion:
filterUnpopulateddrops tables the target already populated (onboarding's own inserts), but a dropped table whose ids the kept rows still REFERENCE is re-added byreferencedDroppedTables(company-backup.transforms.ts, transitive overid-FKs) — without it the backup'slocationtable was dropped because onboarding had inserted one "Headquarters" row, and every importedworkCenter/joblocationId was nulled (nullable) or left dangling at the SOURCE company's row (NOT NULL, committed under replica mode) — the "schedule says no work centers exist" bug. A re-added table is populated in the target by definition and unique constraints stay enforced under replica mode, so backup rows that collide on a real unique key (both sides have a "Headquarters") are NOT inserted:mapCollidingRows+matchableUniqueGroups(+getUniqueColumnGroupsoverpg_index) map the source id onto the existing target row's id instead, so every FK into the skipped row lands on the target's own row. - Id minting:
newIdForTable(table)→randomUUID()for uuid id columns elsenanoid().buildIdMaps(company-backup.transforms.ts) decides WHICH rows get one, and both the restore and the reseed/import call it so they can't drift. It is gated on having a text/uuididCOLUMN, not onhasId(PK exactlyid) — the ~25 composite-PK tables are referenced by theiridtoo, and five of them back that with a globalUNIQUE (id):changeOrderRequiredAction,balloon,inspectionDocument,inspectionFeature(all PK("id","companyId")) anddemandProjection(PK("itemId","locationId","periodId")). Leaving their source ids in place made a cross-company restore collide with the SOURCE company's still-live rows and roll the whole thing back. Five OTHER composite-PK tables are excluded BY NAME (READABLE_ID_TABLES:part,material,consumable,service,tool) because theiridis a human-authored part number matched by VALUE againstitem."readableId", not an identifier — minting a fresh one orphans every row from its item and every list view renders empty against a full table. They carry only the composite PK (no globalUNIQUE (id)), so preserving their ids cannot collide; the two sets are disjoint by construction and a test enforces it. Serial/int ids are left alone. A 1:1 extension table whoseidis itself an FK (purchaseOrderDelivery.id -> purchaseOrder.id) SHARES its parent's map rather than minting a second id, which would split the pair — and undersession_replication_role='replica'the break would commit. - Storage path rewriting:
rewriteStoragePath(swap{sourceCompanyId}/→{targetCompanyId}/+ remapped id segments),rewriteToTemplateAssetPath({co}/…→_templates/{industryId}/…).STORAGE_PATH_COLUMNS=thumbnailPath,modelPath,originalPath,optimizedModelPath,glbPath,graphPath,pmiPath— everymodelUploadartifact column. A backup enumerates assets by LISTING the bucket under{companyId}/(company-export.ts), not by reading these columns, so raw CAD IS carried; a column missing from this set survives a cross-company restore still pointing at the SOURCE company's prefix (which is what left restored assemblies unable to load their model). - FK-less id refs:
ID_REF_COLUMNS(src/backups/id-refs.ts, per table) lists TEXT/TEXT[] columns that hold a tenant row's id with no FK — generic refs likeinspection.sourceDocumentId/sourceDocumentLineId(receipt line OR job operation),itemLedger.documentId,...Idsarrays.buildRowTransformsrewrites them through the combinedidRewritemap on a remap load (element-wise for arrays, lookup-only so non-id values pass through; an FK column is never treated this way). Unlisted, a cross-company restore keeps the SOURCE ids: theinspection_sourceDocumentLineId_keyunique index (cross-company, on("sourceDocument","sourceDocumentLineId")) then collides with the source company's live rows and rolls the restore back. A migration adding such a column must list it (workflow-database-migration.mdstep 3c). The map is typed against the generated row types (satisfies), so a renamed/dropped column fails typecheck. Neitherdb:check:backupsnor any other check detects an UNLISTED new column yet. - Asset transport:
copyAssetsToBackup(server-sidestorage.copyof the company bucket's{companyId}/…files into a backup'sassets/folder) andrestoreAssetsFromBackup(copy them back into the target company's bucket, rewriting paths + guarding every write to the target{companyId}/prefix).removeStoragePrefixrecursively deletes a backup's asset folder, which isbackupAssetsDir(name)=exports/<name>/assets— derived from the folder name, not from a file suffix. Copies are server-side — no asset bytes pass through the job process, so memory stays flat regardless of asset size.
Buckets (important, two different things):
- The per-company bucket named by
companyIdholds the company's uploaded assets under a{companyId}/prefix, one folder per backup (exports/<name>/— itsmanifest.json,tables/,assets/{companyId}/…), and pre-restore snapshots under the same prefix — seeclient.storage.from(companyId). TEMPLATE_BUCKET = "company-templates",TEMPLATE_ASSET_PREFIX = "_templates".
Export — company-export.ts
Dumps each catalog table scoped by its scope column into its own data-only
tables/<table>.ndjson.gz (serializeTable, line-streamed). Empty tables are
skipped
(if (result.rows.length === 0) continue) — so a backup of a company with no GL
postings simply has no journalLine/costLedger rows; that is data-absence, not a
coverage gap. With includeStorage: "all", buildCompanyBackup records the
in-scope asset paths in manifest.storage and returns them; the job then
copyAssetsToBackup them server-side into the backup's assets/ folder.
Out-of-scope rows are FATAL, deliberately.
findExportScopeViolations(src/backups/scope.ts, which also holdsbuildScopeFilterand the exclusion/purge builders — pure kyselysql, re-exported byschema.tsandcompany-backup.ts) counts rows whose NOT-NULL FK points outside the company's scope, and a non-zero count throws before any table file is written, listing every offending edge.This refusal is a tenant-leak detector, not a backup bug. RLS hides another tenant's rows from every ordinary read; an export runs without RLS, so it is the first thing in the system with a wide enough view to see a cross-tenant reference at all. An implementation that excluded those rows and finished the backup was built and REVERTED on 2026-08-26 for exactly that reason — it made the only detector we have go quiet, in exchange for a nicer error on a company whose data was already wrong.
The ONE sanctioned bypass (spec
.ai/specs/2026-09-01-skip-corrupted-rows-in-backups.md): after a VISIBLE failure the user may click Skip corrupted rows and retry, which re-runs the export withskipCorrupted: true—computeScopeExclusions(src/backups/scope.ts) excludes the violating rows and their dependents, the job logs awarnwith the list, andmanifest.excludedRowsrecords it (the Backups row showsN rows excluded). A restore blocked by the same rows (its pre-restore snapshot runs the same guard) offers Remove corrupted data and restore, whichpurgeScopeViolationsdeletes in one transaction (children first) behind a destructive confirm, then restarts the restore. Both jobs classify the failure withExportScopeViolationError→ markerreason: "scope-violations"; the UI offers the recovery ONLY for that reason. Never make either the default, and never skip silently.That error is rethrown as a
NonRetriableErrorin both jobs. The guard's verdict is deterministic, so Inngest'sretries: 1would only flip the marker back torunningand fail again ~10s later — resurrecting the failure banner underneath a user who has already clicked Skip. Counts are reported fromrowsByTable/perTable(DISTINCT rows per table), never by summingviolations/excludedRows: those carry one entry per FK EDGE, so a row escaping scope through three of its foreign keys would be counted three times — which once put "10 rows" on the confirm button of a delete that removed 4.The other hard export failure is a company with no
companyGroupId.No compatibility verdict is stored with the backup. One was (
compatibility.json, written at export time on this feature's branch) and it was a tautology: the export diffed the manifest against the very catalog it had just been projected from, so it always saidreadyand was never refreshed. The verdict is only meaningful when manifest and schema come from different points in time, so it is computed LIVE in the Backups loader instead (see the ERP layer below).Progress/failure marker: one
externalIntegrationMappingrow per company (integration = "company-export", no run id — exports are one-at-a-time per company). The job writes{ status: "running", startedAt, progress }heartbeats (throttled 250ms), clears the marker on success (the new backup appearing in the list is the completion signal), and flips it to{ status: "failed", error }on error — it is NOT cleared on failure; a cleared marker made a broken export indistinguishable from one that never ran. The failed marker is dismissed from the UI via thedismissExportFailureintent →dismissCompanyExportFailure(service role — the table has no DELETE policy), and the next export'supsertExportMarkerreuses the row.Asset cap (
company-export.ts): no per-file cap (the bucket already bounds uploads — 120MB CAD, 50MB docs; a company bucket's own limit is the dev50MiBglobal inconfig.toml), only aMAX_STORAGE_TOTAL_BYTES = 1GiBguard on the whole backup. Files are included greedily until the budget is hit; each is recorded inmanifest.storagewithincluded: true|false. The cap is storage-cost only (server-side copy is memory-flat) — each backup duplicates the bundled bytes. The old25MiB-per-file /200MiB-total caps silently dropped legit large media.
The drift check — pnpm db:check:backups
A migration can make a backup a customer already holds unrestorable without the backup changing at all: add a NOT NULL column with no DEFAULT and every backup taken before today stops loading. This is the ONLY thing in the system that prevents that, and prevention at commit time is the only place it can be done — once the migration is in production the backup is already dead, and every downstream mechanism can do no more than say so.
That is why there is no stored verdict and no re-check job: the Backups
loader computes reportBackupCompatibility against the LIVE schema on every
load (getCompanyBackups in settings.service.ts), so the badge cannot go
stale, and the hard refusal is assertBackupImportable, which runs against
the live schema inside company-restore.ts. (Two earlier designs died here:
a nightly re-check that alerted nobody and repaired nothing, and an
export-time compatibility.json that compared the manifest against the very
catalog it was projected from and could only ever say ready.)
packages/jobs/src/scripts/check-backups.ts compares ONE committed schema
baseline — packages/jobs/manifests/schema.json, every exportable table with its
column list and rows: 0, SECRET_TABLES excluded exactly as a real export
excludes them — against the live schema through reportBackupCompatibility. Any
blocked finding fails the check.
Nobody maintains that file. On success with --stage (which the hook passes,
and only the hook) the script regenerates it from the live catalog and git adds
it, announced on stdout. A manual pnpm db:check:backups stays read-only. There is
no --write and no directory of dated vintages — that was the earlier design, and
it depended on a person remembering at each release.
The baseline is the copy on main, never the working-tree copy — a file that
regenerates itself cannot be its own baseline. resolveBaseline fetches
https://raw.githubusercontent.com/<owner>/<repo>/main/packages/jobs/manifests/schema.json,
slug parsed from git remote get-url origin so a fork checks itself, with a 3s
timeout. On any fetch failure (including a 404 while the file is not yet on main)
it warns — naming the STALENESS, not just the failure — and falls back to
git show origin/main:…. A network problem never fails a commit. A local copy that
is months behind is a STRICTER baseline, never a blinder one, but it can flag a
column a teammate already removed, and an unexplained false alarm is what earns a
--no-verify. Found in neither place is a hard exit 1: a missing baseline
skipped quietly is indistinguishable from a passing check. The contract is in
packages/jobs/manifests/README.md.
A schema-shaped manifest with no rows is exactly as informative as a real customer backup, because compatibility is decided entirely by table and column names. That is what makes this checkable from a committed file rather than from a database.
It runs from scripts/git-hooks/pre-commit when a staged file is under
packages/database/supabase/migrations/, alongside db:check:datasets, and skips
with CARBON_SKIP_BACKUP_CHECK=1. Read-only — one connection, information_schema
queries, no writes.
Three honest limits. A hook is bypassable with --no-verify, so this is a safety
net rather than a gate (the same limitation db:check:datasets has). It reads the
LIVE schema, so it is only as good as the developer's pnpm db:migrate — which is
why it refuses rather than passes when migrations are pending: an unapplied
migration means the schema does not yet contain the change being committed, so the
baseline would pass for the wrong reason. And it asserts against the LAST SHIPPED
schema only — every migration commit is checked, so a break is caught the moment it
is introduced, but the check cannot pre-certify a two-year-old backup. Git holds
every past version of the one file, so looking further back is
git show <commit>:packages/jobs/manifests/schema.json, not a committed pile.
This deliberately is not a CI job. The design that was tried first replayed
migrations against a throwaway Postgres to rebuild an old schema, and that cannot
work cheaply: a bare Postgres has no auth or storage schema (ERROR: relation "storage.buckets" does not exist on the 9th migration) because the Storage and
GoTrue services build those when their own containers boot, so the job needed most of
the compose stack. A developer's machine already has all three schemas, and once the
check runs there the replay is unnecessary — the committed baseline already IS the old
schema. Note that a backup itself never reads the other two schemas:
getCompanyTableCatalog filters on table_schema = 'public'.
@carbon/checks also carries no-required-column-without-default, which catches the
most common cause at the SQL level. The two are complements: that check reads
migration text, this one reads the resulting schema and knows what the backups
actually contain.
What the pair does NOT catch — the honest coverage list
Neither check is complete, and the shape of the gap follows from manifest.tables
being Array<{ name, rows, columns: string[] }>. Columns are NAMES ONLY — no
type, no nullability, no constraint. So compatibility is decided entirely at the name
level, and everything below passes green today:
ALTER COLUMN … SET NOT NULLon a column that already existed. The biggest hole, and a common migration (25 existing migrations do it).reportBackupCompatibilitytestsbackupCols.has(c.name)FIRST, so a column present in the baseline is skipped before nullability is ever considered; and the SQL check deliberately matches only theADD COLUMNform. Whether it actually breaks a restore is a DATA question — do that company's old rows have nulls there — which a rows-free manifest cannot answer even in principle.- Type changes (
ALTER COLUMN … TYPE, 17 existing migrations).text→numericrestores fine or fails per row depending on the data; the manifest cannot tell. - New
CHECK/UNIQUEconstraints, and new FKs. Old rows may violate them.demandForecastSourceis the precedent for how bad this gets — its discriminator CHECK made a remapped restore crash, and the fix was excluding the table entirely. - Removing a value from an enum that old rows still carry.
- Anything data-shaped at all — the baseline has
rows: 0by design.
Two more limits that are procedural rather than structural: a developer can pass
--no-verify, and the check refuses (rather than passes) when their database is
behind. Both are stated above.
Do NOT close these by teaching the baseline format about types and constraints. The
copy on main would lack the new fields until it is next regenerated, so the check
would be blind for exactly the window it exists to cover, and a richer manifest still could not
answer the data questions that make SET NOT NULL and type changes dangerous. The
honest reading is that this pair catches the whole class of structural breakage —
a table or column the backup has nothing to say about — and that the value-shaped
breakage is what RestoreDisclosure and the snapshot-and-revert path exist for.
Renamed and dropped tables — src/backups/renames.ts
TABLE_RENAMES maps a tenant-scoped table's OLD name to its current one, or to null
when it was dropped along with its feature. "Tenant-scoped" here means anything in the
catalog — direct scope (its own companyId/companyGroupId) and via scope (reached
through a FK to a scoped parent), since both are exported. A migration that renames or drops such a table
must add an entry in the same commit; db:check:backups fails the commit until it does,
and .claude/rules/workflow-database-migration.md step 3b is where the author is told.
The map exists because "this table is not in the schema" is ambiguous and the schema cannot disambiguate it. Dropped → skipping the rows is correct. Renamed → skipping silently discards real data, orphans everything referencing it, and still reports success. So an UNMAPPED missing table refuses the restore and names itself, rather than guessing.
applyTableRenames(catalog, backup) is what makes a mapping actually do something. It runs
ONCE, immediately after readBackup and before assertBackupImportable, in all four load
paths (restore, restore-revert, template-revert, import), rewriting the manifest table names
and the data keys. Everything downstream — the gate, the closure preflight,
wipeAndLoad — keys off table name, so normalising once is what keeps them from disagreeing;
before it existed the mapping reached only reportBackupCompatibility, so the pre-restore
screen said "restorable" and the gate then refused with
table "X" no longer exists in the current schema.
Two rules that make cycles safe. The map is consulted only for a name the live schema no
longer has, so after A→B→A the stale A: "B" entry is never read and cannot redirect rows
away from the table they belong to. And resolution is a single hop, never a chain — it
reads one entry and resolves it once against the catalog, so it cannot spin. Anything it
can't resolve confidently (unmapped, mapped at a name also missing, or mapped at a name this
same backup already carries) is left untouched for the gate to refuse.
No nightly housekeeping — deliberately
A backup exists because a person took one, and stays until a person deletes it.
There is no cron in this feature at all. A backupMaintenanceFunction
(0 3 * * *) with three passes was built on this feature's branch and dropped
before merge — none of it ever shipped on main, so there is no code or
constant to find; this list exists so nobody rebuilds it:
- Compatibility re-check — recomputed a stored verdict nightly, alerted nobody, repaired nothing. The live loader computation replaced the whole idea of a stored verdict. Do not reintroduce a detector with no consumer that ACTS on a finding.
expireBackups(delete past a retention window) — could not tell a backup a person deliberately took from one a cron took for them, so the one the customer chose was the one that disappeared. Deleting customer data on a timer nobody asked for.scheduleStaleExports(take one for any company whose newest was over a week old) — each run is a separate folder, so the person's own backup would sit buried among rows labelledScheduledthey never made, all paid storage.
The parent spec's D7 wanted scheduled creation; that is dropped, not deferred —
not as a setting either. See .ai/specs/2026-08-25-backup-durability.md.
Pre-restore and pre-template snapshots (_pre-*) were never touched by that job
and still are not: they are dropped by the keep/revert path in company-restore.ts
and company-template.ts. A stalled restore can still strand one — pre-existing,
and nothing here changed it.
Restore — company-restore.ts
Three Inngest fns: companyRestoreFunction, companyRestoreFinalizeFunction,
companyRestoreRevertFunction. Shared concurrency key
'company-restore-' + event.data.companyId, scope env, limit 1 — one restore
per company at a time.
Forward flow:
readBackupfrom the per-company bucket —manifest.jsonfirst, then eachtables/<table>.ndjson.gzthroughdeserializeTable(streamed NDJSON gunzip, never a single>512MBstring).- Compute
targetGroupId, thengroupCompanyCount(count of companies with thatcompanyGroupId).includeGroup = groupCompanyCount === 1. A foreign backup (manifest.sourceCompanyId !== companyId) onto a shared group (!includeGroup) throws rather than rewrite the shared chart of accounts. - Auto-snapshot current state to
snapshotPath(idempotent — reuses an existing one). This is whatrevertrestores. wipeAndLoadin ONE transaction withsession_replication_role = 'replica'(relaxes FK checks during load):selectWipeableTables(catalog, { includeGroup })→ company-scoped tables minus kept/secret; group-scoped tables included only whenincludeGroup.wipeScopedDatadeletes each table in reverse-topo order, always scopedWHERE <scopeColumn> = <value>; a null scope value is skipped (no unscoped DELETE is possible).buildRowTransformsre-stampscompanyId→target,companyGroupId→targetGroupId; on a foreign/remap load it mints new ids (text/uuid only) and remaps FKs, with a dangling-ref guard (nullable FK → null, non-nullable → throw).- Closure preflight (
assertReferentiallyClosed→findDanglingReferences) runs before the wipe and refuses a backup whose NOT-NULL FK points at a row it doesn't include. It tracks a parent's ids for any table with anidcolumn, not onlyhasId(sole-idPK) tables — the ~25 composite("id","companyId")-PK tables (stockTransfer,supplierPart, …) are referenced by theiridtoo, and gating onhasIdfalsely flagged every child of them and refused otherwise-valid restores. It also skipsSECRET_TABLESon both sides (a secret table is never written to a backup and never loaded) — an OLDER backup that predates a table joiningSECRET_TABLESstill carries its rows (e.g.apiKeyRateLimit→ the strippedapiKey); they're ignored on load, so the preflight ignores them too.
restoreAssetsFromBackup(outside the txn, non-transactional) copies the files from the backup'sassets/folder back into the target company's bucket at the rewritten path, server-side. It runs before thereadymarker (step below) so the progress dialog only reports "complete" once data AND files are in place — but it never throws (per-file warnings), so a storage hiccup still can't fail a committed restore. Every write is guarded to the target{companyId}/prefix.- State marker →
ready(data committed + files copied). assertWipeSafe(catalog)guards the invariant that a KEPT table has no NOT-NULL FK into a WIPED table.
State marker: a row on externalIntegrationMapping, integration = "company-restore", metadata = { status: running|ready|failed|reverting, snapshotPath, foreign, includeGroup }. revert reads the marker and reloads
snapshotPath (using metadata.includeGroup). Status is polled by the UI.
Second caller. wipeAndLoad, getCompanyGroupId and resolveRestoreScope
(the extracted targetGroupId + includeGroup rule) are exported and are
also used by company-template.ts: applying a demo template from Settings
snapshots with buildCompanyBackup, and reverting it wipes-and-loads that
snapshot through exactly this path. See onboarding-company-templates.md.
Third caller: company deletion. purgeCompany (packages/jobs/.../scheduled/purge-company.ts,
used by the weekly inactive-company cleanup) runs wipeScopedData over the WHOLE catalog in
replica mode, secret and identity tables included, because nothing is reloaded after it. It
then deletes the company row with triggers back on, so tables outside the catalog cascade.
canSetReplicationRole decides whether it can wipe at all. See billing-system.md.
listBucketFilesRecursive lives in src/backups/storage.ts, shared by the export and that
cleanup.
Known caveats in the committed code (not yet hardened)
- Storage restore is best-effort — copy failures
console.warnonly; the restore still reports success (data is the source of truth). The cross-tenant write guard (targetPath.startsWith(\${companyId}/`)) IS now inrestoreAssetsFromBackup`. - Asset copy duplicates bytes — a self-contained backup copies the company's
assets into its
assets/folder, so each backup costs roughly its asset size in storage. Snapshots are transient (theirassets/is removed on keep/revert); exports persist until deleted (deleteCompanyBackupExportremoves the folder). - Sequences: the only pg-native serials on backed-up tables are
entryNumberonitemLedger/costLedger/supplierLedger(not PKs, not unique; PKs are all text). They are global/shared across tenants — neversetvalthem scoped to one company; in-place restore needs no reset (other tenants hold the high-water mark).
ERP layer
backups.service.ts— per-company bucket ops:listCompanyBackupFolders(from(companyId).list("exports")folders, paged with a hard page cap; a folder is "ready" oncemanifest.jsonexists, else "pending"; returns each parsedManifestfor the server wrapper),deleteCompanyBackup(removes the wholeexports/<name>/folder),getCompanyRestoreRuns(restore markers),getCompanyExportRun(the export marker:status: "running" | "failed",progress,startedAt,error).settings.service.ts(server-only) holds the rest.canManageBackupsis the gate.exportCompanyBackupsendscarbon/company-export.getCompanyBackupsis the loader's list: it reads the live catalog ONCE per load (getCompanyTableCatalogovergetDatabaseClient(), via@carbon/jobs/backups), diffs every listed manifest withreportBackupCompatibility, attaches{ status, findings }to each row and strips the manifest. Plus the trigger wrappers (startCompanyRestore,finalizeCompanyRestore,revertCompanyRestore) anddismissCompanyExportFailure(service-role delete of the failed export marker) — kept off the client to avoidBuffer-in-client.routes/x+/settings+/backups.tsx— access-gated (requireBackupAccess) loader/action (export / restore / keep / dismiss / revert / delete / dismissExportFailure intents).filePathis forced underexports/. Export is non-blocking, row-first (Supabase-style): clicking "Create backup" does NOT open a modal — it drops a synthetic "Creating backup…" spinner row into the Backups list immediately (optimistic client staterunningExport, set the instant the action returnsstarted: "export", before the job writes its first marker), and clicking that row opens the detail progress modal.runningExportis also adopted from the marker on mount for a run started elsewhere (reload / another tab); itsbaseline= ready backups when tracking began, and completion = a ready backup outside the baseline appearing in the revalidated list (page revalidates every 2.5s while the modal is closed). A "failed" marker renders a banner ("This backup could not be completed." + the job's own error) with a Dismiss button — the old copy blamed the product ("The system created an invalid backup — please contact Carbon support.") for what was usually a data-integrity finding the error string already named.routes/api+/settings.backup-summary.ts— lazy "what's in a backup" counts, grouped, per-entityscope: company|group.routes/api+/settings.backup-restore-status.$restoreRunId.ts— poll;companyIdfromrequirePermissions, so a user can't poll another company's run.One status vocabulary, four words (
ui/Backups/format.ts:BackupStatus,backupStatusLabel— amsg-descriptor map resolved witht(...)at the render site — andbackupStatusVariant). No synonyms anywhere:Status Meaning Restorable? Readyloads into today's schema unchanged yes Restorable with changesloads, but N things differ; disclosure required yes, after confirming Not restorablea hard refusal, with the reason no Incompleteno manifest.json— the export died partwayno A failed EXPORT is not a row status — it renders as the failure banner from the export marker.
Incompleteoutranks the computed verdict (a half-written folder's incompleteness is the whole story) and is never rendered as "Preparing…" once no export is being tracked — that was the bug where a dead folder looked alive forever. A row shows its taken date, size, label and this badge, and deliberately no expiry date: a printed date reads as a promise the backup is good until then, which nothing can make. The badge answers the real question, for today — computed against the live schema on every load.UI:
modules/settings/ui/Backups/—BackupChoices(Data only / Data + files),BackupSourcePicker,BackupProgressModal,RestoreReviewRow(Keep/Revert),RestoreDisclosure(the pre-restore screen — the ONLY path to a restore now),BackupContentsInfo(lazy popover),format.ts.JobProgressModaltracks real completion, not a timer: restore/revert poll the status marker; export has no marker, so the route component revalidates the list and passescompletedonce the new backup actually appears (exportBaselinediff) — the dialog never claims success before the artifact exists.
RestoreDisclosure — five states, one base sentence
The pre-restore screen. It renders the verdict the loader computed against the
LIVE schema (getCompanyBackups), so it can never disagree with the badge on
the list. It is DISCLOSURE, not authorization: assertBackupImportable runs
the same diff again inside the restore, so this screen can never let through a
restore the gate would refuse.
Every state opens with the same two sentences — This company's data is replaced with the
contents of this backup. Today's data is saved first, so you can revert. Keep and Revert are
NOT explained here; they are explained on RestoreReviewRow, where they are the actual
decision. The four-line version that spelled out delete / snapshot / Revert / Keep upfront
read as a warning label and was cut.
| State | Added copy | Confirm |
|---|---|---|
| clean | none | button, no typing |
| unchecked (an upload-sourced restore — every LISTED backup has a live verdict) | "This backup hasn't been checked yet…" | button, no typing |
| defaults only | Filled with a default group |
button, no typing |
| discards | Discarded group + "Some records won't come back. Type restore to continue." |
typed restore |
| blocked | Can't be restored group |
no button at all |
Findings group by tableArea (backups.areas.ts) — "Production" means something
to the person deciding, jobOperationDependency does not. Areas are stable keys
resolved to copy via areaLabel (msg descriptors); table names live in a
Details expander. There is no "Checked " line — the verdict is live.
The state decision lives in disclosure-state.ts (disclosureState(backup, typed) →
{ unchecked, blocked, discards, canConfirm }), NOT in the component, and is unit-tested in
disclosure-state.test.ts. That split exists because the states worth seeing are the ones
nobody can produce by hand; the app has no browser-rendering test setup, so the branching is
what gets pinned.
There is one state with no purpose-written copy: a backup that is not referentially
closed. assertReferentiallyClosed throws inside the restore job with
This backup can't be restored: the backup is not self-contained — …, which surfaces after
the user clicks Restore, not on this screen — the verdict compares table and column
NAMES, and this is a row-level fact it cannot see. The refusal happens before the wipe, so
nothing is lost. Known gap.
Onboarding seed
routes/onboarding+/industry.tsx → dataChoice: "template" | "import" | "none"
("Use a demo template" / "Restore from a backup" / "I don't need data"). The whole
step is internal-only (isInternalEmail); public signups create their company in the
company step.
template is NOT a backup import. It runs the dev seed's own tier code against the
new company, so the demo data has exactly one definition:
packages/database/src/datasets/ — tiers/ holds the insertion logic, data/<key>/
holds one industry story each (four today: satellite, robotics, precision, motor),
and applyDataset() in datasets/index.ts is the shared entry point.
pnpm db:seed:dev --dataset <key>, onboarding, and Settings → Demo Data are callers of
that one function.
Flow: provisionOnboardingCompany (onboarding.server.ts) does a full clean
seedCompany (the tiers' pre-flight needs a chart of accounts, a location, and
unitOfMeasure code EA), creates the headquarters location and employee job, and
then — deliberately last, because the pre-flight needs that location — triggers
carbon/company-template. packages/jobs/src/inngest/functions/tasks/company-template.ts
handles it: one step.run, concurrency 1 per company, a getPostgresConnectionPool(2)
client (size 1 is the shared pool the event drainer uses), a refuse-if-item-rows-exist
double-apply guard, and a marker on externalIntegrationMapping
(integration = "company-template") cleared on success and set failed on a throw.
applyDataset wraps every tier in one transaction, so a failure leaves zero rows.
industry.id → dataset is a code map (datasetForIndustry), not a column. All four
industries have a dataset today; an industry without one is hidden from the onboarding
picker rather than provisioning a clean company.
Dormant (built, never wired, do not revive without revisiting
.ai/specs/implemented/2026-08-13-onboarding-company-templates.md): the
company-templates bucket, TEMPLATE_BUCKET / TEMPLATE_ASSET_PREFIX, and
templateIndustryId on carbon/company-import. The CI publish script
(ci/src/upload-backup-templates.ts), its Publish backup templates workflow and
packages/database/supabase/backups/ were deleted.