fix(db): run every repository statement the Postgres probe reaches

The probe's baseline is empty now: no repository method Postgres refuses,
none it cannot measure.

- reservations.accommodation_id (TEXT) is cast where it is compared with
  day_accommodations.id (INTEGER), in the four reservation reads and the two
  stay reads; SQLite coerced the bare comparison, Postgres refuses it. The
  joined RS18/RR1 read moves to reservation-join.ts beside the repository.
- The usage counters qualify the target row's column in DO UPDATE SET, which
  Postgres reads as ambiguous with `excluded` otherwise.
- ReservationDayPositions.upsertScoped upserts on its primary key instead of
  INSERT OR REPLACE; position is the only other column and nothing references
  the table, so the row left behind is the same.
- PRAGMA wal_checkpoint and VACUUM INTO move from MaintenanceRepository to
  SqliteMaintenance under src/db/engine/sqlite/, the SQLite engine's folder,
  where lint:dialect allows PRAGMA.
- The places list takes category and tag ids as numbers: the service converts
  the query text with toRowId and answers an empty list for one that is no
  id, instead of binding the text to an integer column.
- The probe samples the `present` flag of a partial update's pair as true, so
  the four update methods that sent no SQL are measured, and it now holds
  SQLSTATE class 22 too: a value of the wrong type reaching a column is the
  code's, since the sampler sends what each parameter declares.
This commit is contained in:
Isaias Tavares
2026-11-02 12:48:08 -03:00
parent 14c767a675
commit 62912e5db1
36 changed files with 475 additions and 274 deletions
@@ -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);
});
@@ -47,6 +47,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 +103,7 @@ describe('pg-probe argument samples', () => {
'literals',
'nullable',
'optionalCallback',
'presence',
'record',
'rest',
'tuple',
@@ -145,6 +147,13 @@ 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-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'] } }),
);