86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
#!/usr/bin/env -S deno run -A
|
|
import { load } from "$std/dotenv/mod.ts";
|
|
import { checkDatabaseConnection } from "./db/db.ts";
|
|
import { Pool } from "pg";
|
|
|
|
async function testConnection() {
|
|
// Load environment variables (allow empty values for optional vars)
|
|
await load({ export: true, allowEmptyValues: true });
|
|
|
|
console.log("Testing database connection...");
|
|
|
|
const isConnected = await checkDatabaseConnection();
|
|
|
|
if (isConnected) {
|
|
console.log("✅ Database connection: SUCCESS");
|
|
console.log("Database is connected and responding to queries");
|
|
const connectionString = Deno.env.get("POSTGRES_OPTIONS");
|
|
if (connectionString) {
|
|
const pool = new Pool({
|
|
connectionString: connectionString.replace(
|
|
"postgresql://",
|
|
"postgres://",
|
|
),
|
|
});
|
|
const client = await pool.connect();
|
|
try {
|
|
const dbInfo = await client.query(`
|
|
SELECT
|
|
current_database() AS current_database,
|
|
current_setting('search_path') AS search_path
|
|
`);
|
|
const row = dbInfo.rows[0] as {
|
|
current_database: string;
|
|
search_path: string;
|
|
};
|
|
console.log(`Connected database: ${row.current_database}`);
|
|
console.log(`search_path: ${row.search_path}`);
|
|
|
|
const tableLocations = await client.query(`
|
|
SELECT table_schema, table_name
|
|
FROM information_schema.tables
|
|
WHERE table_name IN ('users', 'user_tokens', 'state_keys')
|
|
ORDER BY table_name, table_schema
|
|
`);
|
|
|
|
console.log(
|
|
"Table locations (users/user_tokens/state_keys):",
|
|
tableLocations.rows,
|
|
);
|
|
|
|
const hasPublicUsers = tableLocations.rows.some((r) =>
|
|
r.table_schema === "public" && r.table_name === "users"
|
|
);
|
|
const hasPublicUserTokens = tableLocations.rows.some((r) =>
|
|
r.table_schema === "public" && r.table_name === "user_tokens"
|
|
);
|
|
const hasMemoryUsers = tableLocations.rows.some((r) =>
|
|
r.table_schema === "memory" && r.table_name === "users"
|
|
);
|
|
const hasMemoryUserTokens = tableLocations.rows.some((r) =>
|
|
r.table_schema === "memory" && r.table_name === "user_tokens"
|
|
);
|
|
|
|
if (
|
|
hasPublicUsers && hasPublicUserTokens &&
|
|
(hasMemoryUsers || hasMemoryUserTokens)
|
|
) {
|
|
console.warn(
|
|
"⚠️ Detected duplicate auth tables across public and memory schemas. This can cause FK mismatches if search_path is not public-first.",
|
|
);
|
|
}
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
}
|
|
Deno.exit(0);
|
|
} else {
|
|
console.log("❌ Database connection: FAILED");
|
|
console.log("Could not connect to database or query timed out");
|
|
Deno.exit(1);
|
|
}
|
|
}
|
|
|
|
testConnection();
|