mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-23 23:18:10 +02:00
feat: implement server action to save session snapshots, including table auto-creation.
This commit is contained in:
1 parent
3b3021d85f
commit
0bec6b953b
1 file changed
+15
-9
@@ -46,10 +46,15 @@ export async function saveSnapshot(raw: unknown) {
|
|||||||
|
|
||||||
return { success: true, snapshotId: created?.id };
|
return { success: true, snapshotId: created?.id };
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Snapshot DB error, attempting auto-repair:", err);
|
console.error("Snapshot DB error:", err);
|
||||||
|
|
||||||
// Si la table n'existe pas, on tente de la créer
|
// Détection plus large (code 42P01 ou texte relation does not exist)
|
||||||
if (err.message?.includes("relation") && err.message?.includes("does not exist")) {
|
const isTableMissing = err.code === '42P01' ||
|
||||||
|
err.message?.toLowerCase().includes("relation") ||
|
||||||
|
err.message?.toLowerCase().includes("does not exist");
|
||||||
|
|
||||||
|
if (isTableMissing) {
|
||||||
|
console.log("Table missing detected, attempting auto-repair...");
|
||||||
try {
|
try {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
CREATE TABLE IF NOT EXISTS session_snapshots (
|
CREATE TABLE IF NOT EXISTS session_snapshots (
|
||||||
@@ -63,8 +68,9 @@ export async function saveSnapshot(raw: unknown) {
|
|||||||
created_at TIMESTAMP DEFAULT NOW()
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
// On ré-essaie l'insertion une seule fois
|
// On ré-essaie l'insertion une seule fois
|
||||||
const [created] = await db
|
const [retryCreated] = await db
|
||||||
.insert(sessionSnapshots)
|
.insert(sessionSnapshots)
|
||||||
.values({
|
.values({
|
||||||
codeFournisseur,
|
codeFournisseur,
|
||||||
@@ -75,14 +81,14 @@ export async function saveSnapshot(raw: unknown) {
|
|||||||
summaryJson: summary ?? null,
|
summaryJson: summary ?? null,
|
||||||
})
|
})
|
||||||
.returning({ id: sessionSnapshots.id });
|
.returning({ id: sessionSnapshots.id });
|
||||||
return { success: true, snapshotId: created?.id };
|
|
||||||
|
return { success: true, snapshotId: retryCreated?.id };
|
||||||
} catch (repairErr: any) {
|
} catch (repairErr: any) {
|
||||||
console.error("Auto-repair failed:", repairErr);
|
console.error("Repair or retry failed:", repairErr);
|
||||||
return { success: false, error: `Repair failed: ${repairErr.message}` };
|
return { success: false, error: `Repair/Retry failed: ${repairErr.message}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const errorMessage = err instanceof Error ? err.message : String(err);
|
return { success: false, error: `DB Error: ${err.message || String(err)}` };
|
||||||
return { success: false, error: `DB Error: ${errorMessage}` };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user