mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
feat(db): run TREK on PostgreSQL
TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before otherwise: an engine port under src/db/engine, the dialect helpers under src/db/dialect, a Postgres baseline for the migrations up to the cut, pg_dump/pg_restore backups, a probe of every repository method against a real server and a Postgres test lane in CI.
This commit is contained in:
@@ -128,6 +128,8 @@ describe('dialect-lint.mjs', () => {
|
||||
it('DIALECT-006: PRAGMA is allowed in the src/db lifecycle files, not in a repository or a domain', () => {
|
||||
const pragma = "export const p = 'PRAGMA wal_checkpoint(TRUNCATE)';\n";
|
||||
expect(run(serverRoot({ 'src/db/durability.ts': pragma })).status).toBe(0);
|
||||
expect(run(serverRoot({ 'src/db/engine/sqlite/sqlite-maintenance.ts': pragma })).status).toBe(0);
|
||||
expect(run(serverRoot({ 'src/db/engine/postgres/maintenance.ts': pragma })).status).toBe(1);
|
||||
expect(run(serverRoot({ [REPO]: pragma })).status).toBe(1);
|
||||
expect(run(serverRoot({ 'src/nest/backup/probe.ts': pragma })).status).toBe(1);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* scripts/engine-dispatch-ratchet.mjs (lint:engine-dispatch): an engine check
|
||||
* outside src/db/dialect/ and src/db/engine/ may only shrink. Runs as the real
|
||||
* script against throwaway server roots (--dir) and once on this repository,
|
||||
* so the committed baseline is checked by the unit suite.
|
||||
*/
|
||||
import { countEngineDispatch } from '../../../scripts/engine-dispatch-ratchet.mjs';
|
||||
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const SCRIPT = path.join(__dirname, '../../../scripts/engine-dispatch-ratchet.mjs');
|
||||
const SERVER_ROOT = path.join(__dirname, '../../..');
|
||||
const BASELINE = 'scripts/engine-dispatch-baseline.json';
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
function serverRoot(files: Record<string, string>, counts: Record<string, number> = {}): string {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), 'trek-engine-dispatch-'));
|
||||
roots.push(dir);
|
||||
mkdirSync(path.join(dir, 'src'), { recursive: true });
|
||||
mkdirSync(path.join(dir, 'scripts'), { recursive: true });
|
||||
for (const [file, text] of Object.entries(files)) {
|
||||
mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
||||
writeFileSync(path.join(dir, file), text);
|
||||
}
|
||||
writeFileSync(path.join(dir, BASELINE), JSON.stringify({ counts }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
function run(dir: string, ...args: string[]): { status: number | null; out: string } {
|
||||
try {
|
||||
const stdout = execFileSync(process.execPath, [SCRIPT, `--dir=${dir}`, ...args], {
|
||||
encoding: 'utf8',
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
return { status: 0, out: stdout };
|
||||
} catch (err) {
|
||||
const { status, stdout, stderr } = err as { status: number | null; stdout: string; stderr: string };
|
||||
return { status, out: `${stdout}${stderr}` };
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of roots.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
// Spelled in pieces so this file does not read as an engine check itself.
|
||||
const IS_SQLITE = ['isSql', 'ite(platform)'].join('');
|
||||
const IS_POSTGRES = ['isPost', 'gres(p)'].join('');
|
||||
const INSTANCE = ['x instanceof ', 'SqlitePlatform'].join('');
|
||||
|
||||
describe('lint:engine-dispatch', () => {
|
||||
it('ENGDISP-001: counts the platform checks and instanceof tests, not the ones in comments', () => {
|
||||
expect(countEngineDispatch(`if (${IS_SQLITE}) a(); else if (${IS_POSTGRES}) b();`)).toBe(2);
|
||||
expect(countEngineDispatch(`const y = ${INSTANCE};`)).toBe(1);
|
||||
const BY_NAME = ["if (readEnv().db.engine === 'post", 'gres\') a(); if (engine.id !== "sql', 'ite") b();'].join('');
|
||||
expect(countEngineDispatch(BY_NAME)).toBe(2);
|
||||
expect(countEngineDispatch("if (user.id === 'admin') a();")).toBe(0);
|
||||
expect(countEngineDispatch(`// ${IS_SQLITE}\n/* ${INSTANCE} */\nconst ok = 1;`)).toBe(0);
|
||||
});
|
||||
|
||||
it('ENGDISP-002: the dialect layer and the engine port may dispatch, any other file may not', () => {
|
||||
const text = `export const x = ${IS_SQLITE};\n`;
|
||||
expect(run(serverRoot({ 'src/db/dialect/a.ts': text, 'src/db/engine/sqlite/b.ts': text })).status).toBe(0);
|
||||
const domain = run(serverRoot({ 'src/nest/trips/trips.service.ts': text }));
|
||||
expect(domain.status).toBe(1);
|
||||
expect(domain.out).toContain('src/nest/trips/trips.service.ts');
|
||||
});
|
||||
|
||||
it('ENGDISP-003: a baselined file is held at its count, and --update only lowers it', () => {
|
||||
const text = `export const x = ${IS_SQLITE};\n`;
|
||||
const file = 'src/db/repositories/A.repository.ts';
|
||||
expect(run(serverRoot({ [file]: text }, { [file]: 1 })).status).toBe(0);
|
||||
expect(run(serverRoot({ [file]: `${text}${text}` }, { [file]: 1 })).status).toBe(1);
|
||||
const stale = serverRoot({ [file]: 'export const x = 1;\n' }, { [file]: 1 });
|
||||
expect(run(stale).status).toBe(1);
|
||||
expect(run(stale, '--update').status).toBe(0);
|
||||
expect(JSON.parse(readFileSync(path.join(stale, BASELINE), 'utf8'))).toEqual({ counts: {} });
|
||||
});
|
||||
|
||||
it('ENGDISP-004: the repository holds its committed baseline', () => {
|
||||
expect(run(SERVER_ROOT).status).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -7,13 +7,14 @@
|
||||
* sample (or a pinned reason it has none).
|
||||
*/
|
||||
import {
|
||||
PRESENCE_LABEL,
|
||||
planRepositories,
|
||||
readCompilerOptions,
|
||||
sampleString,
|
||||
type MethodPlan,
|
||||
} from '../../../../scripts/pg-probe/arg-samples';
|
||||
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import ts from 'typescript';
|
||||
@@ -47,6 +48,7 @@ export class ThingsRepository {
|
||||
async record(r: Record<string, number>): Promise<void> {}
|
||||
async literals(n: bigint, flag: true): Promise<void> {}
|
||||
async destructured({ id, label }: { id: number; label: string }): Promise<void> {}
|
||||
async presence(write: { name?: readonly [present: boolean, value: string]; flag?: readonly [boolean, number] }): Promise<void> {}
|
||||
}
|
||||
export abstract class AbstractRepository {
|
||||
async skipped(): Promise<void> {}
|
||||
@@ -102,6 +104,7 @@ describe('pg-probe argument samples', () => {
|
||||
'literals',
|
||||
'nullable',
|
||||
'optionalCallback',
|
||||
'presence',
|
||||
'record',
|
||||
'rest',
|
||||
'tuple',
|
||||
@@ -145,6 +148,28 @@ describe('pg-probe argument samples', () => {
|
||||
expect(argsOf('collections')).toEqual([new Set([1]), new Map([['1', 1]])]);
|
||||
});
|
||||
|
||||
it('PGPROBE-037: a presence pair is present, so a patch built from it reaches its SQL', () => {
|
||||
// The repositories' partial updates take `[present: boolean, value]` pairs
|
||||
// and send nothing when no pair is present; a false flag would leave the
|
||||
// method unmeasured. Only the element labelled `present` turns true.
|
||||
expect(argsOf('presence')).toEqual([{ name: [true, 'probe'], flag: [false, 1] }]);
|
||||
});
|
||||
|
||||
it('PGPROBE-038: the presence label the sampler reads is the one presenceSet declares', () => {
|
||||
// The sampler matches the tuple label by name; a rename in the repository
|
||||
// helper would stop the flag being sampled true, silently unmeasuring every
|
||||
// partial update. Read the real signature, not a copy of it.
|
||||
const file = path.join(__dirname, '../../../../src/db/repositories/_shared/presence-set.ts');
|
||||
const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
||||
const labels: string[] = [];
|
||||
const visit = (node: ts.Node): void => {
|
||||
if (ts.isNamedTupleMember(node)) labels.push(node.name.text);
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(source);
|
||||
expect(labels).toContain(PRESENCE_LABEL);
|
||||
});
|
||||
|
||||
it('PGPROBE-035: a required callback makes the method unprobeable, an optional one is left off', () => {
|
||||
expect(planOf('withCallback')).toMatchObject({ unprobeable: 'fn: a function parameter' });
|
||||
expect(argsOf('optionalCallback')).toEqual([1]);
|
||||
|
||||
@@ -130,11 +130,13 @@ describe('pg-probe baseline', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('PGPROBE-010: only syntax and feature errors count toward the ratchet, not data errors from the sample values', () => {
|
||||
for (const code of ['42601', '42883', '42804', '42P01', '42703', '0A000'])
|
||||
it('PGPROBE-010: syntax, feature and data errors count toward the ratchet; constraints and timeouts do not', () => {
|
||||
// A data exception (class 22) is a value of the wrong type reaching a
|
||||
// column: a string the SQLite comparison let through where Postgres
|
||||
// refuses it, so it counts once the sampler sends the declared types.
|
||||
for (const code of ['42601', '42883', '42804', '42P01', '42703', '0A000', '22P02', '22007', '22008'])
|
||||
expect(countsTowardRatchet(code)).toBe(true);
|
||||
for (const code of ['22P02', '22007', '22008', '23505', '57014', 'NOSQLSTATE'])
|
||||
expect(countsTowardRatchet(code)).toBe(false);
|
||||
for (const code of ['23505', '23514', '57014', 'NOSQLSTATE']) expect(countsTowardRatchet(code)).toBe(false);
|
||||
});
|
||||
|
||||
it('PGPROBE-008: the checked-in baseline parses and formats back to itself', () => {
|
||||
|
||||
@@ -47,7 +47,7 @@ const RESULTS: MethodResult[] = [
|
||||
{ key: 'F.thrower', statements: [], threw: 'no trip 1' },
|
||||
{
|
||||
key: 'G.sampled',
|
||||
statements: [refused('select $1::date', '22007', 'invalid input syntax for type date: "probe"'), ok('select 5')],
|
||||
statements: [refused('insert into t values ($1)', '23514', 'violates check constraint "t_check"'), ok('select 5')],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -84,26 +84,30 @@ describe('pg-probe report', () => {
|
||||
new Map([
|
||||
['42883', 1],
|
||||
['42601', 1],
|
||||
['22007', 1],
|
||||
['23514', 1],
|
||||
]),
|
||||
);
|
||||
expect(summary.failedStatements.map((f) => `${f.method} ${f.code} ${f.counted}`)).toEqual([
|
||||
'B.broken 42883 true',
|
||||
'B.broken 42601 true',
|
||||
'G.sampled 22007 false',
|
||||
'G.sampled 23514 false',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGPROBE-068: a data error from a sample value is reported but not held to the baseline', () => {
|
||||
it('PGPROBE-068: a data error is held to the baseline, a constraint over a sample value is only reported', () => {
|
||||
const summary = summarize([
|
||||
{
|
||||
key: 'G.sampled',
|
||||
statements: [refused('select $1::date', '22P02', 'bad'), refused('select x', '0A000', 'nope')],
|
||||
statements: [
|
||||
refused('select $1::date', '22P02', 'bad'),
|
||||
refused('select x', '0A000', 'nope'),
|
||||
refused('insert into t values ($1)', '23514', 'check'),
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(summary.failingByMethod.get('G.sampled')).toBe(1);
|
||||
expect(splitRefusals(summary).counted.map((f) => f.code)).toEqual(['0A000']);
|
||||
expect(splitRefusals(summary).reported.map((f) => f.code)).toEqual(['22P02']);
|
||||
expect(summary.failingByMethod.get('G.sampled')).toBe(2);
|
||||
expect(splitRefusals(summary).counted.map((f) => f.code)).toEqual(['22P02', '0A000']);
|
||||
expect(splitRefusals(summary).reported.map((f) => f.code)).toEqual(['23514']);
|
||||
});
|
||||
|
||||
it('PGPROBE-069: names every method whose SQL the probe did not see, and why', () => {
|
||||
@@ -187,9 +191,9 @@ describe('pg-probe report', () => {
|
||||
expect(text).toContain('FAIL [postgres] dateOf: expected 1, got 2');
|
||||
expect(text).toContain('collation "NOCASE" does not exist');
|
||||
expect(text).toContain(
|
||||
'Statements: 8 distinct, 3 refused by Postgres (2 as SQL, held to the baseline; 1 over a sample value',
|
||||
"Statements: 8 distinct, 3 refused by Postgres (2 as SQL or over a value's type, held to the baseline; 1 over a constraint",
|
||||
);
|
||||
expect(text).toContain(' 22007: 1 (not held)');
|
||||
expect(text).toContain(' 23514: 1 (not held)');
|
||||
expect(text).toContain(' F.thrower: threw before any statement: no trip 1');
|
||||
expect(text).toContain(
|
||||
'FAIL F.thrower threw before any statement: no trip 1, and the baseline does not list it as uncovered.',
|
||||
@@ -217,9 +221,9 @@ describe('pg-probe report', () => {
|
||||
expect(md).toContain("| postgres | a\\|b | got 'x' |");
|
||||
expect(md).toContain('| 42883 | 1 |');
|
||||
expect(md).toContain('| E.skipped | could not be called: fn: a function parameter |');
|
||||
expect(md).toContain('| Refused by Postgres as SQL (held) | 2 |');
|
||||
expect(md).toContain('| Refused over a sample value or the empty tables (reported only) | 1 |');
|
||||
expect(md).toContain('| G.sampled | 22007 | no | invalid input syntax for type date: "probe" |');
|
||||
expect(md).toContain("| Refused by Postgres as SQL or over a value's type (held) | 2 |");
|
||||
expect(md).toContain('| Refused over a constraint, a timeout or the empty tables (reported only) | 1 |');
|
||||
expect(md).toContain('| G.sampled | 23514 | no | violates check constraint "t_check" |');
|
||||
const ratchet = formatMarkdown(
|
||||
input({ verdict: { ...CLEAN, newlyUncovered: ['C.silent'], nowCovered: ['Z.gone'] } }),
|
||||
);
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* How the Postgres probe turns the entities into a Postgres schema
|
||||
* (scripts/pg-probe/schema.ts): SQLite-only defaults, TEXT timestamps, the
|
||||
* NOCASE collation, and splitting the generated DDL so one refused statement
|
||||
* does not take the rest with it.
|
||||
*/
|
||||
import {
|
||||
adjustColumns,
|
||||
NOCASE_COLLATION_SQL,
|
||||
PG_NOW_TEXT_DEFAULT,
|
||||
portableDefault,
|
||||
splitStatements,
|
||||
type ColumnMeta,
|
||||
} from '../../../../scripts/pg-probe/schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
class DbTimestampType {}
|
||||
class TextType {}
|
||||
|
||||
describe('pg-probe schema', () => {
|
||||
it('PGPROBE-040: maps the SQLite clock defaults and leaves every other default alone', () => {
|
||||
expect(PG_NOW_TEXT_DEFAULT).toBe("to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS')");
|
||||
expect(portableDefault('CURRENT_TIMESTAMP')).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(datetime('now'))")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("datetime( 'now' )")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(strftime('%s','now'))")).toBe('CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)');
|
||||
for (const kept of ['0', '1', 'NULL', "'none'", "'#10b981'", "'{}'"]) expect(portableDefault(kept)).toBe(kept);
|
||||
});
|
||||
|
||||
it('PGPROBE-041: makes timestamp columns text, rewrites defaults and the NOCASE collation, and names what it changed', () => {
|
||||
const columns: ColumnMeta[] = [
|
||||
{
|
||||
name: 'created_at',
|
||||
customType: new DbTimestampType(),
|
||||
columnTypes: ['timestamptz'],
|
||||
defaultRaw: 'CURRENT_TIMESTAMP',
|
||||
},
|
||||
{ name: 'title', customType: new TextType(), columnTypes: ['text'], defaultRaw: "'x'" },
|
||||
{ name: 'stamp', columnTypes: ['int'], defaultRaw: "(strftime('%s','now'))" },
|
||||
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
|
||||
{ name: 'id', columnTypes: ['int'] },
|
||||
];
|
||||
expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
|
||||
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
|
||||
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
|
||||
expect(columns[2]).toMatchObject({
|
||||
columnTypes: ['int'],
|
||||
defaultRaw: 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)',
|
||||
});
|
||||
expect(columns[3]).toMatchObject({ collation: 'nocase' });
|
||||
expect(NOCASE_COLLATION_SQL).toContain('CREATE COLLATION IF NOT EXISTS nocase');
|
||||
});
|
||||
|
||||
it('PGPROBE-042: splits DDL on semicolons outside quotes and drops session settings', () => {
|
||||
const ddl = [
|
||||
"set names 'utf8';",
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\');',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\';',
|
||||
'',
|
||||
'create table "b ;" ("id" int)',
|
||||
].join('\n');
|
||||
expect(splitStatements(ddl)).toEqual([
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\')',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\'',
|
||||
'create table "b ;" ("id" int)',
|
||||
]);
|
||||
expect(splitStatements(' ; ; ')).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user