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
@@ -0,0 +1,49 @@
import type { EntityManager } from '@mikro-orm/core';
/**
* The SQLite engine's whole-file maintenance: the two statements a backup,
* a restore and the demo reset need that are about the database file rather
* than any table (`PRAGMA wal_checkpoint`, `VACUUM INTO`). Neither has a
* Postgres spelling, so they live under the engine that owns the file instead
* of in `MaintenanceRepository`, whose remaining statements run on every
* engine (and which the Postgres probe calls method by method).
*
* Provided through Nest DI by `MaintenanceModule` (`nest/database/`), a
* factory over the context-resolving global `EntityManager`, and injected by
* the SQLite backup port (`SqliteDatabaseBackup`). Free of Nest decorators
* like every other file under `db/`, so a test can build one over its own
* `EntityManager`.
*/
export class SqliteMaintenance {
constructor(private readonly em: EntityManager) {}
/**
* MikroORM's own validating `getContext()`: throws outside a request
* context, the fail-closed check `TrekRepository` applies to every
* repository, since a raw `connection.execute()` has no such guard.
*/
private validateRequestContext(): void {
this.em.getContext();
}
/**
* `PRAGMA wal_checkpoint(TRUNCATE)`: flushes the WAL into the main file
* before a snapshot or a swap. Reached through the backup port's
* `checkpoint()`; every caller treats it as best effort.
*/
async walCheckpoint(): Promise<void> {
this.validateRequestContext();
await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
}
/**
* `` VACUUM INTO '<path>' ``: a consistent point-in-time copy of the file
* under concurrent writers, reached through the backup port's
* `snapshot()`. Single quotes in `path` are escaped by doubling.
*/
async vacuumInto(path: string): Promise<void> {
this.validateRequestContext();
const escaped = path.replaceAll("'", "''");
await this.em.getConnection().execute(`VACUUM INTO '${escaped}'`);
}
}