db: Add connectivity and schema version check

This commit is contained in:
Tau
2019-11-06 17:28:30 -05:00
parent 2fd8081a8f
commit 0f67c00664
+25 -1
View File
@@ -3,9 +3,14 @@ import { Pool, PoolClient } from "pg";
export type Id<T> = bigint & { __id: T };
const currentSchemaVer = 0;
const pool = new Pool();
const fence = testConnection();
export async function connect(): Promise<PoolClient> {
await fence;
export function connect(): Promise<PoolClient> {
return pool.connect();
}
@@ -30,3 +35,22 @@ export function generateExtId(): number {
return buf.readUInt32BE(0);
}
async function testConnection(): Promise<void> {
const conn = await pool.connect();
try {
const { rows } = await conn.query("select schemaver from meta");
const { schemaver } = rows[0];
if (schemaver !== currentSchemaVer) {
throw new Error(
`Expected schema version ${currentSchemaVer}, db is on ${schemaver}`
);
}
console.log("SQL DB: Connection established");
} finally {
conn.release();
}
}