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:
Isaias Tavares
2026-11-05 01:15:21 -03:00
committed by Maurice
parent 17b47b38b9
commit 2e3762afe1
176 changed files with 6728 additions and 777 deletions
@@ -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([]);
});
});