Files
TREK/server/scripts/pg-probe/baseline.ts
T
Maurice b8144bb92a ci: probe the server's SQL against a Postgres service
Nothing in CI ran a statement against anything but SQLite, so the claim
that the dialect layer keeps the repositories portable was never measured
and new SQLite-only SQL could land unseen. The new postgres-probe job
(server/scripts/pg-probe.ts, npm run probe:pg) does that against a
postgres:17 service container:

- it derives a Postgres schema from the entities with MikroORM's schema
  generator, keeping TEXT timestamps, mapping the SQLite-only defaults and
  the NOCASE collation, and leaving foreign keys out;
- it runs every dialect helper on an in-memory SQLite and on Postgres
  against the same expectation, and every case has to pass;
- it calls every public repository method once with arguments the
  TypeScript checker builds from the parameter types, through a Postgres
  driver that runs each statement alone in a rolled-back transaction with
  a statement timeout and records whether Postgres accepted it.

The statements Postgres refuses are held per method to
scripts/pg-probe-baseline.json, which may only shrink. The baseline starts
unmeasured (failing: null): the job reports, passes and uploads the first
measurement as the pg-probe-baseline artifact, together with a JSON report,
so the commit that seeds it and every later commit that lowers it needs no
local Postgres. The probe refuses a database holding tables it did not
create.

@mikro-orm/postgresql joins the server's devDependencies at the 7.2.1 the
other MikroORM packages resolve to; the image's production install leaves
it out. The job needs the preflight gates like every test job, runs only
for the code area, stops after ten minutes and is part of ci-ok.
2026-10-08 18:39:34 +02:00

104 lines
4.5 KiB
TypeScript

/**
* The Postgres probe's ratchet: how many statements of each repository method
* Postgres may still refuse.
*
* `scripts/pg-probe-baseline.json` maps `Class.method` to the number of
* distinct statements of that method that failed on Postgres when the entry
* was written. A method may fail no more statements than its entry (none
* without one), and an entry above what its method fails now fails the check
* too, so the change that fixes a statement lowers the baseline with it.
* `--update` lowers entries and drops the ones that reach zero or whose
* method is gone; it never raises or adds one. The only exception is the
* first run: a baseline whose `failing` is `null` has never been measured,
* so the check reports and passes, and `--update` (or the CI artifact)
* writes the first measurement.
*/
import { readFileSync } from 'node:fs';
export interface ProbeBaseline {
/** `Class.method` → failing statements allowed; `null` until the first measurement is written. */
failing: Record<string, number> | null;
}
export interface BaselineVerdict {
/** The baseline has never been measured: report only. */
unseeded: boolean;
/** Methods failing more statements than their entry allows. */
grown: { method: string; allowed: number; now: number }[];
/** Entries above what their method fails now (the method may be gone). */
stale: { method: string; allowed: number; now: number }[];
}
export function parseBaseline(text: string, source: string): ProbeBaseline {
let data: unknown;
try {
data = JSON.parse(text);
} catch (error) {
throw new Error(`${source} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
}
if (data === null || typeof data !== 'object' || Array.isArray(data) || !('failing' in data)) {
throw new Error(`${source} must be an object with a "failing" key`);
}
const failing = (data as { failing: unknown }).failing;
if (failing === null) return { failing: null };
if (typeof failing !== 'object' || Array.isArray(failing)) {
throw new Error(`${source}: "failing" must be null or an object of Class.method to counts`);
}
const entries: Record<string, number> = {};
for (const [method, count] of Object.entries(failing as Record<string, unknown>)) {
if (!/^[A-Za-z_$][\w$]*\.[A-Za-z_$][\w$]*$/.test(method)) {
throw new Error(`${source}: "${method}" is not a Class.method key`);
}
if (typeof count !== 'number' || !Number.isInteger(count) || count < 1) {
throw new Error(`${source}: ${method} holds ${JSON.stringify(count)}, expected a positive integer`);
}
entries[method] = count;
}
return { failing: entries };
}
export function readBaseline(path: string): ProbeBaseline {
return parseBaseline(readFileSync(path, 'utf8'), path);
}
/** Compares what failed now (`Class.method` → failing statements, zero counts allowed) with the baseline. */
export function compareWithBaseline(baseline: ProbeBaseline, failingNow: ReadonlyMap<string, number>): BaselineVerdict {
if (baseline.failing === null) return { unseeded: true, grown: [], stale: [] };
const allowedFor = baseline.failing;
const grown: BaselineVerdict['grown'] = [];
const stale: BaselineVerdict['stale'] = [];
for (const [method, now] of [...failingNow.entries()].sort(([a], [b]) => a.localeCompare(b))) {
const allowed = allowedFor[method] ?? 0;
if (now > allowed) grown.push({ method, allowed, now });
}
for (const method of Object.keys(allowedFor).sort()) {
const allowed = allowedFor[method]!;
const now = failingNow.get(method) ?? 0;
if (now < allowed) stale.push({ method, allowed, now });
}
return { unseeded: false, grown, stale };
}
/**
* The baseline `--update` writes: every entry lowered to what its method
* fails now, zeros dropped, nothing added. An unseeded baseline is seeded
* with everything that fails now.
*/
export function lowerBaseline(baseline: ProbeBaseline, failingNow: ReadonlyMap<string, number>): ProbeBaseline {
const next: Record<string, number> = {};
if (baseline.failing === null) {
for (const [method, now] of failingNow) if (now > 0) next[method] = now;
} else {
for (const [method, allowed] of Object.entries(baseline.failing)) {
const now = Math.min(allowed, failingNow.get(method) ?? 0);
if (now > 0) next[method] = now;
}
}
return { failing: Object.fromEntries(Object.entries(next).sort(([a], [b]) => a.localeCompare(b))) };
}
export function formatBaseline(baseline: ProbeBaseline): string {
return `${JSON.stringify(baseline, null, 2)}\n`;
}