mirror of
https://github.com/djhackersdev/minime.git
synced 2026-07-19 00:31:30 -05:00
db: Add connectivity and schema version check
This commit is contained in:
parent
2fd8081a8f
commit
0f67c00664
28
src/db.ts
28
src/db.ts
|
|
@ -3,9 +3,14 @@ import { Pool, PoolClient } from "pg";
|
|||
|
||||
export type Id<T> = bigint & { __id: T };
|
||||
|
||||
const pool = new Pool();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user