From 70dd610aa20c10d8b52514814447b3180173270f Mon Sep 17 00:00:00 2001 From: Nicolas Fryder Date: Sat, 27 Jun 2026 16:16:28 +0200 Subject: [PATCH] =?UTF-8?q?fix(migrate):=20cr=C3=A9er=20un=20nouveau=20pg.?= =?UTF-8?q?Client=20=C3=A0=20chaque=20tentative=20de=20retry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pg.Client ne peut pas être réutilisé après un connect() échoué. Le retry créait le même client → "Client has already been connected" dès l'attempt 2. On crée maintenant un nouveau client à chaque essai. Co-Authored-By: Claude Sonnet 4.6 --- apps/api/src/migrate.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/api/src/migrate.js b/apps/api/src/migrate.js index f8d9bf7..8694139 100644 --- a/apps/api/src/migrate.js +++ b/apps/api/src/migrate.js @@ -6,12 +6,14 @@ import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const MIGRATIONS_DIR = path.join(__dirname, '../migrations'); -async function connectWithRetry(client, maxAttempts = 12) { +async function connectWithRetry(connStr, maxAttempts = 12) { for (let i = 1; i <= maxAttempts; i++) { + const client = new pg.Client({ connectionString: connStr }); try { await client.connect(); - return; + return client; } catch (err) { + await client.end().catch(() => {}); if (i === maxAttempts) throw err; const delay = Math.min(2000 * i, 30000); console.log(`[migrate] DB not ready (attempt ${i}/${maxAttempts}), retry in ${delay}ms — ${err.message}`); @@ -20,10 +22,8 @@ async function connectWithRetry(client, maxAttempts = 12) { } } -const client = new pg.Client({ connectionString: process.env.DATABASE_URL }); - async function run() { - await connectWithRetry(client); + const client = await connectWithRetry(process.env.DATABASE_URL); await client.query(` CREATE TABLE IF NOT EXISTS schema_migrations (