This commit is contained in:
z.ai committed 2026-10-08 11:09:37 +08:00
1 parent c1541b80df
commit b4c7908029
97 files changed
+12403 -154

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
/**
* 数据库连通性探测(node-postgres 版)。
*
* 用法:node scripts/db-check.mjs
*
* 与之前的版本不同,这里直接用 pg 驱动走 TCP 协议,
* 因此能连接本地/自建的普通 Postgres。
*/
import pg from "pg";
import { config } from "dotenv";
config({ path: ".env.local" });
const url = process.env.DATABASE_URL;
if (!url) {
console.error("缺少 DATABASE_URL(检查 .env.local)");
process.exit(1);
}
const masked = url.replace(/:\/\/([^:]+):[^@]+@/, "://$1:***@");
console.log(`连接目标: ${masked}\n`);
const parsed = new URL(url);
const sslmode = parsed.searchParams.get("sslmode");
const isLocal = /^(localhost|127\.0\.0\.1|::1)$/.test(parsed.hostname);
const useSsl = sslmode === "disable" ? false : sslmode ? true : !isLocal;
console.log(`主机: ${parsed.hostname}:${parsed.port || 5432}`);
console.log(`sslmode: ${sslmode ?? "(未指定)"} → 启用 SSL: ${useSsl}\n`);
const client = new pg.Client({
connectionString: url,
ssl: useSsl ? { rejectUnauthorized: false } : undefined,
});
try {
await client.connect();
const info = await client.query(
"select version() as version, current_database() as db, current_user as usr",
);
console.log("✓ 连接成功");
console.log(` 数据库: ${info.rows[0].db}`);
console.log(` 用户: ${info.rows[0].usr}`);
console.log(` 版本: ${String(info.rows[0].version).split(",")[0]}\n`);
const tables = await client.query(
`select table_name from information_schema.tables
where table_schema = 'public' order by table_name`,
);
console.log(`public schema 下的表 (${tables.rowCount}):`);
if (tables.rowCount === 0) {
console.log(" (空 —— 尚未执行迁移)");
} else {
for (const row of tables.rows) {
const count = await client.query(
`select count(*)::int as n from "${row.table_name}"`,
);
console.log(` - ${row.table_name} (${count.rows[0].n} 行)`);
}
}
} catch (error) {
console.error("✗ 连接失败:", error.message);
process.exitCode = 1;
} finally {
await client.end().catch(() => {});
}