69 lines
3.1 KiB
JavaScript
69 lines
3.1 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Database connection smoke test.
|
|
* Verifies DATABASE_URL credentials before running migrations.
|
|
* Run: npm run db:test
|
|
*/
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const pg_1 = require("pg");
|
|
const backendRoot = path_1.default.resolve(__dirname, '..', '..');
|
|
const repoRoot = path_1.default.resolve(__dirname, '..', '..', '..');
|
|
[path_1.default.join(backendRoot, '.env'), path_1.default.join(repoRoot, '.env')].forEach((envPath) => {
|
|
dotenv_1.default.config({ path: envPath });
|
|
});
|
|
/**
|
|
* Connects to PostgreSQL using DATABASE_URL and prints the connected user and database.
|
|
* Exits with code 1 and actionable hints on auth or connection errors.
|
|
*/
|
|
async function main() {
|
|
const url = process.env.DATABASE_URL;
|
|
if (!url || !url.trim()) {
|
|
console.error('DATABASE_URL is not set in .env');
|
|
process.exit(1);
|
|
}
|
|
const safeUrl = url.replace(/:([^:@]+)@/, ':****@');
|
|
console.log('Connecting with:', safeUrl);
|
|
try {
|
|
const parsed = new URL(url);
|
|
const passwordFromEnv = process.env.DATABASE_PASSWORD;
|
|
const passwordFromUrl = parsed.password ? decodeURIComponent(parsed.password) : '';
|
|
const password = typeof passwordFromEnv === 'string' && passwordFromEnv !== '' ? passwordFromEnv : passwordFromUrl;
|
|
const pool = new pg_1.Pool({
|
|
host: parsed.hostname,
|
|
port: parsed.port ? parseInt(parsed.port, 10) : 5432,
|
|
database: parsed.pathname?.slice(1) || 'crypto_wallet',
|
|
user: parsed.username || 'postgres',
|
|
password: typeof password === 'string' ? password : '',
|
|
connectionTimeoutMillis: 5000,
|
|
});
|
|
const client = await pool.connect();
|
|
const r = await client.query('SELECT current_user, current_database()');
|
|
client.release();
|
|
await pool.end();
|
|
console.log('OK Connected as', r.rows[0].current_user, 'to database', r.rows[0].current_database);
|
|
process.exit(0);
|
|
}
|
|
catch (err) {
|
|
const e = err;
|
|
console.error('Connection failed:', e.message);
|
|
if (e.code === '28P01') {
|
|
console.error('\n--- Password authentication failed ---');
|
|
console.error('Try setting the password separately (no URL encoding needed):');
|
|
console.error(' DATABASE_URL=postgresql://postgres@localhost:5432/crypto_wallet');
|
|
console.error(' DATABASE_PASSWORD=your_postgres_password');
|
|
console.error('Or put the password in the URL; if it has @ # % : / encode them (e.g. @ → %40).');
|
|
}
|
|
if (e.code === 'ECONNREFUSED') {
|
|
console.error('\n--- Connection refused ---');
|
|
console.error('PostgreSQL is not running or not listening on the host/port in DATABASE_URL.');
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
main();
|
|
//# sourceMappingURL=test-connection.js.map
|