feat: db pool + migration runner with idempotency

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-05-31 02:05:53 +10:00
parent 45186f7566
commit 789ab8fca8
5 changed files with 102 additions and 0 deletions

28
tests/db/migrate.test.js Normal file
View File

@@ -0,0 +1,28 @@
import { describe, it, expect, beforeAll } from 'vitest';
import { resetDb, withClient } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
describe('migrate', () => {
beforeAll(async () => { await resetDb(); });
it('creates schema_migrations table on first run', async () => {
await migrateUp();
await withClient(async (c) => {
const { rows } = await c.query(
`SELECT to_regclass('public.schema_migrations') AS t;`
);
expect(rows[0].t).toBe('schema_migrations');
});
});
it('is idempotent — second run is a no-op', async () => {
await migrateUp();
await migrateUp();
await withClient(async (c) => {
const { rows } = await c.query(
`SELECT count(*)::int AS n FROM schema_migrations;`
);
expect(rows[0].n).toBeGreaterThanOrEqual(0);
});
});
});

16
tests/helpers/db.js Normal file
View File

@@ -0,0 +1,16 @@
import { pool } from '../../lib/db/pool.js';
export async function resetDb() {
await pool.query(`
DROP SCHEMA IF EXISTS public CASCADE;
CREATE SCHEMA public;
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE EXTENSION IF NOT EXISTS vector;
`);
}
export async function withClient(fn) {
const client = await pool.connect();
try { await fn(client); }
finally { client.release(); }
}

1
tests/helpers/setup.js Normal file
View File

@@ -0,0 +1 @@
import 'dotenv/config';