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
-3
View File
@@ -26,9 +26,6 @@
"src/db/entities/PluginUserErasureQueue.entity.ts": {
"datetime": 1
},
"src/db/repositories/MaintenanceRepository.ts": {
"pragma": 1
},
"src/nest/backup/backup-archive.ts": {
"pragma": 1
}
+9 -2
View File
@@ -124,9 +124,16 @@ export function classify(file) {
return 'source';
}
/** PRAGMA is fine in src/db/*.ts (connection, durability, baseline, snapshot) and the migrations. */
/**
* PRAGMA is fine in src/db/*.ts (connection, durability, baseline, snapshot),
* the migrations and the SQLite engine's own folder (src/db/engine/sqlite/).
*/
export function pragmaAllowed(file) {
return /^src\/db\/[^/]+\.ts$/.test(file) || file.startsWith('src/db/migrations/');
return (
/^src\/db\/[^/]+\.ts$/.test(file) ||
file.startsWith('src/db/migrations/') ||
file.startsWith('src/db/engine/sqlite/')
);
}
function isSqlTag(node) {
+2 -19
View File
@@ -1,21 +1,4 @@
{
"failing": {
"DayAccommodationsRepository.listForTripWithPlaceAndBooking": 1,
"DayAccommodationsRepository.listRoadtripStays": 1,
"GoogleApiUsageRepository.recordCall": 1,
"MaintenanceRepository.vacuumInto": 1,
"MaintenanceRepository.walCheckpoint": 1,
"ReservationDayPositionsRepository.upsertScoped": 1,
"ReservationsRepository.findWithJoins": 1,
"ReservationsRepository.listForCalendar": 1,
"ReservationsRepository.listForTrip": 1,
"ReservationsRepository.listPublicStaysForCalendar": 1,
"RouteUsageDailyRepository.record": 1
},
"uncovered": [
"BucketListRepository.update",
"BudgetItemsRepository.update",
"PackingBagsRepository.update",
"TodoItemsRepository.update"
]
"failing": {},
"uncovered": []
}
+5 -4
View File
@@ -15,10 +15,11 @@
* (pg-probe/arg-samples.ts), through a MikroORM Postgres driver that
* runs each statement in its own rolled-back transaction and records
* whether Postgres accepted it (pg-probe/recorder.ts);
* 4. holds the statements Postgres refuses as SQL (SQLSTATE class 42 or
* 0A), per method, to scripts/pg-probe-baseline.json, which may only
* shrink (pg-probe/baseline.ts); a refusal over a sample value
* (class 22) or the empty tables is reported, not held;
* 4. holds the statements Postgres refuses as SQL or over a value's type
* (SQLSTATE class 42, 0A or 22), per method, to
* scripts/pg-probe-baseline.json, which may only shrink
* (pg-probe/baseline.ts); a constraint over a sample value (class 23) or
* the empty tables is reported, not held;
* 5. holds the methods whose SQL it did not see (it could not call them,
* they sent no SQL, or they timed out) to the same file's `uncovered`
* list, which may only shrink too, so a new method the probe cannot
+13 -4
View File
@@ -4,7 +4,8 @@
* The probe has no fixtures: it reads every repository class with the
* TypeScript checker and builds one sample argument list per public method
* from the declared parameter types (a number is 1, a string is "probe" or a
* date-shaped string when the name says so, an object gets every property
* date-shaped string when the name says so, a boolean false unless it is the
* `present` flag of a partial update's pair, an object gets every property
* it can sample, an array one element). The values only have to get the
* method to send its SQL; what the probe measures is whether Postgres
* accepts that SQL against an empty schema. A sample the column cannot take
@@ -24,6 +25,12 @@ export type MethodPlan =
| { className: string; file: string; method: string; unprobeable: string };
const MAX_DEPTH = 4;
/**
* The label of the flag in the repositories' `[present: boolean, value]`
* pairs (`presenceSet`): a patch whose pairs are all absent sends no SQL, so
* this one boolean is sampled true. Every other boolean stays false.
*/
const PRESENCE_LABEL = 'present';
const SAMPLE_DATE = '2026-01-01';
const SAMPLE_TIMESTAMP = '2026-01-01 00:00:00';
@@ -68,14 +75,14 @@ export function sampleFor(checker: ts.TypeChecker, type: ts.Type, name: string,
if (type.isUnion()) {
const members = type.types.filter((member) => !isNullish(member));
if (members.length === 0) return OK(null);
if (members.every((member) => member.flags & ts.TypeFlags.BooleanLiteral)) return OK(false);
if (members.every((member) => member.flags & ts.TypeFlags.BooleanLiteral)) return OK(name === PRESENCE_LABEL);
const numeric = members.find((member) => member.flags & ts.TypeFlags.NumberLike);
return sampleFor(checker, numeric ?? members[0]!, name, depth + 1);
}
if (type.isStringLiteral()) return OK(type.value);
if (type.isNumberLiteral()) return OK(type.value);
if (flags & ts.TypeFlags.BooleanLiteral) return OK(checker.typeToString(type) === 'true');
if (flags & ts.TypeFlags.BooleanLike) return OK(false);
if (flags & ts.TypeFlags.BooleanLike) return OK(name === PRESENCE_LABEL);
if (flags & ts.TypeFlags.BigIntLike) return OK(BigInt(1));
if (flags & ts.TypeFlags.NumberLike) return OK(1);
if (flags & (ts.TypeFlags.StringLike | ts.TypeFlags.TemplateLiteral)) return OK(sampleString(name));
@@ -88,8 +95,10 @@ export function sampleFor(checker: ts.TypeChecker, type: ts.Type, name: string,
function sampleObjectType(checker: ts.TypeChecker, type: ts.ObjectType, name: string, depth: number): Sample {
if (checker.isTupleType(type)) {
const values: unknown[] = [];
const labels = ((type as ts.TypeReference).target as ts.TupleType).labeledElementDeclarations;
for (const [i, element] of checker.getTypeArguments(type as ts.TypeReference).entries()) {
const sample = sampleFor(checker, element, `${name}[${i}]`, depth + 1);
const label = labels?.[i]?.name;
const sample = sampleFor(checker, element, label && ts.isIdentifier(label) ? label.text : `${name}[${i}]`, depth + 1);
if (!sample.ok) return sample;
values.push(sample.value);
}
+11 -7
View File
@@ -11,9 +11,9 @@
* `--update` lowers entries and drops the ones that reach zero or whose
* method is gone; it never raises or adds one.
*
* Only statements Postgres refuses as SQL count here (SQLSTATE classes 42
* and 0A, see `countsTowardRatchet`). A value the sample arguments got wrong
* (class 22, a sample string in a date column) says nothing about the
* Statements Postgres refuses as SQL or over a value's type count here
* (SQLSTATE classes 42, 0A and 22, see `countsTowardRatchet`). A constraint
* the sample values break (class 23) or a timeout says nothing about the
* dialect and is reported without being held.
*
* `uncovered` lists the methods whose SQL the probe did not see: the ones it
@@ -56,12 +56,16 @@ export interface BaselineVerdict {
/**
* Whether a refused statement counts toward the ratchet: SQLSTATE class 42
* (syntax error, unknown function, operator or column, datatype mismatch)
* and class 0A (feature not supported) are what a SQLite-only spelling gets.
* Anything else (a data exception such as 22P02 or 22007, a constraint, a
* timeout) comes from the sample values or the empty tables, not the SQL.
* and class 0A (feature not supported) are what a SQLite-only spelling gets,
* and class 22 (a data exception such as 22P02 or 22007) is a value of the
* wrong type reaching a column, which SQLite compares where Postgres refuses
* (a query-string id bound to an integer column). The sampler sends what
* each parameter declares, so a class 22 refusal is the code's, not the
* sample's. A constraint (class 23) or a timeout comes from the sample values
* or the empty tables, not the SQL.
*/
export function countsTowardRatchet(code: string): boolean {
return /^(42|0A)/.test(code);
return /^(42|0A|22)/.test(code);
}
const METHOD_KEY = /^[A-Za-z_$][\w$]*\.[A-Za-z_$][\w$]*$/;
+8 -6
View File
@@ -22,7 +22,7 @@ export interface FailedStatement {
code: string;
message: string;
sql: string;
/** A dialect refusal (SQLSTATE class 42 or 0A) the ratchet holds; anything else is reported only. */
/** A refusal the ratchet holds (SQLSTATE class 42, 0A or 22); anything else is reported only. */
counted: boolean;
}
@@ -38,8 +38,9 @@ export interface ProbeSummary {
/** Every distinct statement Postgres refused, counted toward the ratchet or not. */
failedStatements: FailedStatement[];
/**
* `Class.method` → distinct statements Postgres refused as SQL (SQLSTATE
* class 42 or 0A); every called method appears, zeros included.
* `Class.method` → distinct statements Postgres refused as SQL or over a
* value's type (SQLSTATE class 42, 0A or 22); every called method appears,
* zeros included.
*/
failingByMethod: Map<string, number>;
/**
@@ -192,7 +193,8 @@ export function formatConsole(input: ReportInput): string {
const refusals = splitRefusals(summary);
lines.push(
`Statements: ${summary.statements} distinct, ${summary.failedStatements.length} refused by Postgres ` +
`(${refusals.counted.length} as SQL, held to the baseline; ${refusals.reported.length} over a sample value or the empty tables, reported only).`,
`(${refusals.counted.length} as SQL or over a value's type, held to the baseline; ` +
`${refusals.reported.length} over a constraint, a timeout or the empty tables, reported only).`,
);
for (const [code, count] of [...summary.failuresByCode.entries()].sort((a, b) => b[1] - a[1])) {
lines.push(` ${code}: ${count}${countsTowardRatchet(code) ? '' : ' (not held)'}`);
@@ -254,8 +256,8 @@ export function formatMarkdown(input: ReportInput): string {
);
out.push(`| Methods whose SQL the probe did not see | ${summary.uncovered.size} |`);
out.push(`| Distinct statements | ${summary.statements} |`);
out.push(`| Refused by Postgres as SQL (held) | ${refusals.counted.length} |`);
out.push(`| Refused over a sample value or the empty tables (reported only) | ${refusals.reported.length} |`);
out.push(`| Refused by Postgres as SQL or over a value's type (held) | ${refusals.counted.length} |`);
out.push(`| Refused over a constraint, a timeout or the empty tables (reported only) | ${refusals.reported.length} |`);
out.push('');
if (probedNothing(summary))
out.push('No repository method was called, so there is nothing to hold to the baseline.', '');
+2 -2
View File
@@ -2,7 +2,7 @@
"scripts/db-call-graph.mjs": 1036,
"scripts/generate-entities.ts": 2259,
"src/db/repositories/Places.repository.ts": 1437,
"src/db/repositories/Reservations.repository.ts": 1604,
"src/db/repositories/Reservations.repository.ts": 1563,
"src/db/repositories/Trips.repository.ts": 1121,
"src/db/repositories/Users.repository.ts": 1445,
"src/nest/atlas/atlas.service.ts": 1177,
@@ -12,7 +12,7 @@
"src/nest/doc-sync/doc-sync.service.ts": 1060,
"src/nest/doc-sync/providers/synology-drive.client.ts": 1160,
"src/nest/journey/journey-domain.service.ts": 1659,
"src/nest/places/places.service.ts": 1549,
"src/nest/places/places.service.ts": 1548,
"src/nest/plugins/plugin-runtime.service.ts": 1460,
"src/nest/plugins/runtime/plugin-sdk.ts": 1093,
"src/nest/reservations/reservations.service.ts": 1237,
@@ -143,7 +143,6 @@
"tests/unit/nest/sessions/sessions.service.test.ts": 1,
"tests/unit/nest/settings.service.test.ts": 1,
"tests/unit/nest/share.service.test.ts": 3,
"tests/unit/nest/sqlite-database-backup.test.ts": 2,
"tests/unit/nest/storage/storage-admin.service.test.ts": 5,
"tests/unit/nest/storage/storage-jobs.service.test.ts": 15,
"tests/unit/nest/storage/storage-registry.service.test.ts": 3,