From c5b0896bb4755fac776f5122f27961c6771a496f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 29 Jun 2026 06:30:00 +0000 Subject: [PATCH] fix: dedupe foucad rows by foucode before full refresh The source MSSQL FOUCAD table can return several rows for the same FOUCODE, but the PostgreSQL foucad table uses foucode as PRIMARY KEY. The bulk INSERT in fullRefresh has no conflict handling, so duplicate FOUCODE values triggered: duplicate key value violates unique constraint "foucad_pkey" Deduplicate by foucode before insert, keeping the most recently modified row (suividatemodif). Routes join foucad 1:1 on foucode, so one config row per supplier code is the intended shape. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_011ad1qwhDEHNniSUDveTQHL --- sync/tables/appro.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sync/tables/appro.js b/sync/tables/appro.js index f4e5ab1..f630da1 100644 --- a/sync/tables/appro.js +++ b/sync/tables/appro.js @@ -171,7 +171,19 @@ async function syncAppro(force) { suividatemodif: r.SUIVIDATEMODIF, })); - const count = await fullRefresh(pg, 'foucad', rows, FOUCAD_COLS); + // foucode est PK côté PostgreSQL mais la source FOUCAD peut renvoyer + // plusieurs lignes par FOUCODE → on déduplique en gardant la plus récente + // (suividatemodif), sinon l'INSERT viole foucad_pkey. + const byCode = new Map(); + for (const row of rows) { + const prev = byCode.get(row.foucode); + if (!prev || (row.suividatemodif ?? 0) >= (prev.suividatemodif ?? 0)) { + byCode.set(row.foucode, row); + } + } + const dedupRows = [...byCode.values()]; + + const count = await fullRefresh(pg, 'foucad', dedupRows, FOUCAD_COLS); await logSync(pg, 'foucad', count, 'ok'); console.log(`[foucad] ${count} lignes`); } catch (err) {