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