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
@@ -34,6 +34,7 @@ import {
dateOf,
dayDistance,
foundAgainState,
ilike,
lower,
lowerParam,
lowerTrim,
@@ -175,6 +176,9 @@ describe('sql-functions (postgres)', () => {
'CAST(trunc(CAST(vr.accommodation_id AS numeric)) AS integer)',
);
expect(collateNoCase(pg, 'name').sql).toBe('name COLLATE "nocase"');
expect(ilike(pg, ['p.name', 'p.address'])).toBe("(p.name ILIKE ? ESCAPE '\\' OR p.address ILIKE ? ESCAPE '\\')");
expect(ilike(new SqlitePlatform(), ['p.name'])).toBe("(p.name LIKE ? ESCAPE '\\')");
expect(() => ilike(pg, ['p.name; drop'])).toThrow(/not a column reference/);
const whileSame = coalesceOverrideWhileSame(pg, null, 'u.tls', 'u.url', 'https://a');
expect(whileSame.sql).toBe(
'CASE WHEN u.url IS NOT DISTINCT FROM ? THEN COALESCE(?, u.tls) ELSE COALESCE(?, 0) END',
+13 -1
View File
@@ -4,11 +4,12 @@ import { TrekPostgreSqlDriver } from '../../../../src/db/engine/postgres/postgre
import { sslOptionFor } from '../../../../src/db/engine/postgres/postgres-engine';
import { redactUrl } from '../../../../src/db/engine/redact-url';
import { SharedSqliteDriver } from '../../../../src/db/orm-driver';
import type { EntityManager } from '@mikro-orm/core';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
const PG_URL = 'postgresql://trek:s3cret@db.example:5432/trek';
@@ -113,4 +114,15 @@ describe('buildOrmConfig', () => {
expect(() => engine.close()).not.toThrow();
await expect(engine.reopen()).rejects.toThrow(/not available in this release/);
});
it('ENGINE-033: Postgres serializes write transactions with one advisory lock taken in the transaction, SQLite needs none', async () => {
const execute = vi.fn(async () => []);
const trx = { trx: true };
const em = { getConnection: () => ({ execute }), getTransactionContext: () => trx } as unknown as EntityManager;
await resolveEngine(deriveDb({ TREK_DB_URL: PG_URL })).serializeWrites(em);
expect(execute).toHaveBeenCalledWith('select pg_advisory_xact_lock(?)', [0x5452454b], 'run', trx);
execute.mockClear();
await resolveEngine().serializeWrites(em);
expect(execute).not.toHaveBeenCalled();
});
});