diff --git a/backend/src/db/migrate.js b/backend/src/db/migrate.js index e564d32..f155a8c 100644 --- a/backend/src/db/migrate.js +++ b/backend/src/db/migrate.js @@ -72,12 +72,41 @@ async function executeMigration(pool, filename) { /** * Main migration function */ +function logMigrationError(error) { + const msg = error?.message || String(error); + console.error('\n✗ Migration failed:', msg || '(no message)'); + if (error?.code) { + console.error(' PG code:', error.code); + } + if (error?.address && error?.port) { + console.error(' connect:', `${error.address}:${error.port}`); + } + if (error?.name === 'AggregateError' && Array.isArray(error.errors)) { + for (const e of error.errors) { + console.error(' —', e?.message || e); + } + } + if (error?.code === 'ECONNREFUSED') { + console.error( + ' hint: проверьте, что Postgres запущен и DATABASE_URL / DB_* в backend/.env совпадают с портом (часто 5432 для Postgres_TG_Bots или 5433 для локального compose).' + ); + } + if (process.env.DEBUG_MIGRATE === '1' || !msg) { + console.error(error); + } +} + async function migrate() { const pool = new Pool(getPoolConfig()); try { console.log('Connecting to database...'); - await pool.connect(); + const client = await pool.connect(); + try { + await client.query('SELECT 1'); + } finally { + client.release(); + } console.log('Connected to database\n'); // Ensure migrations table exists @@ -107,7 +136,7 @@ async function migrate() { console.log(`\n✓ Successfully executed ${pendingMigrations.length} migration(s)`); } } catch (error) { - console.error('\n✗ Migration failed:', error.message); + logMigrationError(error); process.exit(1); } finally { await pool.end();