Skip to content

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.

fix: skip user-keyed identity tables on restore/import + optimistic dismiss

Sidwebworks8314bdf018ac
diff --git a/apps/erp/app/routes/x+/settings+/backups.tsx b/apps/erp/app/routes/x+/settings+/backups.tsx
index 04341b1ff9..56bd132e2a 100644
--- a/apps/erp/app/routes/x+/settings+/backups.tsx
+++ b/apps/erp/app/routes/x+/settings+/backups.tsx
@@ -283,11 +283,23 @@ export default function BackupsRoute() {
active?.mode === "export" &&
!!active.exportBaseline &&
files.some((f) => !active.exportBaseline!.has(f.path));
+
+ // keep / dismiss / revert finalize the run via an async Inngest job, so the
+ // marker is still present on the next revalidation. Hide the row optimistically
+ // the moment the user acts so it doesn't linger until the job lands.
+ const [resolvedRunIds, setResolvedRunIds] = useState<Set<string>>(new Set());
+ const resolveRun = (runId: string) =>
+ setResolvedRunIds((prev) => new Set(prev).add(runId));
+ const visibleRestoreRuns = restoreRuns.filter(
+ (r) => !resolvedRunIds.has(r.restoreRunId)
+ );
+
const startRevert = (runId: string) => {
fetcher.submit(
{ intent: "revert", restoreRunId: runId },
{ method: "post" }
);
+ resolveRun(runId);
setActive({ runId, mode: "revert" });
};
@@ -387,7 +399,7 @@ export default function BackupsRoute() {
</Card>
</div>
- {restoreRuns.length > 0 && (
+ {visibleRestoreRuns.length > 0 && (
<Card>
<CardHeader>
<CardTitle>Restored — review</CardTitle>
@@ -398,23 +410,25 @@ export default function BackupsRoute() {
</CardHeader>
<CardContent>
<VStack spacing={2}>
- {restoreRuns.map((run) => (
+ {visibleRestoreRuns.map((run) => (
<RestoreReviewRow
key={run.restoreRunId}
run={run}
- onKeep={() =>
+ onKeep={() => {
fetcher.submit(
{ intent: "keep", restoreRunId: run.restoreRunId },
{ method: "post" }
- )
- }
+ );
+ resolveRun(run.restoreRunId);
+ }}
onRevert={() => startRevert(run.restoreRunId)}
- onDismiss={() =>
+ onDismiss={() => {
fetcher.submit(
{ intent: "dismiss", restoreRunId: run.restoreRunId },
{ method: "post" }
- )
- }
+ );
+ resolveRun(run.restoreRunId);
+ }}
/>
))}
</VStack>
diff --git a/packages/jobs/src/inngest/functions/tasks/company-backup.ts b/packages/jobs/src/inngest/functions/tasks/company-backup.ts
index 884dfbc53b..8f4783a268 100644
--- a/packages/jobs/src/inngest/functions/tasks/company-backup.ts
+++ b/packages/jobs/src/inngest/functions/tasks/company-backup.ts
@@ -153,6 +153,9 @@ export const STRUCTURAL_TABLES = ["company"];
* Additional tables skipped in `reseed` mode — memberships, invites and
* integration state belong to the source company's users, not to a copy.
* The importing company already has its admin membership from onboarding.
+ * (Tables whose PK is keyed by a user/employee FK — employeeJob, customer/
+ * supplierAccount — are skipped automatically via `isUserScopedIdentityTable`,
+ * so they don't need listing here.)
*/
export const RESEED_SKIPPED_TABLES = [
"userToCompany",
@@ -670,6 +673,24 @@ export async function canSetReplicationRole(
* The full set is WIPED (so a table empty in the backup ends up empty, matching
* the snapshot exactly); only the subset with rows in the backup is reloaded.
*/
+/**
+ * A table whose primary key is keyed by a `user`/`employee` FK (e.g. `employeeJob`,
+ * `customerAccount`, `supplierAccount`, `userToCompany`) is per-source-user
+ * identity. A foreign restore / reseed collapses every user FK onto the single
+ * importing user, so all such rows would land on the same `(id, companyId)` PK and
+ * collide. They can never be meaningfully copied to another company — skip them on
+ * both load paths (the source's belong to the source's users; the target keeps its
+ * own). Derived from the catalog so a new table of this shape is covered
+ * automatically rather than silently reintroducing the collision.
+ */
+export function isUserScopedIdentityTable(table: TableInfo): boolean {
+ return table.foreignKeys.some(
+ (fk) =>
+ table.pkColumns.includes(fk.column) &&
+ (fk.refTable === "user" || fk.refTable === "employee")
+ );
+}
+
export function selectWipeableTables(
catalog: Catalog,
opts: { includeGroup?: boolean } = {}
@@ -678,6 +699,7 @@ export function selectWipeableTables(
return catalog.tables.filter(
(t) =>
!skip.has(t.name) &&
+ !isUserScopedIdentityTable(t) &&
(t.scopeColumn === "companyId" ||
(opts.includeGroup === true && t.scopeColumn === "companyGroupId"))
);
diff --git a/packages/jobs/src/inngest/functions/tasks/company-import.ts b/packages/jobs/src/inngest/functions/tasks/company-import.ts
index f1f3b262ff..29988a6100 100644
--- a/packages/jobs/src/inngest/functions/tasks/company-import.ts
+++ b/packages/jobs/src/inngest/functions/tasks/company-import.ts
@@ -14,6 +14,7 @@ import {
filterUnpopulated,
getCompanyTableCatalog,
getJobDatabaseClient,
+ isUserScopedIdentityTable,
newIdForTable,
RESEED_SKIPPED_TABLES,
restoreAssetsFromBackup,
@@ -142,7 +143,10 @@ export const companyImportFunction = inngest.createFunction(
backup.manifest.tables.map((t) => [t.name, new Set(t.columns)])
);
const candidateTables = catalog.tables.filter(
- (t) => !skipped.has(t.name) && (backup.data[t.name]?.length ?? 0) > 0
+ (t) =>
+ !skipped.has(t.name) &&
+ !isUserScopedIdentityTable(t) &&
+ (backup.data[t.name]?.length ?? 0) > 0
);
// Reseed is additive into a fresh company: never touch a table the