fix: migrate.js retry DB connection + align CORS_ORIGINS pattern on dev

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 <noreply@anthropic.com>
This commit is contained in:
Nicolas Fryder 2026-06-27 16:12:10 +02:00
parent bf03647de6
commit c55366fb25
2 changed files with 16 additions and 2 deletions

View file

@ -6,10 +6,24 @@ import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __dirname = path.dirname(fileURLToPath(import.meta.url));
const MIGRATIONS_DIR = path.join(__dirname, '../migrations'); 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 }); const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
async function run() { async function run() {
await client.connect(); await connectWithRetry(client);
await client.query(` await client.query(`
CREATE TABLE IF NOT EXISTS schema_migrations ( CREATE TABLE IF NOT EXISTS schema_migrations (

View file

@ -84,7 +84,7 @@ services:
PORT: "3000" PORT: "3000"
DATABASE_URL: ${DATABASE_URL} DATABASE_URL: ${DATABASE_URL}
BM_IMPORT_TOKEN: ${BM_IMPORT_TOKEN} BM_IMPORT_TOKEN: ${BM_IMPORT_TOKEN}
CORS_ORIGINS: https://dev.metalfrom.eu CORS_ORIGINS: ${CORS_ORIGINS:-https://dev.metalfrom.eu}
networks: networks:
- bm_internal - bm_internal
- coolify - coolify