From c55366fb2594a61aa8d2fff82ce2679a3188a35c Mon Sep 17 00:00:00 2001 From: Nicolas Fryder Date: Sat, 27 Jun 2026 16:12:10 +0200 Subject: [PATCH] fix: migrate.js retry DB connection + align CORS_ORIGINS pattern on dev MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit migrate.js now retries up to 12 times with linear backoff (max 30s per attempt) instead of crashing immediately — prevents container restart loops when the standalone Coolify DB is temporarily unreachable at startup. CORS_ORIGINS in docker-compose.dev.yml now uses ${CORS_ORIGINS:-https://dev.metalfrom.eu} to match the prod pattern and allow Coolify UI override. Co-Authored-By: Claude Sonnet 4.6 --- apps/api/src/migrate.js | 16 +++++++++++++++- docker-compose.dev.yml | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/api/src/migrate.js b/apps/api/src/migrate.js index 4119472..f8d9bf7 100644 --- a/apps/api/src/migrate.js +++ b/apps/api/src/migrate.js @@ -6,10 +6,24 @@ 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) { + for (let i = 1; i <= maxAttempts; i++) { + try { + await client.connect(); + return; + } catch (err) { + 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}`); + await new Promise(r => setTimeout(r, delay)); + } + } +} + const client = new pg.Client({ connectionString: process.env.DATABASE_URL }); async function run() { - await client.connect(); + await connectWithRetry(client); await client.query(` CREATE TABLE IF NOT EXISTS schema_migrations ( diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 7cc6668..9a200a8 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -84,7 +84,7 @@ services: PORT: "3000" DATABASE_URL: ${DATABASE_URL} BM_IMPORT_TOKEN: ${BM_IMPORT_TOKEN} - CORS_ORIGINS: https://dev.metalfrom.eu + CORS_ORIGINS: ${CORS_ORIGINS:-https://dev.metalfrom.eu} networks: - bm_internal - coolify