From 3b3021d85f4d543d858a29148d66d53a2aab0bb1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 22 Feb 2026 13:35:34 +0100 Subject: [PATCH] feat: Add API to save session snapshots and initialize the corresponding database table. --- src/features/debug/api/init-db.ts | 25 ++++++++++++ src/features/snapshots/api/save-snapshot.ts | 43 +++++++++++++++++++-- 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/features/debug/api/init-db.ts diff --git a/src/features/debug/api/init-db.ts b/src/features/debug/api/init-db.ts new file mode 100644 index 0000000..b678789 --- /dev/null +++ b/src/features/debug/api/init-db.ts @@ -0,0 +1,25 @@ +"use server"; + +import { db } from "@/db"; +import { sql } from "drizzle-orm"; + +export async function initSnapshotTable() { + try { + await db.execute(sql` + CREATE TABLE IF NOT EXISTS session_snapshots ( + id SERIAL PRIMARY KEY, + code_fournisseur VARCHAR(20) NOT NULL, + nom_fournisseur VARCHAR(255), + magasin VARCHAR(20) NOT NULL, + changes JSONB NOT NULL, + summary_json JSONB, + label TEXT, + created_at TIMESTAMP DEFAULT NOW() + ); + `); + return { success: true }; + } catch (err) { + console.error("Init DB error:", err); + return { success: false, error: err instanceof Error ? err.message : String(err) }; + } +} diff --git a/src/features/snapshots/api/save-snapshot.ts b/src/features/snapshots/api/save-snapshot.ts index 1b9f4d3..46c2f4d 100644 --- a/src/features/snapshots/api/save-snapshot.ts +++ b/src/features/snapshots/api/save-snapshot.ts @@ -3,6 +3,7 @@ import { db } from "@/db"; import { sessionSnapshots } from "@/db/schema"; import { z } from "zod"; +import { sql } from "drizzle-orm"; const SaveSnapshotSchema = z.object({ codeFournisseur: z.string(), @@ -44,8 +45,44 @@ export async function saveSnapshot(raw: unknown) { .returning({ id: sessionSnapshots.id }); return { success: true, snapshotId: created?.id }; - } catch (err) { - console.error("Snapshot DB insertion error:", err); - return { success: false, error: "Database error during insertion" }; + } catch (err: any) { + console.error("Snapshot DB error, attempting auto-repair:", err); + + // Si la table n'existe pas, on tente de la créer + if (err.message?.includes("relation") && err.message?.includes("does not exist")) { + try { + await db.execute(sql` + CREATE TABLE IF NOT EXISTS session_snapshots ( + id SERIAL PRIMARY KEY, + code_fournisseur VARCHAR(20) NOT NULL, + nom_fournisseur VARCHAR(255), + magasin VARCHAR(20) NOT NULL, + changes JSONB NOT NULL, + summary_json JSONB, + label TEXT, + created_at TIMESTAMP DEFAULT NOW() + ); + `); + // On ré-essaie l'insertion une seule fois + const [created] = await db + .insert(sessionSnapshots) + .values({ + codeFournisseur, + nomFournisseur: nomFournisseur ?? null, + magasin, + label: label ?? `Snapshot — ${new Date().toLocaleDateString("fr-FR")}`, + changes, + summaryJson: summary ?? null, + }) + .returning({ id: sessionSnapshots.id }); + return { success: true, snapshotId: created?.id }; + } catch (repairErr: any) { + console.error("Auto-repair failed:", repairErr); + return { success: false, error: `Repair failed: ${repairErr.message}` }; + } + } + + const errorMessage = err instanceof Error ? err.message : String(err); + return { success: false, error: `DB Error: ${errorMessage}` }; } }