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: tolerate an existing scriptRun row and skip already-migrated objects (#1690)

Anshul sharma7ddca58768db
diff --git a/ci/src/migrations.ts b/ci/src/migrations.ts
index f4f7b2338c..35fb6be869 100644
--- a/ci/src/migrations.ts
+++ b/ci/src/migrations.ts
@@ -74,9 +74,16 @@ async function runPendingScripts(
const { stdout } = await $$`tsx ${script.path}`;
const tail = stdout.trim().split("\n").slice(-20).join("\n");
+ // ignoreDuplicates: a row for this name may already exist — a retried
+ // insert whose first attempt landed, or an earlier run that recorded it.
+ // Either way the run IS recorded; keep the original row (and its ranAt)
+ // rather than failing the whole deploy over bookkeeping.
const { error } = await ledger
.from("scriptRun")
- .insert({ name: script.name, result: { output: tail } });
+ .upsert(
+ { name: script.name, result: { output: tail } },
+ { onConflict: "name", ignoreDuplicates: true }
+ );
if (error) {
// The work is done but unrecorded, so the next deploy runs it again.
diff --git a/scripts/one-off/migrate-private-buckets.ts b/scripts/one-off/migrate-private-buckets.ts
index 88ffb0eeee..3152dedfa8 100644
--- a/scripts/one-off/migrate-private-buckets.ts
+++ b/scripts/one-off/migrate-private-buckets.ts
@@ -67,20 +67,20 @@ async function getCompanyIds(): Promise<string[]> {
return ids;
}
-// Recursively list every object key under `prefix` in the legacy bucket.
+// Recursively list every object key under `prefix` in `bucket`.
// Storage list returns folders as entries with id === null; recurse into them.
-async function listObjectKeys(prefix: string): Promise<string[]> {
+async function listObjectKeys(bucket: string, prefix: string): Promise<string[]> {
const keys: string[] = [];
for (let offset = 0; ; offset += LIST_PAGE_SIZE) {
const { data, error } = await client.storage
- .from(LEGACY_BUCKET)
+ .from(bucket)
.list(prefix, { limit: LIST_PAGE_SIZE, offset });
if (error) {
- throw new Error(`Failed to list ${LEGACY_BUCKET}/${prefix}: ${error.message}`);
+ throw new Error(`Failed to list ${bucket}/${prefix}: ${error.message}`);
}
for (const entry of data as Array<{ name: string; id: string | null }>) {
if (entry.id === null) {
- keys.push(...(await listObjectKeys(`${prefix}/${entry.name}`)));
+ keys.push(...(await listObjectKeys(bucket, `${prefix}/${entry.name}`)));
} else {
keys.push(`${prefix}/${entry.name}`);
}
@@ -90,6 +90,18 @@ async function listObjectKeys(prefix: string): Promise<string[]> {
return keys;
}
+// Keys already present in the destination bucket, so a re-run skips them
+// without issuing a copy request per object. Purely an optimization: if the
+// listing fails (bucket missing on a dry run, transient error), return
+// nothing and let the per-object already-exists check catch the duplicates.
+async function listAlreadyMigratedKeys(companyId: string): Promise<string[]> {
+ try {
+ return await listObjectKeys(companyId, companyId);
+ } catch {
+ return [];
+ }
+}
+
async function ensureBucket(companyId: string) {
const { error } = await client.storage.createBucket(companyId, {
public: false,
@@ -107,9 +119,15 @@ async function migrateCompany(companyId: string): Promise<CompanySummary> {
await ensureBucket(companyId);
}
- const keys = await listObjectKeys(companyId);
+ const keys = await listObjectKeys(LEGACY_BUCKET, companyId);
+ const alreadyMigrated = new Set(await listAlreadyMigratedKeys(companyId));
for (const key of keys) {
+ if (alreadyMigrated.has(key)) {
+ summary.skipped += 1;
+ continue;
+ }
+
if (isDryRun) {
process.stdout.write(` would copy ${LEGACY_BUCKET}/${key} -> ${companyId}/${key}\n`);
summary.copied += 1;