mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-24 15:38:09 +02:00
feat: implement server-side function to save session snapshots, including input validation and database table creation fallback.
This commit is contained in:
1 parent
0bec6b953b
commit
7288b3da72
1 file changed
+11
-14
@@ -46,16 +46,10 @@ 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:", err);
|
console.error("Initial snapshot save failed, attempting auto-repair...", err);
|
||||||
|
|
||||||
// Détection plus large (code 42P01 ou texte relation 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 {
|
||||||
|
// Tentative systématique de création de table (no-op si déjà là)
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
CREATE TABLE IF NOT EXISTS session_snapshots (
|
CREATE TABLE IF NOT EXISTS session_snapshots (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
@@ -69,7 +63,7 @@ export async function saveSnapshot(raw: unknown) {
|
|||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
// On ré-essaie l'insertion une seule fois
|
// Deuxième tentative d'insertion
|
||||||
const [retryCreated] = await db
|
const [retryCreated] = await db
|
||||||
.insert(sessionSnapshots)
|
.insert(sessionSnapshots)
|
||||||
.values({
|
.values({
|
||||||
@@ -84,11 +78,14 @@ export async function saveSnapshot(raw: unknown) {
|
|||||||
|
|
||||||
return { success: true, snapshotId: retryCreated?.id };
|
return { success: true, snapshotId: retryCreated?.id };
|
||||||
} catch (repairErr: any) {
|
} catch (repairErr: any) {
|
||||||
console.error("Repair or retry failed:", repairErr);
|
console.error("Snapshot auto-repair/retry failed:", repairErr);
|
||||||
return { success: false, error: `Repair/Retry failed: ${repairErr.message}` };
|
// On renvoie les deux erreurs pour comprendre si c'est la création ou l'insertion qui échoue
|
||||||
|
const origMsg = (err.message || String(err)).split('\n')[0];
|
||||||
|
const retryMsg = (repairErr.message || String(repairErr)).split('\n')[0];
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: `Erreur initiale: ${origMsg} | Erreur après réparation: ${retryMsg}`
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: false, error: `DB Error: ${err.message || String(err)}` };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user