feat(db): keep SQLite's write and search semantics on Postgres

- UnitOfWork transactions take one advisory lock for the database on
  Postgres (DatabaseEngine.serializeWrites, pg_advisory_xact_lock), so a
  MAX(...) + 1 read inside one cannot race another, as with SQLite's single
  writer. SQLite needs nothing.
- ilike(platform, refs) builds the case-insensitive search condition: LIKE
  on SQLite, ILIKE on Postgres, where LIKE is exact. The place searches use it.
- Plugin data erasure runs each statement in its own savepoint: on Postgres a
  failed statement would abort the user's whole erasure transaction.
This commit is contained in:
Isaias Tavares
2026-11-02 14:58:29 -03:00
parent a77705984e
commit 515caffa79
13 changed files with 98 additions and 15 deletions
+7 -1
View File
@@ -1,5 +1,5 @@
import type { AnyOrm } from '../orm';
import type { Options } from '@mikro-orm/core';
import type { EntityManager, Options } from '@mikro-orm/core';
/** The databases TREK can be pointed at. MySQL/MariaDB are out of scope (server/CLAUDE.md). */
type EngineId = 'sqlite' | 'postgres';
@@ -37,4 +37,10 @@ export interface DatabaseEngine {
* archive is the copy).
*/
schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void>;
/**
* Runs first in every `UnitOfWork` transaction, on its EntityManager: makes
* the transactions that write run one at a time, as SQLite's single writer
* does, so a `MAX(...) + 1` read inside one cannot race another.
*/
serializeWrites(em: EntityManager): Promise<void>;
}
@@ -3,6 +3,7 @@ import type { DatabaseEngine } from '../engine';
import { redactUrl } from '../redact-url';
import { TrekPostgreSqlDriver } from './postgres-driver';
import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap';
import type { EntityManager } from '@mikro-orm/core';
import { readFileSync } from 'node:fs';
@@ -53,6 +54,9 @@ export function sslOptionFor(url: string): PgSslOption | undefined {
}
}
/** The advisory lock key every write transaction takes: "TREK" in ASCII. */
const TREK_WRITE_LOCK = 0x5452454b;
/**
* The Postgres engine: MikroORM's own `pg` driver on `TREK_DB_URL`, with a
* pool of at most `TREK_DB_POOL_MAX` connections that keeps none open while
@@ -80,6 +84,17 @@ export class PostgresEngine implements DatabaseEngine {
return Promise.reject(new Error('Restoring a backup into a Postgres database is not available in this release.'));
}
/**
* `pg_advisory_xact_lock` on one key for the whole database: held until the
* transaction commits or rolls back, taken again by a savepoint in the same
* session without waiting, and shared by every process on the database.
*/
async serializeWrites(em: EntityManager): Promise<void> {
await em
.getConnection()
.execute('select pg_advisory_xact_lock(?)', [TREK_WRITE_LOCK], 'run', em.getTransactionContext());
}
async schemaBootstrap(orm: AnyOrm): Promise<void> {
await bootstrapPostgresSchema(orm, this.bootstrapDeps());
await finishSchemaBootstrap(orm);
@@ -3,6 +3,7 @@ import { resolveDbPath } from '../../db-path';
import { runSchemaBootstrap, type AnyOrm } from '../../orm';
import { SharedSqliteDriver } from '../../orm-driver';
import type { DatabaseEngine } from '../engine';
import type { EntityManager } from '@mikro-orm/core';
/**
* The SQLite file engine, TREK's default: one better-sqlite3 handle that
@@ -40,4 +41,9 @@ export class SqliteEngine implements DatabaseEngine {
schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void> {
return runSchemaBootstrap(orm, options);
}
serializeWrites(_em: EntityManager): Promise<void> {
// One connection, one writer: SQLite serializes the transactions already.
return Promise.resolve();
}
}