fix(migrate): освобождать клиент пула, диагностика ECONNREFUSED и AggregateError
Made-with: Cursor
This commit is contained in:
@@ -72,12 +72,41 @@ async function executeMigration(pool, filename) {
|
|||||||
/**
|
/**
|
||||||
* Main migration function
|
* 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() {
|
async function migrate() {
|
||||||
const pool = new Pool(getPoolConfig());
|
const pool = new Pool(getPoolConfig());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('Connecting to database...');
|
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');
|
console.log('Connected to database\n');
|
||||||
|
|
||||||
// Ensure migrations table exists
|
// Ensure migrations table exists
|
||||||
@@ -107,7 +136,7 @@ async function migrate() {
|
|||||||
console.log(`\n✓ Successfully executed ${pendingMigrations.length} migration(s)`);
|
console.log(`\n✓ Successfully executed ${pendingMigrations.length} migration(s)`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('\n✗ Migration failed:', error.message);
|
logMigrationError(error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} finally {
|
} finally {
|
||||||
await pool.end();
|
await pool.end();
|
||||||
|
|||||||
Reference in New Issue
Block a user