diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 546a08743..33f54c7d8 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -32,6 +32,8 @@ permissions:
# Server: S3 Contract (MinIO)
# the storage driver against MinIO
# Server: Postgres Probe the repository SQL against Postgres
+# Server: Postgres Tests the suites that boot the app, plus PARITY-PG and
+# the write lock, on Postgres (vitest.pg.config.ts)
# Client: Types & Checks typecheck and the strict ratchet; then the page
# pattern and the source ratchets (RTL, size,
# Prettier, layers, offline path, duplication,
@@ -44,7 +46,7 @@ permissions:
# Client: Bundle the production build, one chunk per map engine
# Client: End-to-End the app in Chromium and WebKit
# Plugin SDK: Types & Coverage
-# Repo: Docker Image builds the image and boots it until ready
+# Repo: Docker Image builds the image and boots it until ready, on SQLite and on Postgres
# Repo: Helm Chart & Compose
# Repo: Sonar Scan SonarCloud analysis, not required
# ci-ok the one required check (below)
@@ -65,9 +67,9 @@ permissions:
# test jobs feeds the scan.
# - Job count: GitHub runs at most 20 jobs of the organisation at once and
# does not say in which order it starts queued ones. A code change starts
-# the 14 shards and about a dozen other jobs; seven of those (parity, the
-# SDK, the bundle, S3, Postgres, shared coverage, the chart) are done in
-# a minute or two, so what queues at the start waits about that long. The
+# the 14 shards and about a dozen other jobs; eight of those (parity, the
+# SDK, the bundle, S3, the Postgres probe and tests, shared coverage, the
+# chart) are done in a few minutes, so what queues at the start waits about that long. The
# jobs that run for minutes (shards, gates, e2e, the image) must not
# outnumber the 20 slots, or one of them waits for another to finish and
# the run grows by a whole job: a layout with six gate jobs did exactly
@@ -282,6 +284,13 @@ jobs:
# migration that changes a column regenerates it in the same change.
run: cd server && npm run check:db-types
+ - name: Postgres baseline matches the entities
+ # server/assets/postgres/ is the schema a fresh Postgres database starts
+ # from, generated from the entities by gen:pg-baseline. Until a migration
+ # lands after its cut it must match them exactly; after, it is frozen
+ # and only its list of migrations is checked.
+ run: cd server && npm run check:pg-baseline
+
server-lint-checks:
name: 'Server: Lint & Checks'
needs: changes
@@ -367,6 +376,12 @@ jobs:
# so the code stays portable to a second engine.
run: cd server && npm run lint:dialect
+ - name: Engine checks may only shrink
+ # isSqlite(/isPostgres( and platform instanceof outside src/db/dialect/
+ # and src/db/engine/ are held to scripts/engine-dispatch-baseline.json,
+ # so what differs between engines stays in those two places.
+ run: cd server && npm run lint:engine-dispatch
+
- name: Writes outside a transaction
# A method that writes twice does it inside one uow.transactional.
# Today's exceptions sit in scripts/tx-writes-baseline.json and may
@@ -805,6 +820,50 @@ jobs:
if-no-files-found: ignore
retention-days: 14
+ # The Postgres lane: every suite that boots the app through buildApp() on the
+ # real database module, each file on its own copy of one migrated template,
+ # plus the Postgres-only suites (PARITY-PG, the write lock). The lane fails,
+ # never skips, without TREK_TEST_DB_URL.
+ server-postgres-tests:
+ name: 'Server: Postgres Tests'
+ runs-on: ubuntu-24.04
+ timeout-minutes: 15
+ needs: changes
+ if: needs.changes.outputs.code == 'true'
+ services:
+ postgres:
+ image: postgres:17
+ env:
+ POSTGRES_PASSWORD: lane
+ ports:
+ - 5432:5432
+ options: >-
+ --health-cmd "pg_isready -U postgres"
+ --health-interval 5s
+ --health-timeout 5s
+ --health-retries 20
+ steps:
+ - uses: actions/checkout@v6
+ with:
+ persist-credentials: false
+
+ - uses: actions/setup-node@v6
+ with:
+ node-version-file: .nvmrc
+ cache: npm
+ cache-dependency-path: package-lock.json
+
+ - name: Install dependencies
+ run: npm ci
+
+ - name: Build shared
+ run: npm run build --workspace=shared
+
+ - name: Run the Postgres lane
+ env:
+ TREK_TEST_DB_URL: postgres://postgres:lane@127.0.0.1:5432/postgres
+ run: npm run test:pg --workspace=server
+
# The server suite in seven slices. vitest.shard.config.ts is vitest.config.ts
# without the thresholds and the coverage reports: a slice's blob carries
# its coverage to server-coverage, which checks the whole. fail-fast is off,
@@ -1208,10 +1267,72 @@ jobs:
docker logs trek-ci 2>&1 | grep 'SIGTERM received' >/dev/null \
|| { echo "::error::the server never saw SIGTERM"; exit 1; }
+ # The same image on a Postgres database (TREK_DB_URL): an empty database
+ # gets the baseline schema, the admin is seeded, and the server answers
+ # ready. The database runs beside it on a private network, never exposed.
+ - name: Start the container on Postgres
+ run: |
+ set -euo pipefail
+ docker network create trek-ci-pg
+ docker run -d --name trek-ci-db --network trek-ci-pg \
+ -e POSTGRES_PASSWORD=ci -e POSTGRES_DB=trek postgres:17
+ for _ in $(seq 1 30); do
+ docker exec trek-ci-db pg_isready -U postgres -d trek >/dev/null 2>&1 && break
+ sleep 2
+ done
+ docker exec trek-ci-db pg_isready -U postgres -d trek \
+ || { echo "::error::Postgres did not accept connections within 60 seconds."; exit 1; }
+ mkdir -p "$RUNNER_TEMP/trek-pg/data" "$RUNNER_TEMP/trek-pg/uploads"
+ docker run -d --name trek-ci-on-pg --network trek-ci-pg -p 127.0.0.1:3001:3000 \
+ --read-only --tmpfs /tmp:noexec,nosuid,size=128m \
+ --security-opt no-new-privileges:true \
+ --cap-drop ALL --cap-add CHOWN --cap-add SETUID --cap-add SETGID --cap-add KILL \
+ -v "$RUNNER_TEMP/trek-pg/data:/app/data" \
+ -v "$RUNNER_TEMP/trek-pg/uploads:/app/uploads" \
+ -e ENCRYPTION_KEY="$(openssl rand -hex 32)" \
+ -e TREK_DB_URL=postgres://postgres:ci@trek-ci-db:5432/trek \
+ trek:ci
+
+ # Twice: the first start creates the schema in the empty database, the
+ # restart finds it migrated and must start without touching it.
+ - name: Wait until the server on Postgres is ready, then again after a restart
+ run: |
+ wait_ready() {
+ for _ in $(seq 1 90); do
+ if [ "$(docker inspect -f '{{.State.Running}}' trek-ci-on-pg)" != "true" ]; then
+ echo "::error::The container on Postgres exited before it became ready."
+ exit 1
+ fi
+ if curl -sf http://127.0.0.1:3001/api/health/ready; then
+ echo
+ return 0
+ fi
+ sleep 2
+ done
+ echo "::error::/api/health/ready on Postgres did not answer within 180 seconds."
+ exit 1
+ }
+ wait_ready
+ docker restart -t 30 trek-ci-on-pg >/dev/null
+ wait_ready
+
+ - name: Stop the container on Postgres gracefully
+ run: |
+ set -euo pipefail
+ docker stop -t 30 trek-ci-on-pg >/dev/null
+ code="$(docker inspect -f '{{.State.ExitCode}}' trek-ci-on-pg)"
+ [ "$code" = "0" ] || { echo "::error::docker stop ended with exit code $code, not a graceful shutdown"; exit 1; }
+
- name: Container log
if: always()
run: docker logs trek-ci 2>&1 || true
+ - name: Container log on Postgres
+ if: always()
+ run: |
+ docker logs trek-ci-on-pg 2>&1 || true
+ docker logs trek-ci-db 2>&1 | tail -50 || true
+
# The Helm chart and the compose files are what self-hosters deploy from,
# and before this job only a release ever touched them. The chart is linted,
# rendered with the defaults and with each secret and persistence mode, and
@@ -1332,6 +1453,7 @@ jobs:
- client-e2e
- s3-contract
- postgres-probe
+ - server-postgres-tests
- server-tests
- server-coverage
- client-tests
diff --git a/CLAUDE.md b/CLAUDE.md
index 8b6cf1a0a..41cb44163 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -31,6 +31,7 @@ Tests:
npm run test # all workspaces
npm run test --workspace=server # vitest run (server); also test:unit / test:integration / test:ws
npm run test:e2e # server e2e (boots Nest against a temp SQLite)
+npm run test:pg --workspace=server # the Postgres lane (vitest.pg.config.ts; needs TREK_TEST_DB_URL, e.g. postgres://postgres:trek@127.0.0.1:55432/postgres)
npm run test:cov # coverage (lcov) for all four packages incl. plugin-sdk
cd client && npm run test # client vitest run
cd client && npm run e2e # Playwright (CI runs the public and app projects; the screenshot and help-media ones are local)
@@ -51,7 +52,9 @@ cd client && npm run lint:skips # no .only; skipped tests may
cd client && npm run lint:i18n-keys # every key the client names exists in en; unused en keys may only shrink (CI gate)
cd server && npm run lint:boundaries # no new import cycles or cross-domain reach-ins (CI gate)
cd server && npm run lint:dialect # SQLite-only SQL spellings may only shrink (CI gate; rules in server/CLAUDE.md)
+cd server && npm run lint:engine-dispatch # engine checks outside src/db/dialect/ and src/db/engine/ may only shrink (CI gate)
cd server && npm run probe:pg # dialect helpers + the repository statements it reaches against Postgres (CI job; needs TREK_PG_PROBE_URL)
+cd server && npm run check:pg-baseline # the Postgres baseline in server/assets/postgres/ matches the entities and the migrations (CI gate)
cd server && npm run lint:tx # no new method writing twice outside one transaction (CI gate)
cd server && npm run lint:test-sql # raw SQL fixtures in tests/ may only shrink; seed through tests/helpers/factories (CI gate)
cd server && npm run lint:test-mocks # vi.mock of config, realtime and db modules in tests/ may only shrink (CI gate)
@@ -134,7 +137,7 @@ These principles come out of a full-repo audit and shape all new code:
## Quality gates in CI
-`.github/workflows/test.yml` runs, per package: typecheck (server also `typecheck:tests`, client also the strict-mode ratchet `lint:strict`), lint (client and server through `lint:warnings`, which also holds the warning count per rule), `lint:pages`, `lint:rtl`, `lint:size` (client and server), the server's `lint:strict`, `lint:boundaries`, `lint:tx`, `lint:dialect`, `lint:test-sql`, `lint:test-mocks`, `lint:test-new-service`, `lint:query-api`, `lint:mcp-zod`, `lint:service-http` and `lint:response-contracts`, the client's and server's `lint:format`, `lint:layers`, `lint:offline`, `lint:dup`, `lint:pairs`, `lint:skips`, `lint:i18n-keys` and `theme:lint`, shared's `contracts:open`, the repo-wide `lint:knip` (in server-lint-checks), `check:plugin-facts`, `i18n:parity:strict` (with the untranslated-strings ratchet), the S3 contract suite, the Postgres probe (`postgres-probe`: every dialect helper on SQLite and Postgres, and the statements every repository method reaches with sample arguments against a Postgres service; the SQL errors per method and the list of methods it cannot measure are held to a baseline that only shrinks and fails while it is still unmeasured; see `server/CLAUDE.md`), the browser end-to-end flows (Chromium and WebKit), a client build with `check:gl-split`, coverage for all four packages, then a SonarCloud scan. It also builds the Docker image and boots it until `/api/health/ready` answers (`docker-image`), and lints and renders the Helm chart against the Kubernetes schemas and runs `docker compose config` on every compose file (`deploy-manifests`). `lint-prettier.yml` additionally runs `lint` + `lint:format` on `shared/`, both check-only. `plugin-sdk` has no lint script and is outside the eslint gates. **Not in CI**: the screenshot and help-media Playwright projects; run them locally. There are no git hooks; nothing runs before a commit except you. Other workflows build Docker images, scan them, and publish `trek-plugin-sdk` on `plugin-sdk-v*` tags.
+`.github/workflows/test.yml` runs, per package: typecheck (server also `typecheck:tests`, client also the strict-mode ratchet `lint:strict`), lint (client and server through `lint:warnings`, which also holds the warning count per rule), `lint:pages`, `lint:rtl`, `lint:size` (client and server), the server's `lint:strict`, `lint:boundaries`, `lint:tx`, `lint:dialect`, `lint:engine-dispatch`, `lint:test-sql`, `lint:test-mocks`, `lint:test-new-service`, `lint:query-api`, `lint:mcp-zod`, `lint:service-http` and `lint:response-contracts`, the client's and server's `lint:format`, `lint:layers`, `lint:offline`, `lint:dup`, `lint:pairs`, `lint:skips`, `lint:i18n-keys` and `theme:lint`, shared's `contracts:open`, the repo-wide `lint:knip` (in server-lint-checks), `check:plugin-facts`, `i18n:parity:strict` (with the untranslated-strings ratchet), the S3 contract suite, the Postgres probe (`postgres-probe`: every dialect helper on SQLite and Postgres, and the statements every repository method reaches with sample arguments against a Postgres service; the SQL errors per method and the list of methods it cannot measure are held to a baseline that only shrinks and fails while it is still unmeasured; see `server/CLAUDE.md`), the Postgres lane (`server-postgres-tests`: the suites that boot the app, PARITY-PG and the write lock on a Postgres service), the browser end-to-end flows (Chromium and WebKit), a client build with `check:gl-split`, coverage for all four packages, then a SonarCloud scan. It also builds the Docker image and boots it until `/api/health/ready` answers (`docker-image`), and lints and renders the Helm chart against the Kubernetes schemas and runs `docker compose config` on every compose file (`deploy-manifests`). `lint-prettier.yml` additionally runs `lint` + `lint:format` on `shared/`, both check-only. `plugin-sdk` has no lint script and is outside the eslint gates. **Not in CI**: the screenshot and help-media Playwright projects; run them locally. There are no git hooks; nothing runs before a commit except you. Other workflows build Docker images, scan them, and publish `trek-plugin-sdk` on `plugin-sdk-v*` tags.
- **Job names read "package: what"** (`Server: Types`, `Client: Tests 3/7`), and the list at the top of `test.yml` says what each job holds. A new job follows the scheme and joins that list, and the job-count note there says when a long job needs a slot freed first.
- **Tests run beside the gates, in shards.** The client and server suites run in slices on parallel runners (`client-tests`, `server-tests`, through each package's `vitest.shard.config.ts`), and `client-coverage`/`server-coverage` merge their blob reports and check the thresholds against the whole. A red slice is usually a failing test, a red merge job usually a threshold.
diff --git a/Dockerfile b/Dockerfile
index f1c462e28..e6ab8b731 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -72,10 +72,15 @@ COPY server/package.json ./server/
# package, and a telemetry ping.
# The upgrade takes the Debian security fixes the base image was built before
# (zlib, 11/2026, failed the image scan until node:24-trixie-slim caught up).
+#
+# postgresql-client-17 (Debian 13's own) brings pg_dump and pg_restore to
+# /usr/lib/postgresql/17/bin, which back up an install on Postgres
+# (TREK_DB_URL). pg_dump refuses a server newer than itself, so it stays at the
+# newest major TREK supports.
RUN apt-get update && \
apt-get upgrade -y --no-install-recommends && \
apt-get install -y --no-install-recommends tzdata dumb-init wget ca-certificates \
- libkitinerary-bin libsqlite3-0 && \
+ libkitinerary-bin libsqlite3-0 postgresql-client-17 && \
npm ci --workspace=server --omit=dev --ignore-scripts && \
{ ln -sf "$(find /usr/lib -name kitinerary-extractor -type f | head -1)" /usr/local/bin/kitinerary-extractor || true; } && \
rm -rf /var/lib/apt/lists/* /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npx && \
@@ -94,8 +99,9 @@ ENV KITINERARY_EXTRACTOR_PATH=/usr/local/bin/kitinerary-extractor
COPY --chown=node:node --from=server-builder /app/server/dist ./server/dist
# Runtime data assets read from server/assets at runtime: airports.json (flight
-# transport search) and atlas/*.geojson.gz + atlas/admin1/*.geojson.gz (Atlas
-# country/region map). The build
+# transport search), atlas/*.geojson.gz + atlas/admin1/*.geojson.gz (Atlas
+# country/region map) and postgres/baseline.* (the schema a fresh Postgres
+# database starts from). The build
# only emits dist, so these must be copied explicitly or the features silently
# degrade to empty in the image.
COPY --chown=node:node --from=server-builder /app/server/assets ./server/assets
diff --git a/README.md b/README.md
index 22b711357..7d90c3a1d 100644
--- a/README.md
+++ b/README.md
@@ -321,6 +321,10 @@ docker compose up -d
See [Install with Docker Compose](https://github.com/liketrek/TREK/wiki/Install-Docker-Compose)
for the full walkthrough.
+TREK stores its data in SQLite by default. Set `TREK_DB_URL` to run it on a PostgreSQL 14 to 17 database instead. The
+[`docker-compose.postgres.yml`](docker-compose.postgres.yml) example starts one beside TREK, and the
+[Postgres guide](https://github.com/liketrek/TREK/wiki/Database-Postgres) covers the requirements and the backups.
+
Helm (Kubernetes)
diff --git a/charts/README.md b/charts/README.md
index 62060538b..48726dcb5 100644
--- a/charts/README.md
+++ b/charts/README.md
@@ -46,6 +46,7 @@ See `values.yaml` for more options.
- Both PVCs are `ReadWriteOnce`. Set `persistence.data.accessModes` and/or `persistence.uploads.accessModes` to override, for example `[ReadWriteMany]` so a backup pod can mount the volume. On the data volume such a pod may only read: SQLite corrupts `travel.db` when a second process writes it over a network filesystem.
- To use your own PVCs, set `persistence.data.existingClaim` and/or `persistence.uploads.existingClaim`. The other values for that volume (size, storageClassName, accessModes, annotations) are then ignored.
- With `persistence.enabled: false`, the data and uploads volumes use an `emptyDir` — storage is ephemeral and lost on pod restart. Intended for testing only.
+- `database.url` runs TREK on a PostgreSQL database instead of the SQLite file. The chart writes it into its Secret as `TREK_DB_URL`; `database.existingSecret` and `database.existingSecretKey` read it from a Secret you manage. Leave both empty for SQLite. Keep `replicas: 1` either way. See the wiki page `Database-Postgres`.
- `JWT_SECRET` is managed entirely by the server — auto-generated into the data PVC on first start and rotatable via the admin panel (Settings → Danger Zone). No Helm configuration needed.
- `ENCRYPTION_KEY` encrypts stored secrets (API keys, MFA, SMTP, OIDC) at rest. Recommended: set via `secretEnv.ENCRYPTION_KEY` or `existingSecret`. If left empty, the server falls back automatically: existing installs use `data/.jwt_secret` (no action needed on upgrade); fresh installs auto-generate a key persisted to the data PVC.
- Liveness and readiness probes are configurable under `probes`. Overrides are merged over the defaults, so `--set probes.liveness.initialDelaySeconds=60` keeps the `httpGet` handler. When switching to `exec` or `tcpSocket`, clear `httpGet` in the same override. Set `probes.liveness: null` to remove a probe.
diff --git a/charts/trek/templates/deployment.yaml b/charts/trek/templates/deployment.yaml
index 7e426593f..fe844e908 100644
--- a/charts/trek/templates/deployment.yaml
+++ b/charts/trek/templates/deployment.yaml
@@ -1,3 +1,6 @@
+{{- if and .Values.database.url (not .Values.database.existingSecret) .Values.existingSecret }}
+{{- fail "database.url is written into the chart's own Secret, which existingSecret replaces. Put TREK_DB_URL in your Secret and set database.existingSecret instead." }}
+{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
@@ -5,11 +8,13 @@ metadata:
labels:
app: {{ include "trek.name" . }}
spec:
+ # One replica. Its rate limits, pending codes, permission cache and WebSocket
+ # connections live in the process's memory, so a second pod would not share them. That
+ # holds with database.url (PostgreSQL) too; see updateStrategy in values.yaml.
replicas: 1
- # TREK is a single-writer SQLite app on a ReadWriteOnce PVC, so the default
- # RollingUpdate would start a second pod holding the same volume before the old one
- # exits — a Multi-Attach deadlock, or two processes on one travel.db. Recreate tears
- # the old pod down first. Keep it, see updateStrategy in values.yaml.
+ # The default RollingUpdate would start a second pod on the same ReadWriteOnce PVC
+ # before the old one exits: a Multi-Attach deadlock, or two processes on one travel.db.
+ # Recreate tears the old pod down first. Keep it, see updateStrategy in values.yaml.
strategy:
type: {{ .Values.updateStrategy | default "Recreate" }}
selector:
@@ -61,6 +66,14 @@ spec:
name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.existingSecret }}
key: {{ .Values.existingSecretKey | default "ENCRYPTION_KEY" }}
optional: true
+ {{- if or .Values.database.url .Values.database.existingSecret }}
+ # Required when set: a missing key stops the pod rather than starting it on SQLite.
+ - name: TREK_DB_URL
+ valueFrom:
+ secretKeyRef:
+ name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.database.existingSecret }}
+ key: {{ if .Values.database.existingSecret }}{{ .Values.database.existingSecretKey | default "TREK_DB_URL" }}{{ else }}TREK_DB_URL{{ end }}
+ {{- end }}
- name: ADMIN_EMAIL
valueFrom:
secretKeyRef:
diff --git a/charts/trek/templates/secret.yaml b/charts/trek/templates/secret.yaml
index eacc4da4e..f9c1440e2 100644
--- a/charts/trek/templates/secret.yaml
+++ b/charts/trek/templates/secret.yaml
@@ -8,6 +8,9 @@ metadata:
type: Opaque
data:
{{ .Values.existingSecretKey | default "ENCRYPTION_KEY" }}: {{ .Values.secretEnv.ENCRYPTION_KEY | b64enc | quote }}
+ {{- if .Values.database.url }}
+ TREK_DB_URL: {{ .Values.database.url | b64enc | quote }}
+ {{- end }}
{{- if .Values.secretEnv.ADMIN_EMAIL }}
ADMIN_EMAIL: {{ .Values.secretEnv.ADMIN_EMAIL | b64enc | quote }}
{{- end }}
@@ -65,6 +68,9 @@ stringData:
{{- else }}
{{ .Values.existingSecretKey | default "ENCRYPTION_KEY" }}: {{ randAlphaNum 32 | quote }}
{{- end }}
+ {{- if .Values.database.url }}
+ TREK_DB_URL: {{ .Values.database.url | quote }}
+ {{- end }}
{{- if .Values.secretEnv.ADMIN_EMAIL }}
ADMIN_EMAIL: {{ .Values.secretEnv.ADMIN_EMAIL | quote }}
{{- end }}
diff --git a/charts/trek/values.yaml b/charts/trek/values.yaml
index 648058884..6abe14f01 100644
--- a/charts/trek/values.yaml
+++ b/charts/trek/values.yaml
@@ -4,10 +4,13 @@ image:
# tag: latest
pullPolicy: IfNotPresent
-# Deployment update strategy. Keep Recreate: TREK is a single-writer SQLite app, and
-# during a RollingUpdate the new pod migrates the database while the old one still
-# writes to it. A ReadWriteMany volume does not make that safe; SQLite's WAL needs both
-# processes on one host, which network filesystems do not provide.
+# Deployment update strategy. Keep Recreate. The new pod migrates the database when it
+# starts, and during a RollingUpdate it would do that while the old pod still writes to
+# it. Recreate stops the old pod first. On SQLite two processes on one travel.db are
+# unsafe, and a ReadWriteOnce volume cannot be attached to both pods at once.
+# With database.url the database is PostgreSQL, but the reason for one replica stays: TREK
+# keeps rate limits, pending codes, the permission cache and WebSocket connections in
+# memory, so a second pod would not share them. Postgres does not make more replicas safe.
updateStrategy: Recreate
# Pod-level security context. fsGroup gives the node user (uid/gid 1000) group access
@@ -178,6 +181,19 @@ env:
# address is sent unless you set one here.
+# Database. Empty (the default) keeps TREK on its SQLite file in the data volume. Set url to
+# run TREK on a PostgreSQL database instead (see the wiki page Database-Postgres). The URL
+# carries the password, so it is never a plain env value: with url, the chart writes it into
+# its own Secret as TREK_DB_URL; with existingSecret, TREK reads TREK_DB_URL from that Secret
+# under existingSecretKey and the chart writes nothing. existingSecret wins over url. The
+# variable is required when either is set: a missing Secret key stops the pod instead of
+# starting it on SQLite.
+# url: "postgres://trek:@postgres.example.internal:5432/trek?sslmode=require"
+database:
+ url: ""
+ existingSecret: ""
+ existingSecretKey: TREK_DB_URL
+
# Secret environment variables stored in a Kubernetes Secret.
# JWT_SECRET is managed entirely by the server (auto-generated into the data PVC,
# rotatable via the admin panel) — it is not configured here.
diff --git a/docker-compose.postgres.yml b/docker-compose.postgres.yml
new file mode 100644
index 000000000..dc5aaa17f
--- /dev/null
+++ b/docker-compose.postgres.yml
@@ -0,0 +1,87 @@
+# TREK on a Postgres database instead of the SQLite file. See the wiki page
+# Database-Postgres for what this changes, the requirements and the backups.
+#
+# Set the database password in a .env file beside this file (or in the
+# shell) before the first start:
+# POSTGRES_PASSWORD=
+# The password goes into TREK_DB_URL below, so keep it to URL-safe characters
+# or percent-encode it. Without POSTGRES_PASSWORD the postgres image refuses to
+# initialize its database, and TREK does not start.
+services:
+ db:
+ image: postgres:17
+ container_name: trek-db
+ restart: unless-stopped
+ environment:
+ - POSTGRES_DB=${POSTGRES_DB:-trek}
+ - POSTGRES_USER=${POSTGRES_USER:-trek}
+ - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-}
+ security_opt:
+ - no-new-privileges:true
+ # No ports are published: only the trek service on the compose network reaches the database.
+ volumes:
+ - trek-pgdata:/var/lib/postgresql/data
+ healthcheck:
+ test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
+ interval: 10s
+ timeout: 5s
+ retries: 5
+ start_period: 10s
+ logging:
+ driver: json-file
+ options:
+ max-size: 10m
+ max-file: "3"
+
+ app:
+ image: mauriceboe/trek:latest
+ container_name: trek
+ depends_on:
+ db:
+ condition: service_healthy
+ read_only: true
+ security_opt:
+ - no-new-privileges:true
+ cap_drop:
+ - ALL
+ cap_add:
+ - CHOWN
+ - SETUID
+ - SETGID
+ # dumb-init runs as root and forwards SIGTERM to node, which runs as the
+ # node user; signalling another user's process needs KILL. See docker-compose.yml.
+ - KILL
+ tmpfs:
+ - /tmp:noexec,nosuid,size=128m
+ ports:
+ - "3000:3000"
+ environment:
+ - NODE_ENV=production
+ - PORT=3000
+ - ENCRYPTION_KEY=${ENCRYPTION_KEY:-} # Recommended. Generate with: openssl rand -hex 32.
+ - TZ=${TZ:-UTC}
+ - LOG_LEVEL=${LOG_LEVEL:-info}
+ - ALLOWED_ORIGINS=${ALLOWED_ORIGINS:-}
+ # The database URL. Its host is the service name above; sslmode=disable is
+ # right on the compose network. Use sslmode=verify-full with sslrootcert=
+ # for a database reached over a network you do not control.
+ - TREK_DB_URL=postgres://${POSTGRES_USER:-trek}:${POSTGRES_PASSWORD:-}@db:5432/${POSTGRES_DB:-trek}?sslmode=disable
+ # Scale-out is not supported: keep one trek container (see Database-Postgres).
+ volumes:
+ - ./data:/app/data
+ - ./uploads:/app/uploads
+ restart: unless-stopped
+ logging:
+ driver: json-file
+ options:
+ max-size: 10m
+ max-file: "3"
+ healthcheck:
+ test: ["CMD-SHELL", "wget -qO- http://localhost:$${PORT:-3000}/api/health || exit 1"]
+ interval: 30s
+ timeout: 10s
+ retries: 3
+ start_period: 15s
+
+volumes:
+ trek-pgdata:
diff --git a/package-lock.json b/package-lock.json
index 75e0dd845..0921101ff 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5684,7 +5684,6 @@
"version": "7.2.8",
"resolved": "https://registry.npmjs.org/@mikro-orm/postgresql/-/postgresql-7.2.8.tgz",
"integrity": "sha512-spAGvR8xfXGK0+mwsAsN3Kt5cg2Ge3DgXT3rNOeA7IxaBXpl+NMwXu5cSWes4jHlhWKJ4jzXyTo6xH+we9fbNA==",
- "dev": true,
"license": "MIT",
"dependencies": {
"@mikro-orm/sql": "7.2.8",
@@ -19855,7 +19854,6 @@
"version": "8.23.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.23.1.tgz",
"integrity": "sha512-aL96AHANtWjPLDOLqnhx+ngp9+UK7ETEU8VJrDCGvsSSi/mGLcWYsS6Herg7lmaBJe4uwrfqsa7gTEFaSizDoQ==",
- "dev": true,
"license": "MIT",
"dependencies": {
"pg-connection-string": "^2.14.1",
@@ -19883,7 +19881,6 @@
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.4.1.tgz",
"integrity": "sha512-6PQbsFWZcp9EmJEwy5cGQ2La+AMWpP46lgbb8X+U/XsHIUweYDNCpeuKck5RxL2MdVFi7krbbEi5nX4Zh7JhrQ==",
- "dev": true,
"license": "MIT",
"optional": true
},
@@ -19891,14 +19888,12 @@
"version": "2.14.1",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.14.1.tgz",
"integrity": "sha512-qR3kGNPBLpCNtz0evbKA0Y/MRFXwSSdT+pTJvYp/bXTcReZbvX1kzF0IyTc1QnxqF7AZbOeBhNL8R5mYQZV/MA==",
- "dev": true,
"license": "MIT"
},
"node_modules/pg-cursor": {
"version": "2.22.1",
"resolved": "https://registry.npmjs.org/pg-cursor/-/pg-cursor-2.22.1.tgz",
"integrity": "sha512-X10ldv3wwBONQkOSwgWEbaEITMGLd92V2Zd+xsFPhDt3nhhU3hpAT08SERBChnIFmomczLax6lKXLJrHPPXfxA==",
- "dev": true,
"license": "MIT",
"peerDependencies": {
"pg": "^8"
@@ -19908,7 +19903,6 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
- "dev": true,
"license": "ISC",
"engines": {
"node": ">=4.0.0"
@@ -19918,7 +19912,6 @@
"version": "3.14.0",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz",
"integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==",
- "dev": true,
"license": "MIT",
"peerDependencies": {
"pg": ">=8.0"
@@ -19928,14 +19921,12 @@
"version": "1.16.1",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.16.1.tgz",
"integrity": "sha512-p9VOFMiHB/ZbJATetbg+99PxssTVSQRnyuPSQ67mN1+1KBOjZaZ83ZQzltnxPhJwSsC3nwVjJ10DVJlerbFzLg==",
- "dev": true,
"license": "MIT"
},
"node_modules/pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
- "dev": true,
"license": "MIT",
"dependencies": {
"pg-int8": "1.0.1",
@@ -19952,7 +19943,6 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=4"
@@ -19962,7 +19952,6 @@
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
@@ -19972,7 +19961,6 @@
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
- "dev": true,
"license": "MIT",
"dependencies": {
"xtend": "^4.0.0"
@@ -19985,7 +19973,6 @@
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
- "dev": true,
"license": "MIT",
"dependencies": {
"split2": "^4.1.0"
@@ -20355,7 +20342,6 @@
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-3.0.4.tgz",
"integrity": "sha512-nAUSGfSDGOaOAEGwqsRY27GPOea7CNipJPOA7lPbdEpx5Kg3qzdP0AaWC5MlhTWV9s4hFX39nomVZ+C4tnGOJQ==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=12"
@@ -20365,7 +20351,6 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz",
"integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
@@ -20375,7 +20360,6 @@
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-2.1.0.tgz",
"integrity": "sha512-K7Juri8gtgXVcDfZttFKVmhglp7epKb1K4pgrkLxehjqkrgPhfG6OO8LHLkfaqkbpjNRnra018XwAr1yQFWGcA==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=12"
@@ -20385,7 +20369,6 @@
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-4.1.0.tgz",
"integrity": "sha512-nJLACcOHi2iyRuMJeW6CuIA0Dc0i1YEpRr26XDyHAfxPjYUJOFxwZjmOc5oFFctTTh/OlRAMlnlXEJvr170fJg==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=12"
@@ -25724,7 +25707,6 @@
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=0.4"
@@ -25933,6 +25915,7 @@
"@mikro-orm/core": "^7.2.7",
"@mikro-orm/migrations": "^7.2.7",
"@mikro-orm/nestjs": "^7.1.0",
+ "@mikro-orm/postgresql": "^7.2.7",
"@mikro-orm/seeder": "^7.2.7",
"@mikro-orm/sql": "^7.2.1",
"@mikro-orm/sqlite": "^7.2.7",
@@ -25987,7 +25970,6 @@
"@aws-lite/s3-types": "^0.2.8",
"@eslint/js": "^10.0.1",
"@mikro-orm/entity-generator": "^7.2.7",
- "@mikro-orm/postgresql": "^7.2.7",
"@nestjs/testing": "^11.2.5",
"@swc/core": "^1.16.22",
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
diff --git a/server/.env.example b/server/.env.example
index e0bf2fcbe..b9549bece 100644
--- a/server/.env.example
+++ b/server/.env.example
@@ -47,13 +47,16 @@ DEMO_MODE=false # Demo mode: resets data hourly
# DEMO_ADMIN_EMAIL=admin@trek.app # Email of that seeded demo admin. (default: admin@trek.app)
# DEMO_ADMIN_PASS=admin12345 # Initial password of that seeded demo admin. Change it on a public demo. (default: admin12345)
-# RESTORE_FROM_BACKUP=/app/data/backup.zip # Restore this backup on the first start, before any setup. Ignored once a database exists.
+# RESTORE_FROM_BACKUP=/app/data/backup.zip # Restore this backup on the first start, before any setup. Ignored once a database exists (on Postgres: once the database holds a table).
# FILE_UPLOAD_LIMIT_MB=50 # Max size (MB) of a file uploaded to a trip, a booking or a note. Videos keep their own 500 MB cap. Raise your reverse proxy's upload limit too.
# BACKUP_MAX_DECOMPRESSED_MB=5120 # Max size (MB) a restore archive may unpack to, on both restore paths: the zip-bomb guard. Raise it if a large backup is refused with "Backup exceeds the maximum decompressed size". (default: 5120)
# BACKUP_UPLOAD_LIMIT_MB=500 # Max size (MB) of a backup archive you can upload when restoring. Raise it if your backup exceeds 500 MB. If you sit behind a reverse proxy, raise its upload limit too (e.g. nginx client_max_body_size).
+# TREK_DB_URL=postgres://trek:password@db:5432/trek # Run on a Postgres database instead of the SQLite file in data/. TREK creates its schema in an empty database and refuses one holding tables it did not create. sslmode=disable, require, verify-ca or verify-full (the last three always verify the certificate; sslrootcert= names the CA file). Cannot be combined with TREK_DB_FILE, TREK_DB_JOURNAL_MODE or TREK_DB_SYNCHRONOUS.
+# TREK_DB_PG_BIN_DIR=/usr/lib/postgresql/17/bin # The directory holding pg_dump, pg_restore and psql, which back up and restore a Postgres database. Unset, TREK looks in /usr/lib/postgresql/<17..14>/bin, /usr/local/bin and /usr/bin (the Docker image installs the version 17 tools, which cannot dump a newer server). Unused on SQLite.
+# TREK_DB_POOL_MAX=10 # Most connections TREK opens to the Postgres database (1 to 100). Unused on SQLite. (default: 10)
# TREK_DB_JOURNAL_MODE=WAL # SQLite journal mode (DELETE, TRUNCATE, PERSIST, MEMORY, WAL, OFF). WAL is fastest and the right choice on local disk. Set DELETE if the data directory lives on network storage (Azure App Service, an SMB/NFS volume on a NAS) — WAL coordinates writers through a shared-memory file that those filesystems do not implement reliably. The mode is stored inside the database file, so it survives a restart; changing this applies on the next boot.
-# TREK_DB_PRE_MIGRATE_SNAPSHOT=true # Copy the database to data/pre-migrate--.db before a startup migrates it, so the previous image can run on the copy again. A copy that cannot be written stops the startup; false migrates without one.
+# TREK_DB_PRE_MIGRATE_SNAPSHOT=true # Copy the database to data/pre-migrate--.db (a .pgdump made with pg_dump on Postgres) before a startup migrates it, so the previous image can run on the copy again. A copy that cannot be written stops the startup; false migrates without one.
# TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP=3 # How many pre-migrate copies to keep, newest first (1 to 100). (default: 3)
# TREK_DB_SYNCHRONOUS=NORMAL # How hard SQLite flushes to disk (OFF, NORMAL, FULL, EXTRA). Defaults to NORMAL under WAL and FULL under any other journal mode — leave it alone unless you know why you want otherwise. Both variables fall back to their default (with a warning in the log) if the value is not a mode SQLite knows.
diff --git a/server/CLAUDE.md b/server/CLAUDE.md
index 6de4ca3e3..22f06324c 100644
--- a/server/CLAUDE.md
+++ b/server/CLAUDE.md
@@ -20,12 +20,16 @@ npm run gen:plugin-facts # regenerate the plugin-protocol tables into plugin-sd
npm run check:plugin-facts # CI gate — fails if the generated copies drifted
npm run gen:db-types # regenerate the Kysely table types in src/db/kysely/ from the migrated schema
npm run check:db-types # CI gate: fails when src/db/kysely/ no longer matches the migrations
+npm run gen:pg-baseline # regenerate the Postgres baseline in assets/postgres/ from the entities
+npm run check:pg-baseline # CI gate: fails when the baseline drifted from the entities, its manifest from the migrations, or a SQLite trigger has no Postgres port
node scripts/coverage-thresholds.mjs # after test:coverage — prints the ratchet block for vitest.config.ts
npm run db:call-graph -- --sync --tx [--domains nest/days,…] # sync DB-touching methods + transaction sites (exit 1 if any)
npm run lint:size # no file in src/ or scripts/ past 1000 lines, none in tests/ past 2000, longer ones only shrink (CI gate)
npm run lint:boundaries # import cycles and cross-domain reach-ins may only shrink (CI gate)
npm run lint:dialect # SQLite-only SQL spellings under src/ may only shrink (CI gate)
+npm run lint:engine-dispatch # isSqlite(/isPostgres(/platform instanceof outside src/db/dialect/ and src/db/engine/ may only shrink (CI gate)
npm run probe:pg # dialect helpers + the repository statements it reaches against Postgres (CI job postgres-probe; needs TREK_PG_PROBE_URL)
+npm run test:pg # the Postgres lane (CI job server-postgres-tests; needs TREK_TEST_DB_URL)
npm run lint:tx # methods writing more than once outside one transaction may only shrink (CI gate)
npm run lint:test-sql # raw better-sqlite3 statements in tests/ may only shrink (CI gate)
npm run lint:test-mocks # vi.mock of src/config, src/nest/realtime and src/db in tests/ may only shrink (CI gate)
@@ -63,7 +67,7 @@ Single test: `npx vitest run tests/unit/nest/weather.controller.test.ts`, or `np
**`lint:dialect`** (`scripts/dialect-lint.mjs`) reads every string and template literal under `src/` (comments do not count) and every `.insertId` read, and counts the SQLite-only spellings per file and rule: `INSERT OR IGNORE`/`REPLACE`, `datetime()`/`julianday()`/`unixepoch()` (also as the bare name handed to `eb.fn`), `strftime`, `json_extract` and its siblings, `GLOB`, `last_insert_rowid`, `AUTOINCREMENT`, a `PRAGMA` statement outside the `src/db/*.ts` lifecycle files and the migrations, and `||` concatenation in SQL. The counts are held to `scripts/dialect-baseline.json`; a new hit fails, and so does an entry above what its file holds now, so the change that removes one runs `npm run lint:dialect -- --update` (it never raises or adds an entry). The dialect layer `src/db/dialect/` is not scanned, and neither are the migrations up to `FROZEN_MIGRATIONS_THROUGH` in the script; a newer migration is held to the DML rules, while its DDL stays engine specific.
-**`probe:pg`** (`scripts/pg-probe.ts`, CI job `postgres-probe`) measures what of the SQL runs on Postgres. Given an empty database (`TREK_PG_PROBE_URL`, or `--url=`; it refuses one holding tables it did not create) it derives a Postgres schema from the entities with MikroORM's schema generator (timestamps stay TEXT, SQLite-only defaults and `COLLATE NOCASE` get their Postgres spelling, no foreign keys), runs every dialect helper on an in-memory SQLite and on Postgres and checks both against the same expectation (`scripts/pg-probe/helper-cases.ts`; every case must pass), then calls every public method of every class in `src/db/repositories/*.ts` once. The arguments come from the parameter types, read with the TypeScript checker (`pg-probe/arg-samples.ts`), and the calls go through a MikroORM Postgres driver that runs each statement alone in a rolled-back transaction with a statement timeout and records whether Postgres accepted it (`pg-probe/recorder.ts`), so every statement meets empty tables and nothing persists. What it measures is the statements each method reaches with those sample arguments against empty tables, not every statement the code can send. The distinct statements Postgres refuses as SQL (SQLSTATE class 42 or 0A: syntax, an unknown function or column, a datatype mismatch, an unsupported feature) are held per `Class.method` to `failing` in `scripts/pg-probe-baseline.json`: a method may fail no more than its entry, and an entry above what it fails now fails too. A refusal over a sample value (class 22, say a `probe` string in a date column) or the empty tables is reported in the log and the step summary but not held, so renaming a parameter cannot turn a sample into dialect debt. The methods whose SQL the probe did not see (it could not build their arguments, they sent no SQL because they threw first or never reach the database, or they timed out) are held to the same file's `uncovered` list: a method outside the list that goes unmeasured fails the run, and so does a listed method that is measured now or gone, so the list only shrinks and a new method the sampler cannot reach does not land unchecked. Fix such a method by giving it parameters the sampler can build or by making it reach its SQL with sample values. The job uploads the measured baseline and a JSON report (`pg-probe-baseline` artifact), so the change that fixes a statement copies the lowered baseline from there or runs `npm run probe:pg -- --update` against a local Postgres (`docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=probe postgres:17`); `--update` never raises or adds an entry in either part. While the baseline's `failing` or `uncovered` is `null` it has never been measured, and the run fails (outside `--update`) until the first measurement from the artifact is committed, so the gate cannot land on `dev` holding nothing. A run that called no repository method fails too, so a broken plan can neither pass nor seed an empty baseline. The unit tests under `tests/unit/scripts/pg-probe/` cover the probe's logic, including every helper case on SQLite.
+**`probe:pg`** (`scripts/pg-probe.ts`, CI job `postgres-probe`) measures what of the SQL runs on Postgres. Given an empty database (`TREK_PG_PROBE_URL`, or `--url=`; it refuses one holding tables it did not create) it derives a Postgres schema from the entities with MikroORM's schema generator (timestamps stay TEXT, integers are `bigint`, SQLite-only defaults and `COLLATE NOCASE` get their Postgres spelling, no foreign keys), runs every dialect helper on an in-memory SQLite and on Postgres and checks both against the same expectation (`scripts/pg-probe/helper-cases.ts`; every case must pass), then calls every public method of every class in `src/db/repositories/*.ts` once. The arguments come from the parameter types, read with the TypeScript checker (`pg-probe/arg-samples.ts`), and the calls go through a MikroORM Postgres driver that runs each statement alone in a rolled-back transaction with a statement timeout and records whether Postgres accepted it (`pg-probe/recorder.ts`), so every statement meets empty tables and nothing persists. What it measures is the statements each method reaches with those sample arguments against empty tables, not every statement the code can send. The distinct statements Postgres refuses as SQL or over a value's type (SQLSTATE class 42, 0A or 22: syntax, an unknown function or column, a datatype mismatch, an unsupported feature, a string reaching an integer or date column) are held per `Class.method` to `failing` in `scripts/pg-probe-baseline.json`: a method may fail no more than its entry, and an entry above what it fails now fails too. Class 22 counts because the sampler sends what each parameter declares (a `number` is 1, a `string` named like a date is a date, the `present` flag of a partial update's pair is true), so a value of the wrong type reaching a column is the code's: a parameter typed `string` bound to an integer column compares on SQLite and is refused on Postgres; type it as the column is and convert at the service (`toRowId`). A constraint the sample values break (class 23) or the empty tables is reported in the log and the step summary but not held. The methods whose SQL the probe did not see (it could not build their arguments, they sent no SQL because they threw first or never reach the database, or they timed out) are held to the same file's `uncovered` list: a method outside the list that goes unmeasured fails the run, and so does a listed method that is measured now or gone, so the list only shrinks and a new method the sampler cannot reach does not land unchecked. Fix such a method by giving it parameters the sampler can build or by making it reach its SQL with sample values. The job uploads the measured baseline and a JSON report (`pg-probe-baseline` artifact), so the change that fixes a statement copies the lowered baseline from there or runs `npm run probe:pg -- --update` against a local Postgres (`docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=probe postgres:17`); `--update` never raises or adds an entry in either part. While the baseline's `failing` or `uncovered` is `null` it has never been measured, and the run fails (outside `--update`) until the first measurement from the artifact is committed, so the gate cannot land on `dev` holding nothing. A run that called no repository method fails too, so a broken plan can neither pass nor seed an empty baseline. The unit tests under `tests/unit/scripts/pg-probe/` cover the probe's logic, including every helper case on SQLite.
## Architecture invariants
@@ -76,12 +80,13 @@ Nest owns everything. Every domain is a DI module under `src/nest//` (`c
- **Plugins** (`src/nest/plugins/`): the child-process runtime (`supervisor/`, `runtime/plugin-host-entry.ts`, `protocol/envelope.ts`) is **intentionally not Nest**; it is the security boundary. `host/rpc-host.ts` registers an RPC method at spawn only if the plugin holds the unlocking permission (registration = authorization). Install-time gates live in `install/`; the manifest's `trek` version range fails closed, and `TREK_PLUGINS_IGNORE_TREK_RANGE` downgrades that to a warning. The protocol tables in `protocol/envelope.ts` (+ `src/plugin-event-sink.ts`) are **generated** into `plugin-sdk/` and `shared/`: run `gen:plugin-facts` after touching them and commit the output. What a plugin gets back is `protocol/output-contract.ts`: every wire method has an entry, and an entity result (`trips.getById`, `todos.list`, `vacay.mine`, ...) is cut down to its published fields in `rpc-host.ts` on the way out, together with every child row it carries whose field is listed in `PLUGIN_ENTITY_NESTED` (a day's `notes_items`, a reservation's `endpoints`). A new column on any whole-row table behind those entities, child tables included, fails `tests/unit/plugins/output-contract.schema.test.ts` until it is listed as published or withheld (the SDK's `PLUGIN_ENTITY_FIELDS` and `PLUGIN_ENTITY_NESTED` are generated from it); a method left as `readModel` must return a value built field by field from named columns. A plugin's own SQL (`host/plugin-data.service.ts`) is held to a row cap, and `query`, `tx` and an `exec` script to a time budget checked between rows and statements (`migrate` is deliberately not timed). `runtime/egress-policy.ts` is a hand-kept mirror guarded by a parity test in `plugin-sdk/`; change both sides together.
- **Storage**: `src/nest/storage/` is the upload driver abstraction (local / S3 / mirror behind one interface, with an admin surface and a usage job). Domain services never touch the filesystem or the S3 SDK directly. Where the data lives (`data/`, `uploads/`, backups, logs, key files, the DB file) is resolved in one place, `src/app-config/data-paths.ts`; a provider that reads one injects `DataPathsService`, plain helper modules shared with code outside the container call `resolveDataPaths()` (the list is in `src/app-config/README.md`), and nothing anchors a data path on its own `__dirname` or on `process.cwd()`.
- **DB** (`src/db/`): `better-sqlite3`, WAL, FK on. `database.ts` only holds the connection and applies pragmas. It does not build a schema, and importing it opens nothing (the first reader does). The connection's lifecycle belongs to the `DatabaseLifecycle` provider (`nest/database/database-lifecycle.service.ts`): `buildApp()` opens it through `open()` (which binds the ORM to later swaps and runs the schema bootstrap below), the shutdown closes it through `close()`, and a restore or the demo reset swaps the file only through the database backup port, `DATABASE_BACKUP` (`nest/database/database-backup.interface.ts`; the SQLite implementation is `nest/backup/sqlite-database-backup.ts`: `VACUUM INTO` snapshot, close, atomic file swap, `reopen()`). The port works on `DatabaseLifecycle.file`, the one resolved path (`TREK_DB_FILE` or `data/travel.db`), and a snapshot that fails fails the backup rather than archiving the live file. `MaintenanceRepository` is provided only by `MaintenanceModule`'s factory and injected everywhere else; no consumer constructs it. A provider that has to follow every reopen (the demo reset job saves the first demo baseline after a restore's re-bootstrap seeded the example trips) registers with `DatabaseLifecycle.onReopened()`, which runs after the reopen's schema bootstrap; a listener contains its own failures, since one that throws fails the reopen. MikroORM's migrations (`db/migrations/`) are the only schema source, **including for tests**: the hand-kept `schema.ts`/`migrations.ts`/`seeds.ts` builders and the parity test that guarded them are gone. Seeding is the MikroORM seeders in `db/seeders/`, applied by `db/orm.ts` from `buildApp()` before anything reads. An install the retired positional runner migrated (a `schema_version` row, no `mikro_orm_migrations` rows) is baselined by `db/legacy-baseline.ts`: the migrations whose `Legacy migration step N` docstring is ≤ its version are recorded as executed (never replayed) in the same transaction as the migrator run, while the baseline migration itself still runs first, as the legacy runner's `createTables()` did on every boot. It refuses the boot when `schema_version` is not exactly one row holding an integer ≥ 1, or is above the last mapped step (258); a value from 1 to 258 is read as dev's steps of those numbers, so a pre-ORM feature-branch database whose steps differ is not detected (the Tours branch is the one case it cuts back). Recorded `mikro_orm_migrations` names this build does not ship refuse it too (`refuseNewerDatabase`), whether a newer release or a development build wrote them. 4.3.x and earlier have no such check and start on a database 5.x migrated. Before a boot applies pending migrations to a file database that holds anything, `db/pre-migrate-snapshot.ts` writes a `VACUUM INTO` copy next to it (`pre-migrate--.db`, `` being `legacy-` or the last recorded migration's timestamp; the newest `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP` stay) and the newer-database refusal names the matching copy; a copy that cannot be written refuses the boot unless `TREK_DB_PRE_MIGRATE_SNAPSHOT=false`. It is off under `NODE_ENV=test` unless a suite sets that variable to true, and a restore's re-bootstrap skips it (the archive is the copy). After the migrator, `db/email-case-index.ts` creates the case-insensitive unique index on `users.email` that its migration had to defer on an install holding two accounts whose emails differ only in case (flag `email_case_index_pending`). `db/orm-driver.ts` binds the ORM to that same connection instead of opening a second one — with `:memory:` under test, a second connection is a second, empty database. Kysely statements (`this.kysely<...>()`) type against `DB` from `db/kysely/db.ts`, narrowed with `Pick`; those files are generated from the migrated schema by `scripts/generate-db-types.ts`, so a migration that adds or changes a column is followed by `npm run gen:db-types`, never by a hand-written table interface. Every read and write goes through a repository in `db/repositories/` (each extends the shared `TrekRepository` base in `db/repositories/_shared/`, which validates the request context before delegating to MikroORM) and, for multi-statement writes, `UnitOfWork.transactional(...)` (`nest/database/unit-of-work.ts`, provided by the global `OrmModule`) — there is no `DatabaseService`/`DatabaseModule` to inject any more. Entrypoints outside an HTTP request — an MCP tool call, a plugin RPC dispatch, a WebSocket message, a cron job, boot-time seeding, a backup restore — open one explicitly with `withRequestContext()` (`nest/database/request-context.ts`), since only the pathless `mikroOrmRequestContext` middleware `buildApp()` mounts (with `@mikro-orm/nestjs`'s own `registerRequestContext` turned off in `app.module.ts`, UPLOADS-P16) forks an `EntityManager` per HTTP request. `NODE_ENV=test` ⇒ isolated `:memory:` DB per vitest worker, opened from a snapshot `tests/global-setup.ts` migrates once per run; `TREK_DB_FILE` ⇒ explicit file DB (Playwright harness).
+- **Engines** (`src/db/engine/`): `TREK_DB_URL` (a `postgres://` URL) runs TREK on Postgres instead of the SQLite file; without it nothing changes. `resolveEngine()` picks the engine, and what differs by engine (the ORM options, opening and closing, how a database reaches the schema) is the engine's, behind the `DatabaseEngine` contract that `DatabaseLifecycle` calls; code elsewhere never checks which engine it runs on (`lint:engine-dispatch`). The Postgres driver (`src/db/engine/postgres/postgres-driver.ts`) reads `bigint` as a number. A Postgres database never replays the SQLite migrations: an empty one gets `assets/postgres/baseline.sql` (generated from the entities by `gen:pg-baseline`, with the triggers ported by hand in `POSTGRES_TRIGGERS`) and every migration up to its cut recorded as executed, in one transaction (`src/db/engine/postgres/schema-bootstrap.ts`); one holding tables without TREK's migration history is refused; one TREK migrated runs the newer-database refusal and the pending migrations. Before pending migrations the boot takes a `pg_dump` copy to `data/pre-migrate--.pgdump` under the same `TREK_DB_PRE_MIGRATE_SNAPSHOT(_KEEP)` policy as the SQLite file. The backup port's Postgres side (`src/nest/backup/postgres-database-backup.ts`) dumps with `pg_dump`; a restore turns the dump into a script with `pg_restore` and has `psql` clear the current schema and run it in one transaction (a dump only recreates what it holds, so `pg_restore --clean` would leave a later migration's tables behind); the restore refuses a pg_restore or psql older than 17.6/16.10/15.14/14.19, whose script would lack the `\restrict` line that keeps a psql meta-command smuggled into a dump from running (CVE-2025-8714). The tools run by absolute path (`src/db/engine/postgres/pg-tools.ts`, `TREK_DB_PG_BIN_DIR`) with the password in their environment, never on the command line; the image ships the version 17 client, which cannot dump a newer server. A restore refuses a SQLite archive or a newer TREK's dump before anything is replaced, and replaces everything in the schema, so TREK wants a database of its own and a role that is not a superuser. `RESTORE_FROM_BACKUP` loads the archive's dump the same way into a database that holds no table yet, and is ignored once it holds one. The Postgres lane (`vitest.pg.config.ts`, `npm run test:pg`, CI job `server-postgres-tests`) runs the suites that boot the app through `buildApp()` on the real database module, each file on its own copy of one template database migrated by the engine's bootstrap (`tests/postgres/global-setup.ts`), plus `tests/postgres/` (PARITY-PG: the comparator finds nothing between the bootstrapped database and the entities but the differences it lists with their reason; the write lock; case-insensitive search). `TREK_TEST_DB_URL` names a database it may create and drop databases from; without it the lane fails. The list is kept by hand: a new suite that boots the app through `buildApp()` without mocking `db/database` belongs on it, unless it depends on SQLite answering synchronously (the config names the two that do). Two processes starting on one Postgres database at once are not coordinated yet: the second fails its boot (the first one's schema transaction wins), so start one instance for a first boot or an upgrade before scaling out.
## Rules for new code
- **Thin controller → injectable provider.** Never resurrect a plain function-module service; don't grow god services — split a domain into several modules when it outgrows one class (trips and auth already are).
- **Inject, don't reach for module globals.** No imports of the global `db` proxy from `nest/` layers at all any more — the last production importer converted onto a repository, and the export now survives only so ~190 test files' `vi.mock('…/db/database', …)` factories keep typechecking; no new code depending on the `ws` singleton or event-sink globals. There is no `DatabaseService` to inject either — take the repository(ies) you need plus `UnitOfWork` for transactions. Modules must be importable without side effects.
-- **Transactions are not optional.** Multi-statement writes go in `await this.uow.transactional(async () => …)` (`UnitOfWork` from `nest/database`, provided by the global `OrmModule`); never hand-roll `BEGIN`/`COMMIT` and never call `db.transaction()` (the Phase 1 async sweep removes it). The sequence lives in the service, never in a controller, MCP tool or RPC handler, so every surface calls the one method that holds the transaction (`ReservationsService.createWithCost` is the shape: side effects such as broadcasts come back to the caller and go out after the commit). A position or id taken as `MAX(...) + 1` is read inside the same transaction as the insert that uses it: the transaction holds the connection, so two requests at once cannot read the same MAX. `lint:tx` holds the rest. Every DB-touching method is `async`; `@typescript-eslint/no-floating-promises` and `no-misused-promises` are errors, so an unawaited call or a `Promise` in a condition fails lint. Bind values with `?`; identifiers only via a literal allow-list. In `src/db/repositories/` and `src/db/seeders/`, ESLint refuses raw SQL in any spelling: `raw()`, the `sql` tag, and SQL text handed to `execute`/`run`/`all`/`get`. Build the statement with the query builder; `MaintenanceRepository`, `AdminSeeder` and `CategorySeeder` are the named exceptions in `eslint.config.mjs`, and `tests/unit/eslint-rules/restricted-syntax-selectors.test.ts` pins how many SQL strings each still holds, so they can only lose SQL, never gain it. An insert that needs the new id reads it with `.returning('id')`: `InsertResult.insertId` is SQLite's `last_insert_rowid` and undefined on Postgres, and ESLint refuses it in `src/db/repositories/` and `src/db/seeders/`.
+- **Transactions are not optional.** Multi-statement writes go in `await this.uow.transactional(async () => …)` (`UnitOfWork` from `nest/database`, provided by the global `OrmModule`); never hand-roll `BEGIN`/`COMMIT` and never call `db.transaction()` (the Phase 1 async sweep removes it). The sequence lives in the service, never in a controller, MCP tool or RPC handler, so every surface calls the one method that holds the transaction (`ReservationsService.createWithCost` is the shape: side effects such as broadcasts come back to the caller and go out after the commit). A position or id taken as `MAX(...) + 1` is read inside the same transaction as the insert that uses it: the transaction holds the connection, so two requests at once cannot read the same MAX. On Postgres, where a pool runs transactions side by side, `UnitOfWork` first takes one advisory lock for the database (`DatabaseEngine.serializeWrites`), so the same holds; a transaction opened any other way does not get it. `lint:tx` holds the rest. Every DB-touching method is `async`; `@typescript-eslint/no-floating-promises` and `no-misused-promises` are errors, so an unawaited call or a `Promise` in a condition fails lint. Bind values with `?`; identifiers only via a literal allow-list. In `src/db/repositories/` and `src/db/seeders/`, ESLint refuses raw SQL in any spelling: `raw()`, the `sql` tag, and SQL text handed to `execute`/`run`/`all`/`get`. Build the statement with the query builder; `MaintenanceRepository`, `AdminSeeder` and `CategorySeeder` are the named exceptions in `eslint.config.mjs`, and `tests/unit/eslint-rules/restricted-syntax-selectors.test.ts` pins how many SQL strings each still holds, so they can only lose SQL, never gain it. An insert that needs the new id reads it with `.returning('id')`: `InsertResult.insertId` is SQLite's `last_insert_rowid` and undefined on Postgres, and ESLint refuses it in `src/db/repositories/` and `src/db/seeders/`.
- **Migrations are append-only.** A migration is identified by its class name in `mikro_orm_migrations`, and files sort by the timestamp in that name. Never rename or delete one that has shipped, and never edit its body; add a new one. No env interpolation in SQL. (The old positional `schema_version` array and the hand-kept `db/migrations.ts` test builder are both gone; the numbered migrations are the only schema history, for tests too.) A new foreign key comes with an index that leads with its column(s), or deleting the parent scans the child table (a partial index counts only when its WHERE clause is `IS NOT NULL` on those columns); `tests/unit/db/foreign-key-indexes.test.ts` fails on one without.
- **Every write endpoint validates** through the Zod pipe with a schema from `@trek/shared` — no `Record` + ad-hoc checks. Also verify every referenced id exists and belongs to the same trip.
- **Every outbound `fetch`** gets a timeout, a response-size cap and boundary validation: no `as`-casting provider responses, no silent `catch → null`. User-influenced URLs go through the SSRF guard. Never fall back to another user's API key. ESLint requires an init with a `signal` on `fetch`, `safeFetch`, `safeFetchFollow` and `safeFetchAdminConfigured` alike, or a literal `timeoutMs` in the options object of `safeFetch`/`safeFetchFollow`; an init spelled `undefined`, `null` or `void 0` counts as none (`safeFetchLlm` takes `LLM_TIMEOUT_MS` inside). Behind that, the guarded wrappers (`utils/ssrfGuard.ts`) cap every body at `DEFAULT_MAX_RESPONSE_BYTES` (64 MB) and, for a call without a signal, wait `DEFAULT_RESPONSE_TIMEOUT_MS` (30 s) for the headers. Pass a tighter `maxBytes` when you buffer a small answer, `timeoutMs` for a deadline over the whole exchange, and `maxBytes: null` only where the body is streamed on or counted by the caller (`pipeAsset`, the doc-sync transfers, the Ollama pull).
@@ -91,7 +96,7 @@ Nest owns everything. Every domain is a DI module under `src/nest//` (`c
## SQL dialect
-SQLite is the engine today and Postgres is the planned second one. Code under `src/` is written so that a Postgres port only adds a branch to the dialect layer, never a sweep through the repositories:
+SQLite is the default engine and Postgres the second (`TREK_DB_URL`, see Engines above). Code under `src/` is written so that an engine difference only adds a branch to the dialect layer, never a sweep through the repositories:
- **Build statements, don't spell them.** Use the entity methods or `this.kysely>()` on the generated table types. Repositories and seeders may not hold SQL text at all (ESLint), and `lint:dialect` holds every other file to its baseline.
- **Which query API.** New repository code picks by the shape of the statement, not by what the file already uses:
@@ -101,12 +106,13 @@ SQLite is the engine today and Postgres is the planned second one. Code under `s
- **Insert or ignore / upsert**: `.onConflict((oc) => oc.columns([...]).doNothing())` or `.doUpdateSet({...})`, or `em.upsert`. Name the conflict target where there is one. Never `INSERT OR IGNORE`/`INSERT OR REPLACE`.
- **The new id**: `.returning('id').executeTakeFirstOrThrow()`. Never `InsertResult.insertId` or `last_insert_rowid()`; both are SQLite's rowid and undefined on Postgres.
- **Time**: `currentTimestamp`/`currentTimestampKysely`, `nowMinusDays`, `nowMinusHours`, `nowPlusSeconds`/`nowPlusSecondsKysely`, `nowDateOffset`, `dateOf`, `dateAdd`, `unixEpochToIsoKysely` from `src/db/dialect/sql-functions.ts` (the MikroORM `raw()` helpers) and `src/db/dialect/kysely-functions.ts` (their Kysely-expression twins). Never `datetime('now')`, `strftime` or `julianday`.
+- **Search that ignores case**: `ilike(platform, refs)` builds the `(a LIKE ? ESCAPE '\' OR …)` condition, `ILIKE` on Postgres, where `LIKE` is exact. A bare `LIKE` is for a prefix that is exact on both engines (a settings key, a storage path).
- **Strings and casts**: `concat`/`concatKysely` instead of `||`, `substring`/`substringKysely`, `lower`/`lowerTrim`, `collateNoCase`, `castInteger`/`castIntegerKysely`, and `startsWithIsoDate`/`startsWithIsoDateKysely` instead of `GLOB`.
- **JSON columns** are read whole and parsed in the service; no `json_extract` in SQL. Each one is declared once in `src/db/json-columns.ts` (its Zod shape and the fallback a reader gets for an empty or broken row) and read with `decodeJson`/`decodeJsonResult`, written from a server-built value with `encodeJson` (`src/utils/json-column.ts`), never with a bare `JSON.parse` at the call site: one column, one answer to a broken row, logged.
- **Timestamps** in a DATETIME column are the canonical `YYYY-MM-DD HH:MM:SS` UTC text, never `toISOString()`: in SQL the two spellings do not compare. A value written from JS is a `DbTimestamp` from `dbNow()` (`src/db/types`), and a stored value is read back as an instant with `parseDbTimestamp()` (a bare `new Date()` reads the canonical text as local time). Where a response has always carried the ISO spelling, convert on the way out (`utcSuffix`), not in the column.
-- **PRAGMA** belongs to the `src/db/` lifecycle files (`database.ts`, `connection.ts`, `durability.ts`, the migrations). `MaintenanceRepository` is the baselined SQLite maintenance adapter (`wal_checkpoint`, `VACUUM INTO`).
+- **PRAGMA** belongs to the `src/db/` lifecycle files (`database.ts`, `connection.ts`, `durability.ts`, the migrations) and to the SQLite engine's folder, `src/db/engine/sqlite/`, where `SqliteMaintenance` holds the file's own maintenance (`wal_checkpoint`, `VACUUM INTO`) for the backup port. `MaintenanceRepository` keeps only what runs on every engine (the readiness ping, the plugin-table erasure).
- **A missing helper** goes into `sql-functions.ts` (or `kysely-functions.ts` for a Kysely expression), dispatching on the live platform through `src/db/dialect/platform.ts`: a SQLite branch and a Postgres branch (one spelling under `isKnownPlatform` where both engines read it alike), `unsupported(platform)` for anything else. The Postgres branch keeps the SQLite storage model, so a clock helper renders the same `YYYY-MM-DD HH:MM:SS` UTC text. Pin the Postgres text in `tests/unit/db/dialect/sql-functions.postgres.test.ts` and add a case to `scripts/pg-probe/helper-cases.ts` so the probe runs it on both engines. `src/db/dialect/` is the only place allowed to spell an engine's own SQL.
-- **Migrations** that have shipped are frozen as they are. A new migration's data statements follow the rules above; its DDL is written for the engine it runs on.
+- **Migrations** that have shipped are frozen as they are. The ones up to the Postgres baseline's cut (`through` in `assets/postgres/baseline.json`) never run on Postgres; every later one runs on both engines. Its data statements follow the rules above, and its DDL either asks the catalog through the helpers in `src/db/migration-utils.ts` (`tableExists`, `columnNames`, `addColumnIfMissing`, which read `src/db/dialect/catalog.ts`) or branches on the engine where the spelling differs (a SQLite table rebuild is an `ALTER TABLE` on Postgres). Once a migration lands after the cut, the baseline is frozen and `check:pg-baseline` only checks its manifest.
- **Storage model**: the generated Kysely types describe what SQLite stores (0/1 integers for flags, TEXT timestamps, JSON in TEXT). Convert at the service boundary, as the contracts already require.
- **MySQL/MariaDB are out of scope.** The repositories rely on `INSERT ... ON CONFLICT` (Kysely's `onConflict`, often a target-less `doNothing()`) and on `RETURNING`, and MySQL has neither: it would need an insert-ignore/upsert wrapper in the dialect layer and a different way to read new ids before it could be considered.
diff --git a/server/assets/postgres/baseline.json b/server/assets/postgres/baseline.json
new file mode 100644
index 000000000..12dddea08
--- /dev/null
+++ b/server/assets/postgres/baseline.json
@@ -0,0 +1,338 @@
+{
+ "through": "Migration20200101046360_a_new_install_starts_with_more_addons_on",
+ "migrations": [
+ "Migration20200101000000_baseline_schema",
+ "Migration20200101000100_users_add_unsplash_api_key",
+ "Migration20200101000200_users_add_openweather_api_key",
+ "Migration20200101000300_places_add_duration_minutes",
+ "Migration20200101000400_places_add_notes",
+ "Migration20200101000500_places_add_image_url",
+ "Migration20200101000600_places_add_transport_mode",
+ "Migration20200101000700_days_add_title",
+ "Migration20200101000800_reservations_add_status",
+ "Migration20200101000900_trip_files_add_reservation_id",
+ "Migration20200101001000_reservations_add_type",
+ "Migration20200101001100_trips_add_cover_image",
+ "Migration20200101001200_day_notes_add_icon",
+ "Migration20200101001300_trips_add_is_archived",
+ "Migration20200101001400_categories_add_user_id",
+ "Migration20200101001500_users_add_avatar",
+ "Migration20200101001600_users_add_oidc_sub",
+ "Migration20200101001700_users_add_oidc_issuer",
+ "Migration20200101001800_users_add_last_login",
+ "Migration20200101001900_create_budget_items_new",
+ "Migration20200101002000_day_accommodations_add_check_in",
+ "Migration20200101002100_places_add_end_time",
+ "Migration20200101002200_day_assignments_add_reservation_status",
+ "Migration20200101002300_reservations_add_assignment_id",
+ "Migration20200101002400_create_assignment_participants",
+ "Migration20200101002500_create_collab_notes",
+ "Migration20200101002600_day_assignments_add_assignment_time",
+ "Migration20200101002700_create_budget_item_members",
+ "Migration20200101002800_create_collab_message_reactions",
+ "Migration20200101002900_collab_messages_add_deleted",
+ "Migration20200101003000_trip_files_add_note_id",
+ "Migration20200101003100_reservations_add_reservation_end_time",
+ "Migration20200101003200_places_add_osm_id",
+ "Migration20200101003300_trip_files_add_uploaded_by",
+ "Migration20200101003400_reservations_add_accommodation_id",
+ "Migration20200101003500_create_invite_tokens",
+ "Migration20200101003600_users_add_mfa_enabled",
+ "Migration20200101003700_create_packing_category_assignees",
+ "Migration20200101003800_create_packing_templates",
+ "Migration20200101003900_create_packing_bags",
+ "Migration20200101004000_create_visited_countries",
+ "Migration20200101004100_create_bucket_list",
+ "Migration20200101004200_vacay_plans_add_weekend_days",
+ "Migration20200101004300_users_add_immich_url",
+ "Migration20200101004400_create_file_links",
+ "Migration20200101004500_reservations_add_day_plan_position",
+ "Migration20200101004600_budget_items_add_paid_by_user_id",
+ "Migration20200101004700_bucket_list_add_target_date",
+ "Migration20200101004800_create_notification_preferences",
+ "Migration20200101004900_notification_preferences_add_notify_vacay_invite",
+ "Migration20200101005000_create_share_tokens",
+ "Migration20200101005100_share_tokens_add_share_map",
+ "Migration20200101005200_create_audit_log",
+ "Migration20200101005300_users_add_mfa_backup_codes",
+ "Migration20200101005400_mcp_long_lived_api_tokens",
+ "Migration20200101005500_mcp_addon_entry",
+ "Migration20200101005600_index_on_mcp_tokens",
+ "Migration20200101005700_ensure_mcp_addon_type_is_integration",
+ "Migration20200101005800_places_add_route_geometry",
+ "Migration20200101005900_users_add_must_change_password",
+ "Migration20200101010000_trips_add_reminder_days",
+ "Migration20200101010100_encrypt_any_plaintext_oidc_client_secret_left",
+ "Migration20200101010200_encrypt_any_plaintext_smtp_pass_left_in",
+ "Migration20200101010300_encrypt_any_plaintext_immich_api_key_values",
+ "Migration20200101010400_budget_items_add_expense_date",
+ "Migration20200101010500_create_trip_album_links",
+ "Migration20200101010600_create_notifications",
+ "Migration20200101010700_create_trip_photos",
+ "Migration20200101010800_create_trip_album_links",
+ "Migration20200101010900_users_add_synology_url",
+ "Migration20200101011000_step_070",
+ "Migration20200101011100_step_071",
+ "Migration20200101011200_step_072",
+ "Migration20200101011300_trip_photos_add_album_link_id",
+ "Migration20200101011400_todo_items",
+ "Migration20200101011500_backfill_addons",
+ "Migration20200101011600_place_region_cache_for_sub_national_atlas",
+ "Migration20200101011700_create_visited_regions",
+ "Migration20200101011800_normalized_per_user_per_channel_notification_preferences",
+ "Migration20200101011900_drop_the_old_notification_preferences_table",
+ "Migration20200101012000_add_reservation_id_to_budget_items_for",
+ "Migration20200101012100_add_quantity_to_packing_items_user_id",
+ "Migration20200101012200_per_day_positions_for_multi_day_reservations",
+ "Migration20200101012300_budget_category_ordering",
+ "Migration20200101012400_naver_list_import_addon",
+ "Migration20200101012500_oauth_2",
+ "Migration20200101012600_refresh_token_rotation_chain_tracking_for_replay",
+ "Migration20200101012700_public_client_support_for_browser_initiated_dynamic",
+ "Migration20200101012800_make_oauth_clients",
+ "Migration20200101012900_add_otp_field_skip_ssl_column_device",
+ "Migration20200101013000_journey_addon",
+ "Migration20200101013100_journal",
+ "Migration20200101013200_journey_multi_trip_support_sharing_collaboration",
+ "Migration20200101013300_journey_rebuild",
+ "Migration20200101013400_journey_photos",
+ "Migration20200101013500_journey_cover_image",
+ "Migration20200101013600_pros_cons_for_journey_entries",
+ "Migration20200101013700_journey_share_tokens",
+ "Migration20200101013800_vacay_week_start_setting",
+ "Migration20200101013900_unified_photo_provider_abstraction_layer",
+ "Migration20200101014000_hide_skeletons_per_user_setting_on_journey",
+ "Migration20200101014100_idempotency_keys_for_offline_mutation_replay",
+ "Migration20200101014200_enable_naver_list_import_by_default",
+ "Migration20200101014300_add_check_in_end_column_for_check",
+ "Migration20200101014400_system_notices",
+ "Migration20200101014500_passphrase_support_for_synology_shared_album_links",
+ "Migration20200101014600_persistent_google_place_photo_disk_cache_registry",
+ "Migration20200101014700_persistent_place_details_row_cache",
+ "Migration20200101014800_backfill_expired_signed_google_photo_urls_to",
+ "Migration20200101014900_disk_cache_metadata_for_remote_provider_photo",
+ "Migration20200101015000_reservation_endpoints",
+ "Migration20200101015100_reservations_add_end_day_id",
+ "Migration20200101015200_opt_in_immich_auto_upload",
+ "Migration20200101015300_expose_immich_auto_upload_toggle_in_the",
+ "Migration20200101015400_rfc_8707_resource_indicators",
+ "Migration20200101015500_password_reset",
+ "Migration20200101015600_todo_due_date_reminders",
+ "Migration20200101015700_security_audit_batch_1",
+ "Migration20200101015800_widen_idempotency_keys_primary_key_to",
+ "Migration20200101015900_sec_h6_revoke_all_oauth_tokens_issued",
+ "Migration20200101020000_remove_not_null_constraint_on_day_accommodations",
+ "Migration20200101020100_null_out_proxy_image_url_entries_that",
+ "Migration20200101020200_clear_legacy_google_photo_urls_missed_by",
+ "Migration20200101020300_journey_gallery_refactor",
+ "Migration20200101020400_correct_stale_day_id_end_day_id",
+ "Migration20200101020500_846_make_sort_order_authoritative_within_a",
+ "Migration20200101020600_swap_inverted_start_day_id_end_day",
+ "Migration20200101020700_prepare_migration_to_nest_typeorm",
+ "Migration20200101020800_trim_leading_trailing_whitespace_from_stored_usernames",
+ "Migration20200101020900_create_schema_version_new",
+ "Migration20200101021000_oauth_2",
+ "Migration20200101021100_drop_stale_atlas_cache_rows_for_territories",
+ "Migration20200101021200_costs_rework",
+ "Migration20200101021300_rename_the_budget_planner_addon_to_costs",
+ "Migration20200101021400_webauthn_passkey_support_per_user_credentials_single",
+ "Migration20200101021500_atlas_dropped_natural_earth_for_geoboundaries",
+ "Migration20200101021600_step_136",
+ "Migration20200101021700_users_add_airtrail_url",
+ "Migration20200101021800_reservations_add_external_source",
+ "Migration20200101021900_users_add_airtrail_write_enabled",
+ "Migration20200101022000_store_google_maps_feature_ids_separately_from",
+ "Migration20200101022100_remember_the_app_version_a_notice_was",
+ "Migration20200101022200_budget_item_members_add_amount",
+ "Migration20200101022300_calendar_feed_tokens",
+ "Migration20200101022400_optimistic_concurrency_token_for_offline_conflict_detection",
+ "Migration20200101022500_video_support",
+ "Migration20200101022600_dedicated_booking_url",
+ "Migration20200101022700_private_packing_items",
+ "Migration20200101022800_guest_members",
+ "Migration20200101022900_three_tier_packing_sharing",
+ "Migration20200101023000_collections_addon",
+ "Migration20200101023100_user_added_links_on_collections_saved_places",
+ "Migration20200101023200_per_member_permission_role_on_a_shared",
+ "Migration20200101023300_per_trip_invite_links",
+ "Migration20200101023400_optional_trip_binding_on_an_admin_invite",
+ "Migration20200101023500_plugin_system_scaffold",
+ "Migration20200101023600_separate_the_admin_s_on_off_intent",
+ "Migration20200101023700_plugin_capabilities",
+ "Migration20200101023800_tofu_pin_for_a_plugin_s_author",
+ "Migration20200101023900_hash_chained_capability_audit_log",
+ "Migration20200101024000_per_collection_custom_labels",
+ "Migration20200101024100_plugin_owned_metadata_on_core_entities",
+ "Migration20200101024200_freeze_the_fx_rate_on_settle_up",
+ "Migration20200101024300_1446_guests_are_per_trip_people_but",
+ "Migration20200101024400_plugin_dependencies",
+ "Migration20200101024500_per_user_plugin_settings",
+ "Migration20200101024600_host_brokered_outbound_oauth",
+ "Migration20200101024700_persistent_plugin_scheduler",
+ "Migration20200101024800_durable_gdpr_erasure_queue",
+ "Migration20200101024900_tombstones_for_atlas_countries_the_user_has",
+ "Migration20200101025000_operator_supplied_egress_hosts_for_a_plugin",
+ "Migration20200101025100_settings_page_action_buttons_a_plugin_contributes",
+ "Migration20200101025200_why_a_plugin_s_update_was_refused",
+ "Migration20200101025300_the_semver_range_of_trek_versions_a",
+ "Migration20200101025400_place_regions_is_a_re_derivable_nominatim",
+ "Migration20200101025500_tombstones_for_atlas_regions_the_user_has",
+ "Migration20200101025600_half_vacation_days",
+ "Migration20200101025700_read_only_vacay_calendar_sharing",
+ "Migration20200101025800_collaborative_place_ratings",
+ "Migration20200101025900_per_segment_travel_mode",
+ "Migration20200101030000_school_holidays_are_a_visual_vacay_calendar",
+ "Migration20200101030100_1517",
+ "Migration20200101030200_comp_flex_days",
+ "Migration20200101030300_configurable_vacation_year",
+ "Migration20200101030400_manual_gpx_track_colour",
+ "Migration20200101030500_backfill_the_three_places_feature_toggles_before",
+ "Migration20200101030600_free_the_note_column_on_budget_items",
+ "Migration20200101030700_note_colours",
+ "Migration20200101030800_per_segment_travel_mode_for_boundary_legs",
+ "Migration20200101030900_1298",
+ "Migration20200101031000_a_reservation_day_positions_row_only_makes",
+ "Migration20200101031100_1939",
+ "Migration20200101031200_capture_metadata_on_the_photo_itself",
+ "Migration20200101031300_a_journey_shared_while_the_trip_is",
+ "Migration20200101031400_trek_studio_books",
+ "Migration20200101031500_great_britain_gained_its_counties_and_boroughs",
+ "Migration20200101031600_storage_slice_2",
+ "Migration20200101031700_give_back_the_notes_the_ticketjson_step",
+ "Migration20200101031800_a_deliberate_non_latest_plugin_install_sets",
+ "Migration20200101031900_reservations_an_automated_ingest_parked_for_review",
+ "Migration20200101032000_separate_an_integration_key_from_an_mcp",
+ "Migration20200101032100_naver_list_import_was_typed_trip_but",
+ "Migration20200101032200_whether_a_journey_s_map_draws_the",
+ "Migration20200101032300_settings_field_defaults",
+ "Migration20200101032400_settings_form_actions_gain_a_scope",
+ "Migration20200101032500_a_journal_entry_that_is_not_a",
+ "Migration20200101032600_place_shadow_log_which_search_result_a",
+ "Migration20200101032700_what_kind_of_stop_a_place_is",
+ "Migration20200101032800_points_a_day_s_drive_is_made",
+ "Migration20200101032900_which_imported_track_a_day_s_drive",
+ "Migration20200101033000_rename_the_misnamed_guangdong_province",
+ "Migration20200101033100_let_a_file_hang_off_an_expense",
+ "Migration20200101033200_which_chat_message_an_uploaded_image_belongs",
+ "Migration20200101033300_links_somebody_shared_with_the_trip",
+ "Migration20200101033400_route_usage_counters",
+ "Migration20200101033500_how_full_this_stop_fills_the_tank",
+ "Migration20200101033600_create_school_holiday_countries",
+ "Migration20200101033700_day_assignments_add_end_day",
+ "Migration20200101033800_create_roadtrip_day_boundaries",
+ "Migration20200101033900_create_roadtrip_preferences",
+ "Migration20200101034000_a_settle_up_payment_s_date_was",
+ "Migration20200101034100_amap",
+ "Migration20200101034200_dawarich_connection",
+ "Migration20200101034300_reviewed_visit_suggestions",
+ "Migration20200101034400_bucket_list_entries_can_now_be_ticked",
+ "Migration20200101034500_where_an_atlas_country_came_from",
+ "Migration20200101034600_per_key_read_scopes_for_api_v1",
+ "Migration20200101034700_where_a_place_came_from_when_it",
+ "Migration20200101034800_provenance_for_a_day_stop_that_a",
+ "Migration20200101034900_give_every_stay_booked_before_this_release",
+ "Migration20200101035000_the_same_sweep_once_more_for_the",
+ "Migration20200101035100_a_suggestion_the_traveller_has_waved_away",
+ "Migration20200101035200_the_country_an_entry_happened_in_resolved",
+ "Migration20200101035300_which_of_the_optional_entry_fields_a",
+ "Migration20200101035400_let_go_of_a_booking_the_day",
+ "Migration20200101035500_a_journal_skeleton_belongs_to_a_day",
+ "Migration20200101035600_document_providers_part_1_of_3_the",
+ "Migration20200101035700_document_providers_part_2_of_3_the",
+ "Migration20200101035800_document_providers_part_3_of_3_the",
+ "Migration20200101035900_document_providers_when_trek_itself_put_a",
+ "Migration20200101040000_trips_longer_than_a_year_lost_every",
+ "Migration20200101040100_the_road_trip_day_boundaries_carried_the",
+ "Migration20200101040200_trek_photo_cache_meta_cache_key_not_null",
+ "Migration20200101040300_a_booked_night_finally_took_the_seat_a_new",
+ "Migration20200101040400_immich_learns_the_switch_synology_airtrail_and",
+ "Migration20200101040500_a_place_that_moves_takes_its_atlas",
+ "Migration20200101040600_web_push_one_row_per_browser_a",
+ "Migration20200101040700_the_2527_trigger_once_more_for_an",
+ "Migration20200101040800_a_stop_kept_on_the_day_but",
+ "Migration20200101040900_how_many_of_an_item_are_packed",
+ "Migration20200101041000_a_bucket_list_wish_for_one_state",
+ "Migration20200101041100_half_company_holidays_0_5_covers_the",
+ "Migration20200101041200_a_journey_entry_still_being_written",
+ "Migration20200101041300_two_narrower_ways_to_share_a_trip",
+ "Migration20200101041400_a_note_on_a_settle_up_payment",
+ "Migration20200101041500_google_api_calls_per_utc_day",
+ "Migration20200101041600_packing_templates_remember_what_an_item_weighs",
+ "Migration20200101041700_a_journey_s_state_as_its_owner",
+ "Migration20200101041800_a_place_s_e_mail_and_its",
+ "Migration20200101041900_a_journey_that_puts_an_entry_on",
+ "Migration20200101042000_tours",
+ "Migration20200101042100_a_trip_reminder_remembers_it_was_sent",
+ "Migration20200101042200_an_index_on_every_foreign_key",
+ "Migration20200101042300_every_tour_type_the_contract_names",
+ "Migration20200101042400_one_account_per_email_whatever_its_case",
+ "Migration20200101042500_a_cron_tick_runs_in_one_process",
+ "Migration20200101042600_a_session_row_per_sign_in",
+ "Migration20200101042700_timestamps_stored_in_one_spelling",
+ "Migration20200101042800_an_email_the_account_typed_in_itself",
+ "Migration20200101042900_tour_planned_total_duration",
+ "Migration20200101043000_tour_break_additional_duration",
+ "Migration20200101043100_an_imported_tour_has_no_hiking_difficulty",
+ "Migration20200101043200_via_ferrata_tiles_outlive_a_restart",
+ "Migration20200101043300_a_tour_leg_can_follow_a_via_ferrata",
+ "Migration20200101043400_wanderer_connection_and_tour_sources",
+ "Migration20200101043500_wanderer_difficulty_on_the_sac_scale",
+ "Migration20200101043600_a_tour_remembers_the_import_it_was_derived_from",
+ "Migration20200101043700_an_import_planned_over_keeps_its_recorded_track",
+ "Migration20200101043800_a_tour_records_where_it_came_from",
+ "Migration20200101043900_a_tour_can_be_paddled",
+ "Migration20200101044000_a_paddle_tour_plans_its_portages_and_locks",
+ "Migration20200101044100_paddle_obstacles_from_openstreetmap",
+ "Migration20200101044200_a_paddle_tour_records_its_obstacle_decisions",
+ "Migration20200101044300_a_trip_links_a_journal",
+ "Migration20200101044400_a_paddle_region_builds_its_water_network",
+ "Migration20200101044500_a_tour_waypoint_can_shape_the_route",
+ "Migration20200101044600_a_paddle_tour_remembers_how_it_crosses_lakes",
+ "Migration20200101044700_paddle_water_data_loads_where_a_paddler_plans",
+ "Migration20200101044800_a_tour_counts_its_revisions",
+ "Migration20200101044900_a_tour_waypoint_can_be_a_place_of_the_trip",
+ "Migration20200101045000_a_hike_keeps_its_routing_preferences",
+ "Migration20200101045100_a_tour_remembers_the_trip_it_was_taken_from",
+ "Migration20200101045200_a_tour_can_be_ridden",
+ "Migration20200101045300_a_tour_can_be_a_city_walk",
+ "Migration20200101045400_a_tour_waypoint_remembers_the_kind_of_place_it_was_found_as",
+ "Migration20200101045500_a_hike_can_be_marked_as_a_winter_activity",
+ "Migration20200101045510_a_tour_can_be_a_run",
+ "Migration20200101045520_a_tour_can_be_a_ski_track",
+ "Migration20200101045600_a_tour_can_be_ridden_on_horseback",
+ "Migration20200101045700_a_tour_records_the_changes_saved_from_a_proposal",
+ "Migration20200101045710_a_saved_tour_change_can_be_taken_back",
+ "Migration20200101045900_a_tour_keeps_the_track_it_was_done_with",
+ "Migration20200101045910_a_tour_can_be_left_out_of_ones_activities",
+ "Migration20200101046000_a_tour_waypoint_can_end_a_stage",
+ "Migration20200101046010_a_day_can_hold_one_stage_of_a_tour",
+ "Migration20200101046100_a_collection_keeps_whole_tours",
+ "Migration20200101046110_a_tour_points_at_its_packing_list",
+ "Migration20200101046120_a_wanderer_connection_can_watch_its_sources",
+ "Migration20200101046130_snowshoeing_and_winter_hiking_are_ski_disciplines",
+ "Migration20200101046140_tours_are_on_for_every_install",
+ "Migration20200101046150_a_ski_tour_keeps_its_piste_router_options",
+ "Migration20200101046160_a_journey_keeps_its_recorded_days",
+ "Migration20200101046170_a_journey_entry_remembers_the_stays_it_came_from",
+ "Migration20200101046180_journeys_can_prepare_recorded_days",
+ "Migration20200101046190_a_journey_share_can_show_its_plan_and_recordings",
+ "Migration20200101046200_a_tour_draft_lives_on_the_account",
+ "Migration20200101046200_a_user_can_connect_an_ai_provider",
+ "Migration20200101046210_the_trip_assistant_remembers_its_conversations",
+ "Migration20200101046220_a_chat_message_can_say_it_came_from_the_assistant",
+ "Migration20200101046230_an_admin_can_share_a_chatgpt_plan",
+ "Migration20200101046240_a_photo_keeps_its_quality_signals",
+ "Migration20200101046250_an_assistant_conversation_can_be_about_a_journey",
+ "Migration20200101046260_a_book_keeps_its_last_revisions",
+ "Migration20200101046270_a_photo_knows_its_colours_and_calm_areas",
+ "Migration20200101046280_an_assistant_conversation_can_be_about_no_one_trip",
+ "Migration20200101046290_a_removed_member_cannot_rejoin_by_invite_link",
+ "Migration20200101046300_an_attached_file_stays_with_its_conversation",
+ "Migration20200101046310_an_admin_can_run_a_plugin_outside_its_trek_range",
+ "Migration20200101046320_a_place_knows_where_its_details_came_from",
+ "Migration20200101046330_an_object_no_row_names_is_marked_before_it_goes",
+ "Migration20200101046340_a_released_object_outlasts_every_undo_before_it_goes",
+ "Migration20200101046350_a_place_lies_on_the_real_world",
+ "Migration20200101046360_a_new_install_starts_with_more_addons_on"
+ ]
+}
diff --git a/server/assets/postgres/baseline.sql b/server/assets/postgres/baseline.sql
new file mode 100644
index 000000000..97eabc300
--- /dev/null
+++ b/server/assets/postgres/baseline.sql
@@ -0,0 +1,825 @@
+-- The schema a fresh Postgres database starts from. Generated by `npm run gen:pg-baseline`; do not edit.
+CREATE COLLATION IF NOT EXISTS nocase (provider = icu, locale = 'und-u-ks-level2', deterministic = false);
+create table "addons" ("id" text null, "name" text not null, "description" text null, "type" text not null default 'global', "icon" text null default 'Puzzle', "enabled" boolean null default false, "config" jsonb null default '{}', "sort_order" bigint null default 0, primary key ("id"));
+create table "app_settings" ("key" text null, "value" text null, primary key ("key"));
+create table "document_providers" ("id" text null, "name" text not null, "description" text null, "icon" text null default 'FileText', "enabled" bigint null default 0, "sort_order" bigint null default 0, primary key ("id"));
+create table "document_provider_fields" ("id" bigserial primary key, "provider_id" text not null, "field_key" text not null, "label" text not null, "input_type" text not null default 'text', "placeholder" text null, "hint" text null, "required" bigint null default 0, "secret" bigint null default 0, "sort_order" bigint null default 0);
+alter table "document_provider_fields" add constraint "document_provider_fields_provider_id_field_key_unique" unique ("provider_id", "field_key");
+create table "ferrata_tiles" ("tile_key" text not null, "payload" text not null, "fetched_at" bigint not null, primary key ("tile_key"));
+create table "google_api_usage" ("day" text null, "calls" bigint not null default 0, primary key ("day"));
+create table "google_place_photo_meta" ("place_id" text null, "attribution" text null, "fetched_at" bigint not null, "error_at" bigint null, primary key ("place_id"));
+create table "migrations" ("id" bigserial primary key, "timestamp" bigint not null, "name" varchar(255) not null);
+create table "paddle_region_requests" ("region_id" text not null, "name" text not null, "reason" text not null, "bytes" bigint null, "requested_by" text null, "requested_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "declined_at" text null, primary key ("region_id"));
+alter table "paddle_region_requests" add constraint "paddle_region_requests_reason_check" check ("reason" in ('ask', 'too_large', 'limit'));
+create table "paddle_regions" ("id" text not null, "name" text not null, "boundary" text null, "min_lat" double precision null, "min_lng" double precision null, "max_lat" double precision null, "max_lng" double precision null, "status" text not null default 'queued', "source_timestamp" text null, "imported_at" text null, "feature_count" bigint not null default 0, "error" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "origin" text not null default 'admin', primary key ("id"));
+alter table "paddle_regions" add constraint "paddle_regions_status_check" check ("status" in ('queued', 'importing', 'ready', 'failed'));
+alter table "paddle_regions" add constraint "paddle_regions_origin_check" check ("origin" in ('admin', 'auto'));
+create table "paddle_features" ("id" bigserial primary key, "region_id" text not null, "osm_ref" text not null, "kind" text not null, "group_key" text null, "name" text null, "geometry" text not null, "tags" text not null, "tag_hash" text not null, "min_lat" double precision not null, "min_lng" double precision not null, "max_lat" double precision not null, "max_lng" double precision not null);
+create index "idx_paddle_features_box" on "paddle_features" ("min_lat", "max_lat", "min_lng", "max_lng");
+alter table "paddle_features" add constraint "idx_paddle_features_region_ref" unique ("region_id", "osm_ref");
+create table "paddle_water_graphs" ("region_id" text null, "file" text not null, "rev" text not null, "bytes" bigint not null, "built_at" text not null, "stats" text not null, "min_lat" double precision not null, "min_lng" double precision not null, "max_lat" double precision not null, "max_lng" double precision not null, primary key ("region_id"));
+create index "idx_paddle_water_graphs_box" on "paddle_water_graphs" ("min_lat", "max_lat", "min_lng", "max_lng");
+alter table "paddle_water_graphs" add constraint "paddle_water_graphs_region_id_unique" unique ("region_id");
+create table "photo_providers" ("id" text null, "name" text not null, "description" text null, "icon" text null default 'Image', "enabled" bigint null default 0, "sort_order" bigint null default 0, primary key ("id"));
+create table "photo_provider_fields" ("id" bigserial primary key, "provider_id" text not null, "field_key" text not null, "label" text not null, "input_type" text not null default 'text', "placeholder" text null, "hint" text null, "required" bigint null default 0, "secret" bigint null default 0, "settings_key" text null, "payload_key" text null, "sort_order" bigint null default 0);
+alter table "photo_provider_fields" add constraint "photo_provider_fields_provider_id_field_key_unique" unique ("provider_id", "field_key");
+create table "place_contrib_log" ("id" bigserial primary key, "day" text not null, "contrib_key" text not null, "place_name" text not null, "field" text not null, "value_summary" text null, "result" text not null, "reason" text null);
+create index "idx_place_contrib_log_day" on "place_contrib_log" ("day");
+create table "place_details_cache" ("place_id" text not null, "lang" text not null default '', "expanded" bigint not null default 0, "payload_json" text not null, "fetched_at" bigint not null, primary key ("place_id", "lang", "expanded"));
+create table "place_shadow_picks" ("id" bigserial primary key, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "query" text not null, "lang" text null, "bias_lat" double precision null, "bias_lng" double precision null, "source" text not null, "live_rank" bigint not null, "live_count" bigint not null, "picked_name" text not null, "picked_lat" double precision not null, "picked_lng" double precision not null, "picked_place_id" text null);
+create index "idx_place_shadow_created" on "place_shadow_picks" ("created_at");
+create table "plugin_actions" ("plugin_id" text not null, "action_key" text not null, "label" text not null, "hint" text null, "danger" bigint not null default 0, "sort_order" bigint not null default 0, "scope" text not null default 'user', primary key ("plugin_id", "action_key"));
+create table "plugin_capability_audit" ("id" bigserial primary key, "plugin_id" text not null, "acting_user_id" bigint null, "method" text not null, "resource" text null, "code" text not null, "ts" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "prev_hash" text null, "hash" text not null);
+create index "idx_plugin_audit_plugin" on "plugin_capability_audit" ("plugin_id", "id");
+create table "plugin_egress_hosts" ("id" bigserial primary key, "plugin_id" text not null, "host" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_plugin_egress_hosts_plugin" on "plugin_egress_hosts" ("plugin_id");
+alter table "plugin_egress_hosts" add constraint "plugin_egress_hosts_plugin_id_host_unique" unique ("plugin_id", "host");
+create table "plugin_entity_metadata" ("id" bigserial primary key, "plugin_id" text not null, "entity_type" text not null, "entity_id" bigint not null, "key" text not null, "value" text null, "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_plugin_meta_entity" on "plugin_entity_metadata" ("plugin_id", "entity_type", "entity_id");
+alter table "plugin_entity_metadata" add constraint "plugin_entity_metadata_plugin_id_entity_type_enti_ee00f_unique" unique ("plugin_id", "entity_type", "entity_id", "key");
+create table "plugin_error_log" ("id" bigserial primary key, "plugin_id" text not null, "ts" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "level" text not null default 'error', "message" text null, "stack" text null);
+create index "idx_plugin_error_log_plugin" on "plugin_error_log" ("plugin_id", "ts");
+create table "plugin_meta_migrations" ("plugin_id" text not null, "migration_id" text not null, "applied_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("plugin_id", "migration_id"));
+create table "plugin_oauth_state" ("state" text null, "plugin_id" text not null, "user_id" bigint not null, "verifier" text not null, "created_at" bigint not null, primary key ("state"));
+create table "plugin_oauth_tokens" ("plugin_id" text not null, "user_id" bigint not null, "access_token" text null, "refresh_token" text null, "expires_at" bigint null, "scope" text null, "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("plugin_id", "user_id"));
+create table "plugins" ("id" text null, "name" text not null, "description" text null, "type" text not null default 'integration', "icon" text null default 'Blocks', "version" text null, "api_version" bigint null default 1, "min_trek_version" text null, "permissions" text null default '[]', "granted_permissions" text null default '[]', "status" text not null default 'inactive', "config" text null default '{}', "source_repo" text null, "source_commit" text null, "sha256" text null, "crash_count" bigint not null default 0, "last_error" text null, "reviewed_at" text null, "sort_order" bigint not null default 0, "installed_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "enabled" bigint not null default 0, "capabilities" text not null default '{}', "author_pubkey" text null, "dependencies" text not null default '{}', "operator_egress" bigint not null default 0, "update_block_code" text null, "update_block_detail" text null, "update_block_version" text null, "trek_range" text null, "update_hold" bigint not null default 0, "trek_range_accepted_version" text null, "trek_range_accepted_host" text null, primary key ("id"));
+create table "plugin_scheduled_tasks" ("id" bigserial primary key, "plugin_id" text not null, "name" text not null, "due_at" bigint not null, "payload" text not null default 'null', "every_ms" bigint null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_plugin_sched_due" on "plugin_scheduled_tasks" ("due_at");
+alter table "plugin_scheduled_tasks" add constraint "plugin_scheduled_tasks_plugin_id_name_unique" unique ("plugin_id", "name");
+create table "plugin_settings_fields" ("id" bigserial primary key, "plugin_id" text not null, "field_key" text not null, "label" text null, "input_type" text not null default 'text', "placeholder" text null, "hint" text null, "required" bigint not null default 0, "secret" bigint not null default 0, "scope" text not null default 'instance', "options" text null, "oauth_config" text null, "sort_order" bigint not null default 0, "default_value" text null);
+alter table "plugin_settings_fields" add constraint "plugin_settings_fields_plugin_id_field_key_unique" unique ("plugin_id", "field_key");
+create table "plugin_user_config" ("plugin_id" text not null, "user_id" bigint not null, "config" text not null default '{}', "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("plugin_id", "user_id"));
+create table "plugin_user_erasure_queue" ("id" bigserial primary key, "plugin_id" text not null, "user_id" bigint not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_plugin_erasure_plugin" on "plugin_user_erasure_queue" ("plugin_id");
+alter table "plugin_user_erasure_queue" add constraint "plugin_user_erasure_queue_plugin_id_user_id_unique" unique ("plugin_id", "user_id");
+create table "route_usage_daily" ("day" text not null, "profile" text not null, "surface" text not null, "self_hosted" bigint not null, "requests" bigint not null default 0, "waypoints" bigint not null default 0, "km" double precision not null default 0, "failed" bigint not null default 0, primary key ("day", "profile", "surface", "self_hosted"));
+create index "idx_route_usage_day" on "route_usage_daily" ("day");
+create table "scheduler_leases" ("name" text not null, "owner" text not null, "expires_at" bigint not null, primary key ("name"));
+create table "schema_version" ("id" bigserial primary key, "version" bigint not null);
+create table "school_holiday_countries" ("code" text null, "name" text not null, primary key ("code"));
+create table "school_holiday_regions" ("id" bigserial primary key, "name" text collate "nocase" not null, "revision" bigint not null default 1, "country" text not null);
+alter table "school_holiday_regions" add constraint "school_holiday_regions_country_name_unique" unique ("country", "name");
+create table "school_holiday_periods" ("id" bigserial primary key, "region_id" bigint not null, "name" text not null, "start_date" text not null, "end_date" text not null);
+create index "idx_school_holiday_periods_region" on "school_holiday_periods" ("region_id");
+alter table "school_holiday_periods" add constraint "school_holiday_periods_end_date_check" check (end_date >= start_date);
+create table "storage_reclaim_marks" ("category" text not null, "key" text not null, "first_seen_at" text not null, "size" bigint not null, primary key ("category", "key"));
+create table "storage_release_queue" ("category" text not null, "key" text not null, "due_at" text not null, primary key ("category", "key"));
+create table "tour_types" ("key" text null, "label_key" text not null, "icon" text not null, "color" text not null, "routing_profile" text null, "is_sport" bigint not null default 1, "enabled" bigint not null default 1, "sort_order" bigint not null default 0, primary key ("key"));
+create table "trek_photo_cache_meta" ("cache_key" text not null, "content_type" text not null default 'image/jpeg', "fetched_at" bigint not null, primary key ("cache_key"));
+create index "idx_trek_photo_cache_meta_fetched_at" on "trek_photo_cache_meta" ("fetched_at");
+create table "users" ("id" bigserial primary key, "username" text not null, "email" text not null, "password_hash" text not null, "role" text not null default 'user', "maps_api_key" text null, "unsplash_api_key" text null, "amap_api_key" text null, "openweather_api_key" text null, "avatar" text null, "oidc_sub" text null, "oidc_issuer" text null, "last_login" text null, "mfa_enabled" bigint null default 0, "mfa_secret" text null, "mfa_backup_codes" text null, "immich_url" text null, "immich_access_token" text null, "synology_url" text null, "synology_username" text null, "synology_password" text null, "synology_sid" text null, "must_change_password" bigint null default 0, "password_version" bigint not null default 0, "feed_token" text null, "is_guest" bigint not null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "immich_api_key" text null, "synology_skip_ssl" bigint not null default 0, "synology_did" text null, "first_seen_version" text not null default '0.0.0', "login_count" bigint not null default 0, "immich_auto_upload" bigint not null default 0, "airtrail_url" text null, "airtrail_api_key" text null, "airtrail_allow_insecure_tls" bigint null default 0, "airtrail_write_enabled" bigint null default 0, "display_name" text null, "immich_allow_insecure_tls" bigint not null default 0, "email_self_asserted" bigint not null default 0);
+create index "idx_users_email" on "users" ("email");
+create unique index "idx_users_feed_token" on "users" ("feed_token") where feed_token IS NOT NULL;
+alter table "users" add constraint "users_email_unique" unique ("email");
+alter table "users" add constraint "users_username_unique" unique ("username");
+CREATE UNIQUE INDEX idx_users_email_lower ON users (lower(email));
+create table "user_notice_dismissals" ("user_id" bigint not null, "notice_id" text not null, "dismissed_at" bigint not null, "dismissed_app_version" text null, primary key ("user_id", "notice_id"));
+alter table "user_notice_dismissals" add constraint "user_notice_dismissals_user_id_notice_id_unique" unique ("user_id", "notice_id");
+create table "trips" ("id" bigserial primary key, "user_id" bigint not null, "title" text not null, "description" text null, "start_date" text null, "end_date" text null, "currency" text null default 'EUR', "cover_image" text null, "is_archived" bigint null default 0, "reminder_days" bigint null default 3, "feed_token" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "reminder_sent_for" text null);
+create index "idx_trips_user_id" on "trips" ("user_id");
+create index "idx_trips_created_at" on "trips" ("created_at");
+create unique index "idx_trips_feed_token" on "trips" ("feed_token") where feed_token IS NOT NULL;
+create table "roadtrip_preferences" ("trip_id" bigint not null, "key" text not null, "value" text not null, primary key ("trip_id", "key"));
+alter table "roadtrip_preferences" add constraint "roadtrip_preferences_trip_id_key_unique" unique ("trip_id", "key");
+create table "days" ("id" bigserial primary key, "trip_id" bigint not null, "day_number" bigint not null, "date" text null, "notes" text null, "title" text null, "default_transport_mode" text null);
+create index "idx_days_trip_id" on "days" ("trip_id");
+alter table "days" add constraint "days_trip_id_day_number_unique" unique ("trip_id", "day_number");
+create table "roadtrip_vias" ("id" bigserial primary key, "day_id" bigint not null, "after_order_index" bigint not null, "sequence" bigint not null default 0, "lat" double precision not null, "lng" double precision not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_roadtrip_vias_day" on "roadtrip_vias" ("day_id", "after_order_index", "sequence");
+create table "day_notes" ("id" bigserial primary key, "day_id" bigint not null, "trip_id" bigint not null, "text" text not null, "time" text null, "icon" text null default '📝', "sort_order" double precision null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "color" text null);
+create index "idx_day_notes_day_id" on "day_notes" ("day_id");
+create index "idx_day_notes_trip_id" on "day_notes" ("trip_id");
+create table "budget_category_order" ("trip_id" bigint not null, "category" text not null, "sort_order" bigint not null default 0, primary key ("trip_id", "category"));
+alter table "budget_category_order" add constraint "budget_category_order_trip_id_category_unique" unique ("trip_id", "category");
+create table "trip_members" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "added_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "invited_by" bigint null);
+create index "idx_trip_members_trip_id" on "trip_members" ("trip_id");
+create index "idx_trip_members_user_id" on "trip_members" ("user_id");
+create index "idx_trip_members_invited_by" on "trip_members" ("invited_by");
+alter table "trip_members" add constraint "trip_members_trip_id_user_id_unique" unique ("trip_id", "user_id");
+create table "trip_member_removals" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "removed_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_trip_member_removals_user" on "trip_member_removals" ("user_id");
+alter table "trip_member_removals" add constraint "trip_member_removals_trip_id_user_id_unique" unique ("trip_id", "user_id");
+create table "trip_invite_tokens" ("id" bigserial primary key, "trip_id" bigint not null, "token" text not null, "expires_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "created_by" bigint null);
+create index "idx_trip_invite_tokens_token" on "trip_invite_tokens" ("token");
+create index "idx_trip_invite_tokens_created_by" on "trip_invite_tokens" ("created_by");
+alter table "trip_invite_tokens" add constraint "trip_invite_tokens_token_unique" unique ("token");
+alter table "trip_invite_tokens" add constraint "trip_invite_tokens_trip_id_unique" unique ("trip_id");
+create table "trip_album_links" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "provider" text not null, "album_id" text not null, "album_name" text not null default '', "sync_enabled" bigint not null default 1, "last_synced_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "passphrase" text null default NULL);
+create index "idx_trip_album_links_trip" on "trip_album_links" ("trip_id");
+create index "idx_trip_album_links_user_id" on "trip_album_links" ("user_id");
+alter table "trip_album_links" add constraint "trip_album_links_trip_id_user_id_provider_album_id_unique" unique ("trip_id", "user_id", "provider", "album_id");
+create table "trek_photos" ("id" bigserial primary key, "provider" text not null, "asset_id" text null, "owner_id" bigint null, "file_path" text null, "thumbnail_path" text null, "width" bigint null, "height" bigint null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "passphrase" text null default NULL, "media_type" text not null default 'image', "duration_ms" bigint null, "taken_at" text null, "lat" double precision null, "lng" double precision null);
+create index "idx_trek_photos_owner" on "trek_photos" ("owner_id");
+create index "idx_trek_photos_geo" on "trek_photos" ("lat", "lng") where lat IS NOT NULL AND lng IS NOT NULL;
+create unique index "idx_trek_photos_provider_asset" on "trek_photos" ("provider", "asset_id", "owner_id") where asset_id IS NOT NULL;
+create table "trip_photos" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "photo_id" bigint not null, "shared" bigint not null default 1, "album_link_id" bigint null, "added_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_trip_photos_trip" on "trip_photos" ("trip_id");
+create index "idx_trip_photos_user_id" on "trip_photos" ("user_id");
+create index "idx_trip_photos_photo" on "trip_photos" ("photo_id");
+create index "idx_trip_photos_album_link_id" on "trip_photos" ("album_link_id");
+alter table "trip_photos" add constraint "trip_photos_trip_id_user_id_photo_id_unique" unique ("trip_id", "user_id", "photo_id");
+create table "trek_photo_signals" ("id" bigserial primary key, "photo_id" bigint not null, "algo" bigint not null, "phash" text null, "sharpness" double precision null, "luma" double precision null, "clipped" double precision null, "orig_width" bigint null, "orig_height" bigint null, "computed_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "palette" text null, "busy" text null, "sal_x" double precision null, "sal_y" double precision null);
+alter table "trek_photo_signals" add constraint "trek_photo_signals_photo_id_unique" unique ("photo_id");
+create table "tour_drafts" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "payload" text not null, "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_tour_drafts_user" on "tour_drafts" ("user_id");
+alter table "tour_drafts" add constraint "tour_drafts_trip_id_user_id_unique" unique ("trip_id", "user_id");
+create table "todo_items" ("id" bigserial primary key, "trip_id" bigint not null, "name" text not null, "checked" bigint null default 0, "category" text null, "sort_order" bigint null default 0, "due_date" text null, "description" text null, "assigned_user_id" bigint null, "priority" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "reminded_at" text null);
+create index "idx_todo_items_trip_id" on "todo_items" ("trip_id");
+create index "idx_todo_items_assigned_user_id" on "todo_items" ("assigned_user_id");
+create table "todo_category_assignees" ("id" bigserial primary key, "trip_id" bigint not null, "category_name" text not null, "user_id" bigint not null);
+create index "idx_todo_category_assignees_user_id" on "todo_category_assignees" ("user_id");
+alter table "todo_category_assignees" add constraint "todo_category_assignees_trip_id_category_name_user_id_unique" unique ("trip_id", "category_name", "user_id");
+create table "tags" ("id" bigserial primary key, "user_id" bigint not null, "name" text not null, "color" text null default '#10b981', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_tags_user_id" on "tags" ("user_id");
+create table "share_tokens" ("id" bigserial primary key, "trip_id" bigint not null, "token" text not null, "share_map" bigint null default 1, "share_bookings" bigint null default 1, "share_packing" bigint null default 0, "share_budget" bigint null default 0, "share_collab" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "expires_at" text null, "share_travel_only" bigint not null default 0, "share_hide_images" bigint not null default 0, "created_by" bigint not null);
+create index "idx_share_tokens_trip_id" on "share_tokens" ("trip_id");
+create index "idx_share_tokens_token" on "share_tokens" ("token");
+create index "idx_share_tokens_created_by" on "share_tokens" ("created_by");
+alter table "share_tokens" add constraint "share_tokens_token_unique" unique ("token");
+create table "settings" ("id" bigserial primary key, "user_id" bigint not null, "key" text not null, "value" text null);
+alter table "settings" add constraint "settings_user_id_key_unique" unique ("user_id", "key");
+create table "push_subscriptions" ("id" bigserial primary key, "user_id" bigint not null, "endpoint" text not null, "p256dh" text not null, "auth" text not null, "vapid_public_key" text not null, "user_agent" text null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_success_at" text null, "failure_count" bigint not null default 0);
+create index "idx_push_subscriptions_user" on "push_subscriptions" ("user_id");
+alter table "push_subscriptions" add constraint "push_subscriptions_endpoint_unique" unique ("endpoint");
+create table "password_reset_tokens" ("id" bigserial primary key, "user_id" bigint not null, "token_hash" text not null, "expires_at" text not null, "consumed_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "created_ip" text null);
+create index "idx_prt_user" on "password_reset_tokens" ("user_id");
+create index "idx_prt_hash" on "password_reset_tokens" ("token_hash");
+alter table "password_reset_tokens" add constraint "password_reset_tokens_token_hash_unique" unique ("token_hash");
+create table "packing_templates" ("id" bigserial primary key, "name" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "created_by" bigint not null);
+create index "idx_packing_templates_created_by" on "packing_templates" ("created_by");
+create table "packing_template_categories" ("id" bigserial primary key, "template_id" bigint not null, "name" text not null, "sort_order" bigint not null default 0);
+create index "idx_packing_template_categories_template_id" on "packing_template_categories" ("template_id");
+create table "packing_template_items" ("id" bigserial primary key, "category_id" bigint not null, "name" text not null, "sort_order" bigint not null default 0, "weight_grams" bigint null, "quantity" bigint not null default 1, "bag_name" text null);
+create index "idx_packing_template_items_category_id" on "packing_template_items" ("category_id");
+create table "packing_category_assignees" ("id" bigserial primary key, "trip_id" bigint not null, "category_name" text not null, "user_id" bigint not null);
+create index "idx_packing_category_assignees_user_id" on "packing_category_assignees" ("user_id");
+alter table "packing_category_assignees" add constraint "packing_category_assignees_trip_id_category_name_user_id_unique" unique ("trip_id", "category_name", "user_id");
+create table "packing_bags" ("id" bigserial primary key, "trip_id" bigint not null, "name" text not null, "color" text not null default '#6366f1', "weight_limit_grams" bigint null, "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "user_id" bigint null default NULL);
+create index "idx_packing_bags_trip_id" on "packing_bags" ("trip_id");
+create index "idx_packing_bags_user_id" on "packing_bags" ("user_id");
+create table "packing_items" ("id" bigserial primary key, "trip_id" bigint not null, "name" text not null, "checked" bigint null default 0, "category" text null, "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "weight_grams" bigint null, "bag_id" bigint null, "quantity" bigint not null default 1, "updated_at" text null, "is_private" bigint not null default 0, "owner_id" bigint null, "packed_quantity" bigint null);
+create index "idx_packing_items_trip_id" on "packing_items" ("trip_id");
+create index "idx_packing_items_bag_id" on "packing_items" ("bag_id");
+create index "idx_packing_items_owner_id" on "packing_items" ("owner_id");
+create table "packing_item_contributors" ("item_id" bigint not null, "user_id" bigint not null, "status" text not null default 'accepted', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("item_id", "user_id"));
+create index "idx_packing_item_contributors_user_id" on "packing_item_contributors" ("user_id");
+alter table "packing_item_contributors" add constraint "packing_item_contributors_item_id_user_id_unique" unique ("item_id", "user_id");
+create table "packing_item_recipients" ("item_id" bigint not null, "user_id" bigint not null, primary key ("item_id", "user_id"));
+create table "packing_bag_members" ("bag_id" bigint not null, "user_id" bigint not null, primary key ("bag_id", "user_id"));
+create table "oauth_clients" ("id" text null, "user_id" bigint null, "name" text not null, "client_id" text not null, "client_secret_hash" text not null, "redirect_uris" text not null default '[]', "allowed_scopes" text not null default '[]', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "is_public" bigint not null default 0, "created_via" text not null default 'settings_ui', "allows_client_credentials" bigint not null default 0, primary key ("id"));
+create index "idx_oauth_clients_user" on "oauth_clients" ("user_id");
+alter table "oauth_clients" add constraint "idx_oauth_clients_client_id" unique ("client_id");
+alter table "oauth_clients" add constraint "oauth_clients_client_id_unique" unique ("client_id");
+create table "oauth_tokens" ("id" bigserial primary key, "client_id" text not null, "user_id" bigint not null, "access_token_hash" text not null, "refresh_token_hash" text not null, "scopes" text not null default '[]', "access_token_expires_at" text not null, "refresh_token_expires_at" text not null, "revoked_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "parent_token_id" bigint null, "audience" text null);
+create index "idx_oauth_tokens_client_id" on "oauth_tokens" ("client_id");
+create index "idx_oauth_tokens_user" on "oauth_tokens" ("user_id");
+alter table "oauth_tokens" add constraint "idx_oauth_tokens_access" unique ("access_token_hash");
+alter table "oauth_tokens" add constraint "idx_oauth_tokens_refresh" unique ("refresh_token_hash");
+create index "idx_oauth_tokens_parent" on "oauth_tokens" ("parent_token_id");
+alter table "oauth_tokens" add constraint "oauth_tokens_refresh_token_hash_unique" unique ("refresh_token_hash");
+alter table "oauth_tokens" add constraint "oauth_tokens_access_token_hash_unique" unique ("access_token_hash");
+create table "oauth_consents" ("id" bigserial primary key, "client_id" text not null, "user_id" bigint not null, "scopes" text not null default '[]', "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_oauth_consents_user_id" on "oauth_consents" ("user_id");
+alter table "oauth_consents" add constraint "oauth_consents_client_id_user_id_unique" unique ("client_id", "user_id");
+create table "notifications" ("id" bigserial primary key, "type" text not null, "scope" text not null, "target" bigint not null, "sender_id" bigint null, "recipient_id" bigint not null, "title_key" text not null, "title_params" text null default '{}', "text_key" text not null, "text_params" text null default '{}', "positive_text_key" text null, "negative_text_key" text null, "positive_callback" text null, "negative_callback" text null, "response" text null, "navigate_text_key" text null, "navigate_target" text null, "is_read" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_notifications_sender_id" on "notifications" ("sender_id");
+create index "idx_notifications_target_scope" on "notifications" ("target", "scope");
+create index "idx_notifications_recipient_created" on "notifications" ("recipient_id", "created_at");
+create index "idx_notifications_recipient" on "notifications" ("recipient_id", "is_read", "created_at");
+create table "notification_channel_preferences" ("user_id" bigint not null, "event_type" text not null, "channel" text not null, "enabled" bigint not null default 1, primary key ("user_id", "event_type", "channel"));
+create index "idx_ncp_user" on "notification_channel_preferences" ("user_id");
+alter table "notification_channel_preferences" add constraint "notification_channel_preferences_user_id_event_ty_8d673_unique" unique ("user_id", "event_type", "channel");
+create table "mcp_tokens" ("id" bigserial primary key, "user_id" bigint not null, "name" text not null, "token_hash" text not null, "token_prefix" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_used_at" text null, "kind" text not null default 'mcp', "scope_mode" text not null default 'all', "api_scopes" text null);
+create index "idx_mcp_tokens_user_id" on "mcp_tokens" ("user_id");
+alter table "mcp_tokens" add constraint "idx_mcp_tokens_hash" unique ("token_hash");
+create table "journeys" ("id" bigserial primary key, "user_id" bigint not null, "title" text not null, "subtitle" text null, "cover_gradient" text null, "status" text null default 'draft', "created_at" bigint not null, "updated_at" bigint not null, "cover_image" text null, "show_trip_tracks" bigint not null default 0, "show_verdict" bigint not null default 1, "show_mood" bigint not null default 1, "show_weather" bigint not null default 1, "status_override" text null, "photo_location" bigint not null default 0, "auto_prepare_recordings" bigint not null default 0);
+create index "idx_journeys_user" on "journeys" ("user_id");
+create table "trip_journal_links" ("id" bigserial primary key, "trip_id" bigint not null, "journey_id" bigint not null, "created_at" bigint not null, "linked_by" bigint null);
+create index "idx_trip_journal_links_journey" on "trip_journal_links" ("journey_id");
+create index "idx_trip_journal_links_linked_by" on "trip_journal_links" ("linked_by");
+alter table "trip_journal_links" add constraint "trip_journal_links_trip_id_unique" unique ("trip_id");
+create table "trip_journal_link_members" ("link_id" bigint not null, "user_id" bigint not null, "role" text not null default 'viewer', "hide_skeletons" bigint not null default 0, "added_at" bigint not null, primary key ("link_id", "user_id"));
+create index "idx_trip_journal_link_members_user" on "trip_journal_link_members" ("user_id");
+alter table "trip_journal_link_members" add constraint "trip_journal_link_members_link_id_user_id_unique" unique ("link_id", "user_id");
+alter table "trip_journal_link_members" add constraint "trip_journal_link_members_role_check" check ("role" in ('viewer', 'editor'));
+create table "journey_trips" ("journey_id" bigint not null, "trip_id" bigint not null, "added_at" bigint not null, primary key ("journey_id", "trip_id"));
+create index "idx_journey_trips_journey" on "journey_trips" ("journey_id");
+create index "idx_journey_trips_trip_id" on "journey_trips" ("trip_id");
+alter table "journey_trips" add constraint "journey_trips_journey_id_trip_id_unique" unique ("journey_id", "trip_id");
+create table "journey_share_tokens" ("id" bigserial primary key, "journey_id" bigint not null, "token" text not null, "share_timeline" bigint null default 1, "share_gallery" bigint null default 1, "share_map" bigint null default 1, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "newest_first" bigint not null default 0, "share_plan" bigint not null default 0, "share_recordings" bigint not null default 0, "created_by" bigint not null);
+alter table "journey_share_tokens" add constraint "idx_journey_share_journey" unique ("journey_id");
+create index "idx_journey_share_tokens_created_by" on "journey_share_tokens" ("created_by");
+alter table "journey_share_tokens" add constraint "journey_share_tokens_token_unique" unique ("token");
+create table "journey_photos" ("id" bigserial primary key, "journey_id" bigint not null, "photo_id" bigint not null, "caption" text null, "shared" bigint null default 0, "sort_order" bigint null default 0, "provider" text null, "asset_id" text null, "owner_id" bigint null, "created_at" bigint not null);
+create index "idx_journey_photos_journey" on "journey_photos" ("journey_id");
+create index "idx_journey_photos_photo_id" on "journey_photos" ("photo_id");
+alter table "journey_photos" add constraint "journey_photos_journey_id_photo_id_unique" unique ("journey_id", "photo_id");
+create table "journey_book_revisions" ("id" bigserial primary key, "journey_id" bigint not null, "version" bigint not null, "document" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_journey_book_revisions_journey" on "journey_book_revisions" ("journey_id");
+create table "journey_recording_prep" ("id" bigserial primary key, "journey_id" bigint not null, "user_id" bigint not null, "last_attempt_at" text null, "last_success_at" text null, "last_error" text null);
+create index "idx_journey_recording_prep_user" on "journey_recording_prep" ("user_id");
+alter table "journey_recording_prep" add constraint "idx_journey_recording_prep_person" unique ("journey_id", "user_id");
+create table "journey_recorded_days" ("id" bigserial primary key, "journey_id" bigint not null, "local_date" text not null, "source" text not null default 'dawarich', "source_kind" text not null, "utc_offset_minutes" bigint not null, "window_from" text not null, "window_to" text not null, "segments" text not null, "point_count" bigint not null, "distance_m" double precision null, "content_hash" text not null, "truncated" bigint not null default 0, "fetched_at" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "imported_by" bigint null);
+create index "idx_journey_recorded_days_imported_by" on "journey_recorded_days" ("imported_by");
+alter table "journey_recorded_days" add constraint "idx_journey_recorded_days_day" unique ("journey_id", "local_date", "source");
+create table "journey_contributors" ("journey_id" bigint not null, "user_id" bigint not null, "role" text not null, "added_at" bigint not null, "hide_skeletons" bigint not null default 0, primary key ("journey_id", "user_id"));
+create index "idx_journey_contributors_user" on "journey_contributors" ("user_id");
+alter table "journey_contributors" add constraint "journey_contributors_journey_id_user_id_unique" unique ("journey_id", "user_id");
+create table "journey_books" ("id" bigserial primary key, "journey_id" bigint not null, "title" text not null default '', "document" text not null, "version" bigint not null default 1, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_by" bigint null, "created_by" bigint null);
+create index "idx_journey_books_journey" on "journey_books" ("journey_id");
+create index "idx_journey_books_updated_by" on "journey_books" ("updated_by");
+create index "idx_journey_books_created_by" on "journey_books" ("created_by");
+create table "invite_tokens" ("id" bigserial primary key, "token" text not null, "max_uses" bigint not null default 1, "used_count" bigint not null default 0, "expires_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "trip_id" bigint null, "created_by" bigint not null);
+create index "idx_invite_tokens_trip_id" on "invite_tokens" ("trip_id");
+create index "idx_invite_tokens_created_by" on "invite_tokens" ("created_by");
+alter table "invite_tokens" add constraint "invite_tokens_token_unique" unique ("token");
+create table "idempotency_keys" ("key" text not null, "user_id" bigint not null, "method" text not null, "path" text not null, "status_code" bigint not null, "response_body" text not null, "created_at" bigint not null default CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint), primary key ("key", "user_id", "method", "path"));
+create index "idx_idempotency_keys_user_id" on "idempotency_keys" ("user_id");
+create index "idx_idempotency_keys_created" on "idempotency_keys" ("created_at");
+alter table "idempotency_keys" add constraint "idempotency_keys_key_user_id_method_path_unique" unique ("key", "user_id", "method", "path");
+create table "hidden_regions" ("id" bigserial primary key, "user_id" bigint not null, "region_code" text not null, "country_code" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_hidden_regions_user" on "hidden_regions" ("user_id");
+alter table "hidden_regions" add constraint "hidden_regions_user_id_region_code_unique" unique ("user_id", "region_code");
+create table "hidden_countries" ("id" bigserial primary key, "user_id" bigint not null, "country_code" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_hidden_countries_user" on "hidden_countries" ("user_id");
+alter table "hidden_countries" add constraint "hidden_countries_user_id_country_code_unique" unique ("user_id", "country_code");
+create table "document_connections" ("id" bigserial primary key, "trip_id" bigint not null, "provider_id" text not null, "owner_user_id" bigint not null, "base_url" text not null, "secrets" text null, "settings" text not null default '{}', "allow_insecure_tls" bigint not null default 0, "capabilities" text null, "last_probe_at" text null, "last_probe_state" text not null default 'never', "last_probe_error" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_document_connections_trip" on "document_connections" ("trip_id");
+create index "idx_document_connections_provider_id" on "document_connections" ("provider_id");
+create index "idx_document_connections_owner" on "document_connections" ("owner_user_id");
+alter table "document_connections" add constraint "document_connections_trip_id_provider_id_unique" unique ("trip_id", "provider_id");
+create table "trip_document_links" ("id" bigserial primary key, "trip_id" bigint not null, "connection_id" bigint not null, "provider_id" text not null, "remote_scope_key" text not null, "remote_root_id" text null, "remote_root_path" text null, "remote_label" text not null default '', "direction" text not null default 'both', "delete_policy" text not null default 'unlink', "conflict_policy" text not null default 'manual', "sync_enabled" bigint not null default 1, "webhook_token" text null, "webhook_secret" text null, "webhook_subscription_id" text null, "remote_cursor" text null, "last_sync_at" text null, "last_sync_state" text not null default 'never', "last_sync_error" text null, "failure_count" bigint not null default 0, "next_attempt_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "created_by" bigint null);
+create index "idx_trip_document_links_trip" on "trip_document_links" ("trip_id");
+create index "idx_trip_document_links_connection_id" on "trip_document_links" ("connection_id");
+create index "idx_trip_document_links_created_by" on "trip_document_links" ("created_by");
+create index "idx_trip_document_links_due" on "trip_document_links" ("sync_enabled", "next_attempt_at");
+create unique index "idx_trip_document_links_token" on "trip_document_links" ("webhook_token") where webhook_token IS NOT NULL;
+alter table "trip_document_links" add constraint "trip_document_links_trip_id_connection_id_remote__1c36d_unique" unique ("trip_id", "connection_id", "remote_scope_key");
+create table "dawarich_connections" ("user_id" bigint null, "url" text null, "api_key" text null, "allow_insecure_tls" bigint not null default 0, "sync_enabled" bigint not null default 1, "last_sync_at" text null, "last_sync_state" text not null default 'never', "last_sync_error" text null, "capabilities" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("user_id"));
+create table "collections" ("id" bigserial primary key, "owner_id" bigint not null, "name" text not null, "description" text null, "color" text null default '#6366f1', "icon" text null default 'Bookmark', "cover_image" text null, "links" text null, "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collections_owner_id" on "collections" ("owner_id");
+create table "collection_tours" ("id" bigserial primary key, "collection_id" bigint not null, "name" text not null, "description" text null, "website" text null, "route_geometry" text null, "waypoints" text not null, "distance" double precision null, "elevation_gain" double precision null, "elevation_loss" double precision null, "duration" double precision null, "planned_duration_minutes" bigint null, "break_additional_minutes" bigint null, "difficulty" text null, "wanderer_ref" text null, "match_confidence" double precision null, "max_hiking_difficulty" bigint null, "bike_profile" text null, "run_profile" text null, "run_pace_seconds_per_km" bigint null, "ski_discipline" text null, "ski_technique" text null, "boat_type" text null, "water_context" text null, "paddle_speed_kmh" double precision null, "portage_speed_kmh" double precision null, "paddle_lake_style" text null, "track_marks" text null, "hiking_preferences" text null, "origin" text not null, "origin_file_name" text null, "track_started_at" text null, "track_ended_at" text null, "source_trip_title" text not null, "source_tour_name" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "ski_options" text null, "tour_type" text not null, "saved_by" bigint null);
+create index "idx_collection_tours_collection" on "collection_tours" ("collection_id");
+create index "idx_collection_tours_tour_type" on "collection_tours" ("tour_type");
+create index "idx_collection_tours_saved_by" on "collection_tours" ("saved_by");
+create table "collection_labels" ("id" bigserial primary key, "collection_id" bigint not null, "name" text not null, "color" text null default '#6366f1', "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collection_labels_collection" on "collection_labels" ("collection_id");
+create table "collection_members" ("id" bigserial primary key, "collection_id" bigint not null, "user_id" bigint not null, "status" text not null default 'pending', "role" text not null default 'editor', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collection_members_user" on "collection_members" ("user_id");
+alter table "collection_members" add constraint "collection_members_collection_id_user_id_unique" unique ("collection_id", "user_id");
+create table "collab_polls" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "question" text not null, "options" text not null, "multiple" bigint null default 0, "closed" bigint null default 0, "deadline" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collab_polls_trip" on "collab_polls" ("trip_id");
+create index "idx_collab_polls_user_id" on "collab_polls" ("user_id");
+create table "collab_poll_votes" ("id" bigserial primary key, "poll_id" bigint not null, "user_id" bigint not null, "option_index" bigint not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collab_poll_votes_user_id" on "collab_poll_votes" ("user_id");
+alter table "collab_poll_votes" add constraint "collab_poll_votes_poll_id_user_id_option_index_unique" unique ("poll_id", "user_id", "option_index");
+create table "collab_notes" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "category" text null default 'General', "title" text not null, "content" text null, "color" text null default '#6366f1', "pinned" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "website" text null);
+create index "idx_collab_notes_trip" on "collab_notes" ("trip_id");
+create index "idx_collab_notes_user_id" on "collab_notes" ("user_id");
+create table "collab_messages" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "text" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "deleted" bigint null default 0, "origin" text null, "reply_to" bigint null);
+create index "idx_collab_messages_trip" on "collab_messages" ("trip_id");
+create index "idx_collab_messages_user_id" on "collab_messages" ("user_id");
+create index "idx_collab_messages_reply_to" on "collab_messages" ("reply_to");
+create table "collab_message_reactions" ("id" bigserial primary key, "message_id" bigint not null, "user_id" bigint not null, "emoji" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collab_reactions_msg" on "collab_message_reactions" ("message_id");
+create index "idx_collab_message_reactions_user_id" on "collab_message_reactions" ("user_id");
+alter table "collab_message_reactions" add constraint "collab_message_reactions_message_id_user_id_emoji_unique" unique ("message_id", "user_id", "emoji");
+create table "collab_links" ("id" bigserial primary key, "trip_id" bigint not null, "user_id" bigint not null, "title" text not null, "url" text not null, "pinned" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collab_links_trip" on "collab_links" ("trip_id");
+create index "idx_collab_links_user_id" on "collab_links" ("user_id");
+create table "categories" ("id" bigserial primary key, "name" text not null, "color" text null default '#6366f1', "icon" text null default '📍', "user_id" bigint null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_categories_user_id" on "categories" ("user_id");
+create table "places" ("id" bigserial primary key, "trip_id" bigint not null, "name" text not null, "description" text null, "lat" double precision null, "lng" double precision null, "address" text null, "category_id" bigint null, "price" double precision null, "currency" text null, "reservation_status" text null default 'none', "reservation_notes" text null, "reservation_datetime" text null, "place_time" text null, "end_time" text null, "duration_minutes" bigint null default 60, "notes" text null, "image_url" text null, "google_place_id" text null, "google_ftid" text null, "website" text null, "phone" text null, "transport_mode" text null default 'walking', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "osm_id" text null, "route_geometry" text null, "route_color" text null, "stop_type" text null, "fill_percent" bigint null, "amap_poi_id" text null, "source" text null, "email" text null, "opening_hours" text null);
+create index "idx_places_trip_id" on "places" ("trip_id");
+create index "idx_places_category_id" on "places" ("category_id");
+create table "tours" ("place_id" bigint null, "distance" double precision null, "elevation_gain" double precision null, "elevation_loss" double precision null, "duration" double precision null, "difficulty" text null, "wanderer_ref" text null, "match_confidence" double precision null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "max_hiking_difficulty" bigint null default 2, "planned_duration_minutes" bigint null, "break_additional_minutes" bigint null, "derived_from_place_id" bigint null, "recorded_route_geometry" text null, "origin" text not null default 'trek', "origin_file_name" text null, "boat_type" text null, "water_context" text null, "paddle_speed_kmh" double precision null, "portage_speed_kmh" double precision null, "track_marks" text null, "paddle_lake_style" text null, "revision" bigint not null default 1, "hiking_preferences" text null, "taken_over_from_trip_title" text null, "taken_over_from_tour_name" text null, "bike_profile" text null, "run_profile" text null, "run_pace_seconds_per_km" bigint null, "ski_technique" text null, "track_started_at" text null, "track_ended_at" text null, "ski_discipline" text null, "ski_options" text null, "tour_type" text not null, primary key ("place_id"));
+create index "idx_tours_tour_type" on "tours" ("tour_type");
+create index "idx_tours_derived_from_place_id" on "tours" ("derived_from_place_id") where derived_from_place_id IS NOT NULL;
+alter table "tours" add constraint "tours_run_pace_seconds_per_km_check" check (run_pace_seconds_per_km IS NULL OR (run_pace_seconds_per_km >= 120 AND run_pace_seconds_per_km <= 1200));
+alter table "tours" add constraint "tours_revision_check" check (revision >= 1);
+alter table "tours" add constraint "tours_origin_check" check ("origin" in ('trek', 'gpx', 'wanderer'));
+alter table "tours" add constraint "tours_boat_type_check" check ("boat_type" in ('kayak', 'canoe', 'sup', 'other'));
+alter table "tours" add constraint "tours_water_context_check" check ("water_context" in ('river', 'lake'));
+alter table "tours" add constraint "tours_paddle_lake_style_check" check ("paddle_lake_style" in ('direct', 'shore'));
+alter table "tours" add constraint "tours_bike_profile_check" check ("bike_profile" in ('touring', 'road', 'gravel', 'mtb'));
+alter table "tours" add constraint "tours_run_profile_check" check ("run_profile" in ('running', 'trail'));
+alter table "tours" add constraint "tours_ski_technique_check" check ("ski_technique" in ('classic', 'skating', 'unknown'));
+alter table "tours" add constraint "tours_ski_discipline_check" check ("ski_discipline" in ('alpine', 'snowboard', 'cross_country', 'touring', 'snowshoe', 'winter_hike'));
+create table "tour_waypoints" ("id" bigserial primary key, "place_id" bigint not null, "lat" double precision not null, "lng" double precision not null, "role" text not null, "sequence" bigint not null, "fixed_leg" text null, "lock" text null, "shaping" bigint not null default 0, "linked_place_id" bigint null, "station_key" text null, "name" text null, "stay_minutes" bigint null, "importance" text null, "protected" bigint not null default 0, "opening_hours" text null, "poi_category" text null, "stage_end" bigint not null default 0);
+create index "idx_tour_waypoints_linked_place" on "tour_waypoints" ("linked_place_id") where linked_place_id IS NOT NULL;
+alter table "tour_waypoints" add constraint "tour_waypoints_place_id_sequence_unique" unique ("place_id", "sequence");
+alter table "tour_waypoints" add constraint "tour_waypoints_stage_end_check" check (stage_end IN (0, 1));
+alter table "tour_waypoints" add constraint "tour_waypoints_protected_check" check (protected IN (0, 1));
+alter table "tour_waypoints" add constraint "tour_waypoints_stay_minutes_check" check (stay_minutes IS NULL OR (stay_minutes >= 0 AND stay_minutes <= 1440));
+alter table "tour_waypoints" add constraint "tour_waypoints_shaping_check" check (shaping IN (0, 1));
+alter table "tour_waypoints" add constraint "tour_waypoints_importance_check" check ("importance" in ('optional', 'must'));
+create table "tour_sources" ("place_id" bigint null, "provider" text not null, "instance" text not null, "external_id" text not null, "external_url" text null, "external_updated" text null, "content_hash" text not null, "local_hash" text not null, "imported_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "imported_by" bigint null, primary key ("place_id"));
+create index "idx_tour_sources_imported_by" on "tour_sources" ("imported_by");
+create index "idx_tour_sources_external" on "tour_sources" ("provider", "instance", "external_id");
+create table "tour_source_hints" ("place_id" bigint null, "user_id" bigint not null, "state" text null, "changes" text null, "reason" text null, "external_updated" text null, "seen_hash" text null, "seen_updated" text null, "checked_at" text null, primary key ("place_id"));
+create index "idx_tour_source_hints_user_id" on "tour_source_hints" ("user_id");
+alter table "tour_source_hints" add constraint "tour_source_hints_state_check" check ("state" in ('possible_change', 'changed', 'unverifiable'));
+alter table "tour_source_hints" add constraint "tour_source_hints_reason_check" check ("reason" in ('unavailable', 'no_change_time'));
+create table "tour_packing_links" ("place_id" bigint null, "category" text null, "linked_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "linked_by" bigint null, primary key ("place_id"));
+create index "idx_tour_packing_links_linked_by" on "tour_packing_links" ("linked_by");
+create table "tour_obstacle_reviews" ("place_id" bigint not null, "obstacle_key" text not null, "decision" text not null, "note" text null, "route_fingerprint" text not null, "source_fingerprint" text not null, "obstacle_snapshot" text not null, "decided_at" text not null, "decided_by" bigint null, primary key ("place_id", "obstacle_key"));
+alter table "tour_obstacle_reviews" add constraint "tour_obstacle_reviews_place_id_obstacle_key_unique" unique ("place_id", "obstacle_key");
+alter table "tour_obstacle_reviews" add constraint "tour_obstacle_reviews_decision_check" check ("decision" in ('pass', 'portage', 'reroute', 'not_affected'));
+create table "tour_change_steps" ("id" bigserial primary key, "place_id" bigint not null, "proposal_id" text not null, "base_revision" bigint not null, "revision" bigint not null, "changes" text not null, "before_state" text not null, "saved_by" bigint null, "saved_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "undone_revision" bigint null, "undone_by" bigint null, "undone_at" text null);
+alter table "tour_change_steps" add constraint "tour_change_steps_place_id_revision_unique" unique ("place_id", "revision");
+alter table "tour_change_steps" add constraint "tour_change_steps_undone_revision_check" check (undone_revision IS NULL OR undone_revision > revision);
+alter table "tour_change_steps" add constraint "tour_change_steps_revision_check" check (revision = base_revision + 1);
+alter table "tour_change_steps" add constraint "tour_change_steps_base_revision_check" check (base_revision >= 1);
+create table "tour_activity_exclusions" ("id" bigserial primary key, "user_id" bigint not null, "place_id" bigint not null, "excluded_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_tour_activity_exclusions_place" on "tour_activity_exclusions" ("place_id");
+alter table "tour_activity_exclusions" add constraint "tour_activity_exclusions_user_id_place_id_unique" unique ("user_id", "place_id");
+create table "roadtrip_day_tracks" ("day_id" bigint null, "place_id" bigint not null, "stray_km" double precision null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("day_id"));
+create index "idx_roadtrip_day_tracks_place" on "roadtrip_day_tracks" ("place_id");
+create table "place_regions" ("place_id" bigint null, "country_code" text not null, "region_code" text not null, "region_name" text not null, primary key ("place_id"));
+create index "idx_place_regions_country" on "place_regions" ("country_code");
+create index "idx_place_regions_region" on "place_regions" ("region_code");
+create table "place_ratings" ("id" bigserial primary key, "place_id" bigint not null, "user_id" bigint not null, "rating" bigint not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_place_ratings_place" on "place_ratings" ("place_id");
+create index "idx_place_ratings_user_id" on "place_ratings" ("user_id");
+alter table "place_ratings" add constraint "place_ratings_place_id_user_id_unique" unique ("place_id", "user_id");
+create table "place_field_origin" ("place_id" bigint not null, "field" text not null, "origin" text not null, "value_hash" text not null, "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("place_id", "field"));
+alter table "place_field_origin" add constraint "place_field_origin_place_id_field_unique" unique ("place_id", "field");
+create table "place_contrib_outbox" ("id" bigserial primary key, "contrib_key" text not null, "field" text not null, "value" text not null, "place_id" bigint not null, "place_name" text not null, "lat" double precision null, "lng" double precision null, "country" text null, "cid" text not null, "attempts" bigint not null default 0, "eligible_at" text not null, "next_attempt_at" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_place_contrib_outbox_place" on "place_contrib_outbox" ("place_id");
+create index "idx_place_contrib_outbox_due" on "place_contrib_outbox" ("next_attempt_at");
+alter table "place_contrib_outbox" add constraint "place_contrib_outbox_contrib_key_field_unique" unique ("contrib_key", "field");
+create table "place_tags" ("place_id" bigint not null, "tag_id" bigint not null, primary key ("place_id", "tag_id"));
+create table "photos" ("id" bigserial primary key, "trip_id" bigint not null, "day_id" bigint null, "place_id" bigint null, "filename" text not null, "original_name" text not null, "file_size" bigint null, "mime_type" text null, "caption" text null, "taken_at" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_photos_trip_id" on "photos" ("trip_id");
+create index "idx_photos_day_id" on "photos" ("day_id");
+create index "idx_photos_place_id" on "photos" ("place_id");
+create table "journey_entries" ("id" bigserial primary key, "journey_id" bigint not null, "source_trip_id" bigint null, "source_place_id" bigint null, "author_id" bigint not null, "type" text not null, "title" text null, "story" text null, "entry_date" text not null, "entry_time" text null, "location_name" text null, "location_lat" double precision null, "location_lng" double precision null, "mood" text null, "weather" text null, "tags" text null, "visibility" text null default 'private', "sort_order" bigint null default 0, "created_at" bigint not null, "updated_at" bigint not null, "pros_cons" text null, "stats_excluded" bigint not null default 0, "dismissed" bigint not null default 0, "country_code" text null, "source_assignment_id" bigint null, "is_draft" bigint not null default 0);
+create index "idx_journey_entries_source_trip_id" on "journey_entries" ("source_trip_id");
+create index "idx_journey_entries_source" on "journey_entries" ("source_place_id");
+create index "idx_journey_entries_author_id" on "journey_entries" ("author_id");
+create index "idx_journey_entries_source_assignment" on "journey_entries" ("source_place_id", "source_assignment_id");
+create index "idx_journey_entries_order" on "journey_entries" ("journey_id", "entry_date", "sort_order");
+create index "idx_journey_entries_journey" on "journey_entries" ("journey_id", "entry_date");
+create table "journey_entry_stays" ("id" bigserial primary key, "journey_id" bigint not null, "entry_id" bigint not null, "source" text not null default 'dawarich', "source_visit_id" text not null, "name" text not null, "lat" double precision null, "lng" double precision null, "started_at" text not null, "ended_at" text not null, "duration_minutes" bigint not null default 0, "sort_order" bigint not null default 0, "user_id" bigint null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_journey_entry_stays_entry" on "journey_entry_stays" ("entry_id");
+create index "idx_journey_entry_stays_user" on "journey_entry_stays" ("user_id");
+alter table "journey_entry_stays" add constraint "idx_journey_entry_stays_stay" unique ("journey_id", "source", "user_id", "source_visit_id");
+create table "journey_entry_photos" ("entry_id" bigint not null, "journey_photo_id" bigint not null, "sort_order" bigint null default 0, "created_at" bigint not null, primary key ("entry_id", "journey_photo_id"));
+create index "idx_journey_entry_photos_entry" on "journey_entry_photos" ("entry_id");
+create index "idx_journey_entry_photos_photo" on "journey_entry_photos" ("journey_photo_id");
+alter table "journey_entry_photos" add constraint "journey_entry_photos_entry_id_journey_photo_id_unique" unique ("entry_id", "journey_photo_id");
+create table "day_assignments" ("id" bigserial primary key, "day_id" bigint not null, "place_id" bigint not null, "order_index" bigint null default 0, "notes" text null, "reservation_status" text null default 'none', "reservation_notes" text null, "reservation_datetime" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "assignment_time" text null, "assignment_end_time" text null, "leg_transport_mode" text null, "incoming_leg_transport_mode" text null, "end_day" bigint not null default 0, "accommodation_id" bigint null, "route_excluded" bigint not null default 0, "tour_stage_key" text null);
+create index "idx_day_assignments_day_id" on "day_assignments" ("day_id");
+create index "idx_day_assignments_place_id" on "day_assignments" ("place_id");
+create index "idx_day_assignments_accommodation_id" on "day_assignments" ("accommodation_id");
+create index "idx_day_assignments_tour_stage" on "day_assignments" ("place_id", "tour_stage_key") where tour_stage_key IS NOT NULL;
+create table "tour_station_visits" ("assignment_id" bigint not null, "station_key" text not null, "status" text not null, "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("assignment_id", "station_key"));
+alter table "tour_station_visits" add constraint "tour_station_visits_assignment_id_station_key_unique" unique ("assignment_id", "station_key");
+alter table "tour_station_visits" add constraint "tour_station_visits_status_check" check ("status" in ('visited', 'skipped'));
+create table "tour_activities" ("id" bigserial primary key, "place_id" bigint not null, "track_place_id" bigint not null, "assignment_id" bigint null, "assigned_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "assigned_by" bigint null);
+create index "idx_tour_activities_place" on "tour_activities" ("place_id");
+create index "idx_tour_activities_assigned_by" on "tour_activities" ("assigned_by");
+create unique index "idx_tour_activities_assignment" on "tour_activities" ("assignment_id") where assignment_id IS NOT NULL;
+alter table "tour_activities" add constraint "tour_activities_track_place_id_unique" unique ("track_place_id");
+create table "roadtrip_day_boundaries" ("trip_id" bigint not null, "day_number" bigint not null, "from_assignment_id" bigint not null, "to_assignment_id" bigint null, "fraction" double precision not null, primary key ("trip_id", "day_number"));
+create index "idx_roadtrip_day_boundaries_from_assignment_id" on "roadtrip_day_boundaries" ("from_assignment_id");
+create index "idx_roadtrip_day_boundaries_to_assignment_id" on "roadtrip_day_boundaries" ("to_assignment_id");
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_trip_id_day_number_unique" unique ("trip_id", "day_number");
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_fraction_check" check (fraction BETWEEN 0 AND 1);
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_day_number_check" check (day_number >= 1);
+create table "reservations" ("id" bigserial primary key, "trip_id" bigint not null, "day_id" bigint null, "end_day_id" bigint null, "place_id" bigint null, "assignment_id" bigint null, "title" text not null, "accommodation_id" text null, "reservation_time" text null, "reservation_end_time" text null, "location" text null, "confirmation_number" text null, "notes" text null, "status" text null default 'pending', "type" text null default 'other', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "metadata" text null, "day_plan_position" double precision null default NULL, "needs_review" bigint not null default 0, "external_source" text null, "external_id" text null, "external_owner_user_id" bigint null, "external_synced_at" text null, "sync_enabled" bigint null default 1, "external_hash" text null, "url" text null, "ingest_state" text not null default 'live');
+create index "idx_reservations_trip_id" on "reservations" ("trip_id");
+create index "idx_reservations_day_id" on "reservations" ("day_id");
+create index "idx_reservations_end_day_id" on "reservations" ("end_day_id");
+create index "idx_reservations_place_id" on "reservations" ("place_id");
+create index "idx_reservations_assignment_id" on "reservations" ("assignment_id");
+alter table "reservations" add constraint "idx_reservations_external" unique ("external_source", "external_id", "trip_id");
+create table "trip_files" ("id" bigserial primary key, "trip_id" bigint not null, "place_id" bigint null, "reservation_id" bigint null, "filename" text not null, "original_name" text not null, "file_size" bigint null, "mime_type" text null, "description" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "note_id" bigint null, "starred" bigint null default 0, "deleted_at" text null, "message_id" bigint null, "uploaded_by" bigint null);
+create index "idx_trip_files_trip_id" on "trip_files" ("trip_id");
+create index "idx_trip_files_place_id" on "trip_files" ("place_id");
+create index "idx_trip_files_reservation_id" on "trip_files" ("reservation_id");
+create index "idx_trip_files_note_id" on "trip_files" ("note_id");
+create index "idx_trip_files_message_id" on "trip_files" ("message_id");
+create index "idx_trip_files_uploaded_by" on "trip_files" ("uploaded_by");
+create table "document_sync_items" ("id" bigserial primary key, "link_id" bigint not null, "trip_id" bigint not null, "file_id" bigint null, "trek_doc_uid" text not null, "remote_id" text null, "remote_name" text null, "remote_version" text null, "remote_size" bigint null, "remote_modified_at" text null, "content_sha256" text null, "pushed_sha256" text null, "state" text not null default 'pending', "error_code" text null, "attempts" bigint not null default 0, "next_attempt_at" text null, "remote_missing_at" text null, "first_seen_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_seen_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "synced_at" text null, "remote_trashed_at" text null);
+create index "idx_document_sync_items_file_id" on "document_sync_items" ("file_id");
+create index "idx_document_sync_items_due" on "document_sync_items" ("link_id", "next_attempt_at");
+create index "idx_document_sync_items_hash" on "document_sync_items" ("link_id", "content_sha256");
+create index "idx_document_sync_items_trip_state" on "document_sync_items" ("trip_id", "state");
+alter table "document_sync_items" add constraint "idx_document_sync_items_uid" unique ("link_id", "trek_doc_uid");
+create unique index "idx_document_sync_items_file" on "document_sync_items" ("link_id", "file_id") where file_id IS NOT NULL;
+create unique index "idx_document_sync_items_remote" on "document_sync_items" ("link_id", "remote_id") where remote_id IS NOT NULL;
+create table "reservation_travelers" ("id" bigserial primary key, "reservation_id" bigint not null, "user_id" bigint not null);
+create index "idx_reservation_travelers_res" on "reservation_travelers" ("reservation_id");
+create index "idx_reservation_travelers_user" on "reservation_travelers" ("user_id");
+alter table "reservation_travelers" add constraint "reservation_travelers_reservation_id_user_id_unique" unique ("reservation_id", "user_id");
+create table "reservation_endpoints" ("id" bigserial primary key, "reservation_id" bigint not null, "role" text not null, "sequence" bigint not null default 0, "name" text not null, "code" text null, "lat" double precision not null, "lng" double precision not null, "timezone" text null, "local_time" text null, "local_date" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_reservation_endpoints_reservation_id" on "reservation_endpoints" ("reservation_id");
+create table "reservation_day_positions" ("reservation_id" bigint not null, "day_id" bigint not null, "position" double precision not null, primary key ("reservation_id", "day_id"));
+create index "idx_reservation_day_positions_day_id" on "reservation_day_positions" ("day_id");
+alter table "reservation_day_positions" add constraint "reservation_day_positions_reservation_id_day_id_unique" unique ("reservation_id", "day_id");
+create table "day_accommodations" ("id" bigserial primary key, "trip_id" bigint not null, "place_id" bigint null, "start_day_id" bigint not null, "end_day_id" bigint not null, "check_in" text null, "check_in_end" text null, "check_out" text null, "confirmation" text null, "notes" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_day_accommodations_trip_id" on "day_accommodations" ("trip_id");
+create index "idx_day_accommodations_place_id" on "day_accommodations" ("place_id");
+create index "idx_day_accommodations_start_day_id" on "day_accommodations" ("start_day_id");
+create index "idx_day_accommodations_end_day_id" on "day_accommodations" ("end_day_id");
+create table "collection_places" ("id" bigserial primary key, "collection_id" bigint not null, "owner_id" bigint not null, "name" text not null, "description" text null, "lat" double precision null, "lng" double precision null, "address" text null, "category_id" bigint null, "price" double precision null, "currency" text null, "notes" text null, "image_url" text null, "google_place_id" text null, "google_ftid" text null, "osm_id" text null, "website" text null, "phone" text null, "status" text not null default 'idea', "source_trip_id" bigint null, "source_place_id" bigint null, "links" text null, "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "saved_by" bigint null);
+create index "idx_collection_places_collection" on "collection_places" ("collection_id");
+create index "idx_collection_places_owner_id" on "collection_places" ("owner_id");
+create index "idx_collection_places_category_id" on "collection_places" ("category_id");
+create index "idx_collection_places_saved_by" on "collection_places" ("saved_by");
+create table "collection_place_ratings" ("id" bigserial primary key, "collection_place_id" bigint not null, "user_id" bigint not null, "rating" bigint not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_collection_place_ratings_place" on "collection_place_ratings" ("collection_place_id");
+create index "idx_collection_place_ratings_user_id" on "collection_place_ratings" ("user_id");
+alter table "collection_place_ratings" add constraint "collection_place_ratings_collection_place_id_user_id_unique" unique ("collection_place_id", "user_id");
+create table "collection_place_tags" ("collection_place_id" bigint not null, "tag_id" bigint not null, primary key ("collection_place_id", "tag_id"));
+create table "collection_place_labels" ("collection_place_id" bigint not null, "label_id" bigint not null, primary key ("collection_place_id", "label_id"));
+create table "budget_settlements" ("id" bigserial primary key, "trip_id" bigint not null, "from_user_id" bigint not null, "to_user_id" bigint not null, "amount" double precision not null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "created_by_user_id" bigint null, "currency" text null, "exchange_rate" double precision not null default 1, "settled_at" text null, "note" text null);
+create index "idx_budget_settlements_trip" on "budget_settlements" ("trip_id");
+create index "idx_budget_settlements_from_user_id" on "budget_settlements" ("from_user_id");
+create index "idx_budget_settlements_to_user_id" on "budget_settlements" ("to_user_id");
+create index "idx_budget_settlements_created_by_user_id" on "budget_settlements" ("created_by_user_id");
+create table "budget_items" ("id" bigserial primary key, "trip_id" bigint not null, "category" text not null default 'Other', "name" text not null, "total_price" double precision not null default 0, "persons" bigint null default NULL, "days" bigint null default NULL, "note" text null, "sort_order" bigint null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "paid_by_user_id" bigint null, "expense_date" text null default NULL, "reservation_id" bigint null default NULL, "currency" text null, "exchange_rate" double precision not null default 1, "ticket_json" text null, "place_id" bigint null default NULL);
+create index "idx_budget_items_trip_id" on "budget_items" ("trip_id");
+create index "idx_budget_items_paid_by_user_id" on "budget_items" ("paid_by_user_id");
+create index "idx_budget_items_reservation_id" on "budget_items" ("reservation_id");
+create index "idx_budget_items_place_id" on "budget_items" ("place_id");
+create table "file_links" ("id" bigserial primary key, "file_id" bigint not null, "reservation_id" bigint null, "assignment_id" bigint null, "place_id" bigint null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "budget_item_id" bigint null);
+create index "idx_file_links_reservation_id" on "file_links" ("reservation_id");
+create index "idx_file_links_assignment_id" on "file_links" ("assignment_id");
+create index "idx_file_links_place_id" on "file_links" ("place_id");
+create index "idx_file_links_budget_item_id" on "file_links" ("budget_item_id");
+alter table "file_links" add constraint "idx_file_links_file_budget" unique ("file_id", "budget_item_id");
+alter table "file_links" add constraint "file_links_file_id_place_id_unique" unique ("file_id", "place_id");
+alter table "file_links" add constraint "file_links_file_id_assignment_id_unique" unique ("file_id", "assignment_id");
+alter table "file_links" add constraint "file_links_file_id_reservation_id_unique" unique ("file_id", "reservation_id");
+create table "budget_item_payers" ("id" bigserial primary key, "budget_item_id" bigint not null, "user_id" bigint not null, "amount" double precision not null default 0);
+create index "idx_budget_item_payers_item" on "budget_item_payers" ("budget_item_id");
+create index "idx_budget_item_payers_user_id" on "budget_item_payers" ("user_id");
+alter table "budget_item_payers" add constraint "budget_item_payers_budget_item_id_user_id_unique" unique ("budget_item_id", "user_id");
+create table "budget_item_members" ("id" bigserial primary key, "budget_item_id" bigint not null, "user_id" bigint not null, "paid" bigint not null default 0, "amount" double precision null);
+create index "idx_budget_item_members_item" on "budget_item_members" ("budget_item_id");
+create index "idx_budget_item_members_user" on "budget_item_members" ("user_id");
+alter table "budget_item_members" add constraint "budget_item_members_budget_item_id_user_id_unique" unique ("budget_item_id", "user_id");
+create table "bucket_list" ("id" bigserial primary key, "user_id" bigint not null, "name" text not null, "lat" double precision null, "lng" double precision null, "country_code" text null, "notes" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "target_date" text null default NULL, "visited_at" text null, "visited_source" text null, "region_code" text null);
+create index "idx_bucket_list_user_id" on "bucket_list" ("user_id");
+create table "dawarich_visit_suggestions" ("id" bigserial primary key, "user_id" bigint not null, "source_visit_id" text not null, "trip_id" bigint null, "name" text not null, "lat" double precision null, "lng" double precision null, "started_at" text not null, "ended_at" text not null, "duration_minutes" bigint not null default 0, "local_date" text not null, "source_status" text not null default 'suggested', "confidence" double precision null, "confidence_band" text null, "country_code" text null, "state" text not null default 'new', "target" text null, "accepted_place_id" bigint null, "accepted_journal_entry_id" bigint null, "accepted_bucket_list_item_id" bigint null, "matched_bucket_list_item_id" bigint null, "source_hash" text not null, "accepted_hash" text null, "source_missing_at" text null, "first_seen_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_seen_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_dawarich_suggestions_trip" on "dawarich_visit_suggestions" ("trip_id");
+create index "idx_dawarich_visit_suggestions_accepted_place_id" on "dawarich_visit_suggestions" ("accepted_place_id");
+create index "idx_dawarich_visit_suggestions_accepted_bucket_list_item_id" on "dawarich_visit_suggestions" ("accepted_bucket_list_item_id");
+create index "idx_dawarich_visit_suggestions_matched_bucket_list_item_id" on "dawarich_visit_suggestions" ("matched_bucket_list_item_id");
+create index "idx_dawarich_suggestions_user_state" on "dawarich_visit_suggestions" ("user_id", "state");
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_user_id_source_visit_id_unique" unique ("user_id", "source_visit_id");
+create table "audit_log" ("id" bigserial primary key, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "user_id" bigint null, "action" text not null, "resource" text null, "details" text null, "ip" text null);
+create index "idx_audit_log_created" on "audit_log" ("created_at");
+create index "idx_audit_log_user_id" on "audit_log" ("user_id");
+create table "assistant_usage" ("user_id" bigint not null, "day" text not null, "paid_by" text not null, "turns" bigint not null default 0, "input_tokens" bigint not null default 0, "output_tokens" bigint not null default 0, primary key ("user_id", "day", "paid_by"));
+alter table "assistant_usage" add constraint "assistant_usage_user_id_day_paid_by_unique" unique ("user_id", "day", "paid_by");
+create table "assistant_threads" ("id" bigserial primary key, "user_id" bigint not null, "trip_id" bigint null, "journey_id" bigint null, "title" text not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_message_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_assistant_threads_trip" on "assistant_threads" ("trip_id");
+create index "idx_assistant_threads_journey" on "assistant_threads" ("journey_id");
+create index "idx_assistant_threads_user_trip" on "assistant_threads" ("user_id", "trip_id", "last_message_at");
+create index "idx_assistant_threads_user_journey" on "assistant_threads" ("user_id", "journey_id", "last_message_at");
+create table "assistant_messages" ("id" bigserial primary key, "thread_id" bigint not null, "role" text not null, "parts" text not null, "provider_state" text null, "status" text not null default 'complete', "model" text null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_assistant_messages_thread" on "assistant_messages" ("thread_id", "id");
+create table "assistant_tool_calls" ("id" text not null, "thread_id" bigint not null, "message_id" bigint not null, "provider_call_id" text not null, "tool_name" text not null, "args" text not null, "preview" text not null, "state" text not null default 'approval_requested', "result" text null, "undo_until" text null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "decided_at" text null, primary key ("id"));
+create index "idx_assistant_tool_calls_message" on "assistant_tool_calls" ("message_id");
+alter table "assistant_tool_calls" add constraint "assistant_tool_calls_thread_id_provider_call_id_unique" unique ("thread_id", "provider_call_id");
+create table "assistant_attachments" ("id" text not null, "thread_id" bigint not null, "user_id" bigint not null, "trip_id" bigint not null, "name" text not null, "mime" text not null, "size" bigint not null, "sha256" text not null, "storage_key" text not null, "text" text null, "text_status" text not null, "shared_at" text null, "file_id" bigint null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), primary key ("id"));
+create index "idx_assistant_attachments_user" on "assistant_attachments" ("user_id");
+create index "idx_assistant_attachments_trip" on "assistant_attachments" ("trip_id");
+create index "idx_assistant_attachments_file" on "assistant_attachments" ("file_id");
+create index "idx_assistant_attachments_created" on "assistant_attachments" ("created_at");
+create index "idx_assistant_attachments_thread" on "assistant_attachments" ("thread_id", "created_at");
+create table "assignment_participants" ("id" bigserial primary key, "assignment_id" bigint not null, "user_id" bigint not null);
+create index "idx_assignment_participants_assignment" on "assignment_participants" ("assignment_id");
+create index "idx_assignment_participants_user_id" on "assignment_participants" ("user_id");
+alter table "assignment_participants" add constraint "assignment_participants_assignment_id_user_id_unique" unique ("assignment_id", "user_id");
+create table "ai_connections" ("id" bigserial primary key, "scope" text not null, "user_id" bigint null, "kind" text not null, "model" text not null, "base_url" text null, "secrets" text null, "secrets_version" bigint not null default 0, "hint" text null, "account" text null, "status" text not null default 'ok', "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create unique index "idx_ai_connections_instance" on "ai_connections" ("scope") where user_id IS NULL;
+create unique index "idx_ai_connections_user" on "ai_connections" ("user_id") where user_id IS NOT NULL;
+create table "user_sessions" ("id" text not null, "user_id" bigint not null, "created_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_seen_at" text not null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "expires_at" text not null, "revoked_at" text null, "user_agent" text null, primary key ("id"));
+create index "idx_user_sessions_user" on "user_sessions" ("user_id");
+create index "idx_user_sessions_expires" on "user_sessions" ("expires_at");
+create table "vacay_plans" ("id" bigserial primary key, "owner_id" bigint not null, "block_weekends" bigint null default 1, "holidays_enabled" bigint null default 0, "holidays_region" text null default '', "school_holidays_enabled" bigint null default 0, "company_holidays_enabled" bigint null default 1, "carry_over_enabled" bigint null default 1, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "weekend_days" text null default '0,6', "week_start" bigint not null default 1);
+alter table "vacay_plans" add constraint "vacay_plans_owner_id_unique" unique ("owner_id");
+create table "vacay_plan_members" ("id" bigserial primary key, "plan_id" bigint not null, "user_id" bigint not null, "status" text null default 'pending', "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_vacay_plan_members_user_id" on "vacay_plan_members" ("user_id");
+alter table "vacay_plan_members" add constraint "vacay_plan_members_plan_id_user_id_unique" unique ("plan_id", "user_id");
+create table "vacay_holiday_calendars" ("id" bigserial primary key, "plan_id" bigint not null, "type" text not null default 'public_holiday', "region" text not null, "label" text null, "color" text not null default '#fecaca', "sort_order" bigint not null default 0);
+create index "idx_vacay_holiday_calendars_plan_id" on "vacay_holiday_calendars" ("plan_id");
+create table "vacay_entries" ("id" bigserial primary key, "plan_id" bigint not null, "user_id" bigint not null, "date" text not null, "note" text null default '', "fraction" double precision not null default 1, "kind" text not null default 'vacation');
+create index "idx_vacay_entries_plan_id" on "vacay_entries" ("plan_id");
+alter table "vacay_entries" add constraint "vacay_entries_user_id_plan_id_date_unique" unique ("user_id", "plan_id", "date");
+create table "vacay_company_holidays" ("id" bigserial primary key, "plan_id" bigint not null, "date" text not null, "note" text null default '', "fraction" double precision not null default 1);
+alter table "vacay_company_holidays" add constraint "vacay_company_holidays_plan_id_date_unique" unique ("plan_id", "date");
+create table "vacay_shares" ("id" bigserial primary key, "owner_id" bigint not null, "user_id" bigint not null, "hidden" bigint not null default 0, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_vacay_shares_user" on "vacay_shares" ("user_id");
+alter table "vacay_shares" add constraint "vacay_shares_owner_id_user_id_unique" unique ("owner_id", "user_id");
+create table "vacay_user_colors" ("id" bigserial primary key, "user_id" bigint not null, "plan_id" bigint not null, "color" text null default '#6366f1');
+create index "idx_vacay_user_colors_plan_id" on "vacay_user_colors" ("plan_id");
+alter table "vacay_user_colors" add constraint "vacay_user_colors_user_id_plan_id_unique" unique ("user_id", "plan_id");
+create table "vacay_user_settings" ("user_id" bigint null, "year_type" text not null default 'calendar', "year_start_month" bigint not null default 1, "year_start_day" bigint not null default 1, "hire_date" text null, primary key ("user_id"));
+create table "vacay_user_years" ("id" bigserial primary key, "user_id" bigint not null, "plan_id" bigint not null, "year" bigint not null, "vacation_days" bigint null default 30, "carried_over" bigint null default 0);
+create index "idx_vacay_user_years_plan_id" on "vacay_user_years" ("plan_id");
+alter table "vacay_user_years" add constraint "vacay_user_years_user_id_plan_id_year_unique" unique ("user_id", "plan_id", "year");
+create table "vacay_years" ("id" bigserial primary key, "plan_id" bigint not null, "year" bigint not null);
+alter table "vacay_years" add constraint "vacay_years_plan_id_year_unique" unique ("plan_id", "year");
+create table "visited_countries" ("id" bigserial primary key, "user_id" bigint not null, "country_code" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "source" text not null default 'manual');
+alter table "visited_countries" add constraint "visited_countries_user_id_country_code_unique" unique ("user_id", "country_code");
+create table "visited_regions" ("id" bigserial primary key, "user_id" bigint not null, "region_code" text not null, "region_name" text not null, "country_code" text not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_visited_regions_country" on "visited_regions" ("country_code");
+alter table "visited_regions" add constraint "visited_regions_user_id_region_code_unique" unique ("user_id", "region_code");
+create table "wanderer_connections" ("user_id" bigint null, "url" text not null, "token" text null, "allow_insecure_tls" bigint not null default 0, "last_checked_at" text null, "last_check_ok" bigint null, "last_check_error" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "updated_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "watch_enabled" bigint not null default 0, "watch_last_run_at" text null, "watch_failures" bigint not null default 0, "watch_paused_until" text null, primary key ("user_id"));
+create table "webauthn_challenges" ("id" bigserial primary key, "challenge" text not null, "user_id" bigint null, "type" text not null, "expires_at" bigint not null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'));
+create index "idx_webauthn_challenges_user_id" on "webauthn_challenges" ("user_id");
+create index "idx_webauthn_challenges_expires" on "webauthn_challenges" ("expires_at");
+alter table "webauthn_challenges" add constraint "webauthn_challenges_challenge_unique" unique ("challenge");
+create table "webauthn_credentials" ("id" bigserial primary key, "user_id" bigint not null, "credential_id" text not null, "public_key" bytea not null, "counter" bigint not null default 0, "transports" text null, "device_type" text null, "backed_up" bigint not null default 0, "name" text null, "aaguid" text null, "created_at" text null default to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS'), "last_used_at" text null);
+create index "idx_webauthn_credentials_user" on "webauthn_credentials" ("user_id");
+alter table "webauthn_credentials" add constraint "webauthn_credentials_credential_id_unique" unique ("credential_id");
+alter table "document_provider_fields" add constraint "document_provider_fields_provider_id_foreign" foreign key ("provider_id") references "document_providers" ("id") on delete cascade;
+alter table "paddle_features" add constraint "paddle_features_region_id_foreign" foreign key ("region_id") references "paddle_regions" ("id") on delete cascade;
+alter table "paddle_water_graphs" add constraint "paddle_water_graphs_region_id_foreign" foreign key ("region_id") references "paddle_regions" ("id") on update cascade on delete cascade;
+alter table "photo_provider_fields" add constraint "photo_provider_fields_provider_id_foreign" foreign key ("provider_id") references "photo_providers" ("id") on delete cascade;
+alter table "school_holiday_regions" add constraint "school_holiday_regions_country_foreign" foreign key ("country") references "school_holiday_countries" ("code");
+alter table "school_holiday_periods" add constraint "school_holiday_periods_region_id_foreign" foreign key ("region_id") references "school_holiday_regions" ("id") on delete cascade;
+alter table "user_notice_dismissals" add constraint "user_notice_dismissals_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "trips" add constraint "trips_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "roadtrip_preferences" add constraint "roadtrip_preferences_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "days" add constraint "days_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "roadtrip_vias" add constraint "roadtrip_vias_day_id_foreign" foreign key ("day_id") references "days" ("id") on delete cascade;
+alter table "day_notes" add constraint "day_notes_day_id_foreign" foreign key ("day_id") references "days" ("id") on delete cascade;
+alter table "day_notes" add constraint "day_notes_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "budget_category_order" add constraint "budget_category_order_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_members" add constraint "trip_members_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_members" add constraint "trip_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "trip_members" add constraint "trip_members_invited_by_foreign" foreign key ("invited_by") references "users" ("id") on delete no action;
+alter table "trip_member_removals" add constraint "trip_member_removals_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_member_removals" add constraint "trip_member_removals_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "trip_invite_tokens" add constraint "trip_invite_tokens_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_invite_tokens" add constraint "trip_invite_tokens_created_by_foreign" foreign key ("created_by") references "users" ("id") on delete set null;
+alter table "trip_album_links" add constraint "trip_album_links_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_album_links" add constraint "trip_album_links_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "trek_photos" add constraint "trek_photos_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete set null;
+alter table "trip_photos" add constraint "trip_photos_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_photos" add constraint "trip_photos_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "trip_photos" add constraint "trip_photos_photo_id_foreign" foreign key ("photo_id") references "trek_photos" ("id") on delete cascade;
+alter table "trip_photos" add constraint "trip_photos_album_link_id_foreign" foreign key ("album_link_id") references "trip_album_links" ("id") on delete set null;
+alter table "trek_photo_signals" add constraint "trek_photo_signals_photo_id_foreign" foreign key ("photo_id") references "trek_photos" ("id") on delete cascade;
+alter table "tour_drafts" add constraint "tour_drafts_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "tour_drafts" add constraint "tour_drafts_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "todo_items" add constraint "todo_items_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "todo_items" add constraint "todo_items_assigned_user_id_foreign" foreign key ("assigned_user_id") references "users" ("id") on delete set null;
+alter table "todo_category_assignees" add constraint "todo_category_assignees_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "todo_category_assignees" add constraint "todo_category_assignees_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "tags" add constraint "tags_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "share_tokens" add constraint "share_tokens_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "share_tokens" add constraint "share_tokens_created_by_foreign" foreign key ("created_by") references "users" ("id");
+alter table "settings" add constraint "settings_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "push_subscriptions" add constraint "push_subscriptions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "password_reset_tokens" add constraint "password_reset_tokens_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "packing_templates" add constraint "packing_templates_created_by_foreign" foreign key ("created_by") references "users" ("id") on delete cascade;
+alter table "packing_template_categories" add constraint "packing_template_categories_template_id_foreign" foreign key ("template_id") references "packing_templates" ("id") on delete cascade;
+alter table "packing_template_items" add constraint "packing_template_items_category_id_foreign" foreign key ("category_id") references "packing_template_categories" ("id") on delete cascade;
+alter table "packing_category_assignees" add constraint "packing_category_assignees_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "packing_category_assignees" add constraint "packing_category_assignees_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "packing_bags" add constraint "packing_bags_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "packing_bags" add constraint "packing_bags_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete set null;
+alter table "packing_items" add constraint "packing_items_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "packing_items" add constraint "packing_items_bag_id_foreign" foreign key ("bag_id") references "packing_bags" ("id") on delete set null;
+alter table "packing_items" add constraint "packing_items_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete set null;
+alter table "packing_item_contributors" add constraint "packing_item_contributors_item_id_foreign" foreign key ("item_id") references "packing_items" ("id") on update cascade on delete cascade;
+alter table "packing_item_contributors" add constraint "packing_item_contributors_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "packing_item_recipients" add constraint "packing_item_recipients_item_id_foreign" foreign key ("item_id") references "packing_items" ("id") on update cascade on delete cascade;
+alter table "packing_item_recipients" add constraint "packing_item_recipients_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "packing_bag_members" add constraint "packing_bag_members_bag_id_foreign" foreign key ("bag_id") references "packing_bags" ("id") on update cascade on delete cascade;
+alter table "packing_bag_members" add constraint "packing_bag_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "oauth_clients" add constraint "oauth_clients_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "oauth_tokens" add constraint "oauth_tokens_client_id_foreign" foreign key ("client_id") references "oauth_clients" ("client_id") on delete cascade;
+alter table "oauth_tokens" add constraint "oauth_tokens_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "oauth_tokens" add constraint "oauth_tokens_parent_token_id_foreign" foreign key ("parent_token_id") references "oauth_tokens" ("id") on delete no action;
+alter table "oauth_consents" add constraint "oauth_consents_client_id_foreign" foreign key ("client_id") references "oauth_clients" ("client_id") on delete cascade;
+alter table "oauth_consents" add constraint "oauth_consents_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "notifications" add constraint "notifications_sender_id_foreign" foreign key ("sender_id") references "users" ("id") on delete set null;
+alter table "notifications" add constraint "notifications_recipient_id_foreign" foreign key ("recipient_id") references "users" ("id") on delete cascade;
+alter table "notification_channel_preferences" add constraint "notification_channel_preferences_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "mcp_tokens" add constraint "mcp_tokens_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "journeys" add constraint "journeys_user_id_foreign" foreign key ("user_id") references "users" ("id");
+alter table "trip_journal_links" add constraint "trip_journal_links_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_journal_links" add constraint "trip_journal_links_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "trip_journal_links" add constraint "trip_journal_links_linked_by_foreign" foreign key ("linked_by") references "users" ("id") on delete set null;
+alter table "trip_journal_link_members" add constraint "trip_journal_link_members_link_id_foreign" foreign key ("link_id") references "trip_journal_links" ("id") on update cascade on delete cascade;
+alter table "trip_journal_link_members" add constraint "trip_journal_link_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "journey_trips" add constraint "journey_trips_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on update cascade on delete cascade;
+alter table "journey_trips" add constraint "journey_trips_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on update cascade on delete cascade;
+alter table "journey_share_tokens" add constraint "journey_share_tokens_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_share_tokens" add constraint "journey_share_tokens_created_by_foreign" foreign key ("created_by") references "users" ("id");
+alter table "journey_photos" add constraint "journey_photos_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_photos" add constraint "journey_photos_photo_id_foreign" foreign key ("photo_id") references "trek_photos" ("id") on delete cascade;
+alter table "journey_book_revisions" add constraint "journey_book_revisions_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_recording_prep" add constraint "journey_recording_prep_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_recording_prep" add constraint "journey_recording_prep_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "journey_recorded_days" add constraint "journey_recorded_days_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_recorded_days" add constraint "journey_recorded_days_imported_by_foreign" foreign key ("imported_by") references "users" ("id") on delete set null;
+alter table "journey_contributors" add constraint "journey_contributors_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on update cascade on delete cascade;
+alter table "journey_contributors" add constraint "journey_contributors_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete no action;
+alter table "journey_books" add constraint "journey_books_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_books" add constraint "journey_books_updated_by_foreign" foreign key ("updated_by") references "users" ("id") on delete set null;
+alter table "journey_books" add constraint "journey_books_created_by_foreign" foreign key ("created_by") references "users" ("id") on delete set null;
+alter table "invite_tokens" add constraint "invite_tokens_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete set null;
+alter table "invite_tokens" add constraint "invite_tokens_created_by_foreign" foreign key ("created_by") references "users" ("id") on delete cascade;
+alter table "idempotency_keys" add constraint "idempotency_keys_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "hidden_regions" add constraint "hidden_regions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "hidden_countries" add constraint "hidden_countries_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "document_connections" add constraint "document_connections_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "document_connections" add constraint "document_connections_provider_id_foreign" foreign key ("provider_id") references "document_providers" ("id") on delete cascade;
+alter table "document_connections" add constraint "document_connections_owner_user_id_foreign" foreign key ("owner_user_id") references "users" ("id") on delete cascade;
+alter table "trip_document_links" add constraint "trip_document_links_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_document_links" add constraint "trip_document_links_connection_id_foreign" foreign key ("connection_id") references "document_connections" ("id") on delete cascade;
+alter table "trip_document_links" add constraint "trip_document_links_created_by_foreign" foreign key ("created_by") references "users" ("id") on delete set null;
+alter table "dawarich_connections" add constraint "dawarich_connections_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "collections" add constraint "collections_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete cascade;
+alter table "collection_tours" add constraint "collection_tours_collection_id_foreign" foreign key ("collection_id") references "collections" ("id") on delete cascade;
+alter table "collection_tours" add constraint "collection_tours_tour_type_foreign" foreign key ("tour_type") references "tour_types" ("key");
+alter table "collection_tours" add constraint "collection_tours_saved_by_foreign" foreign key ("saved_by") references "users" ("id") on delete set null;
+alter table "collection_labels" add constraint "collection_labels_collection_id_foreign" foreign key ("collection_id") references "collections" ("id") on delete cascade;
+alter table "collection_members" add constraint "collection_members_collection_id_foreign" foreign key ("collection_id") references "collections" ("id") on delete cascade;
+alter table "collection_members" add constraint "collection_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_polls" add constraint "collab_polls_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "collab_polls" add constraint "collab_polls_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_poll_votes" add constraint "collab_poll_votes_poll_id_foreign" foreign key ("poll_id") references "collab_polls" ("id") on delete cascade;
+alter table "collab_poll_votes" add constraint "collab_poll_votes_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_notes" add constraint "collab_notes_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "collab_notes" add constraint "collab_notes_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_messages" add constraint "collab_messages_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "collab_messages" add constraint "collab_messages_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_messages" add constraint "collab_messages_reply_to_foreign" foreign key ("reply_to") references "collab_messages" ("id") on delete set null;
+alter table "collab_message_reactions" add constraint "collab_message_reactions_message_id_foreign" foreign key ("message_id") references "collab_messages" ("id") on delete cascade;
+alter table "collab_message_reactions" add constraint "collab_message_reactions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collab_links" add constraint "collab_links_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "collab_links" add constraint "collab_links_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "categories" add constraint "categories_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete set null;
+alter table "places" add constraint "places_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "places" add constraint "places_category_id_foreign" foreign key ("category_id") references "categories" ("id") on delete set null;
+alter table "tours" add constraint "tours_place_id_foreign" foreign key ("place_id") references "places" ("id") on update cascade on delete cascade;
+alter table "tours" add constraint "tours_derived_from_place_id_foreign" foreign key ("derived_from_place_id") references "places" ("id") on delete set null;
+alter table "tours" add constraint "tours_tour_type_foreign" foreign key ("tour_type") references "tour_types" ("key");
+alter table "tour_waypoints" add constraint "tour_waypoints_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on delete cascade;
+alter table "tour_waypoints" add constraint "tour_waypoints_linked_place_id_foreign" foreign key ("linked_place_id") references "places" ("id") on delete set null;
+alter table "tour_sources" add constraint "tour_sources_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on update cascade on delete cascade;
+alter table "tour_sources" add constraint "tour_sources_imported_by_foreign" foreign key ("imported_by") references "users" ("id") on delete set null;
+alter table "tour_source_hints" add constraint "tour_source_hints_place_id_foreign" foreign key ("place_id") references "tour_sources" ("place_id") on update cascade on delete cascade;
+alter table "tour_source_hints" add constraint "tour_source_hints_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "tour_packing_links" add constraint "tour_packing_links_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on update cascade on delete cascade;
+alter table "tour_packing_links" add constraint "tour_packing_links_linked_by_foreign" foreign key ("linked_by") references "users" ("id") on delete set null;
+alter table "tour_obstacle_reviews" add constraint "tour_obstacle_reviews_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on delete cascade;
+alter table "tour_change_steps" add constraint "tour_change_steps_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on delete cascade;
+alter table "tour_activity_exclusions" add constraint "tour_activity_exclusions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "tour_activity_exclusions" add constraint "tour_activity_exclusions_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on delete cascade;
+alter table "roadtrip_day_tracks" add constraint "roadtrip_day_tracks_day_id_foreign" foreign key ("day_id") references "days" ("id") on update cascade on delete cascade;
+alter table "roadtrip_day_tracks" add constraint "roadtrip_day_tracks_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "place_regions" add constraint "place_regions_place_id_foreign" foreign key ("place_id") references "places" ("id") on update cascade on delete cascade;
+alter table "place_ratings" add constraint "place_ratings_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "place_ratings" add constraint "place_ratings_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "place_field_origin" add constraint "place_field_origin_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "place_contrib_outbox" add constraint "place_contrib_outbox_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "place_tags" add constraint "place_tags_place_id_foreign" foreign key ("place_id") references "places" ("id") on update cascade on delete cascade;
+alter table "place_tags" add constraint "place_tags_tag_id_foreign" foreign key ("tag_id") references "tags" ("id") on update cascade on delete cascade;
+alter table "photos" add constraint "photos_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "photos" add constraint "photos_day_id_foreign" foreign key ("day_id") references "days" ("id") on delete set null;
+alter table "photos" add constraint "photos_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete set null;
+alter table "journey_entries" add constraint "journey_entries_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_entries" add constraint "journey_entries_source_trip_id_foreign" foreign key ("source_trip_id") references "trips" ("id") on delete set null;
+alter table "journey_entries" add constraint "journey_entries_source_place_id_foreign" foreign key ("source_place_id") references "places" ("id") on delete set null;
+alter table "journey_entries" add constraint "journey_entries_author_id_foreign" foreign key ("author_id") references "users" ("id");
+alter table "journey_entry_stays" add constraint "journey_entry_stays_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "journey_entry_stays" add constraint "journey_entry_stays_entry_id_foreign" foreign key ("entry_id") references "journey_entries" ("id") on delete cascade;
+alter table "journey_entry_stays" add constraint "journey_entry_stays_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete set null;
+alter table "journey_entry_photos" add constraint "journey_entry_photos_entry_id_foreign" foreign key ("entry_id") references "journey_entries" ("id") on update cascade on delete cascade;
+alter table "journey_entry_photos" add constraint "journey_entry_photos_journey_photo_id_foreign" foreign key ("journey_photo_id") references "journey_photos" ("id") on update cascade on delete cascade;
+alter table "day_assignments" add constraint "day_assignments_day_id_foreign" foreign key ("day_id") references "days" ("id") on delete cascade;
+alter table "day_assignments" add constraint "day_assignments_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "tour_station_visits" add constraint "tour_station_visits_assignment_id_foreign" foreign key ("assignment_id") references "day_assignments" ("id") on delete cascade;
+alter table "tour_activities" add constraint "tour_activities_place_id_foreign" foreign key ("place_id") references "tours" ("place_id") on delete cascade;
+alter table "tour_activities" add constraint "tour_activities_track_place_id_foreign" foreign key ("track_place_id") references "tours" ("place_id") on delete cascade;
+alter table "tour_activities" add constraint "tour_activities_assignment_id_foreign" foreign key ("assignment_id") references "day_assignments" ("id") on delete set null;
+alter table "tour_activities" add constraint "tour_activities_assigned_by_foreign" foreign key ("assigned_by") references "users" ("id") on delete set null;
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_from_assignment_id_foreign" foreign key ("from_assignment_id") references "day_assignments" ("id") on delete cascade;
+alter table "roadtrip_day_boundaries" add constraint "roadtrip_day_boundaries_to_assignment_id_foreign" foreign key ("to_assignment_id") references "day_assignments" ("id") on delete cascade;
+alter table "reservations" add constraint "reservations_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "reservations" add constraint "reservations_day_id_foreign" foreign key ("day_id") references "days" ("id") on delete set null;
+alter table "reservations" add constraint "reservations_end_day_id_foreign" foreign key ("end_day_id") references "days" ("id") on delete set null;
+alter table "reservations" add constraint "reservations_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete set null;
+alter table "reservations" add constraint "reservations_assignment_id_foreign" foreign key ("assignment_id") references "day_assignments" ("id") on delete set null;
+alter table "trip_files" add constraint "trip_files_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "trip_files" add constraint "trip_files_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete set null;
+alter table "trip_files" add constraint "trip_files_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on delete set null;
+alter table "trip_files" add constraint "trip_files_note_id_foreign" foreign key ("note_id") references "collab_notes" ("id") on delete set null;
+alter table "trip_files" add constraint "trip_files_message_id_foreign" foreign key ("message_id") references "collab_messages" ("id") on delete cascade;
+alter table "trip_files" add constraint "trip_files_uploaded_by_foreign" foreign key ("uploaded_by") references "users" ("id") on delete set null;
+alter table "document_sync_items" add constraint "document_sync_items_link_id_foreign" foreign key ("link_id") references "trip_document_links" ("id") on delete cascade;
+alter table "document_sync_items" add constraint "document_sync_items_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "document_sync_items" add constraint "document_sync_items_file_id_foreign" foreign key ("file_id") references "trip_files" ("id") on delete set null;
+alter table "reservation_travelers" add constraint "reservation_travelers_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on delete cascade;
+alter table "reservation_travelers" add constraint "reservation_travelers_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "reservation_endpoints" add constraint "reservation_endpoints_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on delete cascade;
+alter table "reservation_day_positions" add constraint "reservation_day_positions_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on update cascade on delete cascade;
+alter table "reservation_day_positions" add constraint "reservation_day_positions_day_id_foreign" foreign key ("day_id") references "days" ("id") on update cascade on delete cascade;
+alter table "day_accommodations" add constraint "day_accommodations_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "day_accommodations" add constraint "day_accommodations_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete set null;
+alter table "day_accommodations" add constraint "day_accommodations_start_day_id_foreign" foreign key ("start_day_id") references "days" ("id") on delete cascade;
+alter table "day_accommodations" add constraint "day_accommodations_end_day_id_foreign" foreign key ("end_day_id") references "days" ("id") on delete cascade;
+alter table "collection_places" add constraint "collection_places_collection_id_foreign" foreign key ("collection_id") references "collections" ("id") on delete cascade;
+alter table "collection_places" add constraint "collection_places_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete cascade;
+alter table "collection_places" add constraint "collection_places_category_id_foreign" foreign key ("category_id") references "categories" ("id") on delete set null;
+alter table "collection_places" add constraint "collection_places_saved_by_foreign" foreign key ("saved_by") references "users" ("id") on delete set null;
+alter table "collection_place_ratings" add constraint "collection_place_ratings_collection_place_id_foreign" foreign key ("collection_place_id") references "collection_places" ("id") on delete cascade;
+alter table "collection_place_ratings" add constraint "collection_place_ratings_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "collection_place_tags" add constraint "collection_place_tags_collection_place_id_foreign" foreign key ("collection_place_id") references "collection_places" ("id") on update cascade on delete cascade;
+alter table "collection_place_tags" add constraint "collection_place_tags_tag_id_foreign" foreign key ("tag_id") references "tags" ("id") on update cascade on delete cascade;
+alter table "collection_place_labels" add constraint "collection_place_labels_collection_place_id_foreign" foreign key ("collection_place_id") references "collection_places" ("id") on update cascade on delete cascade;
+alter table "collection_place_labels" add constraint "collection_place_labels_label_id_foreign" foreign key ("label_id") references "collection_labels" ("id") on update cascade on delete cascade;
+alter table "budget_settlements" add constraint "budget_settlements_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "budget_settlements" add constraint "budget_settlements_from_user_id_foreign" foreign key ("from_user_id") references "users" ("id") on delete cascade;
+alter table "budget_settlements" add constraint "budget_settlements_to_user_id_foreign" foreign key ("to_user_id") references "users" ("id") on delete cascade;
+alter table "budget_settlements" add constraint "budget_settlements_created_by_user_id_foreign" foreign key ("created_by_user_id") references "users" ("id") on delete no action;
+alter table "budget_items" add constraint "budget_items_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "budget_items" add constraint "budget_items_paid_by_user_id_foreign" foreign key ("paid_by_user_id") references "users" ("id") on delete no action;
+alter table "budget_items" add constraint "budget_items_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on delete set null;
+alter table "budget_items" add constraint "budget_items_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete set null;
+alter table "file_links" add constraint "file_links_file_id_foreign" foreign key ("file_id") references "trip_files" ("id") on delete cascade;
+alter table "file_links" add constraint "file_links_reservation_id_foreign" foreign key ("reservation_id") references "reservations" ("id") on delete cascade;
+alter table "file_links" add constraint "file_links_assignment_id_foreign" foreign key ("assignment_id") references "day_assignments" ("id") on delete cascade;
+alter table "file_links" add constraint "file_links_place_id_foreign" foreign key ("place_id") references "places" ("id") on delete cascade;
+alter table "file_links" add constraint "file_links_budget_item_id_foreign" foreign key ("budget_item_id") references "budget_items" ("id") on delete set null;
+alter table "budget_item_payers" add constraint "budget_item_payers_budget_item_id_foreign" foreign key ("budget_item_id") references "budget_items" ("id") on delete cascade;
+alter table "budget_item_payers" add constraint "budget_item_payers_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "budget_item_members" add constraint "budget_item_members_budget_item_id_foreign" foreign key ("budget_item_id") references "budget_items" ("id") on delete cascade;
+alter table "budget_item_members" add constraint "budget_item_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "bucket_list" add constraint "bucket_list_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete set null;
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_accepted_place_id_foreign" foreign key ("accepted_place_id") references "places" ("id") on delete set null;
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_accepted_bucket_list_item_id_foreign" foreign key ("accepted_bucket_list_item_id") references "bucket_list" ("id") on delete set null;
+alter table "dawarich_visit_suggestions" add constraint "dawarich_visit_suggestions_matched_bucket_list_item_id_foreign" foreign key ("matched_bucket_list_item_id") references "bucket_list" ("id") on delete set null;
+alter table "audit_log" add constraint "audit_log_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete set null;
+alter table "assistant_usage" add constraint "assistant_usage_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "assistant_threads" add constraint "assistant_threads_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "assistant_threads" add constraint "assistant_threads_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "assistant_threads" add constraint "assistant_threads_journey_id_foreign" foreign key ("journey_id") references "journeys" ("id") on delete cascade;
+alter table "assistant_messages" add constraint "assistant_messages_thread_id_foreign" foreign key ("thread_id") references "assistant_threads" ("id") on delete cascade;
+alter table "assistant_tool_calls" add constraint "assistant_tool_calls_thread_id_foreign" foreign key ("thread_id") references "assistant_threads" ("id") on delete cascade;
+alter table "assistant_tool_calls" add constraint "assistant_tool_calls_message_id_foreign" foreign key ("message_id") references "assistant_messages" ("id") on delete cascade;
+alter table "assistant_attachments" add constraint "assistant_attachments_thread_id_foreign" foreign key ("thread_id") references "assistant_threads" ("id") on delete cascade;
+alter table "assistant_attachments" add constraint "assistant_attachments_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "assistant_attachments" add constraint "assistant_attachments_trip_id_foreign" foreign key ("trip_id") references "trips" ("id") on delete cascade;
+alter table "assistant_attachments" add constraint "assistant_attachments_file_id_foreign" foreign key ("file_id") references "trip_files" ("id") on delete set null;
+alter table "assignment_participants" add constraint "assignment_participants_assignment_id_foreign" foreign key ("assignment_id") references "day_assignments" ("id") on delete cascade;
+alter table "assignment_participants" add constraint "assignment_participants_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "ai_connections" add constraint "ai_connections_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "user_sessions" add constraint "user_sessions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_plans" add constraint "vacay_plans_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete cascade;
+alter table "vacay_plan_members" add constraint "vacay_plan_members_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_plan_members" add constraint "vacay_plan_members_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_holiday_calendars" add constraint "vacay_holiday_calendars_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_entries" add constraint "vacay_entries_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_entries" add constraint "vacay_entries_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_company_holidays" add constraint "vacay_company_holidays_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_shares" add constraint "vacay_shares_owner_id_foreign" foreign key ("owner_id") references "users" ("id") on delete cascade;
+alter table "vacay_shares" add constraint "vacay_shares_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_user_colors" add constraint "vacay_user_colors_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_user_colors" add constraint "vacay_user_colors_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_user_settings" add constraint "vacay_user_settings_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "vacay_user_years" add constraint "vacay_user_years_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "vacay_user_years" add constraint "vacay_user_years_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "vacay_years" add constraint "vacay_years_plan_id_foreign" foreign key ("plan_id") references "vacay_plans" ("id") on delete cascade;
+alter table "visited_countries" add constraint "visited_countries_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "visited_regions" add constraint "visited_regions_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "wanderer_connections" add constraint "wanderer_connections_user_id_foreign" foreign key ("user_id") references "users" ("id") on update cascade on delete cascade;
+alter table "webauthn_challenges" add constraint "webauthn_challenges_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+alter table "webauthn_credentials" add constraint "webauthn_credentials_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete cascade;
+insert into "app_settings" ("key", "value") values ('app_version', '3.0.15'), ('places_photos_enabled', 'true'), ('places_autocomplete_enabled', 'true'), ('places_details_enabled', 'true');
+insert into "addons" ("id", "name", "description", "type", "icon", "enabled", "config", "sort_order") values ('collab', 'Collab', 'Notes, polls, and live chat for trip collaboration', 'trip', 'Users', '1', '{}', '6'), ('memories', 'Photos', NULL, 'trip', 'Image', '0', '{}', '7'), ('mcp', 'MCP', 'Model Context Protocol for AI assistant integration', 'integration', 'Terminal', '0', '{}', '12'), ('naver_list_import', 'Naver List Import', 'Import places from shared Naver Maps lists', 'integration', 'Link2', '1', '{}', '13'), ('journey', 'Journey', 'Trip tracking & travel journal — check-ins, photos, daily stories', 'global', 'Compass', '1', '{}', '35'), ('airtrail', 'AirTrail', 'Sync flights from your self-hosted AirTrail instance', 'integration', 'Plane', '0', '{}', '14'), ('collections', 'Collections', 'Personal place library — save places across trips into named lists, copy into any trip, share with others', 'global', 'Bookmark', '1', '{}', '16');
+insert into "photo_providers" ("id", "name", "description", "icon", "enabled", "sort_order") values ('synologyphotos', 'Synology Photos', 'Synology Photos integration with separate account settings', 'Image', '0', '1');
+insert into "photo_provider_fields" ("id", "provider_id", "field_key", "label", "input_type", "placeholder", "hint", "required", "secret", "settings_key", "payload_key", "sort_order") values ('1', 'synologyphotos', 'synology_url', 'providerUrl', 'url', 'https://synology.example.com', 'providerUrlHintSynology', '1', '0', 'synology_url', 'synology_url', '0'), ('2', 'synologyphotos', 'synology_username', 'providerUsername', 'text', 'Username', NULL, '1', '0', 'synology_username', 'synology_username', '1'), ('3', 'synologyphotos', 'synology_password', 'providerPassword', 'password', 'Password', NULL, '1', '1', NULL, 'synology_password', '2'), ('4', 'synologyphotos', 'synology_otp', 'providerOTP', 'text', '123456', NULL, '0', '0', NULL, 'synology_otp', '3'), ('5', 'synologyphotos', 'synology_skip_ssl', 'skipSSLVerification', 'checkbox', NULL, NULL, '0', '0', 'synology_skip_ssl', 'synology_skip_ssl', '4');
+select setval(seq, greatest(max_id, 1)) from (select pg_get_serial_sequence('"photo_provider_fields"', 'id') as seq, max("id") as max_id from "photo_provider_fields") ids where seq is not null;
+insert into "document_providers" ("id", "name", "description", "icon", "enabled", "sort_order") values ('paperless', 'Paperless-ngx', 'Two-way document sync with a Paperless-ngx instance, scoped by tag', 'FileText', '0', '0'), ('papra', 'Papra', 'Two-way document sync with a Papra organisation, scoped by tag', 'FileText', '0', '1'), ('nextcloud', 'Nextcloud', 'Two-way document sync with a Nextcloud folder over WebDAV', 'Cloud', '0', '2'), ('opencloud', 'OpenCloud', 'Two-way document sync with an OpenCloud space over WebDAV', 'Cloud', '0', '3'), ('synologydrive', 'Synology Drive', 'Two-way document sync with a folder on a Synology NAS', 'HardDrive', '0', '4');
+insert into "document_provider_fields" ("id", "provider_id", "field_key", "label", "input_type", "placeholder", "hint", "required", "secret", "sort_order") values ('1', 'paperless', 'base_url', 'providerUrl', 'url', 'https://paperless.example.com', NULL, '1', '0', '0'), ('2', 'paperless', 'api_token', 'providerApiToken', 'password', 'API token', 'hintPaperlessToken', '1', '1', '1'), ('3', 'paperless', 'allow_insecure_tls', 'allowInsecureTls', 'checkbox', NULL, NULL, '0', '0', '2'), ('4', 'papra', 'base_url', 'providerUrl', 'url', 'https://papra.example.com', NULL, '1', '0', '0'), ('5', 'papra', 'api_key', 'providerApiKey', 'password', 'ppapi_…', 'hintPapraKey', '1', '1', '1'), ('6', 'papra', 'organization_id', 'providerOrganization', 'text', 'org_…', 'hintPapraOrg', '1', '0', '2'), ('7', 'papra', 'allow_insecure_tls', 'allowInsecureTls', 'checkbox', NULL, NULL, '0', '0', '3'), ('8', 'nextcloud', 'base_url', 'providerUrl', 'url', 'https://cloud.example.com', NULL, '1', '0', '0'), ('9', 'nextcloud', 'login_name', 'providerUsername', 'text', 'username', 'hintNextcloudLogin', '1', '0', '1'), ('10', 'nextcloud', 'app_password', 'providerAppPassword', 'password', 'app password', 'hintNextcloudAppPassword', '1', '1', '2'), ('11', 'nextcloud', 'base_path', 'providerBasePath', 'text', '/TREK', 'hintBasePath', '0', '0', '3'), ('12', 'nextcloud', 'allow_insecure_tls', 'allowInsecureTls', 'checkbox', NULL, NULL, '0', '0', '4'), ('13', 'opencloud', 'base_url', 'providerUrl', 'url', 'https://opencloud.example.com', NULL, '1', '0', '0'), ('14', 'opencloud', 'username', 'providerUsername', 'text', 'username', NULL, '1', '0', '1'), ('15', 'opencloud', 'app_token', 'providerAppToken', 'password', 'app token', 'hintOpenCloudToken', '1', '1', '2'), ('16', 'opencloud', 'allow_insecure_tls', 'allowInsecureTls', 'checkbox', NULL, NULL, '0', '0', '3'), ('17', 'synologydrive', 'base_url', 'providerUrl', 'url', 'https://nas.example.com:5001', 'hintSynologyUrl', '1', '0', '0'), ('18', 'synologydrive', 'username', 'providerUsername', 'text', 'username', 'hintSynologyUser', '1', '0', '1'), ('19', 'synologydrive', 'password', 'providerPassword', 'password', 'password', NULL, '1', '1', '2'), ('20', 'synologydrive', 'otp_code', 'providerOTP', 'text', '123456', 'hintSynologyOtp', '0', '1', '3'), ('21', 'synologydrive', 'base_path', 'providerBasePath', 'text', '/trek', 'hintBasePath', '0', '0', '4'), ('22', 'synologydrive', 'allow_insecure_tls', 'allowInsecureTls', 'checkbox', NULL, NULL, '0', '0', '5');
+select setval(seq, greatest(max_id, 1)) from (select pg_get_serial_sequence('"document_provider_fields"', 'id') as seq, max("id") as max_id from "document_provider_fields") ids where seq is not null;
+insert into "tour_types" ("key", "label_key", "icon", "color", "routing_profile", "is_sport", "enabled", "sort_order") values ('hike', 'tourTypes.hike', 'Mountain', '#16a34a', 'pedestrian', '1', '1', '0'), ('bike', 'tourTypes.bike', 'Bike', '#2563eb', 'bicycle', '1', '1', '1'), ('run', 'tourTypes.run', 'Footprints', '#dc2626', 'pedestrian', '1', '1', '2'), ('ski', 'tourTypes.ski', 'MountainSnow', '#0891b2', NULL, '1', '1', '3'), ('kayak', 'tourTypes.kayak', 'Sailboat', '#0d9488', NULL, '1', '1', '4'), ('walk', 'tourTypes.walk', 'Landmark', '#9333ea', 'pedestrian', '0', '1', '5'), ('food', 'tourTypes.food', 'UtensilsCrossed', '#ea580c', 'pedestrian', '0', '0', '6'), ('horse', 'tourTypes.horse', 'Horseshoe', '#a16207', NULL, '1', '1', '7');
+CREATE FUNCTION trg_release_stop_on_stay_delete() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ UPDATE day_assignments SET accommodation_id = NULL WHERE accommodation_id = OLD.id;
+ RETURN OLD;
+END
+$$;
+CREATE TRIGGER trg_release_stop_on_stay_delete AFTER DELETE ON day_accommodations FOR EACH ROW EXECUTE FUNCTION trg_release_stop_on_stay_delete();
+CREATE FUNCTION trg_place_regions_follow_place() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ DELETE FROM place_regions WHERE place_id = NEW.id;
+ RETURN NEW;
+END
+$$;
+CREATE TRIGGER trg_place_regions_follow_place AFTER UPDATE OF lat, lng, address ON places FOR EACH ROW WHEN (OLD.lat IS DISTINCT FROM NEW.lat OR OLD.lng IS DISTINCT FROM NEW.lng OR OLD.address IS DISTINCT FROM NEW.address) EXECUTE FUNCTION trg_place_regions_follow_place();
diff --git a/server/package.json b/server/package.json
index 0a8ae09a3..8e77040e6 100644
--- a/server/package.json
+++ b/server/package.json
@@ -21,6 +21,7 @@
"lint:strict": "node scripts/strict-lint.mjs",
"lint:boundaries": "node scripts/import-boundaries.mjs",
"lint:dialect": "node scripts/dialect-lint.mjs",
+ "lint:engine-dispatch": "node scripts/engine-dispatch-ratchet.mjs",
"lint:tx": "node scripts/tx-writes.mjs",
"lint:test-sql": "node scripts/test-sql-ratchet.mjs",
"lint:test-mocks": "node scripts/test-mocks-ratchet.mjs",
@@ -36,6 +37,8 @@
"check:entities": "node --import tsx scripts/generate-entities.ts --check",
"gen:db-types": "node --import tsx scripts/generate-db-types.ts",
"check:db-types": "node --import tsx scripts/generate-db-types.ts --check",
+ "gen:pg-baseline": "node --import tsx scripts/generate-pg-baseline.ts",
+ "check:pg-baseline": "node --import tsx scripts/generate-pg-baseline.ts --check",
"probe:pg": "node --import tsx scripts/pg-probe.ts",
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
"format:check": "prettier --check \"src/**/*.ts\" \"tests/**/*.ts\"",
@@ -48,6 +51,7 @@
"test:integration": "vitest run tests/integration",
"test:ws": "vitest run tests/websocket",
"test:e2e": "vitest run tests/e2e",
+ "test:pg": "vitest run --config vitest.pg.config.ts",
"test:coverage": "vitest run --coverage"
},
"mikro-orm": {
@@ -63,6 +67,7 @@
"@mikro-orm/core": "^7.2.7",
"@mikro-orm/migrations": "^7.2.7",
"@mikro-orm/nestjs": "^7.1.0",
+ "@mikro-orm/postgresql": "^7.2.7",
"@mikro-orm/seeder": "^7.2.7",
"@mikro-orm/sql": "^7.2.1",
"@mikro-orm/sqlite": "^7.2.7",
@@ -117,7 +122,6 @@
"@aws-lite/s3-types": "^0.2.8",
"@eslint/js": "^10.0.1",
"@mikro-orm/entity-generator": "^7.2.7",
- "@mikro-orm/postgresql": "^7.2.7",
"@nestjs/testing": "^11.2.5",
"@swc/core": "^1.16.22",
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
diff --git a/server/scripts/dialect-baseline.json b/server/scripts/dialect-baseline.json
index a10b22214..c9ef31ada 100644
--- a/server/scripts/dialect-baseline.json
+++ b/server/scripts/dialect-baseline.json
@@ -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
}
diff --git a/server/scripts/dialect-lint.mjs b/server/scripts/dialect-lint.mjs
index 72681c47b..0f207376e 100644
--- a/server/scripts/dialect-lint.mjs
+++ b/server/scripts/dialect-lint.mjs
@@ -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) {
diff --git a/server/scripts/engine-dispatch-baseline.json b/server/scripts/engine-dispatch-baseline.json
new file mode 100644
index 000000000..a4d2d367a
--- /dev/null
+++ b/server/scripts/engine-dispatch-baseline.json
@@ -0,0 +1,3 @@
+{
+ "counts": {}
+}
diff --git a/server/scripts/engine-dispatch-ratchet.mjs b/server/scripts/engine-dispatch-ratchet.mjs
new file mode 100644
index 000000000..29e4ca0cb
--- /dev/null
+++ b/server/scripts/engine-dispatch-ratchet.mjs
@@ -0,0 +1,50 @@
+#!/usr/bin/env node
+/*
+ * lint:engine-dispatch: code outside the dialect layer and the engine port
+ * never asks which database it runs on.
+ *
+ * What differs between SQLite and Postgres has two homes: the SQL helpers in
+ * src/db/dialect/ and the engine implementations in src/db/engine/ (the
+ * driver, the schema bootstrap, maintenance, backups). Anywhere else, an
+ * `isSqlite(`/`isPostgres(` check, an `instanceof` on a platform class or a
+ * comparison on the engine's name (`readEnv().db.engine === 'postgres'`) is a
+ * branch a third engine would have to find by searching, so each file is held
+ * to its entry in scripts/engine-dispatch-baseline.json and a file without one
+ * holds none (scripts/lib/count-ratchet.mjs has the rules). Comments do not
+ * count.
+ *
+ * npm run lint:engine-dispatch check against the baseline (CI)
+ * npm run lint:engine-dispatch -- --update lower the counts; never raises or adds an entry
+ */
+import { cliMain, stripComments } from './lib/count-ratchet.mjs';
+
+const DISPATCH = new RegExp(
+ [
+ /\bis(?:Sqlite|Postgres)\s*\(/.source,
+ /\binstanceof\s+(?:Sqlite|BasePostgreSql|PostgreSql)Platform\b/.source,
+ // A comparison on the engine's name: `db.engine === 'postgres'`, `engine.id !== 'sqlite'`.
+ /\.(?:engine|id)\s*[!=]==?\s*['"](?:sqlite|postgres)['"]/.source,
+ ].join('|'),
+ 'g',
+);
+
+/** Where an engine may be named: the dialect helpers and the engine port. */
+const ENGINE_HOMES = ['src/db/dialect/', 'src/db/engine/'];
+
+/** The engine checks in one file's text, comments left out. */
+export function countEngineDispatch(text) {
+ return stripComments(text).match(DISPATCH)?.length ?? 0;
+}
+
+export const check = {
+ name: 'engine-dispatch',
+ script: 'lint:engine-dispatch',
+ root: 'src',
+ baseline: 'scripts/engine-dispatch-baseline.json',
+ unit: 'engine check(s)',
+ advice: 'Dispatch through a helper in src/db/dialect/ or the DatabaseEngine in src/db/engine/ instead.',
+ include: (file) => file.endsWith('.ts') && !ENGINE_HOMES.some((home) => file.startsWith(home)),
+ count: (text) => countEngineDispatch(text),
+};
+
+cliMain(check, import.meta.url);
diff --git a/server/scripts/generate-pg-baseline.ts b/server/scripts/generate-pg-baseline.ts
new file mode 100644
index 000000000..3c578c88d
--- /dev/null
+++ b/server/scripts/generate-pg-baseline.ts
@@ -0,0 +1,229 @@
+/**
+ * Generates the Postgres baseline: the schema a fresh Postgres database starts
+ * from (src/db/engine/postgres/baseline.ts says how a boot uses it).
+ *
+ * The entities describe the migrated SQLite schema and are pinned to it
+ * (`check:entities`), so the baseline is MikroORM's schema generator over the
+ * entities, for the Postgres platform, after the adjustments every Postgres
+ * schema in this repository shares (src/db/engine/postgres/schema-adjust.ts):
+ * TEXT timestamps, portable defaults, the `nocase` collation. Unlike the
+ * probe's schema it keeps the foreign keys. No database is needed: the
+ * generator reads only the metadata and the platform.
+ *
+ * The baseline stands for every migration up to the newest one when it was
+ * generated (`through`). Once a migration lands after it, the baseline is
+ * frozen like a shipped migration: the newer migration runs on Postgres
+ * through the migrator, and `--check` only verifies the manifest.
+ *
+ * Usage:
+ * node --import tsx scripts/generate-pg-baseline.ts # rewrite server/assets/postgres/
+ * node --import tsx scripts/generate-pg-baseline.ts --check # exit 1 when it drifted (CI)
+ */
+import {
+ baselineMatches,
+ readPostgresBaseline,
+ writePostgresBaseline,
+ type PostgresBaseline,
+} from '../src/db/engine/postgres/baseline';
+import { POSTGRES_TRIGGERS } from '../src/db/engine/postgres/postgres-triggers';
+import { applyPostgresColumnAdjustments, NOCASE_COLLATION_SQL } from '../src/db/engine/postgres/schema-adjust';
+import { splitStatements } from '../src/db/engine/postgres/sql-split';
+import { ALL_ENTITIES } from '../src/db/entities';
+import { knownMigrationNames } from '../src/db/known-migrations';
+import { migrateTempDb, tempDbPath } from './generate-entities';
+import { MikroORM } from '@mikro-orm/postgresql';
+
+import Database from 'better-sqlite3';
+import fs from 'node:fs';
+
+/** What one pass over the migrated SQLite database yields: the baseline, and the triggers the migrations leave there. */
+interface BaselinePass {
+ baseline: PostgresBaseline;
+ sqliteTriggers: string[];
+}
+
+/** The baseline the entities and the migrations on disk describe now, from one migration of the SQLite database. */
+async function generateBaseline(known: Set = knownMigrationNames()): Promise {
+ const migrations = [...known].sort();
+ const through = migrations.at(-1);
+ if (!through) throw new Error('no migrations found');
+ const orm = await MikroORM.init({
+ clientUrl: 'postgresql://generator@localhost/trek',
+ entities: [...ALL_ENTITIES],
+ discovery: { warnWhenNoEntities: false },
+ });
+ try {
+ for (const meta of orm.getMetadata().getAll().values()) applyPostgresColumnAdjustments(meta.props);
+ const migrated = await readMigratedSqlite();
+ const statements = [
+ NOCASE_COLLATION_SQL,
+ ...splitStatements(await orm.schema.getCreateSchemaSQL({ wrap: false })),
+ ...migrated.tables.flatMap((table) => rowStatements(table.name, table.rows)),
+ ...Object.values(POSTGRES_TRIGGERS).flat(),
+ ];
+ return { baseline: { through, migrations, statements }, sqliteTriggers: migrated.triggers };
+ } finally {
+ await orm.close();
+ }
+}
+
+/**
+ * What `--check` holds, as the problems it finds. Every manifest must name
+ * migrations that exist and every migration up to `through`. While no migration
+ * is newer than the committed baseline (`through` matches), the baseline is
+ * not frozen: it must be exactly what the entities generate, and each SQLite
+ * trigger needs a port. Once a newer migration lands, the baseline is frozen
+ * and only the manifest is checked.
+ */
+export function checkBaseline(
+ committed: PostgresBaseline,
+ generated: PostgresBaseline,
+ matches: boolean,
+ sqliteTriggers: readonly string[],
+): string[] {
+ const known = new Set(generated.migrations);
+ const problems: string[] = [];
+ const missing = committed.migrations.filter((name) => !known.has(name));
+ if (missing.length > 0) problems.push(`the baseline names migrations that do not exist: ${missing.join(', ')}`);
+ const unlisted = generated.migrations.filter(
+ (name) => name <= committed.through && !committed.migrations.includes(name),
+ );
+ if (unlisted.length > 0)
+ problems.push(`migrations up to ${committed.through} are missing from it: ${unlisted.join(', ')}`);
+ const frozen = committed.through !== generated.through;
+ if (frozen) return problems;
+ if (!matches) problems.push('the entities generate a different schema; run npm run gen:pg-baseline');
+ problems.push(...triggerPortProblems(sqliteTriggers, Object.keys(POSTGRES_TRIGGERS)));
+ return problems;
+}
+
+/** Migration bookkeeping: the baseline's manifest stands for it on Postgres. */
+const BOOKKEEPING_TABLES = new Set(['mikro_orm_migrations', 'migrations', 'schema_version']);
+
+type Row = Record;
+
+/** What the migrations leave in a SQLite database: its triggers, and the rows they insert, table by table in creation order. */
+interface MigratedSchema {
+ triggers: string[];
+ tables: Array<{ name: string; rows: Row[] }>;
+}
+
+/** The tables and triggers of a SQLite database in creation order, each as its type and name. */
+function listSchemaObjects(conn: Database.Database): Array<{ type: string; name: string }> {
+ return conn
+ .prepare(
+ "select type, name from sqlite_master where type in ('table', 'trigger') and name not like 'sqlite_%' order by rowid",
+ )
+ .all() as Array<{ type: string; name: string }>;
+}
+
+/** Reads a migrated SQLite database, read-only. */
+function readMigratedSchema(dbPath: string): MigratedSchema {
+ const conn = new Database(dbPath, { readonly: true });
+ try {
+ const objects = listSchemaObjects(conn);
+ const tables = objects
+ .filter((object) => object.type === 'table' && !BOOKKEEPING_TABLES.has(object.name))
+ .map((object) => ({
+ name: object.name,
+ rows: conn.prepare(`select * from "${object.name}" order by rowid`).all() as Row[],
+ }))
+ .filter((table) => table.rows.length > 0);
+ const triggers = objects
+ .filter((object) => object.type === 'trigger')
+ .map((object) => object.name)
+ .sort();
+ return { triggers, tables };
+ } finally {
+ conn.close();
+ }
+}
+
+/** Migrates a temporary SQLite database and reads what the migrations leave in it; the file is removed afterwards. */
+async function readMigratedSqlite(): Promise {
+ const dbPath = tempDbPath();
+ try {
+ await migrateTempDb(dbPath);
+ return readMigratedSchema(dbPath);
+ } finally {
+ fs.rmSync(dbPath, { force: true });
+ }
+}
+
+/** A value as a Postgres literal the column's own type takes in: quoted text, which Postgres casts on insert. */
+function literal(value: unknown): string {
+ if (value === null || value === undefined) return 'NULL';
+ if (Buffer.isBuffer(value)) throw new Error('a migration inserted binary data, which the baseline cannot carry');
+ return `'${String(value).replace(/'/g, "''")}'`;
+}
+
+/**
+ * The rows a migration inserted into `table` (reference data such as the tour
+ * types and the provider catalogues), as the statements that put them in a
+ * Postgres database the baseline creates, then move the table's id sequence,
+ * if it has one, past them. A migration up to the cut never runs on Postgres,
+ * so without these the database would start without what every SQLite
+ * install has before its first seeder.
+ */
+export function rowStatements(table: string, rows: readonly Row[]): string[] {
+ if (rows.length === 0) return [];
+ const columns = Object.keys(rows[0]);
+ const values = rows.map((row) => `(${columns.map((column) => literal(row[column])).join(', ')})`);
+ const statements = [
+ `insert into "${table}" (${columns.map((c) => `"${c}"`).join(', ')}) values ${values.join(', ')}`,
+ ];
+ if (rows.every((row) => typeof row.id === 'number')) {
+ statements.push(
+ `select setval(seq, greatest(max_id, 1)) from (select pg_get_serial_sequence('"${table}"', 'id') as seq, ` +
+ `max("id") as max_id from "${table}") ids where seq is not null`,
+ );
+ }
+ return statements;
+}
+
+/**
+ * The SQLite triggers without a Postgres port in `POSTGRES_TRIGGERS`, and the
+ * ports whose trigger is gone. The entities carry no triggers, so this is
+ * what keeps the hand-written ports level with the migrations.
+ */
+export function triggerPortProblems(sqliteTriggers: readonly string[], ported: readonly string[]): string[] {
+ const problems: string[] = [];
+ const missing = sqliteTriggers.filter((name) => !ported.includes(name));
+ if (missing.length > 0)
+ problems.push(`SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: ${missing.join(', ')}`);
+ const stale = ported.filter((name) => !sqliteTriggers.includes(name));
+ if (stale.length > 0)
+ problems.push(`POSTGRES_TRIGGERS ports triggers the migrations no longer create: ${stale.join(', ')}`);
+ return problems;
+}
+
+/** The default mode: writes the baseline the entities and the migrations describe. */
+async function writeMode(): Promise {
+ const { baseline } = await generateBaseline();
+ writePostgresBaseline(baseline);
+ console.log(`[generate-pg-baseline] wrote ${baseline.statements.length} statements through ${baseline.through}`);
+}
+
+/** `--check` (CI): exits 1, naming every problem, when the committed baseline has drifted. */
+async function checkMode(): Promise {
+ const { baseline: generated, sqliteTriggers } = await generateBaseline();
+ const committed = readPostgresBaseline();
+ const problems = checkBaseline(committed, generated, baselineMatches(generated), sqliteTriggers);
+ if (problems.length > 0) {
+ for (const problem of problems) console.error(`[generate-pg-baseline] ${problem}`);
+ process.exit(1);
+ }
+ console.log('[generate-pg-baseline] --check: the Postgres baseline matches the entities and the migrations');
+}
+
+async function main(): Promise {
+ if (process.argv.slice(2).includes('--check')) await checkMode();
+ else await writeMode();
+}
+
+if (require.main === module) {
+ main().catch((error: unknown) => {
+ console.error(error);
+ process.exit(1);
+ });
+}
diff --git a/server/scripts/pg-probe-baseline.json b/server/scripts/pg-probe-baseline.json
index c4916d571..60193d1ed 100644
--- a/server/scripts/pg-probe-baseline.json
+++ b/server/scripts/pg-probe-baseline.json
@@ -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": []
}
diff --git a/server/scripts/pg-probe.ts b/server/scripts/pg-probe.ts
index 3d9cb2baf..62f39d86e 100644
--- a/server/scripts/pg-probe.ts
+++ b/server/scripts/pg-probe.ts
@@ -6,7 +6,7 @@
* repositories. Nothing tested that claim until this probe. Given an empty
* Postgres database it
*
- * 1. derives a Postgres schema from the entities (pg-probe/schema.ts);
+ * 1. derives a Postgres schema from the entities (src/db/engine/postgres/schema-adjust.ts);
* 2. runs every dialect helper on SQLite and on Postgres and checks both
* return what the helper promises (pg-probe/helper-cases.ts); every
* case must pass;
@@ -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
diff --git a/server/scripts/pg-probe/arg-samples.ts b/server/scripts/pg-probe/arg-samples.ts
index e6273f1f4..37ef2f02f 100644
--- a/server/scripts/pg-probe/arg-samples.ts
+++ b/server/scripts/pg-probe/arg-samples.ts
@@ -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,13 @@ 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. The
+ * label is spelled in `presenceSet`'s signature, which a test pins.
+ */
+export const PRESENCE_LABEL = 'present';
const SAMPLE_DATE = '2026-01-01';
const SAMPLE_TIMESTAMP = '2026-01-01 00:00:00';
@@ -68,14 +76,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 +96,15 @@ 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);
}
diff --git a/server/scripts/pg-probe/baseline.ts b/server/scripts/pg-probe/baseline.ts
index 879604355..f3590daff 100644
--- a/server/scripts/pg-probe/baseline.ts
+++ b/server/scripts/pg-probe/baseline.ts
@@ -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$]*$/;
diff --git a/server/scripts/pg-probe/engines.ts b/server/scripts/pg-probe/engines.ts
index 0903d4ec4..35388edc9 100644
--- a/server/scripts/pg-probe/engines.ts
+++ b/server/scripts/pg-probe/engines.ts
@@ -5,6 +5,8 @@
* SQLite database the helper cases are compared on, and the loop that calls
* every repository method.
*/
+import { applyPostgresColumnAdjustments, NOCASE_COLLATION_SQL } from '../../src/db/engine/postgres/schema-adjust';
+import { splitStatements } from '../../src/db/engine/postgres/sql-split';
import { ALL_ENTITIES } from '../../src/db/entities';
import type { MethodPlan } from './arg-samples';
import {
@@ -18,8 +20,7 @@ import {
} from './helper-cases';
import { ProbePostgresDialect, type StatementRecorder } from './recorder';
import type { MethodResult } from './report';
-import { adjustColumns, NOCASE_COLLATION_SQL, splitStatements, type ColumnMeta } from './schema';
-import { RequestContext, type Configuration, type EntityManager, type EntityProperty } from '@mikro-orm/core';
+import { RequestContext, type Configuration, type EntityManager } from '@mikro-orm/core';
import { MikroORM, PostgreSqlConnection, PostgreSqlDriver, type Options } from '@mikro-orm/postgresql';
import { SqlitePlatform } from '@mikro-orm/sql';
@@ -129,7 +130,7 @@ export async function prepareDatabase(
await run(NOCASE_COLLATION_SQL);
let adjusted = 0;
for (const meta of orm.getMetadata().getAll().values()) {
- adjusted += adjustColumns(meta.props as EntityProperty[] as ColumnMeta[]).length;
+ adjusted += applyPostgresColumnAdjustments(meta.props).length;
}
const statements = splitStatements(await orm.schema.getCreateSchemaSQL({ wrap: false }));
for (const statement of statements) await run(statement);
diff --git a/server/scripts/pg-probe/helper-cases.ts b/server/scripts/pg-probe/helper-cases.ts
index 3974ec3d7..92b5b7936 100644
--- a/server/scripts/pg-probe/helper-cases.ts
+++ b/server/scripts/pg-probe/helper-cases.ts
@@ -44,6 +44,7 @@ import {
dateOf,
dayDistance,
foundAgainState,
+ ilike,
lower,
lowerParam,
lowerTrim,
@@ -269,6 +270,18 @@ export const HELPER_CASES: readonly HelperCase[] = [
raw: (p) => rawOf(dayDistance(p, 'a', '2026-09-24')),
expect: { equals: 3 },
},
+ {
+ name: 'ilike matches in other case',
+ row: { a: 'Probe Place' },
+ raw: (p) => ({ sql: ilike(p, ['a']), params: ['%PLACE%'] }),
+ expect: { equals: true },
+ },
+ {
+ name: 'ilike keeps the escaped wildcard literal',
+ row: { a: 'Probe Place' },
+ raw: (p) => ({ sql: ilike(p, ['a']), params: ['probe\\_place'] }),
+ expect: { equals: false },
+ },
{
name: 'collateNoCase',
row: { a: 'Probe' },
diff --git a/server/scripts/pg-probe/report.ts b/server/scripts/pg-probe/report.ts
index 10feb0ea7..75c9a2965 100644
--- a/server/scripts/pg-probe/report.ts
+++ b/server/scripts/pg-probe/report.ts
@@ -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;
/**
@@ -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.', '');
diff --git a/server/scripts/pg-probe/schema.ts b/server/scripts/pg-probe/schema.ts
deleted file mode 100644
index f2b7042f6..000000000
--- a/server/scripts/pg-probe/schema.ts
+++ /dev/null
@@ -1,106 +0,0 @@
-/**
- * The Postgres schema the probe runs against, derived from the entities.
- *
- * The entities are generated from the migrated SQLite schema and a parity
- * test pins them to it (`npm run check:entities`), so they are the one
- * description of the tables both engines can read. MikroORM's schema
- * generator turns them into Postgres DDL after three adjustments that keep
- * the probe about the SQL the repositories send, not about the storage
- * decisions a real Postgres port still has to make:
- *
- * - timestamps stay TEXT, the storage model the services and the dialect
- * helpers are written for (server/CLAUDE.md, "Storage model");
- * - a SQLite-only default (`datetime('now')`, `strftime('%s','now')`) gets
- * its Postgres equivalent, and a column declared `COLLATE NOCASE` uses
- * the `nocase` collation `collateNoCase()` names on Postgres;
- * - no foreign keys, so a statement is never refused only because the
- * sample row it writes points at nothing (every statement meets empty
- * tables).
- *
- * Unique and partial unique indexes stay, because `ON CONFLICT` needs them.
- */
-import { pgTimestampText, PG_UTC_NOW } from '../../src/db/dialect/platform';
-
-/** SQLite's timestamp text on Postgres, for a column default. */
-export const PG_NOW_TEXT_DEFAULT = pgTimestampText(PG_UTC_NOW);
-
-/** The case-insensitive collation `collateNoCase()` names on Postgres. */
-export const NOCASE_COLLATION_SQL =
- "CREATE COLLATION IF NOT EXISTS nocase (provider = icu, locale = 'und-u-ks-level2', deterministic = false)";
-
-/** The Postgres spelling of a column default the entities carry in SQLite's. */
-export function portableDefault(raw: string): string {
- const text = raw
- .trim()
- .replace(/^\((.*)\)$/s, '$1')
- .trim();
- if (/^current_timestamp$/i.test(text) || /^datetime\(\s*'now'\s*\)$/i.test(text)) return PG_NOW_TEXT_DEFAULT;
- if (/^strftime\(\s*'%s'\s*,\s*'now'\s*\)$/i.test(text))
- return 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)';
- return raw;
-}
-
-/**
- * Splits generated DDL into single statements on `;` outside quotes, so one
- * statement Postgres refuses does not take the rest of the schema with it.
- */
-export function splitStatements(sql: string): string[] {
- const statements: string[] = [];
- let current = '';
- let quote: '"' | "'" | null = null;
- for (const char of sql) {
- if (quote) {
- if (char === quote) quote = null;
- current += char;
- } else if (char === "'" || char === '"') {
- quote = char;
- current += char;
- } else if (char === ';') {
- if (current.trim()) statements.push(current.trim());
- current = '';
- } else {
- current += char;
- }
- }
- if (current.trim()) statements.push(current.trim());
- return statements.filter((statement) => !/^set\s/i.test(statement));
-}
-
-/** The subset of MikroORM's property metadata the schema adjustments touch. */
-export interface ColumnMeta {
- name: string;
- defaultRaw?: string;
- collation?: string;
- columnTypes?: string[];
- customType?: { constructor: { name: string } };
-}
-
-/**
- * Applies the adjustments above to one entity's properties in place:
- * timestamp columns become `text`, SQLite-only defaults and the NOCASE
- * collation their Postgres spelling. Returns the names of the properties
- * it changed, for the log.
- */
-export function adjustColumns(properties: readonly ColumnMeta[]): string[] {
- const changed: string[] = [];
- for (const property of properties) {
- let touched = false;
- if (property.customType?.constructor.name === 'DbTimestampType' && property.columnTypes) {
- property.columnTypes = property.columnTypes.map(() => 'text');
- touched = true;
- }
- if (typeof property.defaultRaw === 'string') {
- const next = portableDefault(property.defaultRaw);
- if (next !== property.defaultRaw) {
- property.defaultRaw = next;
- touched = true;
- }
- }
- if (property.collation?.toUpperCase() === 'NOCASE') {
- property.collation = 'nocase';
- touched = true;
- }
- if (touched) changed.push(property.name);
- }
- return changed;
-}
diff --git a/server/scripts/size-baseline.json b/server/scripts/size-baseline.json
index 1a7259048..9a6725939 100644
--- a/server/scripts/size-baseline.json
+++ b/server/scripts/size-baseline.json
@@ -1,8 +1,8 @@
{
"scripts/db-call-graph.mjs": 1036,
"scripts/generate-entities.ts": 2259,
- "src/db/repositories/Places.repository.ts": 1437,
- "src/db/repositories/Reservations.repository.ts": 1558,
+ "src/db/repositories/Places.repository.ts": 1435,
+ "src/db/repositories/Reservations.repository.ts": 1551,
"src/db/repositories/Trips.repository.ts": 1120,
"src/db/repositories/Users.repository.ts": 1445,
"src/nest/atlas/atlas.service.ts": 1174,
@@ -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": 1546,
"src/nest/plugins/plugin-runtime.service.ts": 1460,
"src/nest/plugins/runtime/plugin-sdk.ts": 1093,
"src/nest/reservations/reservations.service.ts": 1168,
diff --git a/server/scripts/test-new-service-baseline.json b/server/scripts/test-new-service-baseline.json
index 2c665ccbc..d47d20111 100644
--- a/server/scripts/test-new-service-baseline.json
+++ b/server/scripts/test-new-service-baseline.json
@@ -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,
diff --git a/server/src/app-config/db-settings.ts b/server/src/app-config/db-settings.ts
new file mode 100644
index 000000000..fc225efdb
--- /dev/null
+++ b/server/src/app-config/db-settings.ts
@@ -0,0 +1,14 @@
+/** The databases TREK can run on. MySQL and MariaDB are out of scope (server/CLAUDE.md). */
+export type EngineId = 'sqlite' | 'postgres';
+
+/**
+ * Which database this process runs on, as `deriveDb` reads it from
+ * `TREK_DB_URL`. The Postgres arm carries what its pool needs, so nothing
+ * downstream has to check the URL again.
+ */
+export type DbEngineSettings = { engine: 'sqlite' } | { engine: 'postgres'; url: string; poolMax: number };
+
+/** The most connections the Postgres pool opens (`TREK_DB_POOL_MAX` is refused above it). */
+export const DB_POOL_MAX_CEILING = 100;
+/** The pool size when `TREK_DB_POOL_MAX` is unset. */
+export const DB_POOL_MAX_DEFAULT = 10;
diff --git a/server/src/app-config/derive.ts b/server/src/app-config/derive.ts
index 7c51d27d0..22f562462 100644
--- a/server/src/app-config/derive.ts
+++ b/server/src/app-config/derive.ts
@@ -14,6 +14,7 @@
* call, which is what keeps the runtime env-mutation semantics the test suite
* depends on. Zod validation runs once at boot (env.schema.ts), never here.
*/
+import { DB_POOL_MAX_CEILING, DB_POOL_MAX_DEFAULT, type DbEngineSettings } from './db-settings';
import { imageVersion } from './image-version';
import {
csvList,
@@ -348,9 +349,25 @@ export function deriveBackup(raw: RawEnv) {
};
}
+/** The database settings `readEnv().db` answers with. */
+export type DbSettings = ReturnType;
+
export function deriveDb(raw: RawEnv) {
const durability = resolveDurability(raw.TREK_DB_JOURNAL_MODE, raw.TREK_DB_SYNCHRONOUS);
+ const url = raw.TREK_DB_URL?.trim() || undefined;
+ const engine: DbEngineSettings = url
+ ? {
+ engine: 'postgres',
+ // The connection URL carries the password, so it is never logged (db/engine/redact-url.ts).
+ url,
+ poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, DB_POOL_MAX_DEFAULT), DB_POOL_MAX_CEILING),
+ }
+ : { engine: 'sqlite' };
return {
+ /** Which database this process runs on: Postgres when TREK_DB_URL names one, the SQLite file otherwise. */
+ ...engine,
+ /** Where pg_dump and pg_restore are; unset, the usual install directories are tried (never PATH). */
+ pgBinDir: raw.TREK_DB_PG_BIN_DIR?.trim() || undefined,
trekDbFile: raw.TREK_DB_FILE,
/** Resolved journal_mode — WAL unless the operator asked for something else (network storage needs DELETE/TRUNCATE). */
journalMode: durability.journalMode,
diff --git a/server/src/app-config/env.schema.ts b/server/src/app-config/env.schema.ts
index e05a2d202..1bca4a111 100644
--- a/server/src/app-config/env.schema.ts
+++ b/server/src/app-config/env.schema.ts
@@ -9,6 +9,7 @@
* listed here pass through untouched (Zod strips unknown keys, it does not
* reject them).
*/
+import { DB_POOL_MAX_CEILING } from './db-settings';
import {
isP256PrivateKey,
isServiceKeyShape,
@@ -51,6 +52,14 @@ const url = optionalWith((v) => {
return false;
}
}, 'must be a valid URL (with protocol)');
+/** A Postgres connection URL: TREK_DB_URL names no other engine. */
+const postgresUrl = optionalWith((v) => {
+ try {
+ return ['postgres:', 'postgresql:'].includes(new URL(v).protocol);
+ } catch {
+ return false;
+ }
+}, 'must be a postgres:// or postgresql:// URL');
const duration = optionalWith((v) => parseDurationMs(v) != null, 'must be a duration like "1h", "7d" or "30d"');
const oneOf = (values: string[]) =>
optionalWith((v) => values.includes(v.toLowerCase()), `must be one of: ${values.join(', ')}`);
@@ -170,6 +179,11 @@ export const envSchema = z.object({
Path: anyString,
// Data / paths
+ // A Postgres database instead of the SQLite file. Its value is a secret (it
+ // carries the password), so it is masked in the boot report.
+ TREK_DB_URL: postgresUrl,
+ TREK_DB_POOL_MAX: integer(1, DB_POOL_MAX_CEILING),
+ TREK_DB_PG_BIN_DIR: anyString,
TREK_DB_FILE: anyString,
// The two SQLite pragmas are deliberately unvalidated: a typo here must fall
// back and warn rather than abort boot, because reset-admin.js — the way out
diff --git a/server/src/app-config/env.ts b/server/src/app-config/env.ts
index 7187b749a..4d213194d 100644
--- a/server/src/app-config/env.ts
+++ b/server/src/app-config/env.ts
@@ -33,6 +33,7 @@ export const SECRET_ENV_KEYS: ReadonlySet = new Set([
'MAPBOX_ACCESS_TOKEN',
'CARTO_API_KEY',
'DEMO_ADMIN_PASS',
+ 'TREK_DB_URL',
'MAIL_INGEST_IMAP_PASS',
'MAIL_INGEST_WEBHOOK_SECRET',
]);
@@ -97,7 +98,7 @@ export function validateEnvAtBoot(raw: RawEnv = process.env as RawEnv): void {
const key = String(issue.path[0]);
return ` - ${key}=${reportedValue(key, raw[key])}: ${issue.message}`;
});
- lines.push(...managedPreconditions(raw));
+ lines.push(...databasePreconditions(raw), ...managedPreconditions(raw));
if (lines.length === 0) return;
console.error(`Invalid environment configuration:\n${lines.join('\n')}`);
throw new Error(
@@ -106,6 +107,25 @@ export function validateEnvAtBoot(raw: RawEnv = process.env as RawEnv): void {
);
}
+/** The SQLite settings that mean nothing next to TREK_DB_URL. */
+const SQLITE_ONLY_DB_KEYS = ['TREK_DB_FILE', 'TREK_DB_JOURNAL_MODE', 'TREK_DB_SYNCHRONOUS'] as const;
+
+/**
+ * Cross-field rules for the database engine. TREK_DB_URL beside a SQLite
+ * setting names two databases, and either guess would leave the operator on
+ * the one they did not mean: it refuses instead (fail closed, see
+ * server/CLAUDE.md). The URL itself never reaches the report.
+ */
+function databasePreconditions(raw: RawEnv): string[] {
+ if (!raw.TREK_DB_URL?.trim()) return [];
+ const sqliteKeys = SQLITE_ONLY_DB_KEYS.filter((key) => raw[key]?.trim());
+ if (sqliteKeys.length === 0) return [];
+ return [
+ ` - ambiguous database configuration: TREK_DB_URL is set together with ${sqliteKeys.join(', ')}, ` +
+ 'which only apply to the SQLite file. Unset one side.',
+ ];
+}
+
/**
* Cross-field rules that only apply to a centrally administered install.
*
diff --git a/server/src/app-config/index.ts b/server/src/app-config/index.ts
index f5e3c392f..bfea8b697 100644
--- a/server/src/app-config/index.ts
+++ b/server/src/app-config/index.ts
@@ -2,6 +2,7 @@ export { readEnv, validateEnvAtBoot } from './env';
export { getAppUrl, getMcpSafeUrl } from './app-url';
export { runningVersion } from './running-version';
export type { AppEnv, RawEnv } from './env';
+export type { DbSettings } from './derive';
export {
deriveAll,
deriveApp,
diff --git a/server/src/db/dialect/catalog.ts b/server/src/db/dialect/catalog.ts
new file mode 100644
index 000000000..79964b936
--- /dev/null
+++ b/server/src/db/dialect/catalog.ts
@@ -0,0 +1,37 @@
+import { isPostgres, isSqlite, unsupported } from './platform';
+import type { Platform } from '@mikro-orm/core';
+
+/**
+ * The schema catalog, for the migrations' helpers (`db/migration-utils.ts`):
+ * which tables and columns exist, each spelled the way its engine keeps them.
+ * Both statements take the table name as their one bound parameter and answer
+ * one row per hit with the name in `name`. Postgres reads the current schema
+ * only, the one TREK's own tables live in.
+ */
+
+/** Lists the columns of the table bound as `?`, one `name` per row; none for a missing table. */
+export function columnNamesSql(platform: Platform): string {
+ if (isSqlite(platform)) return 'select name from pragma_table_info(?)';
+ if (isPostgres(platform)) {
+ return 'select column_name as name from information_schema.columns where table_schema = current_schema() and table_name = ?';
+ }
+ return unsupported(platform);
+}
+
+/** One row when the table bound as `?` exists, none otherwise. */
+export function tableExistsSql(platform: Platform): string {
+ if (isSqlite(platform)) return "select name from sqlite_master where type = 'table' and name = ?";
+ if (isPostgres(platform)) {
+ return 'select table_name as name from information_schema.tables where table_schema = current_schema() and table_name = ?';
+ }
+ return unsupported(platform);
+}
+
+/**
+ * The tables in the current schema, one `table_name` per row in name order:
+ * the schema TREK's own tables live in. Postgres only, and the one definition
+ * of that question: the bootstrap reads it through the connection, the client
+ * tools' table listing hands it to psql.
+ */
+export const CURRENT_SCHEMA_TABLES_SQL =
+ 'select table_name from information_schema.tables where table_schema = current_schema() order by table_name';
diff --git a/server/src/db/dialect/sql-functions.ts b/server/src/db/dialect/sql-functions.ts
index a511960f3..55abdd14e 100644
--- a/server/src/db/dialect/sql-functions.ts
+++ b/server/src/db/dialect/sql-functions.ts
@@ -620,6 +620,17 @@ export function collateNoCase(platform: Platform, ref: string): RawQueryFragment
return unsupported(platform);
}
+/**
+ * A search that ignores case over `refs`: `(a LIKE ? ESCAPE '\' OR b LIKE ?
+ * ...)`, one bound pattern per column, the caller escaping `%`, `_` and `\`.
+ * SQLite's `LIKE` already ignores case (ASCII letters only); Postgres's is
+ * exact, so it gets `ILIKE`, which folds every letter.
+ */
+export function ilike(platform: Platform, refs: readonly string[]): string {
+ const operator = isSqlite(platform) ? 'LIKE' : isPostgres(platform) ? 'ILIKE' : unsupported(platform);
+ return `(${refs.map((ref) => `${column(ref)} ${operator} ? ESCAPE '\\'`).join(' OR ')})`;
+}
+
/**
* `datetime('now', '-N hours')` — the hour-granularity sibling of
* `nowMinusDays()` above, for `reminder-jobs.service.ts#todoTick`'s
diff --git a/server/src/db/email-case-index.ts b/server/src/db/email-case-index.ts
index c6b80e3c3..7df239fd0 100644
--- a/server/src/db/email-case-index.ts
+++ b/server/src/db/email-case-index.ts
@@ -18,13 +18,22 @@ export async function ensureEmailCaseIndex(connection: Connection): Promise = await connection.execute(
- `SELECT group_concat(id, ', ') AS ids FROM (SELECT id, email FROM users ORDER BY id) GROUP BY lower(email) HAVING COUNT(*) > 1`,
+ // Grouped here rather than with group_concat, which Postgres spells string_agg.
+ const rows: Array<{ id: number; email_key: string }> = await connection.execute(
+ `SELECT id, lower(email) AS email_key FROM users WHERE lower(email) IN ` +
+ `(SELECT lower(email) FROM users GROUP BY lower(email) HAVING COUNT(*) > 1) ORDER BY id`,
);
- if (duplicates.length > 0) {
+ const duplicates = new Map();
+ for (const row of rows) {
+ const group = duplicates.get(row.email_key);
+ if (group) group.push(row.id);
+ else duplicates.set(row.email_key, [row.id]);
+ }
+ if (duplicates.size > 0) {
+ const ids = [...duplicates.values()].map((group) => group.join(', '));
console.warn(
- `[DB] ${duplicates.length} email address(es) still belong to more than one account when case is ignored ` +
- `(user ids ${duplicates.map((row) => row.ids).join('; ')}), so users.email has no case-insensitive unique ` +
+ `[DB] ${duplicates.size} email address(es) still belong to more than one account when case is ignored ` +
+ `(user ids ${ids.join('; ')}), so users.email has no case-insensitive unique ` +
'index yet. Give each of those accounts its own address under Admin > Users; the next start creates it.',
);
return;
diff --git a/server/src/db/engine/engine.ts b/server/src/db/engine/engine.ts
new file mode 100644
index 000000000..5a62bdeaa
--- /dev/null
+++ b/server/src/db/engine/engine.ts
@@ -0,0 +1,49 @@
+import type { EngineId } from '../../app-config/db-settings';
+import type { AnyOrm } from '../orm';
+import type { EntityManager, Options } from '@mikro-orm/core';
+
+/**
+ * One database engine: what the ORM needs to reach it, how the connection
+ * opens, closes and comes back after a restore, and how a database is brought
+ * to the current schema. Code that differs by engine dispatches through this,
+ * or through the helpers in `db/dialect/`, never through its own platform
+ * check (`lint:engine-dispatch`).
+ *
+ * Free of Nest like every other file under `db/`: `src/db` does not import
+ * `src/nest` (`lint:boundaries`). `DatabaseLifecycle` is the provider that
+ * calls it.
+ */
+export interface DatabaseEngine {
+ readonly id: EngineId;
+ /** For log lines and errors: the file path, or the URL without its password. */
+ describe(): string;
+ /** What MikroORM needs to reach the database: the driver and its target, the pool, driver options. */
+ ormOptions(): Partial;
+ /**
+ * Called once at boot, before the schema bootstrap. `onReopened` runs after
+ * each later reopen, once the ORM is pointed at the reopened database: the
+ * engine does what its own reopen needs first (SQLite hands the ORM a new
+ * handle, so the ORM's connection is closed and connected again), then calls
+ * it, and the caller migrates the database and runs whatever follows.
+ */
+ open(orm: AnyOrm, onReopened: () => Promise): void;
+ /** Closes what the engine itself holds; the ORM's own pool is closed with the Nest container. */
+ close(): void;
+ /**
+ * Comes back after the database was replaced by a restore, then runs the
+ * `onReopened` that `open` registered. Throws when `open` has not run.
+ */
+ reopen(): Promise;
+ /**
+ * Brings the database to the current schema, then seeds it. `snapshot:
+ * false` skips the copy taken before pending migrations (a restore, whose
+ * archive is the copy).
+ */
+ schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise;
+ /**
+ * 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;
+}
diff --git a/server/src/db/engine/index.ts b/server/src/db/engine/index.ts
new file mode 100644
index 000000000..fb80cf17e
--- /dev/null
+++ b/server/src/db/engine/index.ts
@@ -0,0 +1,58 @@
+import { readEnv } from '../../app-config';
+import type { DbEngineSettings } from '../../app-config/db-settings';
+import type { DbSettings } from '../../app-config/derive';
+import { ALL_ENTITIES } from '../entities';
+import type { DatabaseEngine } from './engine';
+import { PostgresEngine } from './postgres/postgres-engine';
+import { SqliteEngine } from './sqlite/sqlite-engine';
+import { defineConfig, type Options } from '@mikro-orm/core';
+import { Migrator } from '@mikro-orm/migrations';
+import { SeedManager } from '@mikro-orm/seeder';
+
+export type { DatabaseEngine } from './engine';
+
+/**
+ * The engine this process runs on, from `TREK_DB_URL` (`readEnv().db`): the
+ * SQLite file unless the URL names a Postgres database. Read afresh on every
+ * call, like everything `readEnv()` answers, so nothing caches a choice a test
+ * or a restart changes.
+ */
+export function resolveEngine(db: DbEngineSettings = readEnv().db): DatabaseEngine {
+ if (db.engine === 'postgres') return new PostgresEngine(db);
+ return new SqliteEngine();
+}
+
+/**
+ * The Postgres URL and the directory of its client tools, when the settings
+ * name a Postgres database; null on SQLite. For the code that drives pg_dump
+ * and pg_restore, which needs both and must not read them from the environment
+ * again.
+ */
+export function postgresTarget(db: DbSettings): { url: string; binDir: string | undefined } | null {
+ if (db.engine !== 'postgres') return null;
+ return { url: db.url, binDir: db.pgBinDir };
+}
+
+/**
+ * The MikroORM config for `engine`: what every engine shares (entities,
+ * migrations, seeders) and what the engine adds. `src/mikro-orm.config.ts`
+ * exports it for the CLI and the Nest container, so both reach the same
+ * database.
+ */
+export function buildOrmConfig(engine: DatabaseEngine): Options {
+ return defineConfig({
+ entities: ALL_ENTITIES,
+ extensions: [Migrator, SeedManager],
+ migrations: {
+ path: 'dist/db/migrations',
+ pathTs: 'src/db/migrations',
+ snapshot: false,
+ },
+ seeder: {
+ path: 'dist/db/seeders',
+ pathTs: 'src/db/seeders',
+ defaultSeeder: 'DatabaseSeeder',
+ },
+ ...engine.ormOptions(),
+ }) as Options;
+}
diff --git a/server/src/db/engine/postgres/baseline.ts b/server/src/db/engine/postgres/baseline.ts
new file mode 100644
index 000000000..bb370d2f7
--- /dev/null
+++ b/server/src/db/engine/postgres/baseline.ts
@@ -0,0 +1,93 @@
+import { splitStatements } from './sql-split';
+
+import fs from 'node:fs';
+import path from 'node:path';
+
+/**
+ * Where the Postgres baseline lives: `server/assets/postgres/`, which the
+ * Docker image copies beside `dist/` (the same place the airports and atlas
+ * data are read from), reached the same way from `src/` and from `dist/`.
+ */
+const BASELINE_DIR = path.join(__dirname, '..', '..', '..', '..', 'assets', 'postgres');
+const SQL_FILE = 'baseline.sql';
+const MANIFEST_FILE = 'baseline.json';
+
+/** The manifest beside the SQL: the last migration the baseline stands for, and every migration it replaces. */
+interface BaselineManifest {
+ through: string;
+ migrations: string[];
+}
+
+/**
+ * The schema a fresh Postgres database starts from.
+ *
+ * The migrations up to `through` are written in SQLite and frozen, so a
+ * Postgres database does not replay them: it runs these statements, generated
+ * from the entities (`npm run gen:pg-baseline`), and records every migration
+ * in `migrations` as executed in the same transaction. Migrations after
+ * `through` run on both engines through the migrator.
+ */
+export interface PostgresBaseline extends BaselineManifest {
+ statements: string[];
+}
+
+/** The two files a baseline is written as, ready to compare or write. */
+export function renderBaseline(baseline: PostgresBaseline): { sql: string; manifest: string } {
+ return {
+ sql:
+ '-- The schema a fresh Postgres database starts from. Generated by `npm run gen:pg-baseline`; do not edit.\n' +
+ baseline.statements.map((statement) => `${statement};\n`).join(''),
+ manifest: `${JSON.stringify({ through: baseline.through, migrations: baseline.migrations }, null, 2)}\n`,
+ };
+}
+
+/**
+ * Whether `data` is a manifest a baseline can be read from: a `through` naming
+ * one of a non-empty list of `migrations`, every entry a string.
+ */
+function isBaselineManifest(data: unknown): data is BaselineManifest {
+ if (typeof data !== 'object' || data === null || !('through' in data) || !('migrations' in data)) return false;
+ const { through, migrations } = data;
+ return (
+ typeof through === 'string' &&
+ Array.isArray(migrations) &&
+ migrations.length > 0 &&
+ migrations.every((name): name is string => typeof name === 'string') &&
+ migrations.includes(through)
+ );
+}
+
+function parseManifest(text: string, file: string): BaselineManifest {
+ const data: unknown = JSON.parse(text);
+ if (!isBaselineManifest(data)) throw new Error(`${file} must name "through" and list it among "migrations"`);
+ return { through: data.through, migrations: data.migrations };
+}
+
+/** Reads the committed baseline; a missing or malformed one throws, so a boot cannot start from half a schema. */
+export function readPostgresBaseline(dir = BASELINE_DIR): PostgresBaseline {
+ const manifestFile = path.join(dir, MANIFEST_FILE);
+ const manifest = parseManifest(fs.readFileSync(manifestFile, 'utf8'), manifestFile);
+ const statements = splitStatements(fs.readFileSync(path.join(dir, SQL_FILE), 'utf8').replace(/^--[^\n]*\n/gm, ''));
+ if (statements.length === 0) throw new Error(`${path.join(dir, SQL_FILE)} holds no statements`);
+ // The boot sends the baseline as one statement without parameters, where a `?` would be read as a binding.
+ if (statements.some((statement) => statement.includes('?'))) {
+ throw new Error(`${path.join(dir, SQL_FILE)} holds a "?", which the boot would read as a parameter`);
+ }
+ return { ...manifest, statements };
+}
+
+/** Writes the two files (the generator's `--write`). */
+export function writePostgresBaseline(baseline: PostgresBaseline, dir = BASELINE_DIR): void {
+ const { sql, manifest } = renderBaseline(baseline);
+ fs.mkdirSync(dir, { recursive: true });
+ fs.writeFileSync(path.join(dir, SQL_FILE), sql);
+ fs.writeFileSync(path.join(dir, MANIFEST_FILE), manifest);
+}
+
+/** Whether the committed files are what `baseline` renders to (the generator's `--check`). */
+export function baselineMatches(baseline: PostgresBaseline, dir = BASELINE_DIR): boolean {
+ const { sql, manifest } = renderBaseline(baseline);
+ const read = (file: string) =>
+ fs.existsSync(path.join(dir, file)) ? fs.readFileSync(path.join(dir, file), 'utf8') : null;
+ return read(SQL_FILE) === sql && read(MANIFEST_FILE) === manifest;
+}
diff --git a/server/src/db/engine/postgres/clear-schema.ts b/server/src/db/engine/postgres/clear-schema.ts
new file mode 100644
index 000000000..0020bd859
--- /dev/null
+++ b/server/src/db/engine/postgres/clear-schema.ts
@@ -0,0 +1,41 @@
+/**
+ * Drops every table, view, sequence, routine, collation and standalone type in
+ * the current schema, except an extension's, before a dump is loaded: a dump
+ * only recreates what it holds, so whatever a later migration added would
+ * otherwise survive the restore and break the migrations the reopen runs. It
+ * runs in the same transaction as the load (`pgRestore`), so a restore that
+ * fails leaves the database as it was.
+ */
+export const CLEAR_SCHEMA_SQL = `DO $clear$
+DECLARE r record;
+BEGIN
+ FOR r IN SELECT c.oid::regclass AS name, c.relkind FROM pg_class c
+ WHERE c.relnamespace = current_schema()::regnamespace AND c.relkind IN ('r', 'p', 'v', 'm', 'S', 'f')
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = c.oid AND d.deptype IN ('e', 'a', 'i'))
+ LOOP
+ EXECUTE format('DROP %s IF EXISTS %s CASCADE',
+ CASE r.relkind WHEN 'v' THEN 'VIEW' WHEN 'm' THEN 'MATERIALIZED VIEW' WHEN 'S' THEN 'SEQUENCE'
+ WHEN 'f' THEN 'FOREIGN TABLE' ELSE 'TABLE' END, r.name);
+ END LOOP;
+ FOR r IN SELECT p.oid::regprocedure AS sig FROM pg_proc p
+ WHERE p.pronamespace = current_schema()::regnamespace
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = p.oid AND d.deptype = 'e')
+ LOOP
+ EXECUTE format('DROP ROUTINE IF EXISTS %s CASCADE', r.sig);
+ END LOOP;
+ FOR r IN SELECT format('%I.%I', n.nspname, c.collname) AS name FROM pg_collation c
+ JOIN pg_namespace n ON n.oid = c.collnamespace
+ WHERE n.nspname = current_schema()
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = c.oid AND d.deptype = 'e')
+ LOOP
+ EXECUTE format('DROP COLLATION IF EXISTS %s CASCADE', r.name);
+ END LOOP;
+ FOR r IN SELECT t.oid::regtype AS name FROM pg_type t
+ WHERE t.typnamespace = current_schema()::regnamespace AND t.typtype IN ('e', 'd', 'r')
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = t.oid AND d.deptype IN ('e', 'i'))
+ LOOP
+ EXECUTE format('DROP TYPE IF EXISTS %s CASCADE', r.name);
+ END LOOP;
+END
+$clear$;
+`;
diff --git a/server/src/db/engine/postgres/pg-process.ts b/server/src/db/engine/postgres/pg-process.ts
new file mode 100644
index 000000000..d54de4261
--- /dev/null
+++ b/server/src/db/engine/postgres/pg-process.ts
@@ -0,0 +1,119 @@
+import { execFile } from 'node:child_process';
+import fs from 'node:fs';
+import path from 'node:path';
+import { promisify } from 'node:util';
+
+const execFileAsync = promisify(execFile);
+
+/** The Postgres client tools a backup runs. */
+export type PgTool = 'pg_dump' | 'pg_restore' | 'psql';
+
+/**
+ * Where the client tools are looked for when `TREK_DB_PG_BIN_DIR` is unset:
+ * Debian's versioned directories newest first (the Docker image installs 17),
+ * then the usual prefixes. Never PATH: a binary is run by its absolute path.
+ */
+const DEFAULT_BIN_DIRS = [
+ '/usr/lib/postgresql/17/bin',
+ '/usr/lib/postgresql/16/bin',
+ '/usr/lib/postgresql/15/bin',
+ '/usr/lib/postgresql/14/bin',
+ '/usr/local/bin',
+ '/usr/bin',
+];
+
+/** A dump or a restore of a large database takes minutes, not hours. */
+const TOOL_TIMEOUT_MS = 30 * 60 * 1000;
+const TOOL_MAX_OUTPUT_BYTES = 64 * 1024 * 1024;
+/** An unreachable host fails the tool in seconds instead of holding a boot or a restore. */
+const CONNECT_TIMEOUT_SECONDS = '10';
+/** How much of a tool's stderr an error keeps. */
+const STDERR_LIMIT = 2000;
+/** stderr lines that quote SQL or row data: they stay out of errors and logs. */
+const DATA_LINE = /^\s*(Command was:|CONTEXT:|DETAIL:|LINE \d+:|QUERY:)/;
+
+/** The absolute path of `tool`, or null when it is not installed where TREK looks. */
+export function resolvePgTool(tool: PgTool, binDir: string | undefined): string | null {
+ const dirs = binDir ? [binDir] : DEFAULT_BIN_DIRS;
+ const executable = (candidate: string): boolean => {
+ try {
+ fs.accessSync(candidate, fs.constants.X_OK);
+ return true;
+ } catch {
+ return false; // missing or not executable: the next directory may have it
+ }
+ };
+ return dirs.map((dir) => path.join(dir, tool)).find(executable) ?? null;
+}
+
+/**
+ * The libpq environment for `url`: the tools read the password from
+ * PGPASSWORD, so it never appears in an argument list another process can
+ * read. An sslmode that asks for SSL verifies the server's certificate, as the
+ * app's own pool does (`postgres-engine.ts`): against the URL's sslrootcert,
+ * else the system's trusted CAs.
+ */
+export function pgConnectionEnv(url: string): Record {
+ const parsed = new URL(url);
+ const env: Record = {
+ PGHOST: parsed.hostname.replace(/^\[(.*)\]$/, '$1'),
+ PGPORT: parsed.port || '5432',
+ PGDATABASE: decodeURIComponent(parsed.pathname.replace(/^\//, '')),
+ PGCONNECT_TIMEOUT: CONNECT_TIMEOUT_SECONDS,
+ };
+ if (parsed.username) env.PGUSER = decodeURIComponent(parsed.username);
+ if (parsed.password) env.PGPASSWORD = decodeURIComponent(parsed.password);
+ const sslmode = parsed.searchParams.get('sslmode');
+ if (sslmode === 'disable') env.PGSSLMODE = 'disable';
+ else if (sslmode) {
+ env.PGSSLMODE = 'verify-full';
+ env.PGSSLROOTCERT = parsed.searchParams.get('sslrootcert') ?? 'system';
+ }
+ return env;
+}
+
+/** A client tool, refused with a clear message when it is not installed. */
+export class PgToolMissingError extends Error {
+ constructor(tool: PgTool) {
+ super(
+ `${tool} was not found, so a Postgres database cannot be backed up or restored. ` +
+ 'Install the PostgreSQL client tools (the Docker image has them) or point TREK_DB_PG_BIN_DIR at them.',
+ );
+ this.name = 'PgToolMissingError';
+ }
+}
+
+/** The tool's own message, without the lines that quote SQL or rows, and bounded. */
+function summarizeStderr(stderr: string | undefined): string {
+ const kept = (stderr ?? '')
+ .split('\n')
+ .filter((line) => line.trim() !== '' && !DATA_LINE.test(line))
+ .join('\n');
+ return kept.length > STDERR_LIMIT ? `${kept.slice(0, STDERR_LIMIT)}…` : kept;
+}
+
+/**
+ * Runs `tool` by its absolute path and resolves with its stdout. A failure
+ * throws ` failed: `, with the original error as
+ * the cause.
+ */
+export async function run(
+ tool: PgTool,
+ binDir: string | undefined,
+ args: string[],
+ env: Record,
+): Promise {
+ const binary = resolvePgTool(tool, binDir);
+ if (!binary) throw new PgToolMissingError(tool);
+ try {
+ const { stdout } = await execFileAsync(binary, args, {
+ env,
+ timeout: TOOL_TIMEOUT_MS,
+ maxBuffer: TOOL_MAX_OUTPUT_BYTES,
+ });
+ return stdout;
+ } catch (err) {
+ const message = summarizeStderr((err as { stderr?: string }).stderr);
+ throw new Error(`${tool} failed${message ? `: ${message}` : ''}`, { cause: err });
+ }
+}
diff --git a/server/src/db/engine/postgres/pg-tools.ts b/server/src/db/engine/postgres/pg-tools.ts
new file mode 100644
index 000000000..d095e1bf6
--- /dev/null
+++ b/server/src/db/engine/postgres/pg-tools.ts
@@ -0,0 +1,206 @@
+import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog';
+import { CLEAR_SCHEMA_SQL } from './clear-schema';
+import { pgConnectionEnv, run } from './pg-process';
+import { refuseToolsWithoutRestrict } from './restore-safety';
+
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import readline from 'node:readline';
+
+/**
+ * Restore's session settings: a restore waiting on a lock another session
+ * holds gives up, and rolls back, instead of hanging; the NOTICEs every
+ * CASCADE prints stay out of its stderr.
+ */
+const RESTORE_SESSION_OPTIONS = '-c lock_timeout=60s -c client_min_messages=warning';
+
+/** An empty file only its owner can read: the tool writing into it keeps the mode. */
+function createPrivateFile(file: string): void {
+ fs.writeFileSync(file, '', { mode: 0o600 });
+}
+
+/** The schema TREK's tables live in: its name, and the name as SQL quotes it (`quote_ident`). */
+interface PgSchema {
+ name: string;
+ quoted: string;
+}
+
+/**
+ * The current schema of the database at `url`, the one TREK creates its
+ * tables in. A backup and a restore stay inside it: a hosted database
+ * (Supabase) keeps the platform's own schemas, `auth` among them, beside it,
+ * and they are neither TREK's to copy nor to replace.
+ */
+async function pgCurrentSchema(url: string, binDir: string | undefined): Promise {
+ const out = await run(
+ 'psql',
+ binDir,
+ [
+ '--no-psqlrc',
+ '--no-align',
+ '--tuples-only',
+ "--command=select concat_ws(E'\\t', current_schema(), quote_ident(current_schema()))",
+ ],
+ pgConnectionEnv(url),
+ );
+ const [name, quoted] = out.replace(/\n$/, '').split('\t');
+ if (!name || !quoted) {
+ throw new Error('The database has no current schema: set the search_path of the role in TREK_DB_URL.');
+ }
+ return { name, quoted };
+}
+
+/**
+ * Writes a custom-format dump of the current schema of the database at `url`
+ * to `target`, readable by its owner only. It is written beside the target and
+ * renamed into place, so a dump that fails or is killed never leaves a file
+ * that passes for a copy.
+ */
+export async function pgDump(url: string, target: string, binDir: string | undefined): Promise {
+ const partial = `${target}.partial`;
+ try {
+ createPrivateFile(partial);
+ const schema = await pgCurrentSchema(url, binDir);
+ await run(
+ 'pg_dump',
+ binDir,
+ [
+ '--format=custom',
+ '--no-owner',
+ '--no-privileges',
+ `--file=${partial}`,
+ `--schema="${schema.name.replaceAll('"', '""')}"`,
+ ],
+ pgConnectionEnv(url),
+ );
+ fs.renameSync(partial, target);
+ } catch (err) {
+ fs.rmSync(partial, { force: true });
+ throw err;
+ }
+}
+
+/** Escapes every character a RegExp would read as syntax. */
+function escapeRegExp(text: string): string {
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+}
+
+/**
+ * Whether the restore script creates TREK's migration table in `schema`: a
+ * dump of TREK's own schema does. The script is read line by line and the
+ * read stops at the first match: the COPY data that fills a large script is
+ * never loaded, and a script past V8's maximum string length would not load
+ * at all.
+ */
+async function scriptHoldsTrekSchema(script: string, schema: PgSchema): Promise {
+ const createsHistory = new RegExp(`^CREATE TABLE ${escapeRegExp(`${schema.quoted}.mikro_orm_migrations`)} \\(`);
+ const input = fs.createReadStream(script, { encoding: 'utf8' });
+ const lines = readline.createInterface({ input, crlfDelay: Infinity });
+ try {
+ for await (const line of lines) {
+ if (createsHistory.test(line)) return true;
+ }
+ return false;
+ } finally {
+ lines.close();
+ input.destroy();
+ }
+}
+
+/**
+ * Replaces the current schema of the database at `url` with the dump at
+ * `source`: pg_restore turns the dump's objects in that schema into a script
+ * beside it (anything else the dump holds is left out), then psql clears the
+ * schema and runs the script in one transaction, so a restore that fails
+ * leaves the database as it was. A dump without TREK's tables in that schema
+ * is refused before anything is cleared.
+ */
+export async function pgRestore(url: string, source: string, binDir: string | undefined): Promise {
+ await refuseToolsWithoutRestrict(binDir);
+ const schema = await pgCurrentSchema(url, binDir);
+ const workDir = fs.mkdtempSync(path.join(path.dirname(source), '.pg-restore-'));
+ try {
+ const clear = path.join(workDir, 'clear-schema.sql');
+ const script = path.join(workDir, 'restore.sql');
+ fs.writeFileSync(clear, CLEAR_SCHEMA_SQL, { mode: 0o600 });
+ createPrivateFile(script);
+ await run(
+ 'pg_restore',
+ binDir,
+ ['--no-owner', '--no-privileges', `--schema=${schema.name}`, `--file=${script}`, '--', source],
+ {},
+ );
+ if (!(await scriptHoldsTrekSchema(script, schema))) {
+ throw new Error(`The backup holds no TREK tables in the schema "${schema.name}"; nothing was replaced.`);
+ }
+ await run(
+ 'psql',
+ binDir,
+ [
+ '--no-psqlrc',
+ '--quiet',
+ '--single-transaction',
+ '--set=ON_ERROR_STOP=1',
+ `--output=${os.devNull}`,
+ `--file=${clear}`,
+ `--file=${script}`,
+ ],
+ { ...pgConnectionEnv(url), PGOPTIONS: RESTORE_SESSION_OPTIONS },
+ );
+ } finally {
+ fs.rmSync(workDir, { recursive: true, force: true });
+ }
+}
+
+/** The tables in the current schema of the database at `url`, read before TREK opens it. */
+export async function pgTableNames(url: string, binDir: string | undefined): Promise {
+ const out = await run(
+ 'psql',
+ binDir,
+ ['--no-psqlrc', '--no-align', '--tuples-only', `--command=${CURRENT_SCHEMA_TABLES_SQL}`],
+ pgConnectionEnv(url),
+ );
+ return out.split('\n').filter((line) => line !== '');
+}
+
+/**
+ * The migration names in a restore script's COPY block for
+ * `mikro_orm_migrations`, read from its `name` column; none when the script
+ * has no such block.
+ */
+function migrationNamesFromCopy(script: string): string[] {
+ const lines = script.split('\n');
+ const copy = lines.findIndex((line) => /^COPY \S*mikro_orm_migrations \(/.test(line));
+ if (copy < 0) return [];
+ const columns =
+ /\(([^)]*)\)/
+ .exec(lines[copy])?.[1]
+ .split(',')
+ .map((column) => column.trim()) ?? [];
+ const nameAt = columns.indexOf('name');
+ if (nameAt < 0) return [];
+ const names: string[] = [];
+ for (const line of lines.slice(copy + 1)) {
+ if (line === '\\.' || line === '') break;
+ const name = line.split('\t')[nameAt];
+ if (name) names.push(name);
+ }
+ return names;
+}
+
+/**
+ * The migrations a dump records as executed, read from the dump itself (its
+ * `mikro_orm_migrations` rows as COPY text), so a restore can refuse a newer
+ * TREK's backup before anything is replaced. Throws on a file that is not a
+ * dump; an empty list means the dump holds no TREK history.
+ */
+export async function pgDumpMigrations(source: string, binDir: string | undefined): Promise {
+ const script = await run(
+ 'pg_restore',
+ binDir,
+ ['--data-only', '--table=mikro_orm_migrations', '--file=-', '--', source],
+ {},
+ );
+ return migrationNamesFromCopy(script);
+}
diff --git a/server/src/db/engine/postgres/postgres-driver.ts b/server/src/db/engine/postgres/postgres-driver.ts
new file mode 100644
index 000000000..5dde04db2
--- /dev/null
+++ b/server/src/db/engine/postgres/postgres-driver.ts
@@ -0,0 +1,67 @@
+import { type Configuration } from '@mikro-orm/core';
+import { PostgreSqlConnection, PostgreSqlDriver } from '@mikro-orm/postgresql';
+
+/** `int8` (bigint), a `pg_type.oid`: every integer column and every `count(*)` on Postgres. */
+const INT8_OID = 20;
+/** `numeric`: what `sum()` of a bigint and `avg()` answer. */
+const NUMERIC_OID = 1700;
+
+/**
+ * Reads a `bigint` as a JavaScript number, the way better-sqlite3 hands
+ * SQLite's 64-bit INTEGER to the services. `pg` returns it as a string to
+ * keep precision past 2^53; no TREK value comes near that (ids, counts,
+ * epoch milliseconds, cents), so one that does is refused rather than
+ * rounded.
+ */
+export function parseInt8(text: string): number {
+ const value = Number(text);
+ if (!Number.isSafeInteger(value)) throw new Error(`A bigint of ${text} is beyond what TREK reads exactly (2^53).`);
+ return value;
+}
+
+/**
+ * Reads a `numeric` as a number, as SQLite answers `sum()` and `avg()`. `NaN`
+ * and the infinities are refused, as is an integral result past 2^53, which is
+ * refused like a bigint; a fraction is a double, the precision SQLite's REAL has.
+ */
+export function parseNumeric(text: string): number {
+ const value = Number(text);
+ if (!Number.isFinite(value)) {
+ throw new Error(`A numeric of ${text} is not a finite number, which TREK does not read.`);
+ }
+ if (/^-?\d+$/.test(text) && !Number.isSafeInteger(value)) {
+ throw new Error(`A numeric of ${text} is beyond what TREK reads exactly (2^53).`);
+ }
+ return value;
+}
+
+/**
+ * MikroORM's `PostgreSqlConnection`, with the pool's own type parsers replaced
+ * for `int8` and `numeric`. MikroORM keeps dates as text, so only these two
+ * change. `mapOptions` builds the `pg` pool options, whose `types` is the
+ * `pg` TypeOverrides, the object that holds the parsers.
+ */
+class NumberTypesConnection extends PostgreSqlConnection {
+ override mapOptions(overrides: Parameters[0]) {
+ const options = super.mapOptions(overrides);
+ const types = options.types as { setTypeParser(oid: number, parse: (text: string) => unknown): void };
+ types.setTypeParser(INT8_OID, parseInt8);
+ types.setTypeParser(NUMERIC_OID, parseNumeric);
+ return options;
+ }
+}
+
+/**
+ * MikroORM's Postgres driver on a connection that reads `bigint` and `numeric`
+ * as numbers. The parsers belong to this pool only: nothing global in `pg`
+ * changes.
+ */
+export class TrekPostgreSqlDriver extends PostgreSqlDriver {
+ constructor(config: Configuration) {
+ super(config);
+ // PostgreSqlDriver's constructor hardcodes its connection class, as
+ // SqliteDriver's does (`db/orm-driver.ts`). It keeps the connection in a
+ // field, which is not public, so it is replaced here, before anything connects.
+ (this as unknown as { connection: PostgreSqlConnection }).connection = new NumberTypesConnection(config);
+ }
+}
diff --git a/server/src/db/engine/postgres/postgres-engine.ts b/server/src/db/engine/postgres/postgres-engine.ts
new file mode 100644
index 000000000..e7b48b137
--- /dev/null
+++ b/server/src/db/engine/postgres/postgres-engine.ts
@@ -0,0 +1,90 @@
+import type { DbEngineSettings } from '../../../app-config/db-settings';
+import { NO_SAFETY_NET } from '../../legacy-baseline';
+import { finishSchemaBootstrap, type AnyOrm } from '../../orm';
+import type { DatabaseEngine } from '../engine';
+import { redactUrl } from '../redact-url';
+import { TrekPostgreSqlDriver } from './postgres-driver';
+import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap';
+import { sslOptionFor } from './ssl';
+import type { EntityManager } from '@mikro-orm/core';
+
+/** What `TREK_DB_URL` and `TREK_DB_POOL_MAX` resolve to, when they name a Postgres database. */
+type PostgresSettings = Extract;
+
+/** 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
+ * idle. The pool belongs to the ORM, which the Nest container closes; the
+ * schema comes from the baseline plus the migrations after it
+ * (`schema-bootstrap.ts`).
+ */
+export class PostgresEngine implements DatabaseEngine {
+ readonly id = 'postgres';
+
+ constructor(
+ private readonly settings: PostgresSettings,
+ private readonly bootstrapDeps: (url: string) => PostgresBootstrapDeps = defaultPostgresBootstrapDeps,
+ ) {}
+
+ private onReopened: (() => Promise) | null = null;
+
+ /**
+ * Nothing of its own to open: the ORM connects the pool. Keeps what a reopen
+ * runs. A reopen replaces the database's contents, which the pool's own
+ * connections keep reaching, so the ORM is not pointed at anything new and
+ * the pool is never closed (ending a pool waits for every connection it has
+ * lent out, and one lent to a job during the restore would never come back).
+ */
+ open(_orm: AnyOrm, onReopened: () => Promise): void {
+ this.onReopened = onReopened;
+ }
+
+ close(): void {
+ // Nothing of its own to close: the pool goes with the ORM.
+ }
+
+ /**
+ * After `pg_restore` replaced the database's contents: the restored
+ * database is migrated forward on the same pool (`onReopened`, which
+ * `DatabaseLifecycle` hands to `open`).
+ */
+ async reopen(): Promise {
+ if (!this.onReopened) throw new Error('The Postgres engine was reopened before it was opened.');
+ await this.onReopened();
+ }
+
+ /**
+ * `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 {
+ await em
+ .getConnection()
+ .execute('select pg_advisory_xact_lock(?)', [TREK_WRITE_LOCK], 'run', em.getTransactionContext());
+ }
+
+ /** `snapshot: false` (a restore, whose archive is the copy) migrates without a pg_dump first. */
+ async schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise {
+ const deps = this.bootstrapDeps(this.settings.url);
+ await bootstrapPostgresSchema(orm, options.snapshot ? deps : { ...deps, safetyNet: NO_SAFETY_NET });
+ await finishSchemaBootstrap(orm);
+ }
+
+ describe(): string {
+ return `Postgres ${redactUrl(this.settings.url)}`;
+ }
+
+ ormOptions() {
+ const ssl = sslOptionFor(this.settings.url);
+ return {
+ driver: TrekPostgreSqlDriver,
+ clientUrl: this.settings.url,
+ pool: { min: 0, max: this.settings.poolMax },
+ driverOptions: ssl === undefined ? {} : { ssl },
+ };
+ }
+}
diff --git a/server/src/db/engine/postgres/postgres-triggers.ts b/server/src/db/engine/postgres/postgres-triggers.ts
new file mode 100644
index 000000000..fbbf8de8c
--- /dev/null
+++ b/server/src/db/engine/postgres/postgres-triggers.ts
@@ -0,0 +1,32 @@
+/**
+ * The SQLite schema's triggers as Postgres writes them. The entities describe
+ * tables only, so the baseline (`scripts/generate-pg-baseline.ts`) adds these
+ * after the tables, and `check:pg-baseline` fails while a trigger in the
+ * migrated SQLite schema has no entry here. Each entry is the trigger's
+ * function and the trigger that runs it, under the SQLite trigger's name.
+ */
+export const POSTGRES_TRIGGERS: Readonly> = {
+ // A deleted stay releases the day stops it claimed (no foreign key holds the link).
+ trg_release_stop_on_stay_delete: [
+ `CREATE FUNCTION trg_release_stop_on_stay_delete() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ UPDATE day_assignments SET accommodation_id = NULL WHERE accommodation_id = OLD.id;
+ RETURN OLD;
+END
+$$`,
+ 'CREATE TRIGGER trg_release_stop_on_stay_delete AFTER DELETE ON day_accommodations ' +
+ 'FOR EACH ROW EXECUTE FUNCTION trg_release_stop_on_stay_delete()',
+ ],
+ // A place that moved or was renamed drops the region it was resolved to (#2527).
+ trg_place_regions_follow_place: [
+ `CREATE FUNCTION trg_place_regions_follow_place() RETURNS trigger LANGUAGE plpgsql AS $$
+BEGIN
+ DELETE FROM place_regions WHERE place_id = NEW.id;
+ RETURN NEW;
+END
+$$`,
+ 'CREATE TRIGGER trg_place_regions_follow_place AFTER UPDATE OF lat, lng, address ON places FOR EACH ROW ' +
+ 'WHEN (OLD.lat IS DISTINCT FROM NEW.lat OR OLD.lng IS DISTINCT FROM NEW.lng OR OLD.address IS DISTINCT FROM NEW.address) ' +
+ 'EXECUTE FUNCTION trg_place_regions_follow_place()',
+ ],
+};
diff --git a/server/src/db/engine/postgres/restore-safety.ts b/server/src/db/engine/postgres/restore-safety.ts
new file mode 100644
index 000000000..cf07907d2
--- /dev/null
+++ b/server/src/db/engine/postgres/restore-safety.ts
@@ -0,0 +1,50 @@
+import { run } from './pg-process';
+
+/**
+ * The client-tool versions a restore accepts (CVE-2025-8714). A script from a
+ * dump an admin uploaded is run by psql, which would also run a psql
+ * meta-command (`\!` runs a shell command) smuggled into an object name. A
+ * fixed pg_restore opens the script with `\restrict `, and a fixed
+ * psql then refuses every meta-command until the matching `\unrestrict`.
+ *
+ * RESTRICT_SINCE holds, per major, the first minor that carries the fix. Every
+ * major after the last one listed has it from its first release.
+ */
+const RESTRICT_SINCE: Readonly>> = { 14: 19, 15: 14, 16: 10, 17: 6 };
+const LAST_MAJOR_WITHOUT_IT = Math.max(...Object.keys(RESTRICT_SINCE).map(Number));
+
+/** Whether `version` (`pg_restore (PostgreSQL) 17.11 (Debian …)`) carries the fix. */
+export function hasRestrictFix(version: string): boolean {
+ const match = /\(PostgreSQL\) (\d+)\.(\d+)/.exec(version);
+ if (!match) return false;
+ const major = Number(match[1]);
+ const minor = Number(match[2]);
+ if (major > LAST_MAJOR_WITHOUT_IT) return true;
+ const firstFixedMinor = RESTRICT_SINCE[major];
+ return firstFixedMinor !== undefined && minor >= firstFixedMinor;
+}
+
+/** The minimum versions as the error names them, newest major first: "17.6, 16.10, 15.14 or 14.19". */
+function minimumVersionsText(): string {
+ const floors = Object.keys(RESTRICT_SINCE)
+ .map(Number)
+ .sort((a, b) => b - a)
+ .map((major) => `${major}.${RESTRICT_SINCE[major]}`);
+ const last = floors.pop() ?? '';
+ return floors.length > 0 ? `${floors.join(', ')} or ${last}` : last;
+}
+
+/**
+ * Refuses the restore when pg_restore or psql predates the fix. The tools'
+ * own versions are checked, not the script, whose text the dump controls.
+ */
+export async function refuseToolsWithoutRestrict(binDir: string | undefined): Promise {
+ const tools = ['pg_restore', 'psql'] as const;
+ const versions = await Promise.all(tools.map((tool) => run(tool, binDir, ['--version'], {})));
+ const stale = tools.findIndex((_, i) => !hasRestrictFix(versions[i]));
+ if (stale < 0) return;
+ throw new Error(
+ `${tools[stale]} is too old to restore safely (${versions[stale].trim()}): it must be at least ${minimumVersionsText()} ` +
+ '(the Docker image has 17). Upgrade the PostgreSQL client tools or point TREK_DB_PG_BIN_DIR at newer ones.',
+ );
+}
diff --git a/server/src/db/engine/postgres/schema-adjust.ts b/server/src/db/engine/postgres/schema-adjust.ts
new file mode 100644
index 000000000..73376fda8
--- /dev/null
+++ b/server/src/db/engine/postgres/schema-adjust.ts
@@ -0,0 +1,109 @@
+/**
+ * How the entities' schema is written in Postgres: the one definition the
+ * Postgres baseline (`scripts/generate-pg-baseline.ts`), the schema parity
+ * check and the probe (`scripts/pg-probe.ts`) share.
+ *
+ * The entities are generated from the migrated SQLite schema and a parity
+ * test pins them to it (`npm run check:entities`), so they are the one
+ * description of the tables both engines can read. MikroORM's schema
+ * generator turns them into Postgres DDL after the adjustments below, which
+ * keep the storage model the services and the dialect helpers are written
+ * for (server/CLAUDE.md, "Storage model"):
+ *
+ * - timestamps stay TEXT;
+ * - an INTEGER column is `bigint`: SQLite's INTEGER holds 64 bits, so a
+ * column written with epoch milliseconds or a byte count past 2 GB would
+ * overflow Postgres's 32-bit `integer` (the engine's driver reads
+ * `bigint` back as a number, `postgres-driver.ts`);
+ * - a SQLite-only default (`datetime('now')`, `strftime('%s','now')`,
+ * `CURRENT_TIMESTAMP` in a TEXT column) gets its Postgres equivalent, and
+ * a column declared `COLLATE NOCASE` uses the `nocase` collation
+ * `collateNoCase()` names on Postgres.
+ *
+ * The baseline keeps the foreign keys; the probe leaves them out, so a
+ * statement it sends is never refused only because the sample row it writes
+ * points at nothing. The triggers the SQLite schema has are in
+ * `postgres-triggers.ts`.
+ */
+import { pgTimestampText, PG_UTC_NOW } from '../../dialect/platform';
+import { DbTimestampType } from '../../types';
+
+/** SQLite's timestamp text on Postgres, for a column default. */
+export const PG_NOW_TEXT_DEFAULT = pgTimestampText(PG_UTC_NOW);
+
+/** The case-insensitive collation `collateNoCase()` names on Postgres. */
+export const NOCASE_COLLATION_SQL =
+ "CREATE COLLATION IF NOT EXISTS nocase (provider = icu, locale = 'und-u-ks-level2', deterministic = false)";
+
+/** The Postgres spelling of a column default the entities carry in SQLite's. */
+export function portableDefault(raw: string): string {
+ const text = raw
+ .trim()
+ .replace(/^\((.*)\)$/s, '$1')
+ .trim();
+ if (/^current_timestamp$/i.test(text) || /^datetime\(\s*'now'\s*\)$/i.test(text)) return PG_NOW_TEXT_DEFAULT;
+ if (/^strftime\(\s*'%s'\s*,\s*'now'\s*\)$/i.test(text))
+ return 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)';
+ return raw;
+}
+
+/** A column type SQLite stores as its 64-bit INTEGER. */
+const INTEGER_TYPE = /^(integer|int)$/i;
+
+/**
+ * The subset of MikroORM's property metadata the adjustments read and write.
+ * An entity's `props` satisfy it as they are, so no cast is needed.
+ */
+export interface ColumnMeta {
+ name: string;
+ defaultRaw?: string;
+ collation?: string;
+ columnTypes?: string[];
+ customType?: unknown;
+}
+
+/** A timestamp column becomes text. Returns whether it changed. */
+function timestampAsText(column: ColumnMeta): boolean {
+ if (!(column.customType instanceof DbTimestampType) || !column.columnTypes) return false;
+ column.columnTypes = column.columnTypes.map(() => 'text');
+ return true;
+}
+
+/** An integer column becomes bigint. Returns whether it changed. */
+function integerAsBigint(column: ColumnMeta): boolean {
+ if (!column.columnTypes?.some((type) => INTEGER_TYPE.test(type))) return false;
+ column.columnTypes = column.columnTypes.map((type) => (INTEGER_TYPE.test(type) ? 'bigint' : type));
+ return true;
+}
+
+/** A SQLite-only default gets its Postgres spelling. Returns whether it changed. */
+function defaultAsPostgres(column: ColumnMeta): boolean {
+ if (typeof column.defaultRaw !== 'string') return false;
+ const next = portableDefault(column.defaultRaw);
+ if (next === column.defaultRaw) return false;
+ column.defaultRaw = next;
+ return true;
+}
+
+/** A NOCASE column uses the `nocase` collation. Returns whether it changed. */
+function nocaseAsCollation(column: ColumnMeta): boolean {
+ if (column.collation?.toUpperCase() !== 'NOCASE') return false;
+ column.collation = 'nocase';
+ return true;
+}
+
+/**
+ * Applies the adjustments above to an entity's properties, in place, and
+ * returns the names of the properties it changed, for the log. A timestamp
+ * column is text, so the integer adjustment never sees it.
+ */
+export function applyPostgresColumnAdjustments(properties: readonly ColumnMeta[]): string[] {
+ const changed: string[] = [];
+ for (const property of properties) {
+ const typeChanged = timestampAsText(property) || integerAsBigint(property);
+ const defaultChanged = defaultAsPostgres(property);
+ const collationChanged = nocaseAsCollation(property);
+ if (typeChanged || defaultChanged || collationChanged) changed.push(property.name);
+ }
+ return changed;
+}
diff --git a/server/src/db/engine/postgres/schema-bootstrap.ts b/server/src/db/engine/postgres/schema-bootstrap.ts
new file mode 100644
index 000000000..9ad523c68
--- /dev/null
+++ b/server/src/db/engine/postgres/schema-bootstrap.ts
@@ -0,0 +1,185 @@
+import { readEnv, type AppEnv } from '../../../app-config';
+import { resolveDataPaths } from '../../../app-config/data-paths';
+import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog';
+import { knownMigrationNames } from '../../known-migrations';
+import { migrationLabel, REFUSAL, refuseNewerDatabase, type MigrationSafetyNet } from '../../legacy-baseline';
+import type { AnyOrm } from '../../orm';
+import { listSnapshots, PREFIX, pruneSnapshots, stamp } from '../../pre-migrate-snapshot';
+import { readPostgresBaseline, type PostgresBaseline } from './baseline';
+import { pgDump } from './pg-tools';
+import type { Connection } from '@mikro-orm/core';
+import type { Migrator } from '@mikro-orm/migrations';
+import { Logger } from '@nestjs/common';
+
+import fs from 'node:fs';
+import path from 'node:path';
+
+const logger = new Logger('DB');
+
+/** A Postgres copy is a pg_dump custom-format archive. */
+const DUMP_EXTENSION = '.pgdump';
+
+/** What a Postgres bootstrap reads, passed in so a test can supply its own. */
+export interface PostgresBootstrapDeps {
+ baseline: () => PostgresBaseline;
+ known: Set;
+ safetyNet: MigrationSafetyNet;
+}
+
+/**
+ * The tables in the current schema. A database with no TREK migration history
+ * must hold none: TREK creates its schema in an empty database and never
+ * adopts tables something else made. Read before anything is written, so a
+ * refused database is left exactly as it was.
+ */
+async function currentTables(connection: Connection): Promise {
+ const rows: Array<{ table_name: string }> = await connection.execute(CURRENT_SCHEMA_TABLES_SQL);
+ return rows.map((row) => row.table_name);
+}
+
+/**
+ * Creates the schema in an empty Postgres database: the baseline's statements
+ * and the record of every migration it stands for, in one transaction (DDL is
+ * transactional on Postgres, so the database ends with all of it or none). A
+ * database holding tables beside no TREK history is refused before anything is
+ * written.
+ */
+async function createFromBaseline(
+ connection: Connection,
+ storage: ReturnType,
+ tables: readonly string[],
+ historyTable: string,
+ baseline: () => PostgresBaseline,
+): Promise {
+ const foreign = tables.filter((table) => table !== historyTable);
+ if (foreign.length > 0) {
+ throw new Error(
+ `${REFUSAL} the database holds ${foreign.length} table(s) (${foreign.slice(0, 5).join(', ')}) but no TREK ` +
+ 'migration history. Point TREK_DB_URL at an empty database.',
+ );
+ }
+ await storage.ensureTable();
+ const schema = baseline();
+ logger.log(`Creating the schema in an empty Postgres database (through ${migrationLabel(schema.through)})`);
+ await connection.transactional(async (trx) => {
+ await connection.execute(schema.statements.join(';\n'), [], 'run', trx);
+ await Promise.all(schema.migrations.map((name) => storage.logMigration({ name }, trx)));
+ });
+}
+
+/**
+ * Refuses a database whose recorded history names a migration this build does
+ * not know: a newer TREK migrated it. The refusal names the copy taken before
+ * that migration, when there is one.
+ */
+async function refuseNewerHistory(executed: string[], deps: PostgresBootstrapDeps): Promise {
+ const thisRelease = migrationLabel([...deps.known].sort().at(-1) ?? '');
+ refuseNewerDatabase(executed, deps.known, await deps.safetyNet.restoreHint(thisRelease));
+}
+
+/**
+ * Brings a Postgres database to the current schema: inspect what it holds,
+ * then create the schema or verify the history it has, then migrate.
+ *
+ * - No migration recorded and no table: the baseline is created
+ * (`createFromBaseline`).
+ * - No migration recorded but tables present: refused, naming a few of them.
+ * - Migrations recorded: refused when one is unknown to this build, else the
+ * pending migrations run, after the safety net. A database the baseline just
+ * created runs the migrations after the baseline's cut without it: there is
+ * nothing yet to lose.
+ */
+export async function bootstrapPostgresSchema(orm: AnyOrm, deps: PostgresBootstrapDeps): Promise {
+ const migrator = orm.config.getExtension('@mikro-orm/migrator') as Migrator;
+ const connection = orm.em.getConnection();
+ const historyTable = orm.config.get('migrations').tableName ?? 'mikro_orm_migrations';
+
+ const tables = await currentTables(connection);
+ const executed = tables.includes(historyTable) ? (await migrator.getExecuted()).map((row) => row.name) : [];
+
+ if (executed.length === 0) {
+ await createFromBaseline(connection, migrator.getStorage(), tables, historyTable, deps.baseline);
+ } else {
+ await refuseNewerHistory(executed, deps);
+ }
+
+ const pending = await migrator.getPending();
+ if (pending.length === 0) return;
+ // Only a database with history has a last migration to label its copy by; a
+ // database the baseline just created has nothing yet to lose.
+ const latest = [...executed].sort().at(-1);
+ if (latest !== undefined) await deps.safetyNet.beforeMigrate(migrationLabel(latest));
+ logger.log(`Applying ${pending.length} pending migration(s)`);
+ await migrator.up();
+}
+
+/** Dumps a killed boot left half-written: never a copy, only disk taken. */
+function removeOrphanedPartials(dir: string): void {
+ for (const file of fs.readdirSync(dir)) {
+ if (file.startsWith(PREFIX) && file.endsWith(`${DUMP_EXTENSION}.partial`)) {
+ fs.rmSync(path.join(dir, file), { force: true });
+ }
+ }
+}
+
+/**
+ * The Postgres safety net before pending migrations, as the SQLite file's
+ * (`db/pre-migrate-snapshot.ts`): a `pg_dump` of the database to
+ * `/pre-migrate--.pgdump`, the newest
+ * `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP` kept. A copy that cannot be written
+ * (pg_dump missing, the data dir full) refuses the boot before anything is
+ * migrated, unless `TREK_DB_PRE_MIGRATE_SNAPSHOT=false`. Off under
+ * NODE_ENV=test unless a suite asks for it.
+ */
+export function postgresSafetyNet(
+ url: string,
+ env: () => AppEnv = readEnv,
+ now: () => Date = () => new Date(),
+ dataDir: () => string = () => resolveDataPaths(env()).dataDir,
+): MigrationSafetyNet {
+ return {
+ async beforeMigrate(from) {
+ const { db, app } = env();
+ if (db.preMigrateSnapshot === undefined && app.isTest) return;
+ if (db.preMigrateSnapshot === false) {
+ logger.warn('TREK_DB_PRE_MIGRATE_SNAPSHOT is off: migrating the Postgres database without a copy');
+ return;
+ }
+ const target = path.join(dataDir(), `${PREFIX}${from ?? 'unversioned'}-${stamp(now())}${DUMP_EXTENSION}`);
+ try {
+ fs.mkdirSync(dataDir(), { recursive: true });
+ removeOrphanedPartials(dataDir());
+ await pgDump(url, target, db.pgBinDir);
+ } catch (err) {
+ throw new Error(
+ `${REFUSAL} could not write a copy of the database to ${target} before migrating it ` +
+ `(${err instanceof Error ? err.message : String(err)}). Nothing was migrated. Fix that, ` +
+ 'or set TREK_DB_PRE_MIGRATE_SNAPSHOT=false to migrate without a copy.',
+ { cause: err },
+ );
+ }
+ logger.log(`Copy of the database before migrating: ${target}`);
+ pruneSnapshots(dataDir(), db.preMigrateSnapshotKeep, DUMP_EXTENSION);
+ },
+
+ async restoreHint(thisRelease) {
+ const snapshots = listSnapshots(dataDir(), DUMP_EXTENSION);
+ const match = snapshots.find((snapshot) => path.basename(snapshot).startsWith(`${PREFIX}${thisRelease}-`));
+ if (match) {
+ return (
+ `The copy taken right before the newer TREK migrated it is ${match}: stop TREK, empty the database ` +
+ '(drop and recreate it), and restore it with pg_restore --no-owner --no-privileges --single-transaction.'
+ );
+ }
+ if (snapshots.length > 0) {
+ return `Copies taken before earlier migrations are in ${dataDir()} (${PREFIX}*${DUMP_EXTENSION}), none of them from this release.`;
+ }
+ return null;
+ },
+ };
+}
+
+/** What a production boot passes: the committed baseline, the migrations on disk, the safety net above. */
+export function defaultPostgresBootstrapDeps(url: string): PostgresBootstrapDeps {
+ return { baseline: readPostgresBaseline, known: knownMigrationNames(), safetyNet: postgresSafetyNet(url) };
+}
diff --git a/server/src/db/engine/postgres/sql-split.ts b/server/src/db/engine/postgres/sql-split.ts
new file mode 100644
index 000000000..9c91ca14f
--- /dev/null
+++ b/server/src/db/engine/postgres/sql-split.ts
@@ -0,0 +1,59 @@
+/**
+ * Splits generated DDL into single statements, so one statement Postgres
+ * refuses does not take the rest of the schema with it.
+ *
+ * Understood: `;` outside quotes, `'` and `"` quoted text, and the `$$` dollar
+ * quote a function body is written in, which is the only dollar quote TREK's
+ * generated DDL and baseline contain. A tagged dollar quote (`$tag$`) is not
+ * understood, so it is refused rather than split inside.
+ */
+
+/** A tagged dollar quote (`$tag$`), matched where the scan stands. */
+const TAGGED_DOLLAR_QUOTE = /\$[A-Za-z_]\w*\$/y;
+
+function refuseTaggedDollarQuote(sql: string, at: number): void {
+ TAGGED_DOLLAR_QUOTE.lastIndex = at;
+ const tag = TAGGED_DOLLAR_QUOTE.exec(sql)?.[0];
+ if (tag) throw new Error(`splitStatements understands only the $$ dollar quote, not ${tag} (at ${at})`);
+}
+
+/**
+ * The statements in `sql`, trimmed and in order. A statement that sets a
+ * session parameter (`SET ...`) is dropped: a dump's preamble, which a schema
+ * the entities generate never needs.
+ */
+export function splitStatements(sql: string): string[] {
+ const statements: string[] = [];
+ let current = '';
+ let quote: '"' | "'" | '$$' | null = null;
+ for (let i = 0; i < sql.length; i++) {
+ const char = sql[i];
+ const dollar = sql.startsWith('$$', i);
+ if (quote === '$$') {
+ if (dollar) {
+ quote = null;
+ current += '$';
+ i++;
+ }
+ current += char;
+ } else if (quote) {
+ if (char === quote) quote = null;
+ current += char;
+ } else if (dollar) {
+ quote = '$$';
+ current += '$$';
+ i++;
+ } else if (char === "'" || char === '"') {
+ quote = char;
+ current += char;
+ } else if (char === ';') {
+ if (current.trim()) statements.push(current.trim());
+ current = '';
+ } else {
+ if (char === '$') refuseTaggedDollarQuote(sql, i);
+ current += char;
+ }
+ }
+ if (current.trim()) statements.push(current.trim());
+ return statements.filter((statement) => !/^set\s/i.test(statement));
+}
diff --git a/server/src/db/engine/postgres/ssl.ts b/server/src/db/engine/postgres/ssl.ts
new file mode 100644
index 000000000..5b96a0c50
--- /dev/null
+++ b/server/src/db/engine/postgres/ssl.ts
@@ -0,0 +1,42 @@
+import { readFileSync } from 'node:fs';
+
+/** The sslmode values that verify the server, read the way the `pg` driver reads them. */
+const VERIFYING_MODES = new Set(['require', 'verify-ca', 'verify-full']);
+
+/** The `ssl` option of a `pg` pool, as `sslOptionFor` builds it. */
+type PgSslOption = false | { rejectUnauthorized: true; ca?: string };
+
+/**
+ * The `ssl` option for the pool, from the URL's `sslmode` and `sslrootcert`.
+ *
+ * MikroORM builds the pool from the URL's host, port, user, password and
+ * database alone and drops the rest of the query, so an `sslmode` in
+ * `TREK_DB_URL` would be ignored without this. `require`, `verify-ca` and
+ * `verify-full` all verify the server's certificate, as the `pg` driver itself
+ * treats them; `disable` turns SSL off, and no `sslmode` leaves the driver's
+ * default (no SSL). A mode that would connect without verifying (`prefer`,
+ * `allow`, `no-verify`) or that is misspelt refuses, rather than quietly
+ * connecting with less than was asked. `sslrootcert` names the CA file a host
+ * that signs its own certificates needs (Supabase's, say).
+ */
+export function sslOptionFor(url: string): PgSslOption | undefined {
+ const params = new URL(url).searchParams;
+ const mode = params.get('sslmode');
+ const rootCert = params.get('sslrootcert');
+ if (mode === null) {
+ if (rootCert !== null) throw new Error('TREK_DB_URL names an sslrootcert without sslmode=verify-full.');
+ return undefined;
+ }
+ if (mode === 'disable') return false;
+ if (!VERIFYING_MODES.has(mode)) {
+ throw new Error(
+ `TREK_DB_URL has sslmode "${mode}"; use disable, require, verify-ca or verify-full (all but disable verify the server).`,
+ );
+ }
+ if (rootCert === null) return { rejectUnauthorized: true };
+ try {
+ return { rejectUnauthorized: true, ca: readFileSync(rootCert, 'utf8') };
+ } catch (error) {
+ throw new Error(`TREK_DB_URL names sslrootcert ${rootCert}, which cannot be read.`, { cause: error });
+ }
+}
diff --git a/server/src/db/engine/redact-url.ts b/server/src/db/engine/redact-url.ts
new file mode 100644
index 000000000..c74caa274
--- /dev/null
+++ b/server/src/db/engine/redact-url.ts
@@ -0,0 +1,17 @@
+/**
+ * A connection URL fit for a log line: the password masked and the query
+ * dropped, since libpq reads a password from the query too
+ * (`?password=...`). A value that does not parse is not echoed at all.
+ */
+export function redactUrl(url: string): string {
+ let parsed: URL;
+ try {
+ parsed = new URL(url);
+ } catch {
+ return '';
+ }
+ if (parsed.password) parsed.password = '***';
+ parsed.search = '';
+ parsed.hash = '';
+ return parsed.toString();
+}
diff --git a/server/src/db/engine/sqlite/sqlite-engine.ts b/server/src/db/engine/sqlite/sqlite-engine.ts
new file mode 100644
index 000000000..a57360876
--- /dev/null
+++ b/server/src/db/engine/sqlite/sqlite-engine.ts
@@ -0,0 +1,61 @@
+import { closeDb, getRawConnection, registerReinitializeHook, reinitialize } from '../../database';
+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
+ * `db/database.ts` owns and the ORM's bound driver shares (`db/orm-driver.ts`),
+ * on `TREK_DB_FILE` or `data/travel.db` (`:memory:` under test). A restore
+ * swaps the file and reopens the handle; the schema comes from the migration
+ * chain (`db/orm.ts`).
+ */
+export class SqliteEngine implements DatabaseEngine {
+ readonly id = 'sqlite';
+
+ describe(): string {
+ return `SQLite file ${resolveDbPath()}`;
+ }
+
+ ormOptions() {
+ return { driver: SharedSqliteDriver, dbName: resolveDbPath() };
+ }
+
+ /**
+ * Opens the file (normally a no-op: the ORM's first connect inside
+ * NestFactory.create() already did, said here so the boot does not depend on
+ * that), and registers the reopen. A reopen swaps the file under the same
+ * handle, which Kysely caches, so the ORM's connection is closed and connected
+ * again first: the driver then reaches the new handle through
+ * `createKyselyDialect()`. The bound driver's `destroy()` is a no-op
+ * (`orm-driver.ts`), so the close never reaches the handle `db/database.ts` owns.
+ */
+ open(orm: AnyOrm, onReopened: () => Promise): void {
+ getRawConnection();
+ registerReinitializeHook(async () => {
+ const connection = orm.em.getConnection();
+ await connection.close(true);
+ await connection.connect();
+ await onReopened();
+ });
+ }
+
+ close(): void {
+ closeDb();
+ }
+
+ reopen(): Promise {
+ return reinitialize();
+ }
+
+ schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise {
+ return runSchemaBootstrap(orm, options);
+ }
+
+ /** A no-op: SQLite has one connection and one writer, so its transactions already run one at a time. */
+ serializeWrites(_em: EntityManager): Promise {
+ return Promise.resolve();
+ }
+}
diff --git a/server/src/db/engine/sqlite/sqlite-maintenance.ts b/server/src/db/engine/sqlite/sqlite-maintenance.ts
new file mode 100644
index 000000000..e151c9339
--- /dev/null
+++ b/server/src/db/engine/sqlite/sqlite-maintenance.ts
@@ -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 {
+ this.validateRequestContext();
+ await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
+ }
+
+ /**
+ * `` VACUUM INTO '' ``: 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 {
+ this.validateRequestContext();
+ const escaped = path.replaceAll("'", "''");
+ await this.em.getConnection().execute(`VACUUM INTO '${escaped}'`);
+ }
+}
diff --git a/server/src/db/migration-utils.ts b/server/src/db/migration-utils.ts
index d5a625e56..a691277cb 100644
--- a/server/src/db/migration-utils.ts
+++ b/server/src/db/migration-utils.ts
@@ -1,3 +1,5 @@
+import { columnNamesSql, tableExistsSql } from './dialect/catalog';
+import { TableNotFoundException } from '@mikro-orm/core';
import type { Migration } from '@mikro-orm/migrations';
/**
@@ -8,7 +10,7 @@ import type { Migration } from '@mikro-orm/migrations';
* a migration class.
*/
-/** A single `pragma_table_info` row — only the column name is ever read. */
+/** A catalog row (`dialect/catalog.ts`): only the name is ever read. */
interface ColumnRow {
name: string;
}
@@ -30,21 +32,18 @@ export async function addColumnIfMissing(
column: string,
definition: string,
): Promise {
- const existing = (await migration.execute('select name from pragma_table_info(?) where name = ?', [
- table,
- column,
- ])) as ColumnRow[];
- if (existing.length > 0) return;
- await migration.execute(`alter table \`${table}\` add column ${definition}`);
+ if ((await columnNames(migration, table)).has(column)) return;
+ const platform = migration.getEntityManager().getPlatform();
+ await migration.execute(`alter table ${platform.quoteIdentifier(table)} add column ${definition}`);
}
/** True when `table` exists in the current database. */
export async function tableExists(migration: Migration, table: string): Promise {
- const rows = await migration.execute(`select name from sqlite_master where type = 'table' and name = ?`, [table]);
+ const rows = await migration.execute(tableExistsSql(migration.getEntityManager().getPlatform()), [table]);
return rows.length > 0;
}
-/** The `CREATE TABLE` DDL SQLite stored for `table`, or null when it does not exist. */
+/** The `CREATE TABLE` DDL SQLite stored for `table`, or null when it does not exist. SQLite only: Postgres keeps no such text. */
export async function tableSql(migration: Migration, table: string): Promise {
const rows = (await migration.execute(`select sql from sqlite_master where name = ?`, [table])) as {
sql: string | null;
@@ -68,7 +67,8 @@ export async function execBestEffort(migration: Migration, sql: string, params?:
/** The column names of `table`, or an empty set when the table does not exist. */
export async function columnNames(migration: Migration, table: string): Promise> {
- const rows = (await migration.execute('select name from pragma_table_info(?)', [table])) as ColumnRow[];
+ const sql = columnNamesSql(migration.getEntityManager().getPlatform());
+ const rows = (await migration.execute(sql, [table])) as ColumnRow[];
return new Set(rows.map((r) => r.name));
}
@@ -80,10 +80,20 @@ export async function execUnlessTableMissing(migration: Migration, sql: string,
try {
await migration.execute(sql, params);
} catch (err: unknown) {
- if (!(err instanceof Error) || !err.message.includes('no such table')) throw err;
+ if (!isMissingTableError(err)) throw err;
}
}
+/**
+ * True when a statement failed because its table does not exist. The ORM maps
+ * SQLite's "no such table" and Postgres's SQLSTATE 42P01 to
+ * `TableNotFoundException`; the raw SQLite message is checked too, for an error
+ * that reached the caller unconverted.
+ */
+export function isMissingTableError(err: unknown): boolean {
+ return err instanceof TableNotFoundException || (err instanceof Error && err.message.includes('no such table'));
+}
+
/**
* Runs a group of statements the legacy step wrapped in a single swallowing
* try/catch. A failure part-way through leaves the earlier statements applied,
diff --git a/server/src/db/orm.ts b/server/src/db/orm.ts
index 0364b8484..efb0fa622 100644
--- a/server/src/db/orm.ts
+++ b/server/src/db/orm.ts
@@ -1,4 +1,3 @@
-import mikroOrmConfig from '../mikro-orm.config';
import { withRequestContext } from '../nest/database/request-context';
import { runDemoSeed } from './database';
import { ensureEmailCaseIndex } from './email-case-index';
@@ -6,16 +5,11 @@ import { migrateToHead, NO_SAFETY_NET } from './legacy-baseline';
import { preMigrateSnapshot } from './pre-migrate-snapshot';
import type { AnyEntity, EntityClass, EntityManager, EntitySchema, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
import type { Migrator } from '@mikro-orm/migrations';
-import { MikroORM as SqliteMikroORM } from '@mikro-orm/sqlite';
/**
- * Schema ownership at startup.
- *
- * This replaces `createTables()` → `runMigrations()` → `runSeeds()`, which used
- * to run synchronously inside `database.ts::initDb()` at module load. The
- * migrator is async, so the work moved to `buildApp()`, which is the first place
- * in the boot with an await and still runs before anything reads the database
- * (`bootstrap.ts` resolves SettingsService right after).
+ * Schema ownership at startup. The migrator is async, so the schema is brought
+ * to head from `DatabaseLifecycle.open()`, which `buildApp()` calls before
+ * anything reads the database. Nothing runs at module load.
*/
/**
@@ -35,10 +29,10 @@ export type AnyOrm = MikroORM<
* Migrate to head, then seed. Safe to call again, both halves are idempotent.
*
* `snapshot: false` skips the pre-migration copy (`pre-migrate-snapshot.ts`).
- * Only the restore hook passes it: the file it migrates was just unpacked from
- * a backup archive, which is still there to go back to.
+ * Only the reopen after a restore passes it: the file it migrates was just
+ * unpacked from a backup archive, which is still there to go back to.
*/
-export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { snapshot?: boolean } = {}): Promise {
+export async function runSchemaBootstrap(orm: AnyOrm, { snapshot }: { snapshot: boolean }): Promise {
const migrator = orm.config.getExtension('@mikro-orm/migrator') as Migrator;
const connection = orm.em.getConnection();
// A database the retired positional runner migrated has no migrator rows yet;
@@ -52,8 +46,16 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
undefined,
snapshot ? preMigrateSnapshot(connection) : NO_SAFETY_NET,
);
+ await finishSchemaBootstrap(orm);
+}
+
+/**
+ * What every engine does once its schema is at head: the deferred index, the
+ * seeders, the demo seed. Idempotent, like the migration step before it.
+ */
+export async function finishSchemaBootstrap(orm: AnyOrm): Promise {
// The one index a migration may have had to defer (see email-case-index.ts).
- await ensureEmailCaseIndex(connection);
+ await ensureEmailCaseIndex(orm.em.getConnection());
const defaultSeeder = orm.config.get('seeder').defaultSeeder;
if (defaultSeeder) await orm.seeder.seedString(defaultSeeder);
@@ -77,25 +79,3 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
// fully-synchronous version had by construction.
await withRequestContext(orm, () => runDemoSeed());
}
-
-/**
- * An ORM instance outside Nest, for the vitest global setup that builds the
- * schema snapshot. Production uses the container-owned one from
- * `MikroOrmModule.forRoot()` instead of creating a second.
- */
-export function createStandaloneOrm(): Promise {
- return SqliteMikroORM.init({
- ...mikroOrmConfig,
- // Entity discovery is skipped deliberately. Migrations and seeders are raw
- // SQL and need no metadata, and the global setup that calls this runs as ESM
- // in vitest's main process, where discovering `*.entity.ts` fails on Node's
- // extensionless import resolution.
- entities: [],
- entitiesTs: [],
- discovery: { warnWhenNoEntities: false },
- // The migrator's per-migration progress log is production boot output; the
- // global setup already reports the snapshot in one line of its own, and a
- // failing migration still throws.
- migrations: { ...mikroOrmConfig.migrations, silent: true },
- });
-}
diff --git a/server/src/db/pre-migrate-snapshot.ts b/server/src/db/pre-migrate-snapshot.ts
index 6ecab6f39..03f2b25e0 100644
--- a/server/src/db/pre-migrate-snapshot.ts
+++ b/server/src/db/pre-migrate-snapshot.ts
@@ -25,11 +25,10 @@ import path from 'node:path';
* migrated; `TREK_DB_PRE_MIGRATE_SNAPSHOT=false` is the way past that.
*/
-const PREFIX = 'pre-migrate-';
-const SNAPSHOT_FILE = /^pre-migrate-.+\.db$/;
+export const PREFIX = 'pre-migrate-';
/** `20261008T091500Z`: sortable, and free of the colons Windows refuses in a file name. */
-function stamp(now: Date): string {
+export function stamp(now: Date): string {
return now
.toISOString()
.replace(/\.\d+Z$/, 'Z')
@@ -51,11 +50,11 @@ async function holdsAnything(connection: Connection): Promise {
return rows.length > 0;
}
-/** The snapshots in `dir`, newest first. */
-export function listSnapshots(dir: string): string[] {
+/** The snapshots in `dir`, newest first: the SQLite file's (`.db`) or a Postgres engine's (`.pgdump`). */
+export function listSnapshots(dir: string, extension = '.db'): string[] {
let entries: string[];
try {
- entries = fs.readdirSync(dir).filter((entry) => SNAPSHOT_FILE.test(entry));
+ entries = fs.readdirSync(dir).filter((entry) => entry.startsWith(PREFIX) && entry.endsWith(extension));
} catch {
return [];
}
@@ -66,8 +65,8 @@ export function listSnapshots(dir: string): string[] {
}
/** Deletes all but the newest `keep`. A failure only warns: the new snapshot is already safe. */
-export function pruneSnapshots(dir: string, keep: number): void {
- for (const file of listSnapshots(dir).slice(keep)) {
+export function pruneSnapshots(dir: string, keep: number, extension = '.db'): void {
+ for (const file of listSnapshots(dir, extension).slice(keep)) {
try {
fs.rmSync(file, { force: true });
} catch (err) {
diff --git a/server/src/db/repositories/DayAccommodations.repository.ts b/server/src/db/repositories/DayAccommodations.repository.ts
index 403d6cd1b..e6d45e4ae 100644
--- a/server/src/db/repositories/DayAccommodations.repository.ts
+++ b/server/src/db/repositories/DayAccommodations.repository.ts
@@ -1,6 +1,7 @@
import { coalesceOverride, columnRef } from '../dialect/sql-functions';
import type { DayAccommodations } from '../entities/DayAccommodations.entity';
import type { DB } from '../kysely/db';
+import { accommodationIdMatches } from './_shared/reservation-accommodation';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
@@ -249,10 +250,13 @@ export class DayAccommodationsRepository extends TrekRepository {
+ const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely()
.selectFrom('day_accommodations as a')
.leftJoin('places as p', 'p.id', 'a.place_id')
- .leftJoin('reservations as r', 'r.accommodation_id', 'a.id')
+ .leftJoin('reservations as r', (join) =>
+ join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'a.id')),
+ )
.selectAll('a')
.select([
'p.name as place_name',
@@ -469,6 +473,7 @@ export class DayAccommodationsRepository extends TrekRepository {
+ const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely()
.selectFrom('day_accommodations as a')
.leftJoin('places as p', 'p.id', 'a.place_id')
@@ -485,7 +490,7 @@ export class DayAccommodationsRepository extends TrekRepository eb2.fn.min('r.id').as('m'))
- .whereRef('r.accommodation_id', '=', 'a.id')
+ .where((eb2) => accommodationIdMatches(platform, eb2, 'r.accommodation_id', 'a.id'))
.as('reservation_id'),
])
.where('a.trip_id', '=', trip_id)
diff --git a/server/src/db/repositories/GoogleApiUsage.repository.ts b/server/src/db/repositories/GoogleApiUsage.repository.ts
index 47932a7bc..19720f425 100644
--- a/server/src/db/repositories/GoogleApiUsage.repository.ts
+++ b/server/src/db/repositories/GoogleApiUsage.repository.ts
@@ -43,7 +43,9 @@ export class GoogleApiUsageRepository extends TrekRepository {
await this.kysely()
.insertInto('google_api_usage')
.values({ day, calls: 1 })
- .onConflict((oc) => oc.column('day').doUpdateSet({ calls: (eb) => eb('calls', '+', 1) }))
+ // The target row's column is qualified: Postgres reads a bare name as
+ // ambiguous between it and `excluded` (42702).
+ .onConflict((oc) => oc.column('day').doUpdateSet({ calls: (eb) => eb(eb.ref('google_api_usage.calls'), '+', 1) }))
.execute();
}
diff --git a/server/src/db/repositories/MaintenanceRepository.ts b/server/src/db/repositories/MaintenanceRepository.ts
index 93a82b412..1e988649f 100644
--- a/server/src/db/repositories/MaintenanceRepository.ts
+++ b/server/src/db/repositories/MaintenanceRepository.ts
@@ -1,27 +1,18 @@
+import { isMissingTableError } from '../migration-utils';
import type { EntityManager } from '@mikro-orm/core';
/**
- * Plan 3i Task 3 — R1's raw-connection exception, named ahead of this plan by
- * the program brief's own rule 4 ("connection.execute() (only in
- * MaintenanceRepository/DemoRepository, with a comment)").
- *
- * Not a `TrekRepository` — it backs no table and no entity of its
- * own. It exists purely as the one place two whole-database-file statements
- * (a `PRAGMA` and a `VACUUM INTO`) run against the ORM's own bound driver
- * connection instead of scattering raw `better-sqlite3` calls across
- * `backup.impl.ts`/`demo-reset.ts`. `find/findOne → create/assign/flush →
- * nativeUpdate/nativeDelete → em.upsert → qb() → em.getKysely()` all fail
- * for these two statements — neither is expressible against an entity at
- * all — so `connection.execute()` (the last tier in the program's query-API
- * order) is the correct tool, not a shortcut around it.
+ * Raw-connection statements that run on every engine and that no entity can
+ * express: the readiness ping and the plugin-table erasure. Not a
+ * `TrekRepository`, since it backs no entity. The SQLite file's own
+ * maintenance (`PRAGMA wal_checkpoint`, `VACUUM INTO`) lives in
+ * `SqliteMaintenance`, under `db/engine/sqlite/`.
*
* Provided through Nest DI by `MaintenanceModule` (`nest/database/`), a
* factory over the context-resolving global `EntityManager`; its consumers
- * inject it: the readiness probe, `UserCleanupService` and the SQLite backup
- * port (`SqliteDatabaseBackup`), which the backup, the restore and the demo
- * reset all go through. The class stays free of Nest decorators like every
- * other file under `db/`, so a test can still build one over its own
- * `EntityManager`.
+ * inject it: the readiness probe and `UserCleanupService`. The class stays
+ * free of Nest decorators like every other file under `db/`, so a test can
+ * still build one over its own `EntityManager`.
*/
export class MaintenanceRepository {
constructor(private readonly em: EntityManager) {}
@@ -30,7 +21,7 @@ export class MaintenanceRepository {
* MikroORM's own validating `getContext()` — throws
* `ValidationError.cannotUseGlobalContext()` outside a request context (the
* production default), the same fail-closed ratchet `TrekRepository`
- * applies to every entity-bound repository. Called first by both methods
+ * applies to every entity-bound repository. Called first by every method
* below, since raw `connection.execute()` has no such guard of its own.
*/
private validateRequestContext(): void {
@@ -44,69 +35,27 @@ export class MaintenanceRepository {
}
/**
- * `PRAGMA wal_checkpoint(TRUNCATE)` — rendered text pinned, identical to
- * the legacy `db.exec('PRAGMA wal_checkpoint(TRUNCATE)')` it replaces.
- * Reached through the backup port's `checkpoint()`, which the backup
- * (BK1) and the demo reset (DMR3) call. Flushes the WAL into the main db
- * file before a snapshot or a swap; every caller treats it as best effort.
- */
- async walCheckpoint(): Promise {
- this.validateRequestContext();
- await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
- }
-
- /**
- * `` VACUUM INTO '' `` — rendered text pinned, identical to the
- * legacy `` db.exec(`VACUUM INTO '${dbSnap.replaceAll("'", "''")}'`) ``
- * (BK2, reached through the backup port's `snapshot()`): single quotes in `path` are
- * escaped by doubling, the same escaping the legacy statement used — a
- * consistent point-in-time snapshot under concurrent writers, with no
- * MikroORM/Kysely equivalent.
- */
- async vacuumInto(path: string): Promise {
- this.validateRequestContext();
- const escaped = path.replaceAll("'", "''");
- await this.em.getConnection().execute(`VACUUM INTO '${escaped}'`);
- }
-
- /**
- * Plan 4 Task 4 — UC1 (`UserCleanupService.erasePluginUserData`): a
- * best-effort per-user delete across the three plugin-held tables
- * (`plugin_user_config`/`plugin_oauth_tokens`/`plugin_oauth_state`). These
- * are `nest/plugins`' own tables (Plan 3b Task 5's "stays raw" ruling,
- * narrowed by Plan 4 Task 8a to just this trio — UC2/UC3 already moved to
- * `PluginsRepository`/`PluginUserErasureQueueRepository`), not an entity
- * managed by any repository, so `connection.execute()` (rule 4's last
- * tier) is the correct tool here, same shape as `walCheckpoint`/
- * `vacuumInto` above — with ONE difference those two don't need: this is
- * the first `MaintenanceRepository` caller ever invoked from INSIDE an
- * open `UnitOfWork.transactional(...)` (`deleteUserCompletely`'s
- * transaction). `Connection#execute(query, params, method, ctx)`'s `ctx`
- * defaults to the plain (non-transactional) Kysely client when omitted —
- * `walCheckpoint`/`vacuumInto` always omit it because their callers
- * (the backup port) never run inside a transaction, but
- * omitting it here deadlocked: this repo's `better-sqlite3` connection is
- * single-connection/single-writer, the open transaction holds it for its
- * whole lifetime, and a bare `execute()` tries to check out that SAME
- * connection fresh instead of reusing the held one — a checkout that only
- * frees once the transaction (which is itself awaiting THIS call)
- * completes. `em.getTransactionContext()` returns the open transaction's
- * driver handle when called from inside one (resolved the same
- * `TransactionContext`-async-local-storage way `getContext()` is, so
- * `this.em` need not be the transactional fork itself) and `undefined`
- * otherwise, so passing it through is correct both inside and outside a
- * transaction. Each statement is independently best-effort: a
- * slimmed-down test schema may not carry one or more of these tables at
- * all, matching the legacy per-table try/catch exactly.
+ * The per-user erasure of the three plugin-held tables (`plugin_user_config`,
+ * `plugin_oauth_tokens`, `plugin_oauth_state`), called inside the erasure's
+ * own `UnitOfWork.transactional(...)`.
+ *
+ * A table that does not exist (a slimmed-down test schema) is skipped; any
+ * other failure propagates. Each statement runs in its own savepoint, since
+ * on Postgres a failed statement aborts the whole transaction it is in.
+ * `em.getTransactionContext()` hands the statement the open transaction's
+ * connection, which a bare `execute()` would wait on forever.
*/
async deletePluginUserData(userId: number): Promise {
this.validateRequestContext();
- const ctx = this.em.getTransactionContext();
for (const table of ['plugin_user_config', 'plugin_oauth_tokens', 'plugin_oauth_state']) {
try {
- await this.em.getConnection().execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', ctx);
- } catch {
- /* table absent (slim schema) */
+ await this.em.transactional((em) =>
+ em
+ .getConnection()
+ .execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', em.getTransactionContext()),
+ );
+ } catch (err: unknown) {
+ if (!isMissingTableError(err)) throw err;
}
}
}
diff --git a/server/src/db/repositories/Places.repository.ts b/server/src/db/repositories/Places.repository.ts
index e9fe5e761..98257e2d8 100644
--- a/server/src/db/repositories/Places.repository.ts
+++ b/server/src/db/repositories/Places.repository.ts
@@ -1,6 +1,6 @@
import type { Places } from '../entities/Places.entity';
import { wrapOptionalLongitude } from '@trek/shared';
-import { absDifference, coalesceParam, columnRef, currentTimestamp, lowerTrim } from '../dialect/sql-functions';
+import { absDifference, coalesceParam, columnRef, currentTimestamp, ilike, lowerTrim } from '../dialect/sql-functions';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
import { tourPlaceTextPatch, type TourPlaceWrite } from './tour-place-write';
@@ -659,8 +659,8 @@ export class PlacesRepository extends TrekRepository {
*/
async listForTrip(trip_id: string, filters: {
searchPattern?: string;
- category?: string;
- tag?: string;
+ category?: number;
+ tag?: number;
assignment?: 'all' | 'unassigned' | 'assigned';
}): Promise {
const platform = this.getEntityManager().getPlatform();
@@ -680,15 +680,13 @@ export class PlacesRepository extends TrekRepository {
.where('p.trip_id = ?', [trip_id]);
if (filters.searchPattern) {
- qb.andWhere(
- "(p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\' OR p.description LIKE ? ESCAPE '\\')",
- [filters.searchPattern, filters.searchPattern, filters.searchPattern],
- );
+ const fields = ['p.name', 'p.address', 'p.description'];
+ qb.andWhere(ilike(platform, fields), fields.map(() => filters.searchPattern));
}
- if (filters.category) {
+ if (filters.category !== undefined) {
qb.andWhere('p.category_id = ?', [filters.category]);
}
- if (filters.tag) {
+ if (filters.tag !== undefined) {
qb.andWhere('p.id IN (SELECT place_id FROM place_tags WHERE tag_id = ?)', [filters.tag]);
}
if (filters.assignment === 'unassigned') {
diff --git a/server/src/db/repositories/ReservationDayPositions.repository.ts b/server/src/db/repositories/ReservationDayPositions.repository.ts
index 99259e16f..0b6ca570e 100644
--- a/server/src/db/repositories/ReservationDayPositions.repository.ts
+++ b/server/src/db/repositories/ReservationDayPositions.repository.ts
@@ -52,10 +52,14 @@ export class ReservationDayPositionsRepository extends TrekRepository {
await this.kysely()
.insertInto('reservation_day_positions')
- .orReplace()
.columns(['reservation_id', 'day_id', 'position'])
.expression((eb) =>
eb
@@ -79,6 +82,9 @@ export class ReservationDayPositionsRepository extends TrekRepository
+ oc.columns(['reservation_id', 'day_id']).doUpdateSet((eb) => ({ position: eb.ref('excluded.position') })),
+ )
.execute();
}
}
diff --git a/server/src/db/repositories/Reservations.repository.ts b/server/src/db/repositories/Reservations.repository.ts
index 6479236f0..00ca948ac 100644
--- a/server/src/db/repositories/Reservations.repository.ts
+++ b/server/src/db/repositories/Reservations.repository.ts
@@ -2,7 +2,8 @@ import type { Reservations } from '../entities/Reservations.entity';
import { DayAssignments } from '../entities/DayAssignments.entity';
import { Days } from '../entities/Days.entity';
import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functions';
-import { castIntegerKysely, concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
+import { concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
+import { accommodationIdMatches } from './_shared/reservation-accommodation';
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
import type { DayAssignmentRow } from './day-assignment-rows';
import { TrekRepository } from './_shared/trek-repository';
@@ -32,18 +33,6 @@ export type {
PublicApiUnscheduledReservationRow,
};
-/**
- * Kysely typing for the RS18/RR1 join (`joinedQuery` below) — `reservations
- * .accommodation_id` is a bare `p.text().nullable()` column with no FK to
- * `day_accommodations` (§18.1 of the inventory), so the QueryBuilder's
- * relation-path `.join()` cannot express `LEFT JOIN day_accommodations ap ON
- * r.accommodation_id = ap.id` at all — the same escape hatch
- * `DayAssignmentsRepository.effectiveStart`/`TripsRepository.tripSelectQuery`
- * use. The `id`/`trip_id` the callers pass stay `number | string` (raw-bind,
- * D4's T5 seam) and are cast to the column type only at the bind, so this
- * join keeps the legacy statement's exact raw-bind flexibility.
- */
-
/**
* `reservations` as the two aliases below bind it: `accommodation_id` is a
* TEXT column, widened to `string | number | null` so the plain `number`
@@ -364,7 +353,7 @@ export class ReservationsRepository extends TrekRepository {
findPassContext(id: number, tripId: number) { return passContextQuery(this.kysely(), id, tripId); }
private joinedQuery() {
- return joinedReservationQuery(this.kysely());
+ return joinedReservationQuery(this.kysely(), this.getEntityManager().getPlatform());
}
/**
@@ -875,10 +864,13 @@ export class ReservationsRepository extends TrekRepository {
/** CL2 — see {@link CalendarReservationRow}'s docstring. */
async listForCalendar(trip_id: number | string): Promise {
+ const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely()
.selectFrom('reservations as r')
.leftJoin('places as pl', 'pl.id', 'r.place_id')
- .leftJoin('day_accommodations as a', 'a.id', 'r.accommodation_id')
+ .leftJoin('day_accommodations as a', (join) =>
+ join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'a.id')),
+ )
.leftJoin('days as sd', 'sd.id', 'a.start_day_id')
.leftJoin('days as ed', 'ed.id', 'a.end_day_id')
.leftJoin('days as rd', 'rd.id', 'r.day_id')
@@ -894,7 +886,7 @@ export class ReservationsRepository extends TrekRepository {
eb
.selectFrom('reservations as r2')
.select((eb2) => eb2.fn.min('r2.id').as('c'))
- .whereRef('r2.accommodation_id', '=', 'a.id')
+ .where((eb2) => accommodationIdMatches(platform, eb2, 'r2.accommodation_id', 'a.id'))
.as('stay_first_reservation_id'),
'rd.date as day_date',
'red.date as end_day_date',
@@ -932,6 +924,7 @@ export class ReservationsRepository extends TrekRepository {
/** CL7 — see {@link CalendarStayRow}'s docstring. */
async listPublicStaysForCalendar(trip_id: number | string): Promise {
+ const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely()
.selectFrom('day_accommodations as a')
.where('a.trip_id', '=', trip_id as number)
@@ -953,7 +946,7 @@ export class ReservationsRepository extends TrekRepository {
eb
.selectFrom('reservations as r')
.select('r.title')
- .whereRef('r.accommodation_id', '=', 'a.id')
+ .where((eb2) => accommodationIdMatches(platform, eb2, 'r.accommodation_id', 'a.id'))
.where((eb2) => publicReservationExpr(eb2, 'r.ingest_state'))
.orderBy('r.id', 'asc')
.limit(1)
@@ -1039,7 +1032,7 @@ export class ReservationsRepository extends TrekRepository {
eb
.selectFrom('reservations as res')
.select('res.title')
- .where((eb2) => eb2(castIntegerKysely(platform, eb2, 'res.accommodation_id'), '=', eb2.ref('a.id')))
+ .where((eb2) => accommodationIdMatches(platform, eb2, 'res.accommodation_id', 'a.id'))
.where('res.status', '!=', 'cancelled')
.orderBy('res.id', 'asc')
.limit(1),
@@ -1080,7 +1073,7 @@ export class ReservationsRepository extends TrekRepository {
eb
.selectFrom('reservations as res')
.select('res.title')
- .where((eb2) => eb2(castIntegerKysely(platform, eb2, 'res.accommodation_id'), '=', eb2.ref('a.id')))
+ .where((eb2) => accommodationIdMatches(platform, eb2, 'res.accommodation_id', 'a.id'))
.where('res.status', '!=', 'cancelled')
.orderBy('res.id', 'asc')
.limit(1),
diff --git a/server/src/db/repositories/RouteUsageDaily.repository.ts b/server/src/db/repositories/RouteUsageDaily.repository.ts
index ce011685f..4adaeea9a 100644
--- a/server/src/db/repositories/RouteUsageDaily.repository.ts
+++ b/server/src/db/repositories/RouteUsageDaily.repository.ts
@@ -80,12 +80,14 @@ export class RouteUsageDailyRepository extends TrekRepository {
await this.kysely()
.insertInto('route_usage_daily')
.values(entry)
+ // The target row's columns are qualified: Postgres reads a bare name as
+ // ambiguous between it and `excluded` (42702).
.onConflict((oc) =>
oc.columns(['day', 'profile', 'surface', 'self_hosted']).doUpdateSet({
- requests: (eb) => eb('requests', '+', eb.ref('excluded.requests')),
- waypoints: (eb) => eb('waypoints', '+', eb.ref('excluded.waypoints')),
- km: (eb) => eb('km', '+', eb.ref('excluded.km')),
- failed: (eb) => eb('failed', '+', eb.ref('excluded.failed')),
+ requests: (eb) => eb(eb.ref('route_usage_daily.requests'), '+', eb.ref('excluded.requests')),
+ waypoints: (eb) => eb(eb.ref('route_usage_daily.waypoints'), '+', eb.ref('excluded.waypoints')),
+ km: (eb) => eb(eb.ref('route_usage_daily.km'), '+', eb.ref('excluded.km')),
+ failed: (eb) => eb(eb.ref('route_usage_daily.failed'), '+', eb.ref('excluded.failed')),
}),
)
.execute();
diff --git a/server/src/db/repositories/Trips.repository.ts b/server/src/db/repositories/Trips.repository.ts
index 9adb59582..b4d5c9b8c 100644
--- a/server/src/db/repositories/Trips.repository.ts
+++ b/server/src/db/repositories/Trips.repository.ts
@@ -1,6 +1,6 @@
import { QueryFlag } from '@mikro-orm/core';
import type { TTripsCostsMode, Trips } from '../entities/Trips.entity';
-import { coalesceParam, currentTimestamp, nowDateOffset } from '../dialect/sql-functions';
+import { coalesceParam, currentTimestamp, ilike, nowDateOffset } from '../dialect/sql-functions';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
import { tripAccessExpr } from './_shared/trip-access';
@@ -175,7 +175,7 @@ export class TripsRepository extends TrekRepository {
return await this.accessibleTripsQuery(user_id)
.join('t.places_collection', 'p')
.select(['p.trip', 'p.name'])
- .andWhere("(p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\')", [likePattern, likePattern])
+ .andWhere(ilike(this.getEntityManager().getPlatform(), ['p.name', 'p.address']), [likePattern, likePattern])
.orderBy([{ 'p.trip': 'asc' }, { 'p.name': 'asc' }])
.limit(500)
// The limit is on the joined place rows, like the legacy statement's.
diff --git a/server/src/db/repositories/_shared/reservation-accommodation.ts b/server/src/db/repositories/_shared/reservation-accommodation.ts
new file mode 100644
index 000000000..d6d5aee45
--- /dev/null
+++ b/server/src/db/repositories/_shared/reservation-accommodation.ts
@@ -0,0 +1,22 @@
+import { castIntegerKysely } from '../../dialect/kysely-functions';
+import type { Platform } from '@mikro-orm/core';
+
+import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } from 'kysely';
+
+/**
+ * The join condition between a reservation and the stay it is booked against.
+ *
+ * `reservations.accommodation_id` is TEXT with no foreign key, while
+ * `day_accommodations.id` is INTEGER, so the comparison is written as
+ * `CAST( AS INTEGER) = `. SQLite coerces a bare
+ * text-to-integer comparison and Postgres refuses it (42883), so the cast is
+ * needed on both engines. A stored `'14.0'` casts to 14 on both.
+ */
+export function accommodationIdMatches(
+ platform: Platform,
+ eb: ExpressionBuilder,
+ reservationAccommodationId: StringReference,
+ stayId: StringReference,
+): ExpressionWrapper {
+ return eb(castIntegerKysely(platform, eb, reservationAccommodationId), '=', eb.ref(stayId).$castTo());
+}
diff --git a/server/src/db/repositories/_shared/reservation-joined-query.ts b/server/src/db/repositories/_shared/reservation-joined-query.ts
index 09826c819..533cbb4dc 100644
--- a/server/src/db/repositories/_shared/reservation-joined-query.ts
+++ b/server/src/db/repositories/_shared/reservation-joined-query.ts
@@ -1,4 +1,6 @@
import type { DB } from '../../kysely/db';
+import { accommodationIdMatches } from './reservation-accommodation';
+import type { Platform } from '@mikro-orm/core';
import type { Kysely } from 'kysely';
@@ -9,14 +11,18 @@ export type ReservationJoinDB = Pick) {
+export function joinedReservationQuery(kysely: Kysely, platform: Platform) {
return kysely
.selectFrom('reservations as r')
.leftJoin('days as d', 'd.id', 'r.day_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
- .leftJoin('day_accommodations as ap', 'ap.id', 'r.accommodation_id')
+ .leftJoin('day_accommodations as ap', (join) =>
+ join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),
+ )
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.selectAll('r')
.select([
diff --git a/server/src/db/standalone-orm.ts b/server/src/db/standalone-orm.ts
new file mode 100644
index 000000000..f300bfff2
--- /dev/null
+++ b/server/src/db/standalone-orm.ts
@@ -0,0 +1,32 @@
+import mikroOrmConfig from '../mikro-orm.config';
+import type { AnyOrm } from './orm';
+import { SharedSqliteDriver } from './orm-driver';
+import { MikroORM as SqliteMikroORM } from '@mikro-orm/sqlite';
+
+/**
+ * An ORM instance outside Nest, for the vitest global setup that builds the
+ * schema snapshot. Production uses the container-owned one from
+ * `MikroOrmModule.forRoot()` instead of creating a second.
+ *
+ * SQLite only: the snapshot is the SQLite schema and the init below is the
+ * SQLite driver's, so a config that names another engine is refused.
+ */
+export function createStandaloneOrm(): Promise {
+ if (mikroOrmConfig.driver !== SharedSqliteDriver) {
+ throw new Error('createStandaloneOrm builds the SQLite schema snapshot; unset TREK_DB_URL to run it.');
+ }
+ return SqliteMikroORM.init({
+ ...mikroOrmConfig,
+ // Entity discovery is skipped deliberately. Migrations and seeders are raw
+ // SQL and need no metadata, and the global setup that calls this runs as ESM
+ // in vitest's main process, where discovering `*.entity.ts` fails on Node's
+ // extensionless import resolution.
+ entities: [],
+ entitiesTs: [],
+ discovery: { warnWhenNoEntities: false },
+ // The migrator's per-migration progress log is production boot output; the
+ // global setup already reports the snapshot in one line of its own, and a
+ // failing migration still throws.
+ migrations: { ...mikroOrmConfig.migrations, silent: true },
+ });
+}
diff --git a/server/src/index.ts b/server/src/index.ts
index 7748bfdc6..5accbe2bb 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -14,6 +14,7 @@ import { resolveDataPaths } from './app-config/data-paths';
// keeping the require after the restore keeps that true by construction.
import type * as Bootstrap from './bootstrap';
import { resolveDbPath } from './db/db-path';
+import { postgresTarget } from './db/engine';
import { flushLogFileSync, logError } from './nest/audit/audit-log.logger';
import type { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
import { ReadinessService } from './nest/health/readiness.service';
@@ -150,6 +151,7 @@ async function restoreBeforeTheDatabaseOpens() {
dbFile: resolveDbPath(),
dataDir: paths.dataDir,
reclaimResetFile: paths.reclaimResetFile,
+ postgres: postgresTarget(env.db) ?? undefined,
});
}
diff --git a/server/src/mikro-orm.config.ts b/server/src/mikro-orm.config.ts
index 06806abdf..b6afe6da2 100644
--- a/server/src/mikro-orm.config.ts
+++ b/server/src/mikro-orm.config.ts
@@ -1,30 +1,14 @@
-import { resolveDbPath } from './db/db-path';
-import { ALL_ENTITIES } from './db/entities';
-import { SharedSqliteDriver } from './db/orm-driver';
-import { Migrator } from '@mikro-orm/migrations';
-import { SeedManager } from '@mikro-orm/seeder';
-import { defineConfig } from '@mikro-orm/sqlite';
+import { buildOrmConfig, resolveEngine } from './db/engine';
-export default defineConfig({
- entities: ALL_ENTITIES,
- extensions: [Migrator, SeedManager],
- // Reuses the connection db/database.ts owns rather than opening a second one.
- // See orm-driver.ts — with `:memory:` in tests, a second connection would be a
- // second, empty database.
- driver: SharedSqliteDriver,
- migrations: {
- path: 'dist/db/migrations',
- pathTs: 'src/db/migrations',
- snapshot: false,
- },
- seeder: {
- path: 'dist/db/seeders',
- pathTs: 'src/db/seeders',
- defaultSeeder: 'DatabaseSeeder',
- },
- // Same resolution database.ts uses: `:memory:` under test, TREK_DB_FILE when
- // set, else data/travel.db. The driver above ignores it for the connection
- // itself, but the CLI and the migrator's logging still read it, and naming a
- // different file here than the one actually open is a trap.
- dbName: resolveDbPath(),
-});
+/**
+ * The MikroORM config for the CLI and the Nest container alike: the engine
+ * `TREK_DB_URL` names (the SQLite file by default) and what every engine shares
+ * (`db/engine/index.ts`).
+ *
+ * Built at module level, not from `DATABASE_ENGINE`, because the MikroORM CLI
+ * loads this file without Nest. Only the engine's options come from it, and
+ * those are plain values with no state, so it and the container's engine agree
+ * on the database. The state an engine keeps (Postgres's reopen hook) exists
+ * only on the container's instance.
+ */
+export default buildOrmConfig(resolveEngine());
diff --git a/server/src/nest/assistant/assistant.service.ts b/server/src/nest/assistant/assistant.service.ts
index 0904b98d2..40ce2a14f 100644
--- a/server/src/nest/assistant/assistant.service.ts
+++ b/server/src/nest/assistant/assistant.service.ts
@@ -366,6 +366,7 @@ export class AssistantService {
const decided = await this.claimDecisions(threadId, req.decisions);
// Taken but not run yet: whatever ends the turn before a change ran records it as failed.
const unrun = new Set(decided.filter(({ approved }) => approved).map(({ call }) => call.id));
+ const settle = () => this.failUnrun(threadId, decided, unrun);
try {
const stream = channel.open();
const w = new UiStreamWriter(stream);
@@ -387,20 +388,26 @@ export class AssistantService {
firstRowId: null,
renders: req.renders === true,
};
- await this.converse(spec, stream, w, signal, (session) =>
- this.applyDecisions(
- (name, args) => this.runChange(session, { userId, scope, threadId }, name, args),
- threadId,
- decided,
- {
- w,
- unrun,
- scope,
- },
- ),
+ await this.converse(
+ spec,
+ stream,
+ w,
+ signal,
+ (session) =>
+ this.applyDecisions(
+ (name, args) => this.runChange(session, { userId, scope, threadId }, name, args),
+ threadId,
+ decided,
+ {
+ w,
+ unrun,
+ scope,
+ },
+ ),
+ settle,
);
} finally {
- if (unrun.size > 0) await this.failUnrun(threadId, decided, unrun);
+ await settle();
}
});
}
@@ -440,8 +447,11 @@ export class AssistantService {
w.finish('error');
} finally {
await opened?.close();
- w.close();
- if (unrun.size > 0) await this.failUnrun(threadId, decided, unrun);
+ try {
+ await this.failUnrun(threadId, decided, unrun);
+ } finally {
+ w.close();
+ }
}
});
}
@@ -732,21 +742,26 @@ export class AssistantService {
/**
* The approved changes the turn ended before running (the tool session did
* not open, the stream broke): recorded as failed, so the thread shows them
- * as not applied and nobody takes them for a change that ran.
+ * as not applied and nobody takes them for a change that ran. Once: the set
+ * is emptied first, so the stream's close and the caller's cleanup do not
+ * both record them.
*
* @txIndependent each outcome is written on its own, like the ones that ran.
*/
private async failUnrun(
threadId: number,
decided: Array<{ call: AssistantToolCallRow; approved: boolean }>,
- unrun: ReadonlySet,
+ unrun: Set,
): Promise {
+ if (unrun.size === 0) return;
+ const pending = new Set(unrun);
+ unrun.clear();
const outcomes = new Map();
for (const { call, approved } of decided) {
if (!approved) outcomes.set(call.id, { approved: false, result: DISCARDED, isError: false });
- else if (unrun.has(call.id)) outcomes.set(call.id, { approved: true, result: NOT_RUN, isError: true });
+ else if (pending.has(call.id)) outcomes.set(call.id, { approved: true, result: NOT_RUN, isError: true });
}
- const failed = decided.filter(({ call, approved }) => approved && unrun.has(call.id));
+ const failed = decided.filter(({ call, approved }) => approved && pending.has(call.id));
await Promise.all(
failed.map(({ call }) =>
this.inContext(() =>
@@ -810,6 +825,7 @@ export class AssistantService {
w: UiStreamWriter,
signal: AbortSignal,
prelude?: (session: AssistantToolSession) => Promise,
+ settle?: () => Promise,
): Promise {
const stopKeepalive = armKeepalive(stream);
let opened: AssistantToolSession | null = null;
@@ -884,7 +900,12 @@ export class AssistantService {
} finally {
stopKeepalive();
await opened?.close();
- w.close();
+ try {
+ // Before the close: the client reads the thread again once the stream ends.
+ await settle?.();
+ } finally {
+ w.close();
+ }
}
}
diff --git a/server/src/nest/backup/backup.impl.ts b/server/src/nest/backup/backup.impl.ts
index 152679aec..d01bad257 100644
--- a/server/src/nest/backup/backup.impl.ts
+++ b/server/src/nest/backup/backup.impl.ts
@@ -4,7 +4,9 @@ import { UserSessions } from '../../db/entities/UserSessions.entity';
import type { CarriedUserSessionRow } from '../../db/repositories/UserSessions.repository';
import { dbNow } from '../../db/types';
import { logError, logWarn } from '../audit/audit-log.logger';
-import type { DatabaseBackupStrategy } from '../database/database-backup.interface';
+import { describeError } from '../common/describe-error';
+import { DomainError } from '../common/domain-error';
+import type { DatabaseBackupRefusal, DatabaseBackupStrategy } from '../database/database-backup.interface';
import { invalidatePermissionsCache } from '../permissions/permissions-cache';
import { snapshotAllPluginDataDbs } from '../plugins/host/plugin-data.service';
import { pluginsCodeRoot, pluginsDataRoot } from '../plugins/paths';
@@ -36,8 +38,6 @@ export interface BackupDeps {
database: DatabaseBackupStrategy;
}
-const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err));
-
// The scratch and key directory. The database itself may live elsewhere
// (TREK_DB_FILE); the database port knows where.
const { dataDir, encryptionKeyFile, reclaimResetFile } = resolveDataPaths();
@@ -467,6 +467,12 @@ async function restoreCarriedSessions(rows: readonly CarriedUserSessionRow[]): P
}
}
+/** The restore route's refusal for a `verify` that threw one. Any other failure is not a refusal, and keeps propagating. */
+function refusalOf(err: unknown): DatabaseBackupRefusal {
+ if (err instanceof DomainError) return { error: err.publicMessage, status: err.getStatus() };
+ throw err;
+}
+
export async function restoreFromZip({ storage, database }: BackupDeps, zipPath: string): Promise {
const extractDir = path.join(dataDir, `restore-${Date.now()}`);
let reinitFailed: unknown;
@@ -474,7 +480,7 @@ export async function restoreFromZip({ storage, database }: BackupDeps, zipPath:
const refused = await extractBackupArchive(zipPath, extractDir);
if (refused) return { success: false, ...refused };
- const unreadable = database.verify(extractDir);
+ const unreadable = await database.verify(extractDir).then(() => null, refusalOf);
if (unreadable) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, ...unreadable };
diff --git a/server/src/nest/backup/boot-restore.ts b/server/src/nest/backup/boot-restore.ts
index 1849b17fa..4b96eccb8 100644
--- a/server/src/nest/backup/boot-restore.ts
+++ b/server/src/nest/backup/boot-restore.ts
@@ -1,5 +1,7 @@
+import { pgRestore, pgTableNames } from '../../db/engine/postgres/pg-tools';
import { stageExtractedPluginTrees } from '../plugins/plugin-backup';
import { checkBackupDatabase, extractBackupArchive, markReclaimResetPending } from './backup-archive';
+import { checkPostgresDump, PG_ARCHIVE_ENTRY } from './postgres-database-backup';
import fs from 'fs';
import path from 'path';
@@ -47,6 +49,11 @@ export interface BootRestorePlan {
dataDir: string;
/** `data/.trek-reclaim-reset` (data-paths.ts), left before the database is put in place. */
reclaimResetFile: string;
+ /**
+ * Set when TREK runs on Postgres (TREK_DB_URL): the archive's dump is loaded
+ * into the database, which must hold no table yet, instead of `dbFile`.
+ */
+ postgres?: { url: string; binDir: string | undefined };
}
export type BootRestoreOutcome =
@@ -56,11 +63,38 @@ export type BootRestoreOutcome =
export class BootRestoreError extends Error {}
+/** A database is there already: on a file, the file; on Postgres, any table in its schema. */
+async function existingDatabase(plan: BootRestorePlan): Promise {
+ if (!plan.postgres) return fs.existsSync(plan.dbFile) ? plan.dbFile : null;
+ const tables = await pgTableNames(plan.postgres.url, plan.postgres.binDir);
+ return tables.length > 0 ? `the Postgres database (${tables.length} table(s))` : null;
+}
+
+/** Why the unpacked archive's database cannot be restored here, or null. */
+async function checkArchiveDatabase(plan: BootRestorePlan, staging: string): Promise {
+ if (plan.postgres) return checkPostgresDump(staging, plan.postgres.binDir);
+ return checkBackupDatabase(staging)?.error ?? null;
+}
+
+/** Puts the archive's database in place: renamed in beside the file, or loaded into Postgres in one transaction. */
+async function putDatabaseInPlace(plan: BootRestorePlan, staging: string): Promise {
+ if (plan.postgres) {
+ await pgRestore(plan.postgres.url, path.join(staging, PG_ARCHIVE_ENTRY), plan.postgres.binDir);
+ return;
+ }
+ fs.mkdirSync(path.dirname(plan.dbFile), { recursive: true });
+ // Copied in beside the target and renamed, so a crash leaves either nothing or a whole file.
+ const tmp = `${plan.dbFile}.restore-tmp`;
+ fs.copyFileSync(path.join(staging, 'travel.db'), tmp);
+ fs.renameSync(tmp, plan.dbFile);
+}
+
export async function restoreOnFirstBoot(plan: BootRestorePlan): Promise {
if (!plan.archive) return { restored: false, reason: 'unset' };
- if (fs.existsSync(plan.dbFile)) {
+ const existing = await existingDatabase(plan);
+ if (existing) {
console.warn(
- `[restore] RESTORE_FROM_BACKUP is set, but ${plan.dbFile} already exists. The backup is only restored on a first start; remove the variable to silence this.`,
+ `[restore] RESTORE_FROM_BACKUP is set, but ${existing} already exists. The backup is only restored on a first start; remove the variable to silence this.`,
);
return { restored: false, reason: 'database-exists' };
}
@@ -73,20 +107,16 @@ export async function restoreOnFirstBoot(plan: BootRestorePlan): Promise ({ sqlite, postgres })[engine.id],
+ inject: [DATABASE_ENGINE, SqliteDatabaseBackup, PostgresDatabaseBackup],
+ },
+ ],
exports: [DATABASE_BACKUP],
})
export class DatabaseBackupModule {}
diff --git a/server/src/nest/backup/database-swap.ts b/server/src/nest/backup/database-swap.ts
new file mode 100644
index 000000000..ee06e5613
--- /dev/null
+++ b/server/src/nest/backup/database-swap.ts
@@ -0,0 +1,58 @@
+import { logInfo, logWarn } from '../audit/audit-log.logger';
+import { describeError } from '../common/describe-error';
+import { DatabaseConnectionLostError } from '../database/database-backup.interface';
+
+/** Whether a step ran to the end, and what it threw when it did not. */
+interface Outcome {
+ failed: boolean;
+ error: unknown;
+}
+
+/** Runs `step` and records that it threw even when what it threw is undefined. */
+async function settle(step: () => void | Promise): Promise {
+ try {
+ await step();
+ return { failed: false, error: null };
+ } catch (error) {
+ return { failed: true, error };
+ }
+}
+
+/**
+ * The restore step both backup ports share. `swap` puts the new database in
+ * place; `reopen` always runs after it, so the process is never left without a
+ * connection. A failed swap rethrows its error, or a DatabaseConnectionLostError
+ * carrying both when the reopen failed too. A swap that landed resolves with the
+ * reopen's error, which the caller reports as "restart required".
+ */
+export async function replaceThenReopen(
+ swap: () => void | Promise,
+ reopen: () => Promise,
+): Promise<{ reopenError: unknown }> {
+ const swapped = await settle(swap);
+ const reopened = await settle(reopen);
+ if (swapped.failed) {
+ if (reopened.failed) throw new DatabaseConnectionLostError(swapped.error, reopened.error);
+ throw swapped.error;
+ }
+ return { reopenError: reopened.failed ? reopened.error : null };
+}
+
+/**
+ * Keeps a copy of the database a restore is about to replace, at `target`. Best
+ * effort: a database broken enough to need the restore may not copy, and that
+ * must not block the recovery. Resolves with where the copy went, or null.
+ */
+export async function keepRestoreCopy(
+ target: string,
+ snapshot: (target: string) => Promise,
+): Promise {
+ try {
+ await snapshot(target);
+ logInfo(`Restore: the replaced database was kept as ${target}`);
+ return target;
+ } catch (err) {
+ logWarn(`Restore: could not keep a copy of the current database (${describeError(err)})`);
+ return null;
+ }
+}
diff --git a/server/src/nest/backup/postgres-database-backup.ts b/server/src/nest/backup/postgres-database-backup.ts
new file mode 100644
index 000000000..3a2a05709
--- /dev/null
+++ b/server/src/nest/backup/postgres-database-backup.ts
@@ -0,0 +1,127 @@
+import type { DbSettings } from '../../app-config';
+import { postgresTarget, type DatabaseEngine } from '../../db/engine';
+import { pgDump, pgDumpMigrations, pgRestore } from '../../db/engine/postgres/pg-tools';
+import { unknownMigrations } from '../../db/known-migrations';
+import { DataPathsService } from '../app-config/data-paths.service';
+import { logWarn } from '../audit/audit-log.logger';
+import { describeError } from '../common/describe-error';
+import { DomainError } from '../common/domain-error';
+import type { DatabaseBackupStrategy } from '../database/database-backup.interface';
+import { DATABASE_ENGINE, DATABASE_SETTINGS } from '../database/database-engine.module';
+import { DatabaseLifecycle } from '../database/database-lifecycle.service';
+import { keepRestoreCopy, replaceThenReopen } from './database-swap';
+import { Inject, Injectable } from '@nestjs/common';
+
+import fs from 'fs';
+import path from 'path';
+
+/** The name a Postgres database takes inside a backup archive: a pg_dump custom-format file. */
+export const PG_ARCHIVE_ENTRY = 'travel.pgdump';
+
+/**
+ * Why the database in an unpacked archive cannot be restored into a Postgres
+ * database, or null when it can: it must be a dump, carry TREK's migration
+ * history, and none this build does not know. Both restore paths ask it, the
+ * admin panel's and RESTORE_FROM_BACKUP's.
+ */
+export async function checkPostgresDump(extractDir: string, binDir: string | undefined): Promise {
+ const dump = path.join(extractDir, PG_ARCHIVE_ENTRY);
+ if (!fs.existsSync(dump)) {
+ return fs.existsSync(path.join(extractDir, 'travel.db'))
+ ? 'This backup holds a SQLite database, which cannot be restored into a Postgres database.'
+ : `Invalid backup: ${PG_ARCHIVE_ENTRY} not found`;
+ }
+ let executed: string[];
+ try {
+ executed = await pgDumpMigrations(dump, binDir);
+ } catch (err) {
+ logWarn(`Restore: the archive's database could not be read (${describeError(err)})`);
+ return 'Uploaded file is not a valid Postgres dump';
+ }
+ if (executed.length === 0) {
+ return 'The uploaded dump holds no TREK migration history. This does not appear to be a TREK backup.';
+ }
+ if (unknownMigrations(executed).length > 0) {
+ return 'This backup was made by a newer TREK version. Update TREK before restoring it.';
+ }
+ return null;
+}
+
+/**
+ * The backup port on a Postgres database: `pg_dump` and `pg_restore`
+ * (`db/engine/postgres/pg-tools.ts`), run by their absolute paths with the
+ * password in their environment.
+ *
+ * A snapshot is a custom-format dump, consistent on its own (pg_dump reads one
+ * snapshot of the database). A restore checks the dump first (it must be one,
+ * carry TREK's migration history, and none this build does not know), keeps a
+ * dump of the database it replaces, then clears the schema and loads the dump
+ * in one transaction (`pgRestore`), so a restore that fails leaves the database
+ * as it was and one that succeeds holds nothing a later migration added, and
+ * reopens through `DatabaseLifecycle`, which migrates the restored database
+ * forward. A backup of a SQLite install cannot be restored here.
+ */
+@Injectable()
+export class PostgresDatabaseBackup implements DatabaseBackupStrategy {
+ readonly archiveEntry = PG_ARCHIVE_ENTRY;
+ /** The URL and the tools' directory; null on SQLite, where this provider is built but never called. */
+ private readonly target: ReturnType;
+
+ constructor(
+ private readonly lifecycle: DatabaseLifecycle,
+ private readonly paths: DataPathsService,
+ @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine,
+ @Inject(DATABASE_SETTINGS) settings: DbSettings,
+ ) {
+ this.target = postgresTarget(settings);
+ }
+
+ private get connection(): { url: string; binDir: string | undefined } {
+ if (!this.target) throw new Error('The Postgres backup was used without TREK_DB_URL.');
+ return this.target;
+ }
+
+ location(): string {
+ return this.engine.describe();
+ }
+
+ /** A Postgres database is always there to copy; a missing pg_dump fails the snapshot, loudly. */
+ canSnapshot(): boolean {
+ return true;
+ }
+
+ async checkpoint(): Promise {
+ // Postgres keeps nothing beside its store that a copy would need flushed.
+ }
+
+ /** pg_dump writes beside `target` and renames into place: a failed dump leaves nothing there. */
+ async snapshot(target: string): Promise {
+ const { url, binDir } = this.connection;
+ await pgDump(url, target, binDir);
+ }
+
+ /** Throws a DomainError (400) with the refusal when the archive cannot be restored here, before anything is replaced. */
+ async verify(extractDir: string): Promise {
+ const refusal = await checkPostgresDump(extractDir, this.connection.binDir);
+ if (refusal) throw new DomainError(400, refusal);
+ }
+
+ /** A dump of the database a restore is about to replace, in the data directory. */
+ keepCopyBeforeRestore(): Promise {
+ const target = path.join(this.paths.dataDir, `pre-restore-${Date.now()}.pgdump`);
+ return keepRestoreCopy(target, (copy) => this.snapshot(copy));
+ }
+
+ replace(source: string): Promise<{ reopenError: unknown }> {
+ return replaceThenReopen(
+ async () => {
+ const { url, binDir } = this.connection;
+ // A rejection with no reason still has to fail the swap, so it is never swallowed.
+ await pgRestore(url, source, binDir).catch((err: unknown) => {
+ throw err ?? new Error('pg_restore failed');
+ });
+ },
+ () => this.lifecycle.reopen(),
+ );
+ }
+}
diff --git a/server/src/nest/backup/sqlite-database-backup.ts b/server/src/nest/backup/sqlite-database-backup.ts
index ec10b1107..970491cf2 100644
--- a/server/src/nest/backup/sqlite-database-backup.ts
+++ b/server/src/nest/backup/sqlite-database-backup.ts
@@ -1,29 +1,22 @@
-import { MaintenanceRepository } from '../../db/repositories/MaintenanceRepository';
-import { logInfo, logWarn } from '../audit/audit-log.logger';
-import {
- DatabaseConnectionLostError,
- type DatabaseBackupRefusal,
- type DatabaseBackupStrategy,
-} from '../database/database-backup.interface';
+import { SqliteMaintenance } from '../../db/engine/sqlite/sqlite-maintenance';
+import { DomainError } from '../common/domain-error';
+import type { DatabaseBackupStrategy } from '../database/database-backup.interface';
import { DatabaseLifecycle } from '../database/database-lifecycle.service';
import { checkBackupDatabase } from './backup-archive';
+import { keepRestoreCopy, replaceThenReopen } from './database-swap';
import { Injectable } from '@nestjs/common';
import fs from 'fs';
import path from 'path';
-const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err));
-
/**
- * The backup port for the one engine TREK runs on: a SQLite file.
+ * The backup port on a SQLite file, the engine TREK runs on by default.
*
- * Every step is what `backup.impl.ts` and `demo/demo-reset.ts` did inline
- * before the port existed. A snapshot is `VACUUM INTO`, which takes a
- * consistent copy even under concurrent writers. A restore closes the
- * connection, swaps the file by copy and rename (a crash mid-swap leaves the
- * old or the new file, never neither), drops the old `-wal`/`-shm` sidecars,
- * and reopens through `DatabaseLifecycle`, which migrates the restored file
- * forward.
+ * A snapshot is `VACUUM INTO`, which takes a consistent copy even under
+ * concurrent writers. A restore closes the connection, swaps the file by copy
+ * and rename (a crash mid-swap leaves the old or the new file, never neither),
+ * drops the old `-wal`/`-shm` sidecars, and reopens through `DatabaseLifecycle`,
+ * which migrates the restored file forward.
*
* The file is always `DatabaseLifecycle.file`, the path the connection was
* opened on, so `TREK_DB_FILE` moves the backup, the restore and the demo reset
@@ -35,7 +28,7 @@ export class SqliteDatabaseBackup implements DatabaseBackupStrategy {
constructor(
private readonly lifecycle: DatabaseLifecycle,
- private readonly maintenance: MaintenanceRepository,
+ private readonly maintenance: SqliteMaintenance,
) {}
location(): string {
@@ -62,66 +55,42 @@ export class SqliteDatabaseBackup implements DatabaseBackupStrategy {
}
}
- verify(extractDir: string): DatabaseBackupRefusal | null {
- return checkBackupDatabase(extractDir);
+ /** Throws a DomainError carrying the refusal when the archive's database cannot be restored here. */
+ async verify(extractDir: string): Promise {
+ const refusal = checkBackupDatabase(extractDir);
+ if (refusal) throw new DomainError(refusal.status, refusal.error);
}
- /**
- * A copy of the database a restore is about to replace, next to it. The swap
- * deletes the current file, and a restore of the wrong archive used to leave
- * nothing to go back to.
- */
- async keepCopyBeforeRestore(): Promise {
+ /** A copy of the database a restore is about to replace, next to it: the swap deletes the current file. */
+ keepCopyBeforeRestore(): Promise {
const target = path.join(path.dirname(this.lifecycle.file), `pre-restore-${Date.now()}.db`);
- try {
- await this.snapshot(target);
- logInfo(`Restore: the replaced database was kept as ${target}`);
- return target;
- } catch (err) {
- logWarn(`Restore: could not keep a copy of the current database (${describeError(err)})`);
- return null;
- }
+ return keepRestoreCopy(target, (copy) => this.snapshot(copy));
}
async replace(source: string): Promise<{ reopenError: unknown }> {
const dest = this.lifecycle.file;
this.lifecycle.close();
- let swapFailed = false;
- let swapError: unknown = null;
- try {
- // Copy to a temp file on the SAME filesystem, drop the old sidecars (they
- // belong to the database being replaced and would corrupt the new one),
- // then rename into place. The rename is atomic.
- const tmp = dest + '.restore-tmp';
- fs.copyFileSync(source, tmp);
- for (const ext of ['-wal', '-shm']) {
- try {
- fs.unlinkSync(dest + ext);
- } catch {
- /* no sidecar to drop */
- }
- }
- fs.renameSync(tmp, dest);
- } catch (err) {
- swapFailed = true;
- swapError = err;
- }
-
- // Reopening must always run, even when the swap threw, so the process is
- // never left without a connection. After a swap that landed, a reopen
- // failure is reported, not thrown: the files are in place and the caller
- // has to say "restart". After a swap that failed, both errors go up
- // together, because the swap's alone would hide that the connection is gone.
- let reopenError: unknown = null;
- try {
- await this.lifecycle.reopen();
- } catch (err) {
- reopenError = err;
- }
- if (swapFailed) {
- if (reopenError) throw new DatabaseConnectionLostError(swapError, reopenError);
- throw swapError;
- }
- return { reopenError };
+ return replaceThenReopen(
+ () => swapFile(source, dest),
+ () => this.lifecycle.reopen(),
+ );
}
}
+
+/**
+ * Puts `source` at `dest` by copying it to a temp file on the same filesystem
+ * and renaming it into place. The old sidecars go first: they belong to the
+ * database being replaced and would corrupt the new one.
+ */
+function swapFile(source: string, dest: string): void {
+ const tmp = dest + '.restore-tmp';
+ fs.copyFileSync(source, tmp);
+ for (const ext of ['-wal', '-shm']) {
+ try {
+ fs.unlinkSync(dest + ext);
+ } catch {
+ /* no sidecar to drop */
+ }
+ }
+ fs.renameSync(tmp, dest);
+}
diff --git a/server/src/nest/common/describe-error.ts b/server/src/nest/common/describe-error.ts
new file mode 100644
index 000000000..743668591
--- /dev/null
+++ b/server/src/nest/common/describe-error.ts
@@ -0,0 +1,2 @@
+/** The message of an error, or the value itself as text when it is not an Error (a thrown string, say). */
+export const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err));
diff --git a/server/src/nest/database/database-backup.interface.ts b/server/src/nest/database/database-backup.interface.ts
index c30849a8d..7e9a83e5d 100644
--- a/server/src/nest/database/database-backup.interface.ts
+++ b/server/src/nest/database/database-backup.interface.ts
@@ -1,14 +1,15 @@
+import { describeError } from '../common/describe-error';
+
/**
* How a backup, a restore and the demo reset get at the core database, without
* knowing which engine holds it.
*
* The admin backup (`nest/backup/backup.impl.ts`), its restore, and the demo
* baseline save and hourly reset (`demo/demo-reset.ts`) all go through this
- * port. The only implementation today is `SqliteDatabaseBackup`
- * (`nest/backup/`), which does what those paths always did: `VACUUM INTO` for a
- * consistent copy of the file, a file swap under a closed connection for a
- * restore. An engine that is not one local file would plug in here with a
- * logical dump and load instead.
+ * port. Each engine has an adapter in `nest/backup/`, chosen by the engine in
+ * use: SQLite copies the file (`VACUUM INTO`) and swaps it under a closed
+ * connection; Postgres dumps and restores it with pg_dump and pg_restore. The
+ * two adapters answer every method the same way, so the callers do not branch.
*
* Lives in the shared database kernel, not in the backup domain, so the admin
* domain (the demo reset job, the baseline route) can inject it without
@@ -42,10 +43,11 @@ export interface DatabaseBackupStrategy {
snapshot(target: string): Promise;
/**
- * Checks an unpacked archive's database before anything is replaced. Returns
- * null when it can be restored, or the refusal in the restore route's words.
+ * Checks an unpacked archive's database before anything is replaced. Resolves
+ * when it can be restored; otherwise throws a DomainError (status 400) that
+ * carries the refusal in the restore route's words.
*/
- verify(extractDir: string): DatabaseBackupRefusal | null;
+ verify(extractDir: string): Promise;
/**
* Keeps a copy of the database a restore is about to replace. Best effort: a
@@ -67,7 +69,7 @@ export interface DatabaseBackupStrategy {
replace(source: string): Promise<{ reopenError: unknown }>;
}
-/** A refusal in the restore route's own words and status. */
+/** A refusal in the restore route's own words and status, as `verify` reports it. */
export interface DatabaseBackupRefusal {
error: string;
status: number;
@@ -76,8 +78,6 @@ export interface DatabaseBackupRefusal {
/** Injection token for the active `DatabaseBackupStrategy`. */
export const DATABASE_BACKUP = Symbol('DATABASE_BACKUP');
-const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err));
-
/**
* Thrown by `replace` when the swap failed and the reopen that always follows
* failed too. The process is left without a database connection until a
diff --git a/server/src/nest/database/database-engine.module.ts b/server/src/nest/database/database-engine.module.ts
new file mode 100644
index 000000000..82fd7fb5b
--- /dev/null
+++ b/server/src/nest/database/database-engine.module.ts
@@ -0,0 +1,30 @@
+import { readEnv, type DbSettings } from '../../app-config';
+import { resolveEngine, type DatabaseEngine } from '../../db/engine';
+import { Module } from '@nestjs/common';
+
+/** `readEnv().db`, read once per container: what decides which engine runs and its settings. */
+export const DATABASE_SETTINGS = Symbol('DATABASE_SETTINGS');
+
+/**
+ * The one `DatabaseEngine` of this process. Every injector (the lifecycle, the
+ * unit of work, the backup port and its adapters) gets this instance, so the
+ * state an engine keeps (Postgres's reopen hook) is the same wherever it is used.
+ */
+export const DATABASE_ENGINE = Symbol('DATABASE_ENGINE');
+
+/**
+ * Provides the engine and its settings. Not global: each module that needs
+ * the engine imports it, and Nest keeps one instance per container.
+ */
+@Module({
+ providers: [
+ { provide: DATABASE_SETTINGS, useFactory: (): DbSettings => readEnv().db },
+ {
+ provide: DATABASE_ENGINE,
+ useFactory: (db: DbSettings): DatabaseEngine => resolveEngine(db),
+ inject: [DATABASE_SETTINGS],
+ },
+ ],
+ exports: [DATABASE_SETTINGS, DATABASE_ENGINE],
+})
+export class DatabaseEngineModule {}
diff --git a/server/src/nest/database/database-lifecycle.module.ts b/server/src/nest/database/database-lifecycle.module.ts
index e56c755b2..70434f7c5 100644
--- a/server/src/nest/database/database-lifecycle.module.ts
+++ b/server/src/nest/database/database-lifecycle.module.ts
@@ -1,3 +1,4 @@
+import { DatabaseEngineModule } from './database-engine.module';
import { DatabaseLifecycle } from './database-lifecycle.service';
import { Module } from '@nestjs/common';
@@ -6,5 +7,5 @@ import { Module } from '@nestjs/common';
* import it by name, so a partial test harness that never touches the
* connection lifecycle does not have to supply the `MikroORM` it injects.
*/
-@Module({ providers: [DatabaseLifecycle], exports: [DatabaseLifecycle] })
+@Module({ imports: [DatabaseEngineModule], providers: [DatabaseLifecycle], exports: [DatabaseLifecycle] })
export class DatabaseLifecycleModule {}
diff --git a/server/src/nest/database/database-lifecycle.service.ts b/server/src/nest/database/database-lifecycle.service.ts
index 7cc849d38..ddbf663b0 100644
--- a/server/src/nest/database/database-lifecycle.service.ts
+++ b/server/src/nest/database/database-lifecycle.service.ts
@@ -1,8 +1,8 @@
-import { closeDb, getRawConnection, registerReinitializeHook, reinitialize } from '../../db/database';
import { resolveDbPath } from '../../db/db-path';
-import { runSchemaBootstrap } from '../../db/orm';
+import type { DatabaseEngine } from '../../db/engine';
+import { DATABASE_ENGINE } from './database-engine.module';
import { MikroORM } from '@mikro-orm/core';
-import { Injectable } from '@nestjs/common';
+import { Inject, Injectable } from '@nestjs/common';
/**
* The one owner of the core database connection's lifecycle: open, close and
@@ -13,18 +13,21 @@ import { Injectable } from '@nestjs/common';
* replace that module wholesale with `vi.mock`. What moved here is every
* decision about WHEN it opens and closes: `buildApp()` opens through this,
* a restore and the demo reset close and reopen through this (via the backup
- * port), and the shutdown closes through this. Nothing opens the database at
- * import any more; the first connect does, and this provider makes it explicit.
+ * port), and the shutdown closes through this. The ORM's first connect is what
+ * actually opens the file; this provider makes that step explicit.
*
- * Only the SQLite file engine exists today. The engine specific parts (the file
- * path, the swap) are confined to `file` and to the backup port's SQLite
- * implementation, so another engine changes this provider and that one class.
+ * What differs by engine (a file to open or not, how a reopen reaches the ORM)
+ * is the engine's (`db/engine/`), the one `DATABASE_ENGINE` provides: this
+ * provider decides when, the engine decides how.
*/
@Injectable()
export class DatabaseLifecycle {
private readonly reopenListeners: Array<() => Promise> = [];
- constructor(private readonly orm: MikroORM) {}
+ constructor(
+ private readonly orm: MikroORM,
+ @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine,
+ ) {}
/**
* The database file this process runs on, resolved the one way every caller
@@ -36,31 +39,29 @@ export class DatabaseLifecycle {
}
/**
- * Opens the connection, binds the ORM to its later swaps and brings the schema
- * to head (legacy baseline, migrations, the deferred index, seeders, demo
- * seed, in that order: see `db/orm.ts`). Called once by `buildApp()`, before
- * anything reads the database.
+ * Opens the connection, points the engine's reopen at the ORM and brings the
+ * schema to head (on SQLite: legacy baseline, migrations, the deferred index,
+ * seeders, demo seed, in that order, see `db/orm.ts`; on Postgres: the
+ * baseline or the migrations after it, then the same seeding). Called once
+ * by `buildApp()`, before anything reads the database.
*/
async open(): Promise {
- // Normally a no-op: the ORM's first connect inside NestFactory.create()
- // already opened it. Said here so the boot does not depend on that.
- getRawConnection();
- registerReinitializeHook(() => this.rebindOrm());
- await runSchemaBootstrap(this.orm);
+ this.engine.open(this.orm, () => this.afterReopen());
+ await this.engine.schemaBootstrap(this.orm, { snapshot: true });
}
/** Checkpoints and closes the connection. A closed connection stays closed until `reopen()`. */
close(): void {
- closeDb();
+ this.engine.close();
}
/**
- * Reopens the connection after its file was replaced, rebuilds the ORM's
- * cached client around the new handle and migrates the new file forward (a
- * restored backup may predate this release).
+ * Reopens the connection after the database was replaced. The engine points
+ * the ORM at the new database first; then `afterReopen` migrates it forward
+ * and runs the listeners.
*/
async reopen(): Promise {
- await reinitialize();
+ await this.engine.reopen();
}
/**
@@ -78,19 +79,12 @@ export class DatabaseLifecycle {
}
/**
- * Kysely caches whatever handle it was given, so without this the ORM would
- * keep talking to the closed one. Closing and reconnecting sends the driver
- * back through `createKyselyDialect()`, which picks up the new handle.
+ * What follows a reopen once the engine has pointed the ORM at the reopened
+ * database. No pre-migrate snapshot: the file was just unpacked from an
+ * archive that is still there to go back to.
*/
- private async rebindOrm(): Promise {
- const connection = this.orm.em.getConnection();
- // The handle is owned and closed by `db/database.ts`; the bound driver's
- // `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it.
- await connection.close(true);
- await connection.connect();
- // No pre-migrate snapshot: the file was just unpacked from an archive that
- // is still there to go back to.
- await runSchemaBootstrap(this.orm, { snapshot: false });
+ private async afterReopen(): Promise {
+ await this.engine.schemaBootstrap(this.orm, { snapshot: false });
for (const listener of this.reopenListeners) await listener();
}
}
diff --git a/server/src/nest/database/maintenance.module.ts b/server/src/nest/database/maintenance.module.ts
index 57bb8bbf3..d24d2e7aa 100644
--- a/server/src/nest/database/maintenance.module.ts
+++ b/server/src/nest/database/maintenance.module.ts
@@ -1,14 +1,16 @@
+import { SqliteMaintenance } from '../../db/engine/sqlite/sqlite-maintenance';
import { MaintenanceRepository } from '../../db/repositories/MaintenanceRepository';
import { EntityManager } from '@mikro-orm/core';
import { Module } from '@nestjs/common';
/**
* Provides `MaintenanceRepository`, the home of the few whole-database
- * statements no entity can express (the readiness ping, the WAL checkpoint,
- * `VACUUM INTO`, the plugin-table erasure).
+ * statements no entity can express (the readiness ping, the plugin-table
+ * erasure), and `SqliteMaintenance`, the SQLite file's own (the WAL
+ * checkpoint, `VACUUM INTO`).
*
- * A factory over the context-resolving global `EntityManager`, the same one
- * Nest injects into every service, so the repository resolves the request or
+ * Factories over the context-resolving global `EntityManager`, the same one
+ * Nest injects into every service, so each resolves the request or
* transaction fork that is active when a method runs. The class itself stays
* free of Nest, like every other file under `db/`.
*/
@@ -19,7 +21,12 @@ import { Module } from '@nestjs/common';
useFactory: (em: EntityManager) => new MaintenanceRepository(em),
inject: [EntityManager],
},
+ {
+ provide: SqliteMaintenance,
+ useFactory: (em: EntityManager) => new SqliteMaintenance(em),
+ inject: [EntityManager],
+ },
],
- exports: [MaintenanceRepository],
+ exports: [MaintenanceRepository, SqliteMaintenance],
})
export class MaintenanceModule {}
diff --git a/server/src/nest/database/orm.module.ts b/server/src/nest/database/orm.module.ts
index f423d967b..6afb8de72 100644
--- a/server/src/nest/database/orm.module.ts
+++ b/server/src/nest/database/orm.module.ts
@@ -1,3 +1,4 @@
+import { DatabaseEngineModule } from './database-engine.module';
import { UnitOfWork } from './unit-of-work';
import { Global, Module } from '@nestjs/common';
@@ -10,10 +11,11 @@ import { Global, Module } from '@nestjs/common';
* (`Test.createTestingModule({ imports: [DatabaseModule, ...] })`)
* with no ORM in the container: a provider there that injects the core
* `EntityManager` fails every one of them at `compile()`, because Nest
- * instantiates providers eagerly. Nothing is imported here — the
- * `EntityManager` token comes from `MikroOrmModule.forRoot`, whose
- * `MikroOrmCoreModule` is itself `@Global()`.
+ * instantiates providers eagerly. The `EntityManager` token comes from
+ * `MikroOrmModule.forRoot`, whose
+ * `MikroOrmCoreModule` is itself `@Global()`. It imports `DatabaseEngineModule`,
+ * the engine the unit of work serializes its writes through.
*/
@Global()
-@Module({ providers: [UnitOfWork], exports: [UnitOfWork] })
+@Module({ imports: [DatabaseEngineModule], providers: [UnitOfWork], exports: [UnitOfWork] })
export class OrmModule {}
diff --git a/server/src/nest/database/unit-of-work.ts b/server/src/nest/database/unit-of-work.ts
index 8b2e9b600..1e92efece 100644
--- a/server/src/nest/database/unit-of-work.ts
+++ b/server/src/nest/database/unit-of-work.ts
@@ -1,5 +1,7 @@
+import type { DatabaseEngine } from '../../db/engine';
+import { DATABASE_ENGINE } from './database-engine.module';
import { EntityManager } from '@mikro-orm/core';
-import { Injectable } from '@nestjs/common';
+import { Inject, Injectable } from '@nestjs/common';
/**
* The one way a service opens a transaction.
@@ -17,12 +19,23 @@ import { Injectable } from '@nestjs/common';
*
* Never awaits network I/O inside: the SQLite connection is one Kysely client
* with a mutex, and an open transaction holds it for every other request.
+ *
+ * The services read `MAX(...) + 1` inside these transactions on the strength
+ * of SQLite's single writer. On Postgres a pool of connections would let two
+ * of them read the same MAX, so every transaction first takes the engine's
+ * write lock (`DatabaseEngine.serializeWrites`), which keeps that guarantee.
*/
@Injectable()
export class UnitOfWork {
- constructor(private readonly em: EntityManager) {}
+ constructor(
+ private readonly em: EntityManager,
+ @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine,
+ ) {}
transactional(fn: () => Promise): Promise {
- return this.em.transactional(() => fn());
+ return this.em.transactional(async (em) => {
+ await this.engine.serializeWrites(em);
+ return fn();
+ });
}
}
diff --git a/server/src/nest/places/places.helpers.ts b/server/src/nest/places/places.helpers.ts
index 9533a78d0..5b011a61d 100644
--- a/server/src/nest/places/places.helpers.ts
+++ b/server/src/nest/places/places.helpers.ts
@@ -1,9 +1,26 @@
import type { PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository';
import { haversineMetres } from '../common/geo';
+import { toRowId } from '../common/row-id';
import type { KmlImportSummary } from '../place-import/place-import.types';
import type { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
import { externalIdsOf, normalizePlaceName, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
+/**
+ * The category and tag ids a place list filters by, from their query-string
+ * values. An absent or empty filter is left out; an id that is not canonical
+ * (`toRowId`) matches no place, so the list is `null` and the answer is empty
+ * without a query. The repository binds numbers only.
+ */
+export function placeListIdFilters(filters: {
+ category?: string;
+ tag?: string;
+}): { category?: number; tag?: number } | null {
+ const category = filters.category ? toRowId(filters.category) : undefined;
+ const tag = filters.tag ? toRowId(filters.tag) : undefined;
+ if (category === null || tag === null) return null;
+ return { category, tag };
+}
+
/**
* Pure helpers and module-scope constants of the places domain, moved verbatim
* out of the legacy services/placeService.ts when it went DI-native. Same
diff --git a/server/src/nest/places/places.service.ts b/server/src/nest/places/places.service.ts
index c70460ad4..326a5144e 100644
--- a/server/src/nest/places/places.service.ts
+++ b/server/src/nest/places/places.service.ts
@@ -34,7 +34,7 @@ import { TripMembers } from '../../db/entities/TripMembers.entity';
import { DayAssignments } from '../../db/entities/DayAssignments.entity';
import { Categories } from '../../db/entities/Categories.entity';
import { Trips } from '../../db/entities/Trips.entity';
-import type { PlacesRepository, PlaceListRow, PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository';
+import type { PlacesRepository, PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository';
import type { TagsRepository } from '../../db/repositories/Tags.repository';
import type { PlaceRatingsRepository } from '../../db/repositories/PlaceRatings.repository';
import type { TripMembersRepository } from '../../db/repositories/TripMembers.repository';
@@ -73,6 +73,7 @@ import {
type ListImportOptions,
type ListImportResult,
type PlaceImportResult,
+ placeListIdFilters,
} from './places.helpers';
type Trip = TripAccess;
@@ -247,16 +248,13 @@ export class PlacesService {
filters: { search?: string; category?: string; tag?: string; assignment?: 'all' | 'unassigned' | 'assigned' },
) {
// ESCAPE so a `%` or `_` the user typed matches literally instead of
- // acting as a LIKE wildcard (a bare '%' used to return the whole trip) —
- // the coercion stays here (the service), the repository binds the
+ // acting as a LIKE wildcard (a bare `%` would match every place in the
+ // trip). The coercion stays here (the service); the repository binds the
// already-built pattern verbatim (PL3).
const searchPattern = filters.search ? `%${escapeLikePattern(filters.search)}%` : undefined;
- const places: PlaceListRow[] = await this.placesRepo.listForTrip(tripId, {
- searchPattern,
- category: filters.category,
- tag: filters.tag,
- assignment: filters.assignment,
- });
+ const ids = placeListIdFilters(filters);
+ if (!ids) return [];
+ const places = await this.placesRepo.listForTrip(tripId, { searchPattern, ...ids, assignment: filters.assignment });
const placeIds = places.map(p => p.id);
const tagsByPlaceId = await this.queryHelpers.loadTagsByPlaceIds(placeIds);
diff --git a/server/tests/build-schema-snapshot.ts b/server/tests/build-schema-snapshot.ts
index fb4fa91b8..12bdc8f20 100644
--- a/server/tests/build-schema-snapshot.ts
+++ b/server/tests/build-schema-snapshot.ts
@@ -18,14 +18,15 @@ async function main(): Promise {
fs.rmSync(schemaSnapshotPath(), { force: true });
const { getRawConnection } = await import('../src/db/database');
- const { createStandaloneOrm, runSchemaBootstrap } = await import('../src/db/orm');
+ const { runSchemaBootstrap } = await import('../src/db/orm');
+ const { createStandaloneOrm } = await import('../src/db/standalone-orm');
// The shared driver binds to the connection database.ts opens on the ORM's
// first connect, so this migrates that in-memory database and it can be
// serialised straight after.
const orm = await createStandaloneOrm();
try {
- await runSchemaBootstrap(orm);
+ await runSchemaBootstrap(orm, { snapshot: true });
writeSchemaSnapshot(getRawConnection().serialize());
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
} finally {
diff --git a/server/tests/e2e/assistant.e2e.test.ts b/server/tests/e2e/assistant.e2e.test.ts
index 2b7cdff3c..955f24fb6 100644
--- a/server/tests/e2e/assistant.e2e.test.ts
+++ b/server/tests/e2e/assistant.e2e.test.ts
@@ -27,6 +27,7 @@ import { DayAssignments } from '../../src/db/entities/DayAssignments.entity';
import { Places } from '../../src/db/entities/Places.entity';
import { Reservations } from '../../src/db/entities/Reservations.entity';
import { Trips } from '../../src/db/entities/Trips.entity';
+import { AssistantToolCallsRepository } from '../../src/db/repositories/AssistantToolCalls.repository';
import { dbNow } from '../../src/db/types';
import { TURNS_PER_MINUTE } from '../../src/nest/assistant/assistant-tools.constants';
import { AssistantToolsService } from '../../src/nest/assistant/assistant-tools.service';
@@ -661,6 +662,32 @@ describe('Trip assistant e2e', () => {
expect(again.status).toBe(409);
});
+ it('ASTE2E-026: the change is recorded as failed before the stream ends, however slow the write', async () => {
+ const t = await travelerWithTrip();
+ const { toolCallId, threadId } = await proposePlace(t, 'Fushimi Inari');
+ const open = vi
+ .spyOn(nestApp.get(AssistantToolsService), 'open')
+ .mockRejectedValueOnce(new Error('tool session down'));
+ const record = AssistantToolCallsRepository.prototype.recordOutcome;
+ const slow = vi.spyOn(AssistantToolCallsRepository.prototype, 'recordOutcome').mockImplementation(async function (
+ this: AssistantToolCallsRepository,
+ ...args
+ ) {
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ return record.apply(this, args);
+ });
+ try {
+ await post(`${t.base}/threads/${threadId}/decisions`, t.userId, {
+ ...CTX,
+ decisions: [{ toolCallId, approved: true }],
+ });
+ expect(await findRow(orm, AssistantToolCalls, { id: toolCallId })).toMatchObject({ state: 'failed' });
+ } finally {
+ slow.mockRestore();
+ open.mockRestore();
+ }
+ });
+
it('ASTE2E-015: an attached file is refused as JSON before the stream when nothing reads it', async () => {
const t = await travelerWithTrip();
const bookingImport = nestApp.get(BookingImportService);
diff --git a/server/tests/helpers/fake-pg-tools.ts b/server/tests/helpers/fake-pg-tools.ts
new file mode 100644
index 000000000..bb14098f5
--- /dev/null
+++ b/server/tests/helpers/fake-pg-tools.ts
@@ -0,0 +1,124 @@
+/**
+ * Stand-in PostgreSQL client tools (pg_dump, pg_restore and psql) for the unit
+ * tests of the Postgres backup and restore. Each is an executable shell script
+ * in a bin directory, which the test hands TREK as its client-tool directory.
+ *
+ * A stand-in writes its argv and its PG* environment to `/.log`
+ * before anything else it does, so readToolCall() says what the tool was given.
+ * The log holds the last call. What a call does is a list of shell snippets,
+ * run in order: the builders below name what a test sets up (answersSchema,
+ * writesFile, failsWith, ...), so a test reads as the scenario it describes.
+ * Output files are found by their `--file=` argument, not by position.
+ */
+import { chmodSync, readFileSync, writeFileSync } from 'node:fs';
+import path from 'node:path';
+
+export type PgToolName = 'pg_dump' | 'pg_restore' | 'psql';
+
+export interface FakeToolOptions {
+ /** What `--version` answers after "PostgreSQL". Defaults to 17.11. */
+ version?: string;
+ /** Shell fragments run after the call is logged, in order. */
+ snippets?: readonly string[];
+}
+
+/** What a stand-in was called with: its arguments, and its PG* environment. */
+export interface ToolCall {
+ args: string[];
+ env: Record;
+}
+
+/** A value as one single-quoted shell word. */
+function shellWord(value: string): string {
+ return `'${value.replaceAll("'", "'\\''")}'`;
+}
+
+/** Answers a call whose arguments contain `pattern` with `text`, and ends the call. */
+export function printsWhen(pattern: string, text: string): string {
+ return `case "$*" in *${pattern}*) printf '%s' ${shellWord(text)}; exit 0;; esac`;
+}
+
+/** psql's current-schema answer: the schema's name, and its quote_ident spelling, as pgCurrentSchema reads them. */
+export function answersSchema(name = 'public', quoted = name): string {
+ return printsWhen('quote_ident', `${name}\t${quoted}\n`);
+}
+
+/** psql's table listing, one name per line, as pgTableNames reads it. */
+export function answersTables(names: readonly string[]): string {
+ return printsWhen('table_name', names.map((name) => `${name}\n`).join(''));
+}
+
+/** Writes `content` to the output file the call names with `--file=`. */
+export function writesFile(content: string): string {
+ return `printf '%s' ${shellWord(content)} > "$(fake_file_target "$@")"`;
+}
+
+/** Writes the start of a restore script that restricts psql (CVE-2025-8714) and creates TREK's migration table. */
+export function writesRestrictedScript(table = 'public.mikro_orm_migrations'): string {
+ return writesFile(`\\restrict k1\nCREATE TABLE ${table} (\n);\n`);
+}
+
+/** Answers a `--data-only` call with the COPY block of `mikro_orm_migrations`, one row per name. */
+export function listsMigrations(names: readonly string[]): string {
+ const rows = names.map((name, i) => `${i + 1}\t${name}\tx\n`).join('');
+ const copy = `COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\n${rows}\\.\n`;
+ return `case "$*" in *--data-only*) printf '%s' ${shellWord(copy)}; exit 0;; esac`;
+}
+
+/** Fails the way a tool that cannot connect does: the message on stderr, then a non-zero exit. */
+export function failsWith(message: string, exitCode = 1): string {
+ return `printf '%s\\n' ${shellWord(message)} >&2; exit ${exitCode}`;
+}
+
+/** Creates `file`: a marker that the tool ran. */
+export function touches(file: string): string {
+ return `touch ${shellWord(file)}`;
+}
+
+/** Copies the `--file=` output whose path ends with `suffix` to `destination`, so a test can read a work file the tool has since removed. */
+export function copiesFileEndingWith(suffix: string, destination: string): string {
+ return `for a in "$@"; do case "$a" in --file=*${suffix}) cp "\${a#--file=}" ${shellWord(destination)};; esac; done`;
+}
+
+/** The function every stand-in uses to find its `--file=` argument, wherever it sits. */
+const FILE_TARGET_FUNCTION =
+ 'fake_file_target() { for a in "$@"; do case "$a" in --file=*) printf \'%s\' "${a#--file=}";; esac; done; }';
+
+/**
+ * Installs the stand-in `name` in `bin` and returns the path of its call log.
+ * The script is executable and runs by its absolute path, as TREK runs it.
+ */
+export function installFakeTool(bin: string, name: PgToolName, options: FakeToolOptions = {}): string {
+ const { version = '17.11', snippets = [] } = options;
+ const log = path.join(bin, `${name}.log`);
+ const script = [
+ '#!/bin/sh',
+ FILE_TARGET_FUNCTION,
+ `[ "$1" = --version ] && { echo '${name} (PostgreSQL) ${version} (Debian)'; exit 0; }`,
+ `printf '%s\\n' "$@" > ${shellWord(log)}`,
+ `env | grep '^PG' | sort >> ${shellWord(log)}`,
+ ...snippets,
+ '',
+ ].join('\n');
+ writeFileSync(path.join(bin, name), script);
+ chmodSync(path.join(bin, name), 0o755);
+ return log;
+}
+
+/** What the stand-in logged last: its arguments, and the PG* variables it was run with. */
+export function readToolCall(log: string): ToolCall {
+ const lines = readFileSync(log, 'utf8')
+ .split('\n')
+ .filter((line) => line !== '');
+ const env: Record = {};
+ for (const line of lines.filter((line) => line.startsWith('PG'))) {
+ const eq = line.indexOf('=');
+ env[line.slice(0, eq)] = line.slice(eq + 1);
+ }
+ return { args: lines.filter((line) => !line.startsWith('PG')), env };
+}
+
+/** Every argument that starts with `flag`, with the flag removed (`--file=` gives the file paths). */
+export function flagValues(args: readonly string[], flag: string): string[] {
+ return args.filter((arg) => arg.startsWith(flag)).map((arg) => arg.slice(flag.length));
+}
diff --git a/server/tests/helpers/legacy-upgrade-suite.ts b/server/tests/helpers/legacy-upgrade-suite.ts
index f807ef0d4..367c9433d 100644
--- a/server/tests/helpers/legacy-upgrade-suite.ts
+++ b/server/tests/helpers/legacy-upgrade-suite.ts
@@ -152,7 +152,7 @@ export function describeLegacyUpgrade(
it(`${c.id}-005: a second bootstrap (a restart, or a restore of this file) baselines nothing and applies nothing`, async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
try {
- await runSchemaBootstrap(app.get(MikroORM));
+ await runSchemaBootstrap(app.get(MikroORM), { snapshot: true });
const lines = logSpy.mock.calls.map((args) => args.map(String).join(' '));
expect(
lines.filter((line) => line.startsWith('[DB] Legacy install') || line.startsWith('[DB] Applying')),
diff --git a/server/tests/helpers/test-addons.ts b/server/tests/helpers/test-addons.ts
index ab100e84a..2d3b0d810 100644
--- a/server/tests/helpers/test-addons.ts
+++ b/server/tests/helpers/test-addons.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { Addons } from '../../src/db/entities/Addons.entity';
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
import { PhotoProviderFields } from '../../src/db/entities/PhotoProviderFields.entity';
@@ -42,6 +43,6 @@ export async function createTestAddonsService(db: Database.Database): Promise
// which is exactly what no-misused-promises/checksConditionals rejects.
const existing = perHandle.get(db);
if (existing !== undefined) return existing;
- const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em));
+ const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em, new SqliteEngine()));
perHandle.set(db, pending);
return pending;
}
diff --git a/server/tests/integration/mcp.test.ts b/server/tests/integration/mcp.test.ts
index 13ed28220..c4cbc1180 100644
--- a/server/tests/integration/mcp.test.ts
+++ b/server/tests/integration/mcp.test.ts
@@ -8,6 +8,7 @@
import { getMcpSafeUrl } from '../../src/app-config';
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
+import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
import { McpTokens } from '../../src/db/entities/McpTokens.entity';
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
@@ -85,7 +86,7 @@ beforeAll(async () => {
t.repo(OauthConsents),
await createTestAddonsService(testDb),
new AuditService(t.repo(AuditLog), t.repo(Users)),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
nestApp.get(CimdClientResolver),
);
});
diff --git a/server/tests/integration/oauth-authorize.test.ts b/server/tests/integration/oauth-authorize.test.ts
index 6fc2b2a00..364181b1d 100644
--- a/server/tests/integration/oauth-authorize.test.ts
+++ b/server/tests/integration/oauth-authorize.test.ts
@@ -10,6 +10,7 @@
*/
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
+import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { Addons } from '../../src/db/entities/Addons.entity';
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
@@ -96,7 +97,7 @@ beforeAll(async () => {
t.repo(OauthConsents),
await createTestAddonsService(testDb),
new AuditService(t.repo(AuditLog), t.repo(Users)),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
nestApp.get(CimdClientResolver),
);
});
diff --git a/server/tests/integration/oauth.test.ts b/server/tests/integration/oauth.test.ts
index 50c8dc356..5ea749278 100644
--- a/server/tests/integration/oauth.test.ts
+++ b/server/tests/integration/oauth.test.ts
@@ -5,6 +5,7 @@
*/
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
+import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { Addons } from '../../src/db/entities/Addons.entity';
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
import { OauthClients } from '../../src/db/entities/OauthClients.entity';
@@ -97,7 +98,7 @@ beforeAll(async () => {
t.repo(OauthConsents),
await createTestAddonsService(testDb),
new AuditService(t.repo(AuditLog), t.repo(Users)),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
nestApp.get(CimdClientResolver),
);
});
diff --git a/server/tests/integration/places.test.ts b/server/tests/integration/places.test.ts
index 4ed094955..6fde8aece 100644
--- a/server/tests/integration/places.test.ts
+++ b/server/tests/integration/places.test.ts
@@ -468,6 +468,23 @@ describe('Search places', () => {
expect(res.body.places).toHaveLength(1);
expect(res.body.places[0].name).toBe('Scenic Place');
});
+
+ it('PLACE-017c — GET ?category= or ?tag= with an id that is not a number matches nothing', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ createPlace(testDb, trip.id, { name: 'Somewhere' });
+
+ const queries = ['category=abc', 'tag=abc', 'category=1.5', 'tag=-1', 'category=0', 'tag=0'];
+ const responses = await Promise.all(
+ queries.map((query) =>
+ request(app).get(`/api/trips/${trip.id}/places?${query}`).set('Cookie', authCookie(user.id)),
+ ),
+ );
+ for (const [i, res] of responses.entries()) {
+ expect(res.status, queries[i]).toBe(200);
+ expect(res.body.places, queries[i]).toEqual([]);
+ }
+ });
});
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/server/tests/integration/plugins/boot-registry-order.test.ts b/server/tests/integration/plugins/boot-registry-order.test.ts
index e35dc8207..cead7a413 100644
--- a/server/tests/integration/plugins/boot-registry-order.test.ts
+++ b/server/tests/integration/plugins/boot-registry-order.test.ts
@@ -17,6 +17,7 @@
* asserts the plugin still comes up clean.
*/
import { db as testDb } from '../../../src/db/database';
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Addons } from '../../../src/db/entities/Addons.entity';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
@@ -173,7 +174,7 @@ describe('plugin boot vs registry scan ordering', () => {
(t as TestOrm).repo(NotificationChannelPreferences),
// Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead
// of `registry?`/`hostFactory?`, matching the real constructor.
- new UnitOfWork((t as TestOrm).em),
+ new UnitOfWork((t as TestOrm).em, new SqliteEngine()),
undefined,
hostFactory,
t?.orm,
@@ -264,7 +265,7 @@ describe('plugin boot vs registry scan ordering', () => {
t2.repo(PhotoProviderFields),
t2.repo(AppSettings),
t2.repo(Users),
- new UnitOfWork(t2.em),
+ new UnitOfWork(t2.em, new SqliteEngine()),
);
const auditLogRepo2 = t2.repo(AuditLog);
const usersRepo2 = t2.repo(Users);
@@ -293,7 +294,7 @@ describe('plugin boot vs registry scan ordering', () => {
t2.repo(PluginCapabilityAudit),
t2.repo(Settings),
t2.repo(NotificationChannelPreferences),
- new UnitOfWork(t2.em),
+ new UnitOfWork(t2.em, new SqliteEngine()),
undefined,
hostFactory2,
t2.orm,
diff --git a/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts b/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts
index 94c5c5e5e..c1b562773 100644
--- a/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts
+++ b/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts
@@ -13,6 +13,7 @@
* so every one of the 14 repositories this task injects reads/writes real columns,
* not a hand-slimmed fixture table.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
@@ -76,7 +77,7 @@ async function buildRuntime(registrar?: CronRegistrarService): Promise extract -> validate ->
* move -> register pipeline (with the network download mocked).
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
@@ -148,7 +149,7 @@ beforeEach(() => {
t.repo(PluginActions),
t.repo(PluginSettingsFields),
t.repo(PluginErrorLog),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
);
});
afterEach(() => {
diff --git a/server/tests/postgres/build-template.ts b/server/tests/postgres/build-template.ts
new file mode 100644
index 000000000..e846f96d5
--- /dev/null
+++ b/server/tests/postgres/build-template.ts
@@ -0,0 +1,36 @@
+import { buildOrmConfig, resolveEngine } from '../../src/db/engine';
+import { MikroORM } from '@mikro-orm/core';
+
+/**
+ * Brings the template database TREK_DB_URL names to the current schema, the
+ * way a first boot on an empty Postgres database does: the engine's own
+ * bootstrap (baseline, the migrations after its cut, seeders), with no
+ * pre-migrate copy, since the database is new.
+ */
+async function main(): Promise {
+ const engine = resolveEngine();
+ if (engine.id !== 'postgres') throw new Error('build-template.ts runs on the Postgres engine only (TREK_DB_URL).');
+ const config = buildOrmConfig(engine);
+ const orm = await MikroORM.init({
+ ...config,
+ // As src/db/standalone-orm.ts: the bootstrap runs SQL and seeders, which
+ // need no entity metadata.
+ entities: [],
+ entitiesTs: [],
+ discovery: { warnWhenNoEntities: false },
+ migrations: { ...config.migrations, silent: true },
+ });
+ try {
+ await engine.schemaBootstrap(orm, { snapshot: false });
+ process.stdout.write(`[test-db] Postgres template ready: ${engine.describe()}\n`);
+ } finally {
+ await orm.close(true);
+ }
+}
+
+main().catch((err: unknown) => {
+ process.stderr.write(
+ `[test-db] failed to build the Postgres template: ${err instanceof Error ? err.stack : String(err)}\n`,
+ );
+ process.exit(1);
+});
diff --git a/server/tests/postgres/databases.ts b/server/tests/postgres/databases.ts
new file mode 100644
index 000000000..196fcc6df
--- /dev/null
+++ b/server/tests/postgres/databases.ts
@@ -0,0 +1,37 @@
+import { MikroORM } from '@mikro-orm/postgresql';
+
+/**
+ * The databases the Postgres lane creates on the server TREK_TEST_DB_URL names:
+ * one migrated template per run, and one copy of it per test file.
+ */
+export const TEMPLATE_DATABASE = 'trek_test_template';
+export const WORKER_DATABASE_PREFIX = 'trek_test_w';
+
+/** `adminUrl` pointed at `database`, everything else (credentials, host, sslmode) kept. */
+export function databaseUrl(adminUrl: string, database: string): string {
+ const url = new URL(adminUrl);
+ url.pathname = `/${database}`;
+ return url.toString();
+}
+
+type Run = (sql: string, params?: unknown[]) => Promise;
+
+/**
+ * Runs `fn` on a connection to the admin database, outside any transaction
+ * (CREATE and DROP DATABASE refuse to run in one), through the same Postgres
+ * driver the app uses.
+ */
+export async function withAdmin(adminUrl: string, fn: (run: Run) => Promise): Promise {
+ const orm = await MikroORM.init({
+ clientUrl: adminUrl,
+ entities: [],
+ discovery: { warnWhenNoEntities: false },
+ pool: { min: 0, max: 1 },
+ });
+ try {
+ const connection = orm.em.getConnection();
+ return await fn((sql, params = []) => connection.execute(sql, params, 'all'));
+ } finally {
+ await orm.close(true);
+ }
+}
diff --git a/server/tests/postgres/global-setup.ts b/server/tests/postgres/global-setup.ts
new file mode 100644
index 000000000..9525fdc9a
--- /dev/null
+++ b/server/tests/postgres/global-setup.ts
@@ -0,0 +1,60 @@
+import { databaseUrl, TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, withAdmin } from './databases';
+
+import { execFileSync } from 'node:child_process';
+import path from 'node:path';
+
+/**
+ * Runs once per `npm run test:pg`, before any worker starts: the Postgres
+ * counterpart of tests/global-setup.ts's schema snapshot. One template
+ * database is created and brought to the current schema the way a first boot
+ * does (baseline, migrations after its cut, seeders); every test file then
+ * gets a copy of it (tests/postgres/setup.ts), which Postgres makes at the
+ * file level, far faster than migrating per file.
+ *
+ * TREK_TEST_DB_URL is required: a lane that skipped without it would pass
+ * having tested nothing.
+ */
+export async function setup(): Promise {
+ const adminUrl = process.env.TREK_TEST_DB_URL;
+ if (!adminUrl) {
+ throw new Error(
+ 'The Postgres lane needs TREK_TEST_DB_URL: a database it may create and drop databases from, ' +
+ 'e.g. postgres://postgres:trek@127.0.0.1:55432/postgres.',
+ );
+ }
+
+ // Left behind by an earlier run that was killed; nothing else uses these names.
+ await dropLaneDatabases(adminUrl);
+ await withAdmin(adminUrl, (run) => run(`CREATE DATABASE "${TEMPLATE_DATABASE}"`));
+
+ // A child process under tsx, as tests/global-setup.ts does, for the same
+ // reason: globalSetup runs outside vitest's transform pipeline.
+ execFileSync(process.execPath, ['--import', 'tsx', path.join(__dirname, 'build-template.ts')], {
+ cwd: path.join(__dirname, '..', '..'),
+ stdio: ['ignore', 'inherit', 'inherit'],
+ env: {
+ ...process.env,
+ NODE_ENV: 'test',
+ ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
+ LOG_LEVEL: 'error',
+ TREK_DB_URL: databaseUrl(adminUrl, TEMPLATE_DATABASE),
+ },
+ });
+}
+
+/** The template and every worker's copy: names the lane alone uses (a prefix compared as text, not a LIKE pattern). */
+async function dropLaneDatabases(adminUrl: string): Promise {
+ await withAdmin(adminUrl, async (run) => {
+ const lane = await run<{ datname: string }>(
+ 'select datname from pg_database where datname = ? or left(datname, length(?)) = ?',
+ [TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, WORKER_DATABASE_PREFIX],
+ );
+ await Promise.all(lane.map((row) => run(`DROP DATABASE IF EXISTS "${row.datname}" WITH (FORCE)`)));
+ });
+}
+
+/** After the run: the lane leaves no database behind. */
+export async function teardown(): Promise {
+ const adminUrl = process.env.TREK_TEST_DB_URL;
+ if (adminUrl) await dropLaneDatabases(adminUrl);
+}
diff --git a/server/tests/postgres/schema-parity.test.ts b/server/tests/postgres/schema-parity.test.ts
new file mode 100644
index 000000000..3c1559e75
--- /dev/null
+++ b/server/tests/postgres/schema-parity.test.ts
@@ -0,0 +1,133 @@
+/**
+ * PARITY-PG — the schema a Postgres database ends with after its bootstrap
+ * (the baseline, then every migration after its cut) is the schema the
+ * entities describe, as scripts/generate-pg-baseline.ts writes it for
+ * Postgres (src/db/engine/postgres/schema-adjust.ts). MikroORM's comparator
+ * reads the live database and must find nothing to change: a migration after
+ * the cut that left Postgres behind SQLite, or a baseline that drifted from
+ * the entities, fails here. It runs on the lane's database, never skipped.
+ *
+ * The comparator also reports statements that change nothing, and each kind
+ * is proven so rather than recognised by its text:
+ * - `drop not null` on a primary key column: SQLite lets a key column be null
+ * and the entities say so; Postgres keeps every primary key NOT NULL (and
+ * refuses the statement), checked against the live keys.
+ * - a column default or a check constraint Postgres stores in its own spelling
+ * (casts added, EXTRACT rewritten, BETWEEN as two comparisons): such
+ * statements are applied in a transaction that is rolled back, and the
+ * catalogue's own rendering of every default and check must read the same
+ * after as before. A default or a bound that really differs changes it.
+ * - dropping a trigger the entities cannot carry: only the triggers ported by
+ * hand (POSTGRES_TRIGGERS), by exact name, and each of them must exist.
+ */
+import { POSTGRES_TRIGGERS } from '../../src/db/engine/postgres/postgres-triggers';
+import { applyPostgresColumnAdjustments } from '../../src/db/engine/postgres/schema-adjust';
+import { ALL_ENTITIES } from '../../src/db/entities';
+import type { Transaction } from '@mikro-orm/core';
+import { MikroORM } from '@mikro-orm/postgresql';
+
+import { describe, expect, it } from 'vitest';
+
+const PORTED = Object.keys(POSTGRES_TRIGGERS);
+const DROP_NOT_NULL = /^alter table "(\w+)" alter column "(\w+)" drop not null$/;
+const SPELLING =
+ /^alter table "\w+" (?:alter column "\w+" set default .+|drop constraint "\w+"|add constraint "\w+" check \(.+\))$/s;
+/** MikroORM drops a trigger by its name, and the function it would have made for it as `__fn`. */
+const DROP_TRIGGER = /^drop trigger if exists "(\w+)" on "(\w+)"$/;
+const DROP_FUNCTION = /^drop function if exists "(\w+)"\(\)$/;
+
+/** Every column default and check constraint of the schema, as the catalogue renders them. */
+const FINGERPRINT_SQL = `select string_agg(item, E'\\n' order by item) as fingerprint from (
+ select 'default ' || c.relname || '.' || a.attname || ' = ' || pg_get_expr(d.adbin, d.adrelid) as item
+ from pg_attrdef d join pg_class c on c.oid = d.adrelid join pg_attribute a on a.attrelid = d.adrelid and a.attnum = d.adnum
+ where c.relnamespace = current_schema()::regnamespace
+ union all
+ select 'check ' || conrelid::regclass || '.' || conname || ' = ' || pg_get_constraintdef(oid)
+ from pg_constraint where contype = 'c' and connamespace = current_schema()::regnamespace
+) items`;
+
+const ROLLBACK = new Error('roll back the spelling check');
+
+async function liveSets(orm: MikroORM): Promise<{ primaryKeys: Set; triggers: Set }> {
+ const connection = orm.em.getConnection();
+ const names = (sql: string) =>
+ connection.execute>(sql).then((rows) => new Set(rows.map((row) => row.name)));
+ const [primaryKeys, triggers] = await Promise.all([
+ names(
+ `select c.relname || '.' || a.attname as name from pg_index i
+ join pg_class c on c.oid = i.indrelid join pg_attribute a on a.attrelid = i.indrelid and a.attnum = any(i.indkey)
+ where i.indisprimary and c.relnamespace = current_schema()::regnamespace`,
+ ),
+ names(
+ `select c.relname || '.' || t.tgname as name from pg_trigger t join pg_class c on c.oid = t.tgrelid
+ where not t.tgisinternal and c.relnamespace = current_schema()::regnamespace`,
+ ),
+ ]);
+ return { primaryKeys, triggers };
+}
+
+/** Whether applying `statements` leaves every default and check constraint reading as it did; nothing is kept. */
+async function changesNothing(
+ orm: MikroORM,
+ statements: readonly string[],
+): Promise<{ before: string; after: string }> {
+ const connection = orm.em.getConnection();
+ const fingerprint = async (trx?: Transaction) =>
+ (await connection.execute>(FINGERPRINT_SQL, [], 'all', trx))[0].fingerprint;
+ const before = await fingerprint();
+ let after = '';
+ await connection
+ .transactional(async (trx) => {
+ // One after the other: a drop and the add that follows name the same constraint.
+ await statements.reduce>(
+ (previous, statement) => previous.then(() => connection.execute(statement, [], 'run', trx)),
+ Promise.resolve(),
+ );
+ after = await fingerprint(trx);
+ throw ROLLBACK;
+ })
+ .catch((err: unknown) => {
+ if (err !== ROLLBACK) throw err;
+ });
+ return { before, after };
+}
+
+describe('PARITY-PG', () => {
+ it('PARITY-PG-001: the bootstrapped database holds exactly the schema the entities generate for Postgres', async () => {
+ const orm = await MikroORM.init({
+ clientUrl: process.env.TREK_DB_URL,
+ entities: [...ALL_ENTITIES],
+ discovery: { warnWhenNoEntities: false },
+ });
+ try {
+ for (const meta of orm.getMetadata().getAll().values()) {
+ applyPostgresColumnAdjustments(meta.props);
+ }
+ const live = await liveSets(orm);
+ const statements = (await orm.schema.getUpdateSchemaSQL({ wrap: false }))
+ .split(';\n')
+ .map((statement) => statement.trim().replace(/;$/, ''))
+ .filter((statement) => statement !== '');
+
+ const isKeyNotNull = (s: string) => {
+ const m = DROP_NOT_NULL.exec(s);
+ return m !== null && live.primaryKeys.has(`${m[1]}.${m[2]}`);
+ };
+ const isPortedTrigger = (s: string) => {
+ const trigger = DROP_TRIGGER.exec(s);
+ if (trigger) return PORTED.includes(trigger[1]) && live.triggers.has(`${trigger[2]}.${trigger[1]}`);
+ const fn = DROP_FUNCTION.exec(s);
+ return fn !== null && PORTED.some((name) => fn[1].endsWith(`_${name}_fn`));
+ };
+ const spelling = statements.filter((s) => !isKeyNotNull(s) && !isPortedTrigger(s) && SPELLING.test(s));
+
+ expect(statements.filter((s) => !isKeyNotNull(s) && !isPortedTrigger(s) && !SPELLING.test(s))).toEqual([]);
+ const { before, after } = await changesNothing(orm, spelling);
+ expect(after).toBe(before);
+ // Every hand-ported trigger is in the database, on its table.
+ expect(PORTED.filter((name) => ![...live.triggers].some((t) => t.endsWith(`.${name}`)))).toEqual([]);
+ } finally {
+ await orm.close(true);
+ }
+ }, 60_000);
+});
diff --git a/server/tests/postgres/semantics.test.ts b/server/tests/postgres/semantics.test.ts
new file mode 100644
index 000000000..f97094627
--- /dev/null
+++ b/server/tests/postgres/semantics.test.ts
@@ -0,0 +1,66 @@
+/**
+ * PGSEM-001..002 — what SQLite gives for free and Postgres only through the
+ * engine, against the real app on the lane's Postgres database:
+ * writes in a UnitOfWork run one at a time, as on SQLite's single writer, so
+ * a read-then-insert never races; counts come back as numbers; and a search
+ * ignores case as SQLite's LIKE does.
+ */
+import { buildApp } from '../../src/bootstrap';
+import { ilike } from '../../src/db/dialect/sql-functions';
+import { withRequestContext } from '../../src/nest/database/request-context';
+import { UnitOfWork } from '../../src/nest/database/unit-of-work';
+import { MikroORM } from '@mikro-orm/core';
+import type { INestApplication } from '@nestjs/common';
+
+import { afterAll, beforeAll, describe, expect, it } from 'vitest';
+
+let app: INestApplication;
+let orm: MikroORM;
+
+beforeAll(async () => {
+ app = await buildApp();
+ orm = app.get(MikroORM);
+}, 60_000);
+
+afterAll(async () => {
+ await app?.close();
+});
+
+/** Rows of `sql` read through the current (transactional) EntityManager. */
+function query(sql: string, params: unknown[] = []): Promise {
+ return orm.em.getConnection().execute(sql, params, 'all', orm.em.getTransactionContext());
+}
+
+describe('Postgres keeps SQLite semantics', () => {
+ it('PGSEM-001: concurrent read-then-insert units of work never see the same count, and counts are numbers', async () => {
+ const uow = app.get(UnitOfWork, { strict: false });
+ const claim = () =>
+ withRequestContext(orm, () =>
+ uow.transactional(async () => {
+ const [{ n }] = await query<{ n: number }>(
+ "select count(*) as n from categories where name like 'pg-lock-%'",
+ );
+ // Long enough for every other unit of work to have started and read too, were nothing holding them.
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ await query('insert into categories (name, color, icon) values (?, ?, ?)', [`pg-lock-${n}`, '#000000', 'x']);
+ return n;
+ }),
+ );
+
+ const claimed = await Promise.all([claim(), claim(), claim(), claim()]);
+
+ expect(claimed.every((n) => typeof n === 'number')).toBe(true);
+ expect([...claimed].sort((a, b) => a - b)).toEqual([0, 1, 2, 3]);
+ });
+
+ it('PGSEM-002: a search ignores case, as LIKE does on SQLite', async () => {
+ const rows = await withRequestContext(orm, () =>
+ query<{ name: string }>(`select name from categories where ${ilike(orm.em.getPlatform(), ['name'])}`, [
+ '%HOTEL%',
+ ]),
+ );
+ const names = rows.map((row) => row.name);
+ expect(names).toContain('Hotel');
+ expect(names.every((name) => /hotel/i.test(name))).toBe(true);
+ });
+});
diff --git a/server/tests/postgres/setup.ts b/server/tests/postgres/setup.ts
new file mode 100644
index 000000000..efa174bab
--- /dev/null
+++ b/server/tests/postgres/setup.ts
@@ -0,0 +1,44 @@
+// The Postgres lane's per-file setup (vitest.pg.config.ts), after tests/setup.ts:
+// every test file runs on a fresh copy of the migrated template, under a name of
+// its worker's own, so files never see each other's rows and workers never share
+// a database. TREK_DB_URL is set here, before the test file imports anything
+// that reads it (src/mikro-orm.config.ts reads it on import); the copy itself is
+// made in a beforeAll, which runs before the test file's own (where the app
+// connects).
+import { databaseUrl, TEMPLATE_DATABASE, WORKER_DATABASE_PREFIX, withAdmin } from './databases';
+
+import { beforeAll } from 'vitest';
+
+/** Copies of the template are made one at a time: Postgres refuses to copy a template another copy is reading. */
+const COPY_LOCK = 0x54524b54;
+const COPY_ATTEMPTS = 3;
+const COPY_RETRY_MS = 500;
+const COPY_TIMEOUT_MS = 60_000;
+
+const adminUrl = process.env.TREK_TEST_DB_URL as string;
+const poolId = process.env.VITEST_POOL_ID ?? '0';
+if (!/^\d+$/.test(poolId)) throw new Error(`unexpected VITEST_POOL_ID ${poolId}`);
+const database = `${WORKER_DATABASE_PREFIX}${poolId}`;
+
+process.env.TREK_DB_URL = databaseUrl(adminUrl, database);
+
+async function copyTemplate(attempt = 1): Promise {
+ try {
+ await withAdmin(adminUrl, async (run) => {
+ // One connection (withAdmin's pool holds one), so the session lock covers both statements.
+ await run('select pg_advisory_lock(?)', [COPY_LOCK]);
+ try {
+ await run(`DROP DATABASE IF EXISTS "${database}" WITH (FORCE)`);
+ await run(`CREATE DATABASE "${database}" TEMPLATE "${TEMPLATE_DATABASE}"`);
+ } finally {
+ await run('select pg_advisory_unlock(?)', [COPY_LOCK]);
+ }
+ });
+ } catch (err) {
+ if (attempt >= COPY_ATTEMPTS) throw err;
+ await new Promise((resolve) => setTimeout(resolve, COPY_RETRY_MS * attempt));
+ await copyTemplate(attempt + 1);
+ }
+}
+
+beforeAll(() => copyTemplate(), COPY_TIMEOUT_MS);
diff --git a/server/tests/unit/app-config/derive.test.ts b/server/tests/unit/app-config/derive.test.ts
index 1c8e8ba0e..d149ccbb5 100644
--- a/server/tests/unit/app-config/derive.test.ts
+++ b/server/tests/unit/app-config/derive.test.ts
@@ -396,6 +396,34 @@ describe('deriveDb: the pre-migration snapshot', () => {
});
});
+describe('deriveDb: the engine', () => {
+ it('DB-ENGINE-001: SQLite unless TREK_DB_URL names a database', () => {
+ expect(deriveDb({})).toMatchObject({ engine: 'sqlite' });
+ expect(deriveDb({ TREK_DB_URL: ' ' })).toMatchObject({ engine: 'sqlite' });
+ expect(deriveDb({ TREK_DB_URL: ' postgres://u:p@db:5432/trek ' })).toMatchObject({
+ engine: 'postgres',
+ url: 'postgres://u:p@db:5432/trek',
+ });
+ });
+
+ it('DB-ENGINE-002: a pool of ten connections unless told otherwise, from 1 to 100', () => {
+ const poolMaxOf = (raw: Record) => {
+ const db = deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek', ...raw });
+ return db.engine === 'postgres' ? db.poolMax : null;
+ };
+ expect(deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek' })).toMatchObject({ engine: 'postgres' });
+ expect(poolMaxOf({})).toBe(10);
+ expect(poolMaxOf({ TREK_DB_POOL_MAX: '25' })).toBe(25);
+ expect(poolMaxOf({ TREK_DB_POOL_MAX: '0' })).toBe(10);
+ expect(poolMaxOf({ TREK_DB_POOL_MAX: '500' })).toBe(100);
+ });
+
+ it('DB-ENGINE-003: no pool size on SQLite, which has no pool', () => {
+ expect(deriveDb({ TREK_DB_POOL_MAX: '25' })).toMatchObject({ engine: 'sqlite' });
+ expect(deriveDb({})).not.toHaveProperty('poolMax');
+ });
+});
+
describe('deriveNet', () => {
it('ALLOW_INTERNAL_NETWORK coerces the boolean-like family (ssrfGuard)', () => {
expect(deriveNet({ ALLOW_INTERNAL_NETWORK: 'True' }).allowInternalNetwork).toBe(true);
diff --git a/server/tests/unit/app-config/validate.test.ts b/server/tests/unit/app-config/validate.test.ts
index 89d608ea6..f4d17da7a 100644
--- a/server/tests/unit/app-config/validate.test.ts
+++ b/server/tests/unit/app-config/validate.test.ts
@@ -341,3 +341,40 @@ describe('validateEnvAtBoot: the pre-migration snapshot switches', () => {
);
});
});
+
+describe('validateEnvAtBoot: the database engine', () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('DB-BOOT-001: refuses a TREK_DB_URL that is not a postgres:// or postgresql:// URL, without printing it', () => {
+ for (const value of ['mysql://u:secret@db/trek', 'not a url', 'sqlite:///data/travel.db']) {
+ const { printed, thrown } = bootReport({ TREK_DB_URL: value });
+ expect(thrown, value).toMatch(/Invalid environment configuration/);
+ expect(printed, value).toContain('TREK_DB_URL=***');
+ expect(printed, value).not.toContain('secret');
+ }
+ });
+
+ it('DB-BOOT-002: masks TREK_DB_URL like every other secret', () => {
+ expect(SECRET_ENV_KEYS.has('TREK_DB_URL')).toBe(true);
+ });
+
+ it('DB-BOOT-003: refuses TREK_DB_URL together with a SQLite setting, naming both', () => {
+ for (const key of ['TREK_DB_FILE', 'TREK_DB_JOURNAL_MODE', 'TREK_DB_SYNCHRONOUS']) {
+ const { thrown, printed } = bootReport({ TREK_DB_URL: 'postgres://u:p@db/trek', [key]: 'x' });
+ expect(thrown, key).toMatch(/Invalid environment configuration/);
+ expect(printed, key).toContain('ambiguous database configuration');
+ expect(printed, key).toContain(key);
+ }
+ });
+
+ it('DB-BOOT-004: accepts a Postgres database on its own', () => {
+ expect(() => validateEnvAtBoot({ TREK_DB_URL: 'postgresql://u:p@db:5432/trek?sslmode=require' })).not.toThrow();
+ });
+
+ it('DB-BOOT-005: refuses a pool size outside 1 to 100', () => {
+ expect(bootReport({ TREK_DB_POOL_MAX: '0' }).thrown).toMatch(/Invalid environment configuration/);
+ expect(() => validateEnvAtBoot({ TREK_DB_POOL_MAX: '20' })).not.toThrow();
+ });
+});
diff --git a/server/tests/unit/auto-backup.test.ts b/server/tests/unit/auto-backup.test.ts
index 3da9d6374..70861b687 100644
--- a/server/tests/unit/auto-backup.test.ts
+++ b/server/tests/unit/auto-backup.test.ts
@@ -8,7 +8,7 @@
* on another install. The run now goes through backupService.createBackup(),
* which snapshots the DB with VACUUM INTO and bundles the key.
*/
-import type { MaintenanceRepository } from '../../src/db/repositories/MaintenanceRepository';
+import type { SqliteMaintenance } from '../../src/db/engine/sqlite/sqlite-maintenance';
import { AutoBackupJob } from '../../src/nest/backup/auto-backup.job';
import { createBackup } from '../../src/nest/backup/backup.impl';
import type { BackupService } from '../../src/nest/backup/backup.service';
@@ -78,7 +78,7 @@ const liveDb = path.join(__dirname, '../../data', 'travel.db');
/** The backup port BackupService injects: the real SQLite one over stubs. */
const database = new SqliteDatabaseBackup(
{ file: liveDb, close: vi.fn(), reopen: vi.fn() } as unknown as DatabaseLifecycle,
- maintenanceStub as unknown as MaintenanceRepository,
+ maintenanceStub as unknown as SqliteMaintenance,
);
// createBackup receives StorageService and the database port as parameters
diff --git a/server/tests/unit/db/connection.test.ts b/server/tests/unit/db/connection.test.ts
index b179f2397..ec5f3bf84 100644
--- a/server/tests/unit/db/connection.test.ts
+++ b/server/tests/unit/db/connection.test.ts
@@ -1,4 +1,5 @@
import { openDatabase } from '../../../src/db/connection';
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Places } from '../../../src/db/entities/Places.entity';
import { Trips } from '../../../src/db/entities/Trips.entity';
import { UnitOfWork } from '../../../src/nest/database/unit-of-work';
@@ -147,7 +148,19 @@ async function deleteTripAndWatchTempFiles(db: Database.Database): Promise<{
const none = undefined as never;
try {
- const trips = new TripsService(none, none, none, none, none, none, none, none, new UnitOfWork(t.em), t.em, none);
+ const trips = new TripsService(
+ none,
+ none,
+ none,
+ none,
+ none,
+ none,
+ none,
+ none,
+ new UnitOfWork(t.em, new SqliteEngine()),
+ t.em,
+ none,
+ );
await trips.remove(trip.id, user.id, 'user');
return {
tempFiles: [...seen],
diff --git a/server/tests/unit/db/dialect/catalog.test.ts b/server/tests/unit/db/dialect/catalog.test.ts
new file mode 100644
index 000000000..448453c2a
--- /dev/null
+++ b/server/tests/unit/db/dialect/catalog.test.ts
@@ -0,0 +1,85 @@
+/**
+ * The schema catalog per engine (src/db/dialect/catalog.ts) and the migration
+ * helpers built on it (src/db/migration-utils.ts), so a migration after the
+ * Postgres baseline's cut can ask what exists on either engine.
+ */
+import { columnNamesSql, tableExistsSql } from '../../../../src/db/dialect/catalog';
+import {
+ addColumnIfMissing,
+ columnNames,
+ execUnlessTableMissing,
+ tableExists,
+} from '../../../../src/db/migration-utils';
+import { createSnapshotTestDb } from '../../../helpers/db-mock';
+import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
+import { Platform, TableNotFoundException } from '@mikro-orm/core';
+import type { Migration } from '@mikro-orm/migrations';
+import { BasePostgreSqlPlatform, SqlitePlatform } from '@mikro-orm/sql';
+
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+
+const sqlite = new SqlitePlatform();
+const postgres = new BasePostgreSqlPlatform();
+
+describe('catalog statements', () => {
+ it('CATALOG-001: SQLite reads its own catalog, Postgres the information schema of the current schema', () => {
+ expect(columnNamesSql(sqlite)).toBe('select name from pragma_table_info(?)');
+ expect(tableExistsSql(sqlite)).toBe("select name from sqlite_master where type = 'table' and name = ?");
+ expect(columnNamesSql(postgres)).toBe(
+ 'select column_name as name from information_schema.columns where table_schema = current_schema() and table_name = ?',
+ );
+ expect(tableExistsSql(postgres)).toBe(
+ 'select table_name as name from information_schema.tables where table_schema = current_schema() and table_name = ?',
+ );
+ });
+
+ it('CATALOG-002: any other engine is refused', () => {
+ class OtherPlatform extends Platform {}
+ expect(() => columnNamesSql(new OtherPlatform())).toThrow();
+ expect(() => tableExistsSql(new OtherPlatform())).toThrow();
+ });
+});
+
+describe('migration helpers', () => {
+ const testDb = createSnapshotTestDb();
+ let t: TestOrm;
+ let migration: Migration;
+ const execute = vi.fn();
+
+ beforeAll(async () => {
+ t = await createTestOrm(testDb);
+ execute.mockImplementation((sql: string, params?: unknown[]) => t.em.getConnection().execute(sql, params));
+ migration = { execute, getEntityManager: () => t.em } as unknown as Migration;
+ });
+ afterAll(async () => {
+ await t.close();
+ testDb.close();
+ });
+
+ it('CATALOG-010: tableExists and columnNames answer from the live SQLite schema', async () => {
+ expect(await tableExists(migration, 'users')).toBe(true);
+ expect(await tableExists(migration, 'no_such_table')).toBe(false);
+ expect((await columnNames(migration, 'users')).has('email')).toBe(true);
+ expect((await columnNames(migration, 'no_such_table')).size).toBe(0);
+ });
+
+ it('CATALOG-011: addColumnIfMissing adds once, quoting the table the way the engine does', async () => {
+ execute.mockClear();
+ await addColumnIfMissing(migration, 'users', 'catalog_probe', 'catalog_probe TEXT');
+ await addColumnIfMissing(migration, 'users', 'catalog_probe', 'catalog_probe TEXT');
+ const alters = execute.mock.calls.map(([sql]) => String(sql)).filter((sql) => sql.startsWith('alter table'));
+ expect(alters).toEqual(['alter table `users` add column catalog_probe TEXT']);
+ expect((await columnNames(migration, 'users')).has('catalog_probe')).toBe(true);
+ expect(postgres.quoteIdentifier('users')).toBe('"users"');
+ });
+
+ it('CATALOG-012: execUnlessTableMissing tolerates a missing table on either engine, and nothing else', async () => {
+ const failing = (error: Error) => ({ execute: vi.fn(async () => Promise.reject(error)) }) as unknown as Migration;
+ await expect(execUnlessTableMissing(failing(new Error('no such table: x')), 'x')).resolves.toBeUndefined();
+ const pgMissing = new TableNotFoundException(
+ Object.assign(new Error('relation "x" does not exist'), { code: '42P01' }),
+ );
+ await expect(execUnlessTableMissing(failing(pgMissing), 'x')).resolves.toBeUndefined();
+ await expect(execUnlessTableMissing(failing(new Error('syntax error')), 'x')).rejects.toThrow('syntax error');
+ });
+});
diff --git a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts b/server/tests/unit/db/dialect/sql-functions.postgres.test.ts
index b26f0c54e..6c4d3e89e 100644
--- a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts
+++ b/server/tests/unit/db/dialect/sql-functions.postgres.test.ts
@@ -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',
diff --git a/server/tests/unit/db/engine/engine.test.ts b/server/tests/unit/db/engine/engine.test.ts
new file mode 100644
index 000000000..0b002b733
--- /dev/null
+++ b/server/tests/unit/db/engine/engine.test.ts
@@ -0,0 +1,133 @@
+import { deriveDb } from '../../../../src/app-config/derive';
+import { buildOrmConfig, resolveEngine } from '../../../../src/db/engine';
+import { TrekPostgreSqlDriver } from '../../../../src/db/engine/postgres/postgres-driver';
+import { sslOptionFor } from '../../../../src/db/engine/postgres/ssl';
+import { redactUrl } from '../../../../src/db/engine/redact-url';
+import type { AnyOrm } from '../../../../src/db/orm';
+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, vi } from 'vitest';
+
+const PG_URL = 'postgresql://trek:s3cret@db.example:5432/trek';
+
+describe('resolveEngine', () => {
+ it('ENGINE-001: SQLite when no TREK_DB_URL is set, which is the test environment', () => {
+ const engine = resolveEngine();
+ expect(engine.id).toBe('sqlite');
+ expect(engine.ormOptions()).toEqual({ driver: SharedSqliteDriver, dbName: ':memory:' });
+ expect(engine.describe()).toBe('SQLite file :memory:');
+ });
+
+ it('ENGINE-002: Postgres when TREK_DB_URL names a database, with the pool size and no SSL by default', () => {
+ const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL, TREK_DB_POOL_MAX: '7' }));
+ expect(engine.id).toBe('postgres');
+ expect(engine.ormOptions()).toEqual({
+ driver: TrekPostgreSqlDriver,
+ clientUrl: PG_URL,
+ pool: { min: 0, max: 7 },
+ driverOptions: {},
+ });
+ });
+
+ it('ENGINE-003: describes a Postgres database without its password', () => {
+ const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require&password=other` }));
+ expect(engine.describe()).toBe('Postgres postgresql://trek:***@db.example:5432/trek');
+ expect(engine.describe()).not.toContain('s3cret');
+ expect(engine.describe()).not.toContain('other');
+ });
+});
+
+describe('sslOptionFor', () => {
+ it('ENGINE-010: maps sslmode onto the pg pool option (MikroORM drops the URL query), always verifying the certificate', () => {
+ // As the pg driver itself reads them: require and verify-ca are verify-full.
+ expect(sslOptionFor(PG_URL)).toBeUndefined();
+ expect(sslOptionFor(`${PG_URL}?sslmode=disable`)).toBe(false);
+ for (const mode of ['require', 'verify-ca', 'verify-full']) {
+ expect(sslOptionFor(`${PG_URL}?sslmode=${mode}`), mode).toEqual({ rejectUnauthorized: true });
+ }
+ });
+
+ it('ENGINE-011: refuses an sslmode it does not know or that would skip verification', () => {
+ expect(() => sslOptionFor(`${PG_URL}?sslmode=requrie`)).toThrow(/sslmode "requrie"/);
+ expect(() => sslOptionFor(`${PG_URL}?sslmode=no-verify`)).toThrow(/sslmode "no-verify"/);
+ expect(() => sslOptionFor(`${PG_URL}?sslmode=prefer`)).toThrow(/sslmode "prefer"/);
+ });
+
+ it('ENGINE-012: passes the SSL option to the pool when the URL asks for one', () => {
+ const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require` }));
+ expect(engine.ormOptions().driverOptions).toEqual({ ssl: { rejectUnauthorized: true } });
+ });
+
+ it('ENGINE-013: trusts the CA in sslrootcert (a managed host that signs its own certificates), and refuses a missing file', () => {
+ const dir = mkdtempSync(path.join(tmpdir(), 'trek-ca-'));
+ try {
+ const ca = path.join(dir, 'root.crt');
+ writeFileSync(ca, '-----BEGIN CERTIFICATE-----\nprobe\n-----END CERTIFICATE-----\n');
+ expect(sslOptionFor(`${PG_URL}?sslmode=verify-full&sslrootcert=${encodeURIComponent(ca)}`)).toEqual({
+ rejectUnauthorized: true,
+ ca: readFileSync(ca, 'utf8'),
+ });
+ expect(() =>
+ sslOptionFor(`${PG_URL}?sslmode=require&sslrootcert=${encodeURIComponent(path.join(dir, 'no.crt'))}`),
+ ).toThrow(/sslrootcert/);
+ expect(() => sslOptionFor(`${PG_URL}?sslrootcert=${encodeURIComponent(ca)}`)).toThrow(
+ /sslrootcert without sslmode/,
+ );
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+});
+
+describe('redactUrl', () => {
+ it('ENGINE-020: hides the password and drops the query, which can carry one too', () => {
+ expect(redactUrl('postgres://u:p@h/d?password=x&sslmode=require')).toBe('postgres://u:***@h/d');
+ expect(redactUrl('postgres://u@h:5433/d')).toBe('postgres://u@h:5433/d');
+ expect(redactUrl('not a url')).toBe('');
+ });
+});
+
+describe('buildOrmConfig', () => {
+ it('ENGINE-030: the SQLite config carries the entities, migrations and seeders with the engine options', () => {
+ const config = buildOrmConfig(resolveEngine());
+ expect(config.driver).toBe(SharedSqliteDriver);
+ expect(config.dbName).toBe(':memory:');
+ expect(config.migrations).toMatchObject({ pathTs: 'src/db/migrations', snapshot: false });
+ expect(config.seeder).toMatchObject({ defaultSeeder: 'DatabaseSeeder' });
+ expect(Array.isArray(config.entities) && config.entities.length).toBeGreaterThan(100);
+ });
+
+ it('ENGINE-031: the Postgres config carries the same entities and migrations, on the pg driver', () => {
+ const config = buildOrmConfig(resolveEngine(deriveDb({ TREK_DB_URL: PG_URL })));
+ expect(config.driver).toBe(TrekPostgreSqlDriver);
+ expect(config.clientUrl).toBe(PG_URL);
+ expect(config.migrations).toMatchObject({ pathTs: 'src/db/migrations', snapshot: false });
+ expect(config.entities).toBe(buildOrmConfig(resolveEngine()).entities);
+ });
+
+ it('ENGINE-032: a Postgres engine opens and closes nothing of its own; a reopen (after pg_restore) rebinds and migrates', async () => {
+ const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL }));
+ await expect(engine.reopen()).rejects.toThrow(/reopened before it was opened/);
+ const onReopened = vi.fn(async () => undefined);
+ const orm = {} as unknown as AnyOrm;
+ expect(() => engine.open(orm, onReopened)).not.toThrow();
+ expect(() => engine.close()).not.toThrow();
+ await engine.reopen();
+ expect(onReopened).toHaveBeenCalledTimes(1);
+ });
+
+ 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();
+ });
+});
diff --git a/server/tests/unit/db/engine/postgres/baseline.test.ts b/server/tests/unit/db/engine/postgres/baseline.test.ts
new file mode 100644
index 000000000..5041d9e12
--- /dev/null
+++ b/server/tests/unit/db/engine/postgres/baseline.test.ts
@@ -0,0 +1,170 @@
+/**
+ * The committed Postgres baseline (server/assets/postgres/) and what keeps it
+ * honest: reading and writing it (src/db/engine/postgres/baseline.ts) and the
+ * generator's --check (scripts/generate-pg-baseline.ts).
+ */
+import { checkBaseline, rowStatements, triggerPortProblems } from '../../../../../scripts/generate-pg-baseline';
+import {
+ baselineMatches,
+ readPostgresBaseline,
+ renderBaseline,
+ writePostgresBaseline,
+ type PostgresBaseline,
+} from '../../../../../src/db/engine/postgres/baseline';
+import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
+import { knownMigrationNames } from '../../../../../src/db/known-migrations';
+
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, describe, expect, it } from 'vitest';
+
+const A = 'Migration20200101000000_a';
+const B = 'Migration20200101000100_b';
+const C = 'Migration20200101000200_c';
+const sample: PostgresBaseline = {
+ through: B,
+ migrations: [A, B],
+ statements: ['create table "t" ("note" text default \'x;y\')', 'create index "i" on "t" ("note")'],
+};
+
+let dir: string | undefined;
+afterEach(() => {
+ if (dir) rmSync(dir, { recursive: true, force: true });
+ dir = undefined;
+});
+const scratch = () => (dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-baseline-')));
+
+describe('Postgres baseline files', () => {
+ it('PGBASE-001: what is written reads back the same, comment line and all', () => {
+ const target = scratch();
+ writePostgresBaseline(sample, target);
+ expect(readPostgresBaseline(target)).toEqual(sample);
+ expect(baselineMatches(sample, target)).toBe(true);
+ expect(baselineMatches({ ...sample, statements: [sample.statements[0]] }, target)).toBe(false);
+ expect(renderBaseline(sample).sql.startsWith('-- ')).toBe(true);
+ });
+
+ it('PGBASE-002: a manifest that does not list its own cut, or an empty SQL file, is refused', () => {
+ const target = scratch();
+ writePostgresBaseline(sample, target);
+ writeFileSync(path.join(target, 'baseline.json'), JSON.stringify({ through: C, migrations: [A, B] }));
+ expect(() => readPostgresBaseline(target)).toThrow(/must name "through"/);
+ writePostgresBaseline(sample, target);
+ writeFileSync(path.join(target, 'baseline.sql'), '-- nothing\n');
+ expect(() => readPostgresBaseline(target)).toThrow(/holds no statements/);
+ writeFileSync(path.join(target, 'baseline.sql'), 'create table "q" ("a" text default \'?\');\n');
+ expect(() => readPostgresBaseline(target)).toThrow(/holds a "\?"/);
+ expect(baselineMatches(sample, path.join(target, 'missing'))).toBe(false);
+ });
+
+ it('PGBASE-003: the committed baseline names every migration on disk up to its cut, and only those', () => {
+ const committed = readPostgresBaseline();
+ const known = [...knownMigrationNames()].sort();
+ expect(committed.migrations).toEqual(known.filter((name) => name <= committed.through));
+ expect(committed.statements.some((statement) => /^create table "users"/.test(statement))).toBe(true);
+ expect(committed.statements.join('\n')).not.toMatch(/\binteger\b/);
+ });
+});
+
+describe('checkBaseline', () => {
+ const PORTED = Object.keys(POSTGRES_TRIGGERS);
+ const generated = (through: string, migrations: string[]): PostgresBaseline => ({
+ through,
+ migrations,
+ statements: [],
+ });
+
+ it('PGBASE-010: at the newest migration the baseline must be exactly what the entities generate', () => {
+ expect(checkBaseline(sample, generated(B, [A, B]), true, PORTED)).toEqual([]);
+ expect(checkBaseline(sample, generated(B, [A, B]), false, PORTED)).toEqual([
+ 'the entities generate a different schema; run npm run gen:pg-baseline',
+ ]);
+ });
+
+ it('PGBASE-011: once a migration lands after the cut, the baseline is frozen and only its manifest is checked', () => {
+ expect(checkBaseline(sample, generated(C, [A, B, C]), false, PORTED)).toEqual([]);
+ });
+
+ it('PGBASE-013: while not frozen, a SQLite trigger without a port is reported; once frozen the triggers are not checked', () => {
+ const withNewTrigger = [...PORTED, 'trg_new_trigger'];
+ expect(checkBaseline(sample, generated(B, [A, B]), true, withNewTrigger)).toEqual([
+ 'SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: trg_new_trigger',
+ ]);
+ expect(checkBaseline(sample, generated(C, [A, B, C]), false, withNewTrigger)).toEqual([]);
+ });
+
+ it('PGBASE-012: a manifest naming a migration that is gone, or missing one before its cut, is reported', () => {
+ expect(checkBaseline(sample, generated(C, [B, C]), true, PORTED)).toEqual([
+ `the baseline names migrations that do not exist: ${A}`,
+ ]);
+ const skipped = { ...sample, migrations: [B] };
+ expect(checkBaseline(skipped, generated(B, [A, B]), true, PORTED)).toEqual([
+ `migrations up to ${B} are missing from it: ${A}`,
+ ]);
+ });
+});
+
+describe('readPostgresBaseline manifest', () => {
+ it('PGBASE-004: a manifest whose migrations are not all names, or are empty, is refused', () => {
+ const target = scratch();
+ for (const manifest of [
+ { through: B, migrations: [A, 3] },
+ { through: B, migrations: [] },
+ { through: 7, migrations: [B] },
+ ]) {
+ writePostgresBaseline(sample, target);
+ writeFileSync(path.join(target, 'baseline.json'), JSON.stringify(manifest));
+ expect(() => readPostgresBaseline(target), JSON.stringify(manifest)).toThrow(/must name "through"/);
+ }
+ });
+});
+
+describe('triggerPortProblems', () => {
+ it('PGBASE-020: every SQLite trigger needs a Postgres port, and a port without its trigger is stale', () => {
+ expect(triggerPortProblems(['a', 'b'], ['b', 'a'])).toEqual([]);
+ expect(triggerPortProblems(['a', 'b'], ['a', 'c'])).toEqual([
+ 'SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: b',
+ 'POSTGRES_TRIGGERS ports triggers the migrations no longer create: c',
+ ]);
+ });
+
+ it('PGBASE-021: the committed baseline carries every trigger port after the tables', () => {
+ const statements = readPostgresBaseline().statements;
+ const firstTrigger = statements.findIndex((statement) => statement.startsWith('CREATE FUNCTION'));
+ expect(firstTrigger).toBeGreaterThan(0);
+ expect(statements.slice(firstTrigger).every((statement) => /^CREATE (FUNCTION|TRIGGER) /.test(statement))).toBe(
+ true,
+ );
+ });
+});
+
+describe('rowStatements', () => {
+ it("PGBASE-030: a migration's rows go in as quoted literals the column casts, and a numeric id moves its sequence past them", () => {
+ expect(
+ rowStatements('photo_provider_fields', [
+ { id: 1, provider_id: 'immich', label: "Server's URL", required: 1, hint: null },
+ { id: 2, provider_id: 'immich', label: 'API key', required: 0, hint: 'x' },
+ ]),
+ ).toEqual([
+ 'insert into "photo_provider_fields" ("id", "provider_id", "label", "required", "hint") values ' +
+ "('1', 'immich', 'Server''s URL', '1', NULL), ('2', 'immich', 'API key', '0', 'x')",
+ "select setval(seq, greatest(max_id, 1)) from (select pg_get_serial_sequence('\"photo_provider_fields\"', 'id') as seq, " +
+ 'max("id") as max_id from "photo_provider_fields") ids where seq is not null',
+ ]);
+ });
+
+ it('PGBASE-031: a text key has no sequence to move, an empty table writes nothing, and binary data is refused', () => {
+ expect(rowStatements('addons', [{ id: 'mcp', enabled: 0 }])).toEqual([
+ 'insert into "addons" ("id", "enabled") values (\'mcp\', \'0\')',
+ ]);
+ expect(rowStatements('addons', [])).toEqual([]);
+ expect(() => rowStatements('blobs', [{ data: Buffer.from('x') }])).toThrow(/binary data/);
+ });
+
+ it('PGBASE-032: the committed baseline carries the reference rows the migrations insert', () => {
+ const sql = readPostgresBaseline().statements.join('\n');
+ expect(sql).toContain('insert into "tour_types"');
+ expect(sql).toContain('insert into "app_settings"');
+ });
+});
diff --git a/server/tests/unit/db/engine/postgres/pg-tools.test.ts b/server/tests/unit/db/engine/postgres/pg-tools.test.ts
new file mode 100644
index 000000000..8f3a3f0e1
--- /dev/null
+++ b/server/tests/unit/db/engine/postgres/pg-tools.test.ts
@@ -0,0 +1,260 @@
+/**
+ * The Postgres client tools (src/db/engine/postgres/pg-tools.ts, with
+ * pg-process.ts and restore-safety.ts), run against stand-in pg_dump,
+ * pg_restore and psql scripts (tests/helpers/fake-pg-tools.ts): the arguments
+ * and environment each call hands the binary, the password kept out of the
+ * arguments, the files a dump and a restore leave behind, and the refusal when
+ * a tool is missing or too old. The real tools against a real database are
+ * exercised by the Postgres lane.
+ */
+import { pgConnectionEnv, PgToolMissingError, resolvePgTool } from '../../../../../src/db/engine/postgres/pg-process';
+import { pgDump, pgDumpMigrations, pgRestore } from '../../../../../src/db/engine/postgres/pg-tools';
+import { hasRestrictFix } from '../../../../../src/db/engine/postgres/restore-safety';
+import {
+ answersSchema,
+ copiesFileEndingWith,
+ failsWith,
+ flagValues,
+ installFakeTool,
+ listsMigrations,
+ printsWhen,
+ readToolCall,
+ touches,
+ writesFile,
+ writesRestrictedScript,
+} from '../../../../helpers/fake-pg-tools';
+
+import fs, { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
+import os, { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const URL_WITH_SECRET = 'postgresql://trek:s3cr%40t@db.example:6543/trek_db?sslmode=require';
+/** The restore's session settings, which psql gets through PGOPTIONS. */
+const RESTORE_OPTIONS = '-c lock_timeout=60s -c client_min_messages=warning';
+
+let bin: string;
+let out: string;
+
+beforeEach(() => {
+ bin = mkdtempSync(path.join(tmpdir(), 'trek-pg-bin-'));
+ out = mkdtempSync(path.join(tmpdir(), 'trek-pg-out-'));
+});
+afterEach(() => {
+ rmSync(bin, { recursive: true, force: true });
+ rmSync(out, { recursive: true, force: true });
+});
+
+describe('pg-tools', () => {
+ it('PGTOOLS-001: finds a tool by absolute path in the configured directory, and nowhere else', () => {
+ installFakeTool(bin, 'pg_dump');
+ writeFileSync(path.join(bin, 'psql'), 'not executable');
+ expect(resolvePgTool('pg_dump', bin)).toBe(path.join(bin, 'pg_dump'));
+ expect(resolvePgTool('pg_restore', bin)).toBeNull();
+ expect(resolvePgTool('psql', bin)).toBeNull();
+ });
+
+ it('PGTOOLS-002: the connection goes in the libpq environment, the password decoded and never an argument', () => {
+ expect(pgConnectionEnv(URL_WITH_SECRET)).toEqual({
+ PGHOST: 'db.example',
+ PGPORT: '6543',
+ PGDATABASE: 'trek_db',
+ PGUSER: 'trek',
+ PGPASSWORD: 's3cr@t',
+ PGCONNECT_TIMEOUT: '10',
+ PGSSLMODE: 'verify-full',
+ PGSSLROOTCERT: 'system',
+ });
+ expect(pgConnectionEnv('postgres://u@[::1]/d')).toEqual({
+ PGHOST: '::1',
+ PGPORT: '5432',
+ PGDATABASE: 'd',
+ PGUSER: 'u',
+ PGCONNECT_TIMEOUT: '10',
+ });
+ });
+
+ it('PGTOOLS-003: SSL is verified as the app pool verifies it, against the URL root certificate when it names one', () => {
+ expect(pgConnectionEnv('postgres://u@h/d?sslmode=disable')).toMatchObject({ PGSSLMODE: 'disable' });
+ expect(pgConnectionEnv('postgres://u@h/d?sslmode=disable').PGSSLROOTCERT).toBeUndefined();
+ expect(pgConnectionEnv('postgres://u@h/d?sslmode=verify-ca&sslrootcert=%2Fca%2Froot.crt')).toMatchObject({
+ PGSSLMODE: 'verify-full',
+ PGSSLROOTCERT: '/ca/root.crt',
+ });
+ });
+
+ it('PGTOOLS-004: pg_dump writes a private custom-format dump beside the target and renames it into place', async () => {
+ installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
+ const log = installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
+ const target = path.join(out, 'x.pgdump');
+ await pgDump(URL_WITH_SECRET, target, bin);
+
+ const call = readToolCall(log);
+ expect(call.args).toHaveLength(5);
+ expect(call.args).toEqual(
+ expect.arrayContaining([
+ '--format=custom',
+ '--no-owner',
+ '--no-privileges',
+ `--file=${target}.partial`,
+ '--schema="public"',
+ ]),
+ );
+ expect(call.env.PGPASSWORD).toBe('s3cr@t');
+ expect(call.args.join(' ')).not.toContain('s3cr');
+ expect(readFileSync(target, 'utf8')).toBe('dump');
+ expect(statSync(target).mode & 0o777).toBe(0o600);
+ expect(readdirSync(out)).toEqual(['x.pgdump']);
+ });
+
+ it('PGTOOLS-005: a restore clears the schema and loads the script in one psql transaction, then removes its work files', async () => {
+ const clearCopy = path.join(bin, 'clear.sql');
+ const restoreLog = installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
+ const psqlLog = installFakeTool(bin, 'psql', {
+ snippets: [answersSchema(), copiesFileEndingWith('clear-schema.sql', clearCopy)],
+ });
+ const source = path.join(out, 'in.pgdump');
+ await pgRestore(URL_WITH_SECRET, source, bin);
+
+ const restore = readToolCall(restoreLog);
+ expect(restore.args).toEqual(expect.arrayContaining(['--no-owner', '--no-privileges', '--schema=public']));
+ const [restoreScript] = flagValues(restore.args, '--file=');
+ expect(restoreScript).toMatch(/\.pg-restore-[^/]+\/restore\.sql$/);
+ expect(restore.args.slice(-2)).toEqual(['--', source]);
+ expect(restore.env).toEqual({});
+
+ const psql = readToolCall(psqlLog);
+ expect(psql.args).toEqual(
+ expect.arrayContaining([
+ '--no-psqlrc',
+ '--quiet',
+ '--single-transaction',
+ '--set=ON_ERROR_STOP=1',
+ `--output=${os.devNull}`,
+ ]),
+ );
+ // Order is the point here: the schema is cleared before the script loads.
+ const [clearFile, loadFile] = flagValues(psql.args, '--file=');
+ expect(clearFile).toMatch(/clear-schema\.sql$/);
+ expect(loadFile).toBe(restoreScript);
+ expect(psql.env.PGOPTIONS).toBe(RESTORE_OPTIONS);
+ expect(readFileSync(clearCopy, 'utf8')).toMatch(/DROP %s IF EXISTS %s CASCADE/);
+ expect(readdirSync(out)).toEqual([]);
+ });
+
+ it('PGTOOLS-005b: the restore script is checked line by line, so its COPY data is never read whole', async () => {
+ const source = path.join(out, 'in.pgdump');
+ installFakeTool(bin, 'pg_restore', {
+ snippets: [writesRestrictedScript(), 'seq 1 200000 >> "$(fake_file_target "$@")"'],
+ });
+ installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
+ const readWhole = vi.spyOn(fs, 'readFileSync');
+ try {
+ await pgRestore(URL_WITH_SECRET, source, bin);
+ expect(readWhole.mock.calls.filter(([file]) => String(file).endsWith('restore.sql'))).toEqual([]);
+ } finally {
+ readWhole.mockRestore();
+ }
+ });
+
+ it("PGTOOLS-006: reads the dump's recorded migrations from its COPY block, and none from a dump without them", async () => {
+ const log = installFakeTool(bin, 'pg_restore', {
+ snippets: [listsMigrations(['Migration20200101000000_baseline_schema', 'Migration20200101000100_second'])],
+ });
+ await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([
+ 'Migration20200101000000_baseline_schema',
+ 'Migration20200101000100_second',
+ ]);
+ const call = readToolCall(log);
+ expect(call.args).toEqual(expect.arrayContaining(['--data-only', '--table=mikro_orm_migrations', '--file=-']));
+ expect(call.args.slice(-2)).toEqual(['--', '/data/in.pgdump']);
+
+ installFakeTool(bin, 'pg_restore', { snippets: [printsWhen('--data-only', 'SET statement_timeout = 0;\n')] });
+ await expect(pgDumpMigrations('/data/in.pgdump', bin)).resolves.toEqual([]);
+ });
+
+ it.each([
+ {
+ id: 'PGTOOLS-007a',
+ stderr: 'pg_dump: error: query failed\nCommand was: SELECT secret_row\nDETAIL: Key (email)=(a@b.c)\n',
+ message: /^pg_dump failed: pg_dump: error: query failed$/,
+ },
+ { id: 'PGTOOLS-007b', stderr: 'x'.repeat(5000), message: /^pg_dump failed: x{2000}…$/ },
+ ])(
+ '$id: a failing tool reports its own message without the lines that quote SQL or rows, and leaves no file',
+ async ({ stderr, message }) => {
+ installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
+ installFakeTool(bin, 'pg_dump', { snippets: [failsWith(stderr)] });
+ const target = path.join(out, 'x.pgdump');
+ await expect(pgDump(URL_WITH_SECRET, target, bin)).rejects.toThrow(message);
+ expect(readdirSync(out)).toEqual([]);
+ },
+ );
+
+ it('PGTOOLS-008: a missing tool says how to install it, and a restore that cannot run leaves no work files', async () => {
+ const source = path.join(out, 'in.pgdump');
+ await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(PgToolMissingError);
+ await expect(pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin)).rejects.toThrow(/TREK_DB_PG_BIN_DIR/);
+ installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
+ await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(/psql was not found/);
+ expect(existsSync(out) && readdirSync(out)).toEqual([]);
+ });
+
+ it.each([
+ {
+ id: 'PGTOOLS-009a',
+ tool: 'pg_restore' as const,
+ version: '16.9',
+ message:
+ /pg_restore is too old to restore safely \(pg_restore \(PostgreSQL\) 16\.9 \(Debian\)\): it must be at least 17\.6, 16\.10, 15\.14 or 14\.19/,
+ },
+ { id: 'PGTOOLS-009b', tool: 'psql' as const, version: '17.5', message: /psql is too old/ },
+ ])(
+ '$id: client tools without the \\restrict fix (CVE-2025-8714) never turn a dump into a script',
+ async ({ tool, version, message }) => {
+ const ran = path.join(bin, 'ran');
+ installFakeTool(bin, 'pg_restore', { snippets: [touches(ran)] });
+ installFakeTool(bin, 'psql', { snippets: [touches(ran)] });
+ installFakeTool(bin, tool, { version, snippets: [touches(ran)] });
+ await expect(pgRestore(URL_WITH_SECRET, path.join(out, 'in.pgdump'), bin)).rejects.toThrow(message);
+ expect(existsSync(ran)).toBe(false);
+ expect(readdirSync(out)).toEqual([]);
+ },
+ );
+
+ it('PGTOOLS-010: knows which client versions carry the \\restrict fix', () => {
+ const fixed = ['14.19', '15.14', '16.10', '17.6', '17.11', '18.0', '19.1'];
+ const unfixed = ['13.30', '14.18', '15.13', '16.9', '17.5'];
+ for (const v of fixed) expect(hasRestrictFix(`pg_restore (PostgreSQL) ${v}`), v).toBe(true);
+ for (const v of unfixed) expect(hasRestrictFix(`pg_restore (PostgreSQL) ${v}`), v).toBe(false);
+ expect(hasRestrictFix('not a version')).toBe(false);
+ });
+
+ it('PGTOOLS-011: a dump holds the current schema only, matched by its exact name, which psql reads without the password in an argument', async () => {
+ const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema('My"Trek', '"My""Trek"')] });
+ const dumpLog = installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
+ await pgDump(URL_WITH_SECRET, path.join(out, 'x.pgdump'), bin);
+ expect(readToolCall(dumpLog).args).toContain('--schema="My""Trek"');
+ const psql = readToolCall(psqlLog);
+ expect(psql.args.join(' ')).toContain('current_schema()');
+ expect(psql.args.join(' ')).not.toContain('s3cr');
+
+ installFakeTool(bin, 'psql', { snippets: [printsWhen('quote_ident', '\n')] });
+ await expect(pgDump(URL_WITH_SECRET, path.join(out, 'y.pgdump'), bin)).rejects.toThrow(/no current schema/);
+ expect(readdirSync(out)).toEqual(['x.pgdump']);
+ });
+
+ it('PGTOOLS-012: a restore keeps to the current schema, and refuses a dump without TREK tables there before clearing anything', async () => {
+ const source = path.join(out, 'in.pgdump');
+ const restoreLog = installFakeTool(bin, 'pg_restore', {
+ snippets: [writesFile('\\restrict k1\nCREATE TABLE auth.users (\n);\n')],
+ });
+ const psqlLog = installFakeTool(bin, 'psql', { snippets: [answersSchema('trek')] });
+ await expect(pgRestore(URL_WITH_SECRET, source, bin)).rejects.toThrow(
+ 'The backup holds no TREK tables in the schema "trek"; nothing was replaced.',
+ );
+ expect(readToolCall(restoreLog).args).toContain('--schema=trek');
+ expect(readToolCall(psqlLog).args.join(' ')).not.toContain('--single-transaction');
+ expect(readdirSync(out)).toEqual([]);
+ });
+});
diff --git a/server/tests/unit/db/engine/postgres/postgres-driver.test.ts b/server/tests/unit/db/engine/postgres/postgres-driver.test.ts
new file mode 100644
index 000000000..25c9cf469
--- /dev/null
+++ b/server/tests/unit/db/engine/postgres/postgres-driver.test.ts
@@ -0,0 +1,47 @@
+/**
+ * The Postgres driver reads `bigint` as a number, on its own pool only
+ * (src/db/engine/postgres/postgres-driver.ts). Nothing here connects.
+ */
+import { parseInt8, parseNumeric, TrekPostgreSqlDriver } from '../../../../../src/db/engine/postgres/postgres-driver';
+import { Configuration } from '@mikro-orm/core';
+import type { PostgreSqlConnection } from '@mikro-orm/postgresql';
+
+import { describe, expect, it } from 'vitest';
+
+type Parsers = { getTypeParser(oid: number): (text: string) => unknown };
+
+describe('postgres driver', () => {
+ it('PGDRIVER-001: a bigint is a number up to 2^53 and refused past it, never rounded', () => {
+ expect(parseInt8('0')).toBe(0);
+ expect(parseInt8('1793636850006')).toBe(1793636850006);
+ expect(parseInt8('-42')).toBe(-42);
+ expect(() => parseInt8('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
+ });
+
+ it('PGDRIVER-003: a numeric (sum of a bigint, avg) is a number like SQLite answers, an integral one past 2^53 refused', () => {
+ expect(parseNumeric('4200')).toBe(4200);
+ expect(parseNumeric('2.5000000000000000')).toBe(2.5);
+ expect(parseNumeric('-7')).toBe(-7);
+ expect(() => parseNumeric('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
+ expect(() => parseNumeric('NaN')).toThrow(/not a finite number/);
+ expect(() => parseNumeric('Infinity')).toThrow(/not a finite number/);
+ });
+
+ it("PGDRIVER-002: the pool parses int8 as a number and keeps MikroORM's text dates", () => {
+ const config = new Configuration(
+ {
+ driver: TrekPostgreSqlDriver,
+ clientUrl: 'postgresql://u@localhost/trek',
+ entities: [],
+ discovery: { warnWhenNoEntities: false },
+ },
+ false,
+ );
+ const driver = new TrekPostgreSqlDriver(config);
+ const options = (driver.getConnection() as PostgreSqlConnection).mapOptions({});
+ const types = options.types as unknown as Parsers;
+ expect(types.getTypeParser(20)('12')).toBe(12);
+ expect(types.getTypeParser(1700)('12.5')).toBe(12.5);
+ expect(types.getTypeParser(1114)('2026-01-01 00:00:00')).toBe('2026-01-01 00:00:00');
+ });
+});
diff --git a/server/tests/unit/scripts/pg-probe/schema.test.ts b/server/tests/unit/db/engine/postgres/schema-adjust.test.ts
similarity index 53%
rename from server/tests/unit/scripts/pg-probe/schema.test.ts
rename to server/tests/unit/db/engine/postgres/schema-adjust.test.ts
index 97922171e..4c8bd3304 100644
--- a/server/tests/unit/scripts/pg-probe/schema.test.ts
+++ b/server/tests/unit/db/engine/postgres/schema-adjust.test.ts
@@ -1,21 +1,22 @@
/**
- * How the Postgres probe turns the entities into a Postgres schema
- * (scripts/pg-probe/schema.ts): SQLite-only defaults, TEXT timestamps, the
+ * How the entities' schema is written in Postgres
+ * (src/db/engine/postgres/schema-adjust.ts): SQLite-only defaults, TEXT timestamps, the
* NOCASE collation, and splitting the generated DDL so one refused statement
* does not take the rest with it.
*/
+import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
import {
- adjustColumns,
+ applyPostgresColumnAdjustments,
+ type ColumnMeta,
NOCASE_COLLATION_SQL,
PG_NOW_TEXT_DEFAULT,
portableDefault,
- splitStatements,
- type ColumnMeta,
-} from '../../../../scripts/pg-probe/schema';
+} from '../../../../../src/db/engine/postgres/schema-adjust';
+import { splitStatements } from '../../../../../src/db/engine/postgres/sql-split';
+import { DbTimestampType } from '../../../../../src/db/types';
import { describe, expect, it } from 'vitest';
-class DbTimestampType {}
class TextType {}
describe('pg-probe schema', () => {
@@ -39,19 +40,30 @@ describe('pg-probe schema', () => {
{ name: 'title', customType: new TextType(), columnTypes: ['text'], defaultRaw: "'x'" },
{ name: 'stamp', columnTypes: ['int'], defaultRaw: "(strftime('%s','now'))" },
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
- { name: 'id', columnTypes: ['int'] },
+ { name: 'body', columnTypes: ['text'] },
];
- expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
+ expect(applyPostgresColumnAdjustments(columns)).toEqual(['created_at', 'stamp', 'region']);
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
expect(columns[2]).toMatchObject({
- columnTypes: ['int'],
+ columnTypes: ['bigint'],
defaultRaw: 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)',
});
expect(columns[3]).toMatchObject({ collation: 'nocase' });
expect(NOCASE_COLLATION_SQL).toContain('CREATE COLLATION IF NOT EXISTS nocase');
});
+ it('PGPROBE-043: an integer column is bigint, as wide as SQLite INTEGER (epoch milliseconds overflow integer)', () => {
+ const columns: ColumnMeta[] = [
+ { name: 'id', columnTypes: ['integer'] },
+ { name: 'next_run_at', columnTypes: ['int'] },
+ { name: 'enabled', columnTypes: ['boolean'] },
+ { name: 'note', columnTypes: ['text'] },
+ ];
+ expect(applyPostgresColumnAdjustments(columns)).toEqual(['id', 'next_run_at']);
+ expect(columns.map((column) => column.columnTypes)).toEqual([['bigint'], ['bigint'], ['boolean'], ['text']]);
+ });
+
it('PGPROBE-042: splits DDL on semicolons outside quotes and drops session settings', () => {
const ddl = [
"set names 'utf8';",
@@ -67,4 +79,24 @@ describe('pg-probe schema', () => {
]);
expect(splitStatements(' ; ; ')).toEqual([]);
});
+
+ it('PGPROBE-045: refuses a tagged dollar quote, which it does not understand, rather than splitting inside it', () => {
+ expect(() => splitStatements('create function f() as $f$ select 1; $f$;')).toThrow(
+ /only the \$\$ dollar quote, not \$f\$/,
+ );
+ expect(splitStatements("select '$f$'; select 2")).toEqual(["select '$f$'", 'select 2']);
+ });
+
+ it('PGPROBE-044: a $$ body stays one statement, semicolons and all, and every trigger port splits into its two', () => {
+ const fn =
+ "CREATE FUNCTION f() RETURNS trigger LANGUAGE plpgsql AS $$\nBEGIN\n UPDATE t SET x = ';';\n RETURN NEW;\nEND\n$$";
+ expect(splitStatements(`${fn};\nCREATE TRIGGER g AFTER UPDATE ON t FOR EACH ROW EXECUTE FUNCTION f();`)).toEqual([
+ fn,
+ 'CREATE TRIGGER g AFTER UPDATE ON t FOR EACH ROW EXECUTE FUNCTION f()',
+ ]);
+ for (const [name, statements] of Object.entries(POSTGRES_TRIGGERS)) {
+ expect(splitStatements(statements.join(';\n')), name).toEqual([...statements]);
+ expect(statements.at(-1), name).toContain(`CREATE TRIGGER ${name} `);
+ }
+ });
});
diff --git a/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts b/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts
new file mode 100644
index 000000000..295d094c7
--- /dev/null
+++ b/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts
@@ -0,0 +1,267 @@
+/**
+ * How a Postgres database reaches the schema (src/db/engine/postgres/schema-bootstrap.ts):
+ * the three states the plan names (empty, foreign tables, TREK's own) and the
+ * safety net before pending migrations, over a fake migrator and connection.
+ */
+import type { AppEnv } from '../../../../../src/app-config';
+import {
+ bootstrapPostgresSchema,
+ postgresSafetyNet,
+ type PostgresBootstrapDeps,
+} from '../../../../../src/db/engine/postgres/schema-bootstrap';
+import type { MigrationSafetyNet } from '../../../../../src/db/legacy-baseline';
+import type { AnyOrm } from '../../../../../src/db/orm';
+import { answersSchema, failsWith, installFakeTool, writesFile } from '../../../../helpers/fake-pg-tools';
+import { Logger } from '@nestjs/common';
+
+import { mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const M1 = 'Migration20200101000000_baseline_schema';
+const M2 = 'Migration20200101000100_second';
+const M3 = 'Migration20200101000200_after_the_cut';
+
+interface FakeDb {
+ executed: string[];
+ pending: string[];
+ tables: string[];
+ /** Whether mikro_orm_migrations exists: from the start when anything is recorded. */
+ history: boolean;
+ ensureTable: ReturnType;
+ sql: string[];
+ logged: string[];
+ inTransaction: boolean[];
+ up: ReturnType;
+}
+
+function fakeOrm(state: Pick & { history?: boolean }): {
+ orm: AnyOrm;
+ db: FakeDb;
+} {
+ const db: FakeDb = {
+ ...state,
+ history: state.history ?? state.executed.length > 0,
+ ensureTable: vi.fn(async () => {
+ db.history = true;
+ }),
+ sql: [],
+ logged: [],
+ inTransaction: [],
+ up: vi.fn(async () => undefined),
+ };
+ const trx = { trx: true };
+ const connection = {
+ execute: vi.fn(async (sql: string, _params?: unknown[], _method?: string, ctx?: unknown) => {
+ if (sql.includes('information_schema.tables')) {
+ return [...db.tables, ...(db.history ? ['mikro_orm_migrations'] : [])].map((table_name) => ({ table_name }));
+ }
+ db.sql.push(sql);
+ db.inTransaction.push(ctx === trx);
+ return [];
+ }),
+ transactional: vi.fn(async (work: (ctx: unknown) => Promise) => work(trx)),
+ };
+ const storage = {
+ ensureTable: db.ensureTable,
+ logMigration: vi.fn(async ({ name }: { name: string }, ctx?: unknown) => {
+ db.logged.push(name);
+ db.inTransaction.push(ctx === trx);
+ db.executed = [...db.executed, name];
+ db.pending = db.pending.filter((pending) => pending !== name);
+ }),
+ };
+ const migrator = {
+ getStorage: () => storage,
+ getExecuted: vi.fn(async () => db.executed.map((name) => ({ name }))),
+ getPending: vi.fn(async () => db.pending.map((name) => ({ name }))),
+ up: db.up,
+ };
+ const orm = {
+ config: { getExtension: () => migrator, get: () => ({}) },
+ em: { getConnection: () => connection },
+ } as unknown as AnyOrm;
+ return { orm, db };
+}
+
+function deps(
+ safetyNet: MigrationSafetyNet = { beforeMigrate: vi.fn(async () => undefined), restoreHint: async () => null },
+) {
+ const value: PostgresBootstrapDeps = {
+ baseline: () => ({
+ through: M2,
+ migrations: [M1, M2],
+ statements: ['create table "a" ("id" bigserial)', 'create index "x" on "a" ("id")'],
+ }),
+ known: new Set([M1, M2, M3]),
+ safetyNet,
+ };
+ return value;
+}
+
+afterEach(() => vi.restoreAllMocks());
+
+describe('bootstrapPostgresSchema', () => {
+ beforeEach(() => {
+ vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
+ });
+
+ it('PGBOOT-001: an empty database gets the baseline and its migrations recorded, all in one transaction', async () => {
+ const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [] });
+ const net = deps();
+ await bootstrapPostgresSchema(orm, net);
+ expect(db.sql).toEqual(['create table "a" ("id" bigserial);\ncreate index "x" on "a" ("id")']);
+ expect(db.logged).toEqual([M1, M2]);
+ expect(db.inTransaction.every(Boolean)).toBe(true);
+ expect(db.up).not.toHaveBeenCalled();
+ expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
+ });
+
+ it('PGBOOT-002: a fresh database runs the migrations after the cut without the safety net (nothing to lose)', async () => {
+ const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2, M3], tables: [] });
+ const net = deps();
+ await bootstrapPostgresSchema(orm, net);
+ expect(db.logged).toEqual([M1, M2]);
+ expect(db.up).toHaveBeenCalledTimes(1);
+ expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
+ });
+
+ it('PGBOOT-003: tables without TREK history are refused, named, and nothing is written', async () => {
+ const { orm, db } = fakeOrm({ executed: [], pending: [M1], tables: ['customers', 'orders'] });
+ await expect(bootstrapPostgresSchema(orm, deps())).rejects.toThrow(
+ /Refusing to boot: the database holds 2 table\(s\) \(customers, orders\) but no TREK migration history/,
+ );
+ expect(db.sql).toEqual([]);
+ expect(db.logged).toEqual([]);
+ expect(db.ensureTable).not.toHaveBeenCalled();
+ });
+
+ it('PGBOOT-008: an empty history table beside no other table is a fresh database (a first boot cut short)', async () => {
+ const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [], history: true });
+ await bootstrapPostgresSchema(orm, deps());
+ expect(db.logged).toEqual([M1, M2]);
+ });
+
+ it('PGBOOT-004: a database a newer TREK migrated is refused before anything runs', async () => {
+ const { orm, db } = fakeOrm({ executed: [M1, 'Migration20990101000000_from_the_future'], pending: [], tables: [] });
+ await expect(bootstrapPostgresSchema(orm, deps())).rejects.toThrow(/migrated by a newer TREK/);
+ expect(db.up).not.toHaveBeenCalled();
+ });
+
+ it('PGBOOT-005: pending migrations on a database in use run after the safety net, labelled by the last one', async () => {
+ const { orm, db } = fakeOrm({ executed: [M2, M1], pending: [M3], tables: ['users'] });
+ const net = deps();
+ await bootstrapPostgresSchema(orm, net);
+ expect(net.safetyNet.beforeMigrate).toHaveBeenCalledWith('20200101000100');
+ expect(db.up).toHaveBeenCalledTimes(1);
+ expect(db.sql).toEqual([]);
+ });
+
+ it('PGBOOT-006: a refusing safety net stops the migration', async () => {
+ const { orm, db } = fakeOrm({ executed: [M1, M2], pending: [M3], tables: ['users'] });
+ const refusing = {
+ beforeMigrate: vi.fn(async () => Promise.reject(new Error('no copy'))),
+ restoreHint: async () => null,
+ };
+ await expect(bootstrapPostgresSchema(orm, deps(refusing))).rejects.toThrow('no copy');
+ expect(db.up).not.toHaveBeenCalled();
+ });
+
+ it('PGBOOT-007: a database at head does nothing', async () => {
+ const { orm, db } = fakeOrm({ executed: [M1, M2, M3], pending: [], tables: ['users'] });
+ const net = deps();
+ await bootstrapPostgresSchema(orm, net);
+ expect(db.sql).toEqual([]);
+ expect(db.up).not.toHaveBeenCalled();
+ expect(net.safetyNet.beforeMigrate).not.toHaveBeenCalled();
+ });
+});
+
+describe('postgresSafetyNet', () => {
+ const URL = 'postgresql://trek:pw@db/trek';
+ const NOW = new Date('2026-11-02T13:05:09Z');
+ let dir: string;
+ let bin: string;
+
+ beforeEach(() => {
+ dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-'));
+ bin = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-bin-'));
+ vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
+ });
+ afterEach(() => {
+ rmSync(dir, { recursive: true, force: true });
+ rmSync(bin, { recursive: true, force: true });
+ });
+
+ /**
+ * A stand-in pg_dump that writes its --file, or fails like one that cannot
+ * connect, beside the psql that names the schema the dump is limited to.
+ */
+ const fakePgDump = (ok: boolean) => {
+ installFakeTool(bin, 'pg_dump', { snippets: [ok ? writesFile('dump') : failsWith('could not connect')] });
+ installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
+ };
+ const envWith =
+ (preMigrateSnapshot: boolean | undefined, isTest: boolean, keep = 3) =>
+ () =>
+ ({
+ db: { preMigrateSnapshot, preMigrateSnapshotKeep: keep, pgBinDir: bin },
+ app: { isTest },
+ }) as unknown as AppEnv;
+ const net = (env: () => AppEnv) =>
+ postgresSafetyNet(
+ URL,
+ env,
+ () => NOW,
+ () => dir,
+ );
+
+ it.each([undefined, true])(
+ 'PGBOOT-010: dumps the database before migrating it (setting %s), keeping the newest copies and no half-written one',
+ async (setting) => {
+ fakePgDump(true);
+ for (const old of ['20200101000001', '20200101000002']) {
+ writeFileSync(path.join(dir, `pre-migrate-${old}-20250101T000000Z.pgdump`), 'old');
+ utimesSync(path.join(dir, `pre-migrate-${old}-20250101T000000Z.pgdump`), 1, 1);
+ }
+ writeFileSync(path.join(dir, 'pre-migrate-20200101000003-20250101T000000Z.pgdump.partial'), 'killed');
+ await net(envWith(setting, false, 2)).beforeMigrate('20200101000100');
+ const kept = readdirSync(dir).sort();
+ expect(kept).toHaveLength(2);
+ expect(kept).toContain('pre-migrate-20200101000100-20261102T130509Z.pgdump');
+ expect(kept.some((file) => file.endsWith('.partial'))).toBe(false);
+ },
+ );
+
+ it('PGBOOT-011: migrates without a copy when the operator turned it off, and says so', async () => {
+ const warn = vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
+ await expect(net(envWith(false, false)).beforeMigrate('1')).resolves.toBeUndefined();
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('without a copy'));
+ expect(readdirSync(dir)).toEqual([]);
+ });
+
+ it('PGBOOT-012: stays out of the way under test unless a suite asks for it', async () => {
+ await expect(net(envWith(undefined, true)).beforeMigrate('1')).resolves.toBeUndefined();
+ expect(readdirSync(dir)).toEqual([]);
+ });
+
+ it('PGBOOT-013: a copy that cannot be written refuses the boot, names the switch, and leaves no partial file', async () => {
+ fakePgDump(false);
+ await expect(net(envWith(true, false)).beforeMigrate('1')).rejects.toThrow(
+ /Refusing to boot: could not write a copy.*pg_dump failed: could not connect.*TREK_DB_PRE_MIGRATE_SNAPSHOT=false/s,
+ );
+ expect(readdirSync(dir)).toEqual([]);
+ });
+
+ it('PGBOOT-014: the newer-database refusal points at the copy taken from this release, or at the others', async () => {
+ const hint = net(envWith(true, false));
+ await expect(hint.restoreHint('20200101000100')).resolves.toBeNull();
+ writeFileSync(path.join(dir, 'pre-migrate-20200101000050-20250101T000000Z.pgdump'), 'old');
+ await expect(hint.restoreHint('20200101000100')).resolves.toMatch(/none of them from this release/);
+ writeFileSync(path.join(dir, 'pre-migrate-20200101000100-20250102T000000Z.pgdump'), 'mine');
+ await expect(hint.restoreHint('20200101000100')).resolves.toMatch(
+ /pre-migrate-20200101000100-20250102T000000Z\.pgdump: stop TREK, empty the database .*pg_restore --no-owner/,
+ );
+ });
+});
diff --git a/server/tests/unit/db/engine/sqlite/sqlite-maintenance.test.ts b/server/tests/unit/db/engine/sqlite/sqlite-maintenance.test.ts
new file mode 100644
index 000000000..6374f0188
--- /dev/null
+++ b/server/tests/unit/db/engine/sqlite/sqlite-maintenance.test.ts
@@ -0,0 +1,109 @@
+import { SqliteMaintenance } from '../../../../../src/db/engine/sqlite/sqlite-maintenance';
+import { createSnapshotTestDb } from '../../../../helpers/db-mock';
+import { createTestOrm, type TestOrm } from '../../../../helpers/test-orm';
+
+import Database from 'better-sqlite3';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
+
+const testDb = createSnapshotTestDb();
+let t: TestOrm;
+let maintenance: SqliteMaintenance;
+
+beforeAll(async () => {
+ t = await createTestOrm(testDb);
+ maintenance = new SqliteMaintenance(t.em);
+});
+afterAll(async () => {
+ await t.close();
+ testDb.close();
+});
+
+/**
+ * The first SQL text handed to the connection after the spy is set (the ORM
+ * may read its own metadata on the same connection afterwards).
+ */
+function spyOnExecute(): { first: () => string; restore: () => void } {
+ const connection = t.em.getConnection();
+ const spy = vi.spyOn(connection, 'execute');
+ return {
+ first: () => String(spy.mock.calls[0]?.[0]),
+ restore: () => spy.mockRestore(),
+ };
+}
+
+/** Whether the SQLite file at `file` holds a table named `table`. */
+function holdsTable(file: string, table: string): boolean {
+ const snap = new Database(file, { readonly: true });
+ try {
+ return (snap.pragma(`table_info(${table})`) as unknown[]).length > 0;
+ } finally {
+ snap.close();
+ }
+}
+
+describe('SqliteMaintenance', () => {
+ it('SQLMAINT-001: walCheckpoint sends PRAGMA wal_checkpoint(TRUNCATE)', async () => {
+ const execute = spyOnExecute();
+ try {
+ await expect(maintenance.walCheckpoint()).resolves.toBeUndefined();
+ expect(execute.first()).toBe('PRAGMA wal_checkpoint(TRUNCATE)');
+ } finally {
+ execute.restore();
+ }
+ });
+
+ describe('vacuumInto', () => {
+ let scratchDir: string;
+
+ afterEach(() => {
+ if (scratchDir) fs.rmSync(scratchDir, { recursive: true, force: true });
+ });
+
+ it("SQLMAINT-002: sends `VACUUM INTO ''` and leaves a readable copy at the path", async () => {
+ scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sqlite-maint-'));
+ const target = path.join(scratchDir, 'snap.db');
+ const execute = spyOnExecute();
+ try {
+ await maintenance.vacuumInto(target);
+ expect(execute.first()).toBe(`VACUUM INTO '${target}'`);
+ } finally {
+ execute.restore();
+ }
+ expect(holdsTable(target, 'users')).toBe(true);
+ });
+
+ it('SQLMAINT-003: doubles single quotes in the path', async () => {
+ scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), "sqlite-maint-o'brien-"));
+ const target = path.join(scratchDir, 'snap.db');
+ const execute = spyOnExecute();
+ try {
+ await maintenance.vacuumInto(target);
+ expect(execute.first()).toBe(`VACUUM INTO '${target.replaceAll("'", "''")}'`);
+ } finally {
+ execute.restore();
+ }
+ expect(holdsTable(target, 'users')).toBe(true);
+ });
+ });
+
+ it('SQLMAINT-004: both methods throw outside a request context (fail closed, as TrekRepository)', async () => {
+ const { MikroORM } = await import('@mikro-orm/core');
+ const fresh = await MikroORM.init({
+ entities: [],
+ driver: (await import('../../../../../src/db/orm-driver')).createBoundSqliteDriver(() => testDb),
+ dbName: ':memory:',
+ allowGlobalContext: false,
+ discovery: { warnWhenNoEntities: false },
+ });
+ try {
+ const unwrapped = new SqliteMaintenance(fresh.em);
+ await expect(unwrapped.walCheckpoint()).rejects.toThrow();
+ await expect(unwrapped.vacuumInto(path.join(os.tmpdir(), 'never.db'))).rejects.toThrow();
+ } finally {
+ await fresh.close(false);
+ }
+ });
+});
diff --git a/server/tests/unit/db/pre-migrate-snapshot.test.ts b/server/tests/unit/db/pre-migrate-snapshot.test.ts
index 500e36c92..a7fbf4fb2 100644
--- a/server/tests/unit/db/pre-migrate-snapshot.test.ts
+++ b/server/tests/unit/db/pre-migrate-snapshot.test.ts
@@ -44,7 +44,7 @@ const migrator = () => orm.config.getExtension('@mikro-orm/migrator') as Migrato
function envWith(db: Partial, isTest = false): () => AppEnv {
return () => {
const env = readEnv();
- return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } };
+ return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } as AppEnv['db'] };
};
}
diff --git a/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts b/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts
index 54b03ef32..bef91c610 100644
--- a/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts
+++ b/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts
@@ -6,6 +6,7 @@
* username)` — unlike `AssignmentsService.getParticipants` (AS15) — see the
* repository's own docstring.
*/
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { AssignmentParticipants } from '../../../../src/db/entities/AssignmentParticipants.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { AssignmentParticipantsRepository } from '../../../../src/db/repositories/AssignmentParticipants.repository';
@@ -27,7 +28,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
participants = t.repo(AssignmentParticipants);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/DayAssignments.repository.test.ts b/server/tests/unit/db/repositories/DayAssignments.repository.test.ts
index b383b2791..702baa7b2 100644
--- a/server/tests/unit/db/repositories/DayAssignments.repository.test.ts
+++ b/server/tests/unit/db/repositories/DayAssignments.repository.test.ts
@@ -3,6 +3,7 @@
* assignment-with-place projection contract (built here, Task 3 consumes it
* unchanged for AS1/AS3).
*/
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { DayAccommodations } from '../../../../src/db/entities/DayAccommodations.entity';
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
import { Places } from '../../../../src/db/entities/Places.entity';
@@ -35,7 +36,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
assignments = t.repo(DayAssignments);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/InviteTokens.repository.test.ts b/server/tests/unit/db/repositories/InviteTokens.repository.test.ts
index d8c28307b..2e4b92cbf 100644
--- a/server/tests/unit/db/repositories/InviteTokens.repository.test.ts
+++ b/server/tests/unit/db/repositories/InviteTokens.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { InviteTokens } from '../../../../src/db/entities/InviteTokens.entity';
import type { InviteTokensRepository } from '../../../../src/db/repositories/InviteTokens.repository';
import { withRequestContext } from '../../../../src/nest/database/request-context';
@@ -227,7 +228,7 @@ describe('InviteTokensRepository', () => {
it('INVREPO-017: incrementUsedCount inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => {
const { user: admin } = createUser(testDb, { username: 'invrepo-017-admin' });
const invite = createInviteToken(testDb, { token: 'invrepo-017', max_uses: 5, created_by: admin.id });
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await withRequestContext(t.orm, async () => {
// Projection A — wide, full row, carries used_count.
diff --git a/server/tests/unit/db/repositories/MaintenanceRepository.test.ts b/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
index 15fb2c0aa..414b28e96 100644
--- a/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
+++ b/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
@@ -1,12 +1,9 @@
import { MaintenanceRepository } from '../../../../src/db/repositories/MaintenanceRepository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
+import { TableNotFoundException } from '@mikro-orm/core';
-import Database from 'better-sqlite3';
-import fs from 'node:fs';
-import os from 'node:os';
-import path from 'node:path';
-import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
@@ -22,61 +19,18 @@ afterAll(async () => {
});
describe('MaintenanceRepository', () => {
- it('MAINTREPO-001: walCheckpoint renders PRAGMA wal_checkpoint(TRUNCATE) — pinned text, equivalent to the legacy raw call', async () => {
- // Rendered-SQL-equivalence: the exact text a direct statement on the
- // handle executes without throwing, string-for-string what the legacy
- // `db.exec('PRAGMA wal_checkpoint(TRUNCATE)')` issued.
- const RENDERED = 'PRAGMA wal_checkpoint(TRUNCATE)';
- // test-sql-allow: the pragma text itself is under test, run as the raw statement it renders to.
- expect(() => testDb.prepare(RENDERED).run()).not.toThrow();
- await expect(maintenance.walCheckpoint()).resolves.toBeUndefined();
+ it('MAINTREPO-001: ping sends SELECT 1 and resolves while the database answers', async () => {
+ const spy = vi.spyOn(t.em.getConnection(), 'execute');
+ try {
+ await expect(maintenance.ping()).resolves.toBeUndefined();
+ // The first statement sent; the ORM may read its own metadata afterwards.
+ expect(spy.mock.calls[0]?.[0]).toBe('SELECT 1');
+ } finally {
+ spy.mockRestore();
+ }
});
- describe('vacuumInto', () => {
- let scratchPath: string;
-
- afterEach(() => {
- if (scratchPath && fs.existsSync(scratchPath)) fs.rmSync(scratchPath, { force: true });
- });
-
- it("MAINTREPO-002: vacuumInto renders `VACUUM INTO ''` — pinned text, produces a readable snapshot at the given path", async () => {
- scratchPath = path.join(os.tmpdir(), `maintrepo-vacuum-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
- await maintenance.vacuumInto(scratchPath);
- expect(fs.existsSync(scratchPath)).toBe(true);
-
- const snap = new Database(scratchPath, { readonly: true });
- try {
- // test-sql-allow: a snapshot file no ORM is bound to, read through sqlite_master.
- const row = snap.prepare('SELECT name FROM sqlite_master WHERE type = ? AND name = ?').get('table', 'users');
- expect(row).toBeTruthy();
- } finally {
- snap.close();
- }
- });
-
- it('MAINTREPO-003: single quotes in the path are escaped by doubling, matching the legacy `dbSnap.replaceAll("\'", "\'\'")` shape', async () => {
- const dirWithQuote = path.join(os.tmpdir(), `maintrepo-o'brien-${Date.now()}`);
- fs.mkdirSync(dirWithQuote, { recursive: true });
- scratchPath = path.join(dirWithQuote, 'snap.db');
- try {
- await maintenance.vacuumInto(scratchPath);
- expect(fs.existsSync(scratchPath)).toBe(true);
-
- // Rendered-SQL-equivalence: the SAME escaped text, run directly
- // against another target path, succeeds identically.
- const directPath = path.join(dirWithQuote, 'snap-direct.db');
- const escaped = directPath.replaceAll("'", "''");
- // test-sql-allow: the VACUUM INTO text itself is under test, run as the raw statement it renders to.
- expect(() => testDb.prepare(`VACUUM INTO '${escaped}'`).run()).not.toThrow();
- expect(fs.existsSync(directPath)).toBe(true);
- fs.rmSync(directPath, { force: true });
- } finally {
- fs.rmSync(dirWithQuote, { recursive: true, force: true });
- }
- });
- });
-
- it('MAINTREPO-004: both methods throw outside a request context (fail-closed, matching TrekRepository)', async () => {
+ it('MAINTREPO-004: ping throws outside a request context (fail-closed, matching TrekRepository)', async () => {
const { MikroORM } = await import('@mikro-orm/core');
const fresh = await MikroORM.init({
entities: [],
@@ -87,9 +41,23 @@ describe('MaintenanceRepository', () => {
});
try {
const unwrapped = new MaintenanceRepository(fresh.em);
- await expect(unwrapped.walCheckpoint()).rejects.toThrow();
+ await expect(unwrapped.ping()).rejects.toThrow();
} finally {
await fresh.close(false);
}
});
+
+ it('MAINTREPO-005: deletePluginUserData skips an absent table and rethrows any other failure', async () => {
+ const spy = vi.spyOn(t.em.getConnection(), 'execute');
+ try {
+ // The first statement is the user_config delete, so the one-shot rejection lands on it.
+ spy.mockRejectedValueOnce(new TableNotFoundException(new Error('no such table: plugin_user_config')));
+ await expect(maintenance.deletePluginUserData(1)).resolves.toBeUndefined();
+
+ spy.mockRejectedValueOnce(new Error('connection lost'));
+ await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost');
+ } finally {
+ spy.mockRestore();
+ }
+ });
});
diff --git a/server/tests/unit/db/repositories/McpTokens.repository.test.ts b/server/tests/unit/db/repositories/McpTokens.repository.test.ts
index df1d3b2a8..69e6c746e 100644
--- a/server/tests/unit/db/repositories/McpTokens.repository.test.ts
+++ b/server/tests/unit/db/repositories/McpTokens.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { McpTokens } from '../../../../src/db/entities/McpTokens.entity';
import type { McpTokensRepository } from '../../../../src/db/repositories/McpTokens.repository';
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
@@ -449,7 +450,7 @@ describe('McpTokensRepository', () => {
// read, nativeUpdate, flush, still correct — is still worth pinning.
const { user } = createUser(testDb);
const created = createMcpToken(testDb, user.id, { name: 'stays-original' });
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await uow.transactional(async () => {
const basic = await tokens.findBasic(created.id);
@@ -516,7 +517,7 @@ describe('McpTokensRepository', () => {
it('MCPTOKREPO-023: projection A then projection B then a nativeUpdate on an A-only column, inside uow.transactional — the nativeUpdate survives the closing flush', async () => {
const { user } = createUser(testDb);
const created = createMcpToken(testDb, user.id, { name: 'stays-original', scope_mode: 'all' });
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await uow.transactional(async () => {
// Projection A: selects `scope_mode` among its columns.
diff --git a/server/tests/unit/db/repositories/OauthClients.repository.test.ts b/server/tests/unit/db/repositories/OauthClients.repository.test.ts
index 98accbac1..82e495b26 100644
--- a/server/tests/unit/db/repositories/OauthClients.repository.test.ts
+++ b/server/tests/unit/db/repositories/OauthClients.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity';
import type { OauthClientsRepository } from '../../../../src/db/repositories/OauthClients.repository';
@@ -20,7 +21,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
clients = t.repo(OauthClients);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(async () => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/OauthConsents.repository.test.ts b/server/tests/unit/db/repositories/OauthConsents.repository.test.ts
index 1d54c197b..792574db2 100644
--- a/server/tests/unit/db/repositories/OauthConsents.repository.test.ts
+++ b/server/tests/unit/db/repositories/OauthConsents.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity';
import type { OauthConsentsRepository } from '../../../../src/db/repositories/OauthConsents.repository';
@@ -19,7 +20,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
consents = t.repo(OauthConsents);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/OauthTokens.repository.test.ts b/server/tests/unit/db/repositories/OauthTokens.repository.test.ts
index b2eb73c88..545705e23 100644
--- a/server/tests/unit/db/repositories/OauthTokens.repository.test.ts
+++ b/server/tests/unit/db/repositories/OauthTokens.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { OauthClients } from '../../../../src/db/entities/OauthClients.entity';
import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity';
import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity';
@@ -21,7 +22,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
tokens = t.repo(OauthTokens);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(async () => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts b/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts
index 97fb4609a..6e65e9154 100644
--- a/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts
+++ b/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { PasswordResetTokens } from '../../../../src/db/entities/PasswordResetTokens.entity';
import type { PasswordResetTokensRepository } from '../../../../src/db/repositories/PasswordResetTokens.repository';
import { dbNow } from '../../../../src/db/types';
@@ -209,7 +210,7 @@ describe('PasswordResetTokensRepository', () => {
it('PWDRESETREPO-011: findByTokenHash then markConsumed inside uow.transactional survives the closing flush (cannot fail by construction — see PWDRESETREPO-012 for the mutation-proved guard)', async () => {
const { user } = createUser(testDb);
const id = await seedToken(user.id, hash('survives'));
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await uow.transactional(async () => {
const found = await tokens.findByTokenHash(hash('survives'));
@@ -223,7 +224,7 @@ describe('PasswordResetTokensRepository', () => {
it('PWDRESETREPO-012: findByTokenHash is the FIRST, wider read (it carries consumed_at); a narrower managed read follows; markConsumed still lands', async () => {
const { user } = createUser(testDb);
const id = await seedToken(user.id, hash('dshape'));
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await uow.transactional(async () => {
await tokens.findByTokenHash(hash('dshape')); // projection A — WIDE (carries consumed_at)
@@ -241,7 +242,7 @@ describe('PasswordResetTokensRepository', () => {
it('PWDRESETREPO-013: same D-shape guard for consumeAllLiveForUser', async () => {
const { user } = createUser(testDb);
const id = await seedToken(user.id, hash('dshape-consume-all'));
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await uow.transactional(async () => {
await tokens.findByTokenHash(hash('dshape-consume-all')); // projection A — WIDE (carries consumed_at)
diff --git a/server/tests/unit/db/repositories/Places.repository.test.ts b/server/tests/unit/db/repositories/Places.repository.test.ts
index e75f963bb..363e1f964 100644
--- a/server/tests/unit/db/repositories/Places.repository.test.ts
+++ b/server/tests/unit/db/repositories/Places.repository.test.ts
@@ -640,7 +640,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
const inCat = createPlace(testDb, trip.id, { category_id: cat.id });
const outCat = createPlace(testDb, trip.id);
await deleteRows(t.orm, Categories, { id: { $ne: cat.id } });
- const rows = await places.listForTrip(String(trip.id), { category: String(cat.id) });
+ const rows = await places.listForTrip(String(trip.id), { category: cat.id });
expect(rows.map((r) => r.id)).toEqual([inCat.id]);
void outCat;
});
@@ -652,7 +652,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
const tagged = createPlace(testDb, trip.id, { name: 'Tagged' });
const untagged = createPlace(testDb, trip.id, { name: 'Untagged' });
await tagPlace(t.orm, tagged.id, [tag.id]);
- const rows = await places.listForTrip(String(trip.id), { tag: String(tag.id) });
+ const rows = await places.listForTrip(String(trip.id), { tag: tag.id });
expect(rows.map((r) => r.id)).toEqual([tagged.id]);
void untagged;
});
@@ -684,7 +684,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
await tagPlace(t.orm, other.id, [tag.id]);
// `other` is left unassigned, so `assignment: 'assigned'` alone should exclude it.
const rows = await places.listForTrip(String(trip.id), {
- searchPattern: '%Central Park%', category: String(cat.id), tag: String(tag.id), assignment: 'assigned',
+ searchPattern: '%Central Park%', category: cat.id, tag: tag.id, assignment: 'assigned',
});
expect(rows.map((r) => r.id)).toEqual([winner.id]);
});
@@ -703,7 +703,7 @@ describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 filter combinations', () => {
function legacyListForTrip(
tripId: string,
- filters: { searchPattern?: string; category?: string; tag?: string; assignment?: 'all' | 'unassigned' | 'assigned' },
+ filters: { searchPattern?: string; category?: number | string; tag?: number | string; assignment?: 'all' | 'unassigned' | 'assigned' },
): unknown[] {
let query = `
SELECT DISTINCT p.*, t.place_id AS tour_place_id, c.name as category_name, c.color as category_color, c.icon as category_icon,
@@ -767,8 +767,8 @@ describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 fil
createPlace(testDb, otherTrip.id, { name: 'Central Perk' });
const searchOptions: (string | undefined)[] = [undefined, '%Central%'];
- const categoryOptions: (string | undefined)[] = [undefined, String(cat.id)];
- const tagOptions: (string | undefined)[] = [undefined, String(tag.id)];
+ const categoryOptions: (number | undefined)[] = [undefined, cat.id];
+ const tagOptions: (number | undefined)[] = [undefined, tag.id];
const assignmentOptions: (('all' | 'unassigned' | 'assigned') | undefined)[] = [undefined, 'all', 'assigned', 'unassigned'];
let combinations = 0;
diff --git a/server/tests/unit/db/repositories/ReservationDayPositions.repository.test.ts b/server/tests/unit/db/repositories/ReservationDayPositions.repository.test.ts
index c2d0e83dc..b3df54695 100644
--- a/server/tests/unit/db/repositories/ReservationDayPositions.repository.test.ts
+++ b/server/tests/unit/db/repositories/ReservationDayPositions.repository.test.ts
@@ -2,14 +2,15 @@
* ReservationDayPositionsRepository — parity test (Plan 3d Task 7 whole-plan
* review, item 9/"missing parity tests": this repository had no standalone
* test file). ONE seeded world, one `toEqual()` test for the
- * read method (RS19); RS31 (`upsertScoped`, a write) already has coverage
- * through `reservations.service.test.ts`'s `updatePositions` cases.
+ * read method (RS19); RS31 (`upsertScoped`, a write) has coverage through
+ * `reservations.service.test.ts`'s `updatePositions` cases and, for the
+ * overwrite and the trip scoping its ON CONFLICT clause holds, below.
*/
import { ReservationDayPositions } from '../../../../src/db/entities/ReservationDayPositions.entity';
import type { ReservationDayPositionsRepository } from '../../../../src/db/repositories/ReservationDayPositions.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createDay, createReservation, createTrip, createUser } from '../../../helpers/factories';
-import { insertRow } from '../../../helpers/factories/rows';
+import { findRows, insertRow } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
@@ -86,3 +87,30 @@ describe('ReservationDayPositionsRepository — fully seeded world', () => {
expect(await repo.listForReservation(999999)).toEqual([]);
});
});
+
+describe('ReservationDayPositionsRepository.upsertScoped', () => {
+ it('RDP-UPSERT-001: a second write to the same reservation and day overwrites its position', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const reservation = createReservation(testDb, trip.id, { day_id: day.id });
+
+ await repo.upsertScoped(trip.id, reservation.id, day.id, 1.5);
+ await repo.upsertScoped(trip.id, reservation.id, day.id, 4);
+
+ const rows = await findRows(t.orm, ReservationDayPositions, { reservation_id: reservation.id });
+ expect(rows.map((r) => [r.day_id, r.position])).toEqual([[day.id, 4]]);
+ });
+
+ it('RDP-UPSERT-002: a reservation of another trip writes no row', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const otherTrip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const foreign = createReservation(testDb, otherTrip.id);
+
+ await repo.upsertScoped(trip.id, foreign.id, day.id, 2);
+
+ expect(await findRows(t.orm, ReservationDayPositions, { reservation_id: foreign.id })).toEqual([]);
+ });
+});
diff --git a/server/tests/unit/db/repositories/Tags.repository.test.ts b/server/tests/unit/db/repositories/Tags.repository.test.ts
index b2cf5b406..41a39575c 100644
--- a/server/tests/unit/db/repositories/Tags.repository.test.ts
+++ b/server/tests/unit/db/repositories/Tags.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Tags } from '../../../../src/db/entities/Tags.entity';
import type { TagsRepository } from '../../../../src/db/repositories/Tags.repository';
import { withRequestContext } from '../../../../src/nest/database/request-context';
@@ -19,7 +20,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
tags = t.repo(Tags);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/Tours.repository.test.ts b/server/tests/unit/db/repositories/Tours.repository.test.ts
index 9be5e48b1..bf98634e6 100644
--- a/server/tests/unit/db/repositories/Tours.repository.test.ts
+++ b/server/tests/unit/db/repositories/Tours.repository.test.ts
@@ -4,6 +4,7 @@
* days hold (MDT-02), the trip copy read and write
* (TO6/TO8), and the create/edit writes (TO7/TO9).
*/
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
import { Places } from '../../../../src/db/entities/Places.entity';
import { TourPackingLinks } from '../../../../src/db/entities/TourPackingLinks.entity';
@@ -332,7 +333,9 @@ describe('ToursRepository stages on days (MDT-02)', () => {
createTour(testDb, other.id);
const otherStage = createDayAssignment(testDb, days[2].id, other.id, { tour_stage_key: 'stage-two' });
- const gone = await new UnitOfWork(t.em).transactional(() => repo.deleteTourStagesNotIn(place.id, ['stage-one']));
+ const gone = await new UnitOfWork(t.em, new SqliteEngine()).transactional(() =>
+ repo.deleteTourStagesNotIn(place.id, ['stage-one']),
+ );
expect(gone).toEqual([{ id: second.id, day_id: days[1].id }]);
const left = (await findRows(t, DayAssignments, {}, { id: 'asc' })).map((row) => row.id);
expect(left).toEqual([first.id, whole.id, otherStage.id]);
diff --git a/server/tests/unit/db/repositories/Trips.repository.test.ts b/server/tests/unit/db/repositories/Trips.repository.test.ts
index 754b86aaf..fab7b5aec 100644
--- a/server/tests/unit/db/repositories/Trips.repository.test.ts
+++ b/server/tests/unit/db/repositories/Trips.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Trips } from '../../../../src/db/entities/Trips.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { TripsRepository } from '../../../../src/db/repositories/Trips.repository';
@@ -20,7 +21,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
trips = t.repo(Trips);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts b/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts
index bbcd63c0a..09c3ab770 100644
--- a/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts
+++ b/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { WebauthnCredentials } from '../../../../src/db/entities/WebauthnCredentials.entity';
import type { WebauthnCredentialsRepository } from '../../../../src/db/repositories/WebauthnCredentials.repository';
import { withRequestContext } from '../../../../src/nest/database/request-context';
@@ -317,7 +318,7 @@ describe('WebauthnCredentialsRepository', () => {
it('WEBAUTHN-CRED-REPO-017: a nativeUpdate inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => {
const { user } = createUser(testDb);
const cred = await insertCredential(user.id, { counter: 1 });
- const uow = new UnitOfWork(t.em);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
await withRequestContext(t.orm, async () => {
// Projection A — wide, full row, carries `counter` (PK9's shape: findByCredentialId).
diff --git a/server/tests/unit/db/repositories/identity-map-writeback.test.ts b/server/tests/unit/db/repositories/identity-map-writeback.test.ts
index 7faa636ce..b70c9e0f1 100644
--- a/server/tests/unit/db/repositories/identity-map-writeback.test.ts
+++ b/server/tests/unit/db/repositories/identity-map-writeback.test.ts
@@ -30,6 +30,7 @@
* no `disableIdentityMap`) makes IMWB-001 fail with the stale write-back this
* test is built to catch.
*/
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
import { withRequestContext } from '../../../../src/nest/database/request-context';
@@ -50,7 +51,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
users = t.repo(Users);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/db/repositories/trek-repository.test.ts b/server/tests/unit/db/repositories/trek-repository.test.ts
index 1a480b0d0..beb1f96b5 100644
--- a/server/tests/unit/db/repositories/trek-repository.test.ts
+++ b/server/tests/unit/db/repositories/trek-repository.test.ts
@@ -36,6 +36,7 @@
* backwards until this task; `OAUTHTOKREPO-029` (`OauthTokens.repository
* .test.ts`) already had it right and is the shape to copy.
*/
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Users } from '../../../../src/db/entities/Users.entity';
import { WebauthnChallenges } from '../../../../src/db/entities/WebauthnChallenges.entity';
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
@@ -66,7 +67,7 @@ beforeAll(async () => {
t = await createTestOrm(testDb, { allowGlobalContext: false });
users = t.repo(Users);
challenges = t.repo(WebauthnChallenges);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
// `t.clear()` is deliberately NOT called here (unlike most repository test
// files): `EntityManager.clear()` itself calls the VALIDATING `getContext()`
diff --git a/server/tests/unit/db/restore-migrates-forward.test.ts b/server/tests/unit/db/restore-migrates-forward.test.ts
index 214863577..aebbf7d19 100644
--- a/server/tests/unit/db/restore-migrates-forward.test.ts
+++ b/server/tests/unit/db/restore-migrates-forward.test.ts
@@ -71,10 +71,10 @@ describe('a restored database is migrated forward', () => {
// What reinitialize() does after the file has been swapped in.
const restored = await ormFor(dbFile);
- await runSchemaBootstrap(restored);
+ await runSchemaBootstrap(restored, { snapshot: true });
// Twice: a restore can follow a boot that already bootstrapped, and every
// seeder is written to tolerate that.
- await runSchemaBootstrap(restored);
+ await runSchemaBootstrap(restored, { snapshot: true });
await restored.close(true);
const after = new Database(dbFile);
diff --git a/server/tests/unit/db/transaction-yield.test.ts b/server/tests/unit/db/transaction-yield.test.ts
index 069102540..818700bfc 100644
--- a/server/tests/unit/db/transaction-yield.test.ts
+++ b/server/tests/unit/db/transaction-yield.test.ts
@@ -35,6 +35,7 @@
* docstring: NESTED propagation) rather than the independent, connection-mutex-
* queued transaction two unrelated concurrent requests actually are.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Trips } from '../../../src/db/entities/Trips.entity';
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
import { withRequestContext } from '../../../src/nest/database/request-context';
@@ -57,7 +58,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
trips = t.repo(Trips);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/demo/demo-reset-path.test.ts b/server/tests/unit/demo/demo-reset-path.test.ts
index 62f94b047..99af90f70 100644
--- a/server/tests/unit/demo/demo-reset-path.test.ts
+++ b/server/tests/unit/demo/demo-reset-path.test.ts
@@ -11,7 +11,7 @@
* cases assert is which file each step names.
*
* The port is the real `SqliteDatabaseBackup` over a lifecycle stand-in (the
- * file, close, reopen) and a MaintenanceRepository stand-in (checkpoint,
+ * file, close, reopen) and a SqliteMaintenance stand-in (checkpoint,
* VACUUM INTO). The file system is spied rather than written to, because the
* baseline path is fixed at data/travel-baseline.db and a developer running a
* demo instance has a real one sitting there.
@@ -21,7 +21,7 @@
* `withRequestContext` over a real (empty) test database: the reads find
* nothing and the restore-writes are skipped.
*/
-import type { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
+import type { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
import { resetDemoUser, saveBaseline } from '../../../src/demo/demo-reset';
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup';
import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface';
@@ -65,7 +65,7 @@ describe('demo-reset DB path', () => {
maintenance = { walCheckpoint: vi.fn(async () => {}), vacuumInto: vi.fn(async () => {}) };
database = new SqliteDatabaseBackup(
lifecycle as unknown as DatabaseLifecycle,
- maintenance as unknown as MaintenanceRepository,
+ maintenance as unknown as SqliteMaintenance,
);
});
diff --git a/server/tests/unit/eslint-rules/restricted-syntax-selectors.test.ts b/server/tests/unit/eslint-rules/restricted-syntax-selectors.test.ts
index 9a258b1c6..a26d99f24 100644
--- a/server/tests/unit/eslint-rules/restricted-syntax-selectors.test.ts
+++ b/server/tests/unit/eslint-rules/restricted-syntax-selectors.test.ts
@@ -140,7 +140,7 @@ declare const repo: { run(sql: string, params?: unknown[]): Promise; ge
// text slipping in under the exemption, a lower one means the pin (and at 0
// the ignores entry) has to follow.
const EXEMPT_SQL_STRINGS: Record = {
- 'src/db/repositories/MaintenanceRepository.ts': 4,
+ 'src/db/repositories/MaintenanceRepository.ts': 2,
'src/db/seeders/AdminSeeder.ts': 2,
'src/db/seeders/CategorySeeder.ts': 2,
};
diff --git a/server/tests/unit/nest-rpc/plugin-guards.test.ts b/server/tests/unit/nest-rpc/plugin-guards.test.ts
index d0805de55..c17da3e23 100644
--- a/server/tests/unit/nest-rpc/plugin-guards.test.ts
+++ b/server/tests/unit/nest-rpc/plugin-guards.test.ts
@@ -4,6 +4,7 @@
* the legacy router, because rpc-host.test.ts asserts them and shipped plugins read
* them, so these tests pin the exact strings rather than just the refusal.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import { Users } from '../../../src/db/entities/Users.entity';
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
@@ -205,7 +206,7 @@ describe('PluginGuards — canEditAs/canCreateAs against a real UsersRepository
beforeEach(async () => {
testDb = createSnapshotTestDb();
orm = await createTestOrm(testDb);
- const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em));
+ const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em, new SqliteEngine()));
const db = {
findAccessible: vi.fn(async (tripId: number) => (tripId === 1 ? { id: 1, user_id: ownerId } : undefined)),
} as unknown as TripsRepository;
diff --git a/server/tests/unit/nest/addons.service.test.ts b/server/tests/unit/nest/addons.service.test.ts
index 0d0afabb9..e21d53618 100644
--- a/server/tests/unit/nest/addons.service.test.ts
+++ b/server/tests/unit/nest/addons.service.test.ts
@@ -21,6 +21,7 @@
* filter itself (the disabled row is excluded) alongside the boolean coercion
* of the enabled row that remains.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Addons } from '../../../src/db/entities/Addons.entity';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import { PhotoProviderFields } from '../../../src/db/entities/PhotoProviderFields.entity';
@@ -195,7 +196,7 @@ beforeEach(() => {
photoProviderFieldsRepo,
appSettingsRepo,
usersRepo,
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
);
});
diff --git a/server/tests/unit/nest/backup.impl.test.ts b/server/tests/unit/nest/backup.impl.test.ts
index 1eb858602..13c8ea386 100644
--- a/server/tests/unit/nest/backup.impl.test.ts
+++ b/server/tests/unit/nest/backup.impl.test.ts
@@ -2,7 +2,7 @@
* Unit tests for backupService.
* Covers BACKUP-031 to BACKUP-060.
*/
-import type { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
+import type { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
import {
formatSize,
parseIntField,
@@ -78,9 +78,9 @@ const dbMock = vi.hoisted(() => ({
}));
// The database port's SQLite implementation is built for real below with this
-// stand-in for the MaintenanceRepository Nest injects into it: the WAL
+// stand-in for the SqliteMaintenance Nest injects into it: the WAL
// checkpoint and the VACUUM INTO snapshot are what a backup and a restore ask
-// of it. The real statements are MaintenanceRepository.test.ts's coverage.
+// of it. The real statements are sqlite-maintenance.test.ts's coverage.
const maintenanceRepoMock = vi.hoisted(() => ({
walCheckpoint: vi.fn().mockResolvedValue(undefined),
vacuumInto: vi.fn().mockResolvedValue(undefined),
@@ -121,7 +121,7 @@ const lifecycleStub = {
function deps(storage: StorageService) {
return {
storage,
- database: new SqliteDatabaseBackup(lifecycleStub, maintenanceRepoMock as unknown as MaintenanceRepository),
+ database: new SqliteDatabaseBackup(lifecycleStub, maintenanceRepoMock as unknown as SqliteMaintenance),
};
}
diff --git a/server/tests/unit/nest/backup/database-swap.test.ts b/server/tests/unit/nest/backup/database-swap.test.ts
new file mode 100644
index 000000000..8b88c342d
--- /dev/null
+++ b/server/tests/unit/nest/backup/database-swap.test.ts
@@ -0,0 +1,85 @@
+/**
+ * The restore step the two backup ports share (`database-swap.ts`): the swap,
+ * then a reopen that always runs, and how the two failures surface.
+ */
+import { keepRestoreCopy, replaceThenReopen } from '../../../../src/nest/backup/database-swap';
+import { DatabaseConnectionLostError } from '../../../../src/nest/database/database-backup.interface';
+
+import { describe, expect, it, vi } from 'vitest';
+
+const ok = async () => undefined;
+const fails = (error: unknown) => async () => {
+ throw error;
+};
+
+describe('replaceThenReopen', () => {
+ it('SWAP-001: a swap that lands and a reopen that works resolves with no reopen error', async () => {
+ const swap = vi.fn(ok);
+ const reopen = vi.fn(ok);
+
+ await expect(replaceThenReopen(swap, reopen)).resolves.toEqual({ reopenError: null });
+ expect(swap).toHaveBeenCalledTimes(1);
+ expect(reopen).toHaveBeenCalledTimes(1);
+ });
+
+ it('SWAP-002: a swap that lands and a reopen that fails resolves with the reopen error, so the caller says restart', async () => {
+ const lost = new Error('reconnect failed');
+
+ await expect(replaceThenReopen(ok, fails(lost))).resolves.toEqual({ reopenError: lost });
+ });
+
+ it('SWAP-003: a swap that fails still reopens, and its own error is rethrown', async () => {
+ const reopen = vi.fn(ok);
+ const swapError = new Error('rename failed');
+
+ await expect(replaceThenReopen(fails(swapError), reopen)).rejects.toBe(swapError);
+ expect(reopen).toHaveBeenCalledTimes(1);
+ });
+
+ it('SWAP-004: a swap and a reopen that both fail go up together, as DatabaseConnectionLostError', async () => {
+ const swapError = new Error('rename failed');
+ const reopenError = new Error('reconnect failed');
+
+ const thrown = await replaceThenReopen(fails(swapError), fails(reopenError)).catch((err: unknown) => err);
+
+ expect(thrown).toBeInstanceOf(DatabaseConnectionLostError);
+ expect((thrown as DatabaseConnectionLostError).swapError).toBe(swapError);
+ expect((thrown as DatabaseConnectionLostError).cause).toBe(reopenError);
+ });
+
+ it('SWAP-005: a step that throws undefined still counts as failed', async () => {
+ // A reopen that throws undefined is a failure: with a swap that also failed, the two go up as the lost connection.
+ await expect(replaceThenReopen(fails(new Error('rename failed')), fails(undefined))).rejects.toBeInstanceOf(
+ DatabaseConnectionLostError,
+ );
+ // A swap that throws undefined rethrows undefined, the same value the step threw.
+ await expect(replaceThenReopen(fails(undefined), ok)).rejects.toBeUndefined();
+ });
+
+ it('SWAP-006: a synchronous swap that throws is caught like an async one', async () => {
+ const swapError = new Error('copy failed');
+ const reopen = vi.fn(ok);
+
+ await expect(
+ replaceThenReopen(() => {
+ throw swapError;
+ }, reopen),
+ ).rejects.toBe(swapError);
+ expect(reopen).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('keepRestoreCopy', () => {
+ it('SWAP-007: resolves with the target once the copy is written', async () => {
+ await expect(keepRestoreCopy('/data/pre-restore-1.db', ok)).resolves.toBe('/data/pre-restore-1.db');
+ });
+
+ it('SWAP-008: a copy that cannot be written resolves with null, so the restore goes on', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
+ try {
+ await expect(keepRestoreCopy('/data/pre-restore-2.db', fails(new Error('disk full')))).resolves.toBeNull();
+ } finally {
+ warn.mockRestore();
+ }
+ });
+});
diff --git a/server/tests/unit/nest/boot-restore.test.ts b/server/tests/unit/nest/boot-restore.test.ts
index c3041e721..f4232e665 100644
--- a/server/tests/unit/nest/boot-restore.test.ts
+++ b/server/tests/unit/nest/boot-restore.test.ts
@@ -6,7 +6,16 @@
* calls this file makes. The plugin staging is the one seam stubbed, because
* it moves trees under the plugin roots of the machine running the tests.
*/
+import { knownMigrationNames } from '../../../src/db/known-migrations';
import { BootRestoreError, restoreOnFirstBoot } from '../../../src/nest/backup/boot-restore';
+import {
+ answersSchema,
+ answersTables,
+ installFakeTool,
+ listsMigrations,
+ touches,
+ writesRestrictedScript,
+} from '../../helpers/fake-pg-tools';
import archiver from 'archiver';
import Database from 'better-sqlite3';
@@ -125,6 +134,62 @@ describe('restoreOnFirstBoot', () => {
expect(fs.existsSync(dbFile)).toBe(false);
});
+ describe('on Postgres', () => {
+ /**
+ * Stand-in client tools (tests/helpers/fake-pg-tools.ts): psql lists
+ * `tables` and marks `loaded` when it runs a restore; pg_restore lists a
+ * known migration as the dump's history, or writes a restricted script.
+ * Their arguments are pinned in pg-tools.test.ts.
+ */
+ const postgresPlan = (archivePath: string, tables: string[] = []) => {
+ const bin = path.join(root, 'bin');
+ fs.mkdirSync(bin, { recursive: true });
+ installFakeTool(bin, 'psql', {
+ snippets: [answersSchema(), answersTables(tables), touches(path.join(root, 'loaded'))],
+ });
+ const migration = [...knownMigrationNames()][0];
+ installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([migration]), writesRestrictedScript()] });
+ return {
+ archive: archivePath,
+ dbFile,
+ dataDir,
+ reclaimResetFile,
+ postgres: { url: 'postgres://trek:pw@db/trek', binDir: bin },
+ };
+ };
+ const loaded = () => fs.existsSync(path.join(root, 'loaded'));
+
+ it('BOOT-RESTORE-004b: a database holding tables is left alone, so a restart never rolls back', async () => {
+ const plan = postgresPlan(await archive({ 'travel.pgdump': 'dump' }), ['users', 'trips']);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ expect(await restoreOnFirstBoot(plan)).toEqual({ restored: false, reason: 'database-exists' });
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('the Postgres database (2 table(s))'));
+ expect(loaded()).toBe(false);
+ });
+
+ it("BOOT-RESTORE-004c: an empty database is loaded from the archive's dump, with its key and uploads", async () => {
+ const plan = postgresPlan(
+ await archive({ 'travel.pgdump': 'dump', '.encryption_key': 'b'.repeat(64), 'uploads/a.jpg': 'jpeg' }),
+ );
+ const out = await restoreOnFirstBoot(plan);
+ expect(out.restored).toBe(true);
+ expect(loaded()).toBe(true);
+ expect(fs.readFileSync(path.join(dataDir, '.encryption_key'), 'utf8')).toBe('b'.repeat(64));
+ expect(fs.existsSync(reclaimResetFile)).toBe(true);
+ expect(fs.existsSync(dbFile)).toBe(false);
+ });
+
+ it('BOOT-RESTORE-004d: a SQLite backup is refused before anything is loaded, leaving no scratch', async () => {
+ const plan = postgresPlan(await goodArchive());
+ await expect(restoreOnFirstBoot(plan)).rejects.toThrow(
+ 'RESTORE_FROM_BACKUP: This backup holds a SQLite database, which cannot be restored into a Postgres database.',
+ );
+ expect(loaded()).toBe(false);
+ expect(fs.existsSync(reclaimResetFile)).toBe(false);
+ expect(fs.readdirSync(dataDir).filter((f) => f.startsWith('restore-boot-'))).toEqual([]);
+ });
+ });
+
it('BOOT-RESTORE-005: an archive without a database is refused and leaves no scratch behind', async () => {
const zipPath = await archive({ 'notes.txt': 'hello' });
await expect(restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir, reclaimResetFile })).rejects.toThrow(
diff --git a/server/tests/unit/nest/database/database-engine.module.test.ts b/server/tests/unit/nest/database/database-engine.module.test.ts
new file mode 100644
index 000000000..5164588ff
--- /dev/null
+++ b/server/tests/unit/nest/database/database-engine.module.test.ts
@@ -0,0 +1,29 @@
+/**
+ * DatabaseEngineModule: one engine per container, shared by every injector.
+ */
+import {
+ DatabaseEngineModule,
+ DATABASE_ENGINE,
+ DATABASE_SETTINGS,
+} from '../../../../src/nest/database/database-engine.module';
+import { Test } from '@nestjs/testing';
+
+import { describe, expect, it } from 'vitest';
+
+describe('DatabaseEngineModule', () => {
+ it('ENGINE-DI-001: provides one engine instance, so state an engine keeps is the same wherever it is injected', async () => {
+ const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
+
+ const engine = moduleRef.get(DATABASE_ENGINE);
+ expect(moduleRef.get(DATABASE_ENGINE)).toBe(engine);
+ expect(engine.id).toBe('sqlite');
+ });
+
+ it('ENGINE-DI-002: the settings are read from the environment once and the engine is resolved from them', async () => {
+ const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
+
+ const settings = moduleRef.get(DATABASE_SETTINGS);
+ expect(settings.engine).toBe('sqlite');
+ expect(moduleRef.get(DATABASE_SETTINGS)).toBe(settings);
+ });
+});
diff --git a/server/tests/unit/nest/database/database-lifecycle.service.test.ts b/server/tests/unit/nest/database/database-lifecycle.service.test.ts
index 5fb2c2ff2..e6ec90e50 100644
--- a/server/tests/unit/nest/database/database-lifecycle.service.test.ts
+++ b/server/tests/unit/nest/database/database-lifecycle.service.test.ts
@@ -7,6 +7,8 @@
* baseline depends on) and that the ORM is rebuilt around a reopened handle.
* The real open, migrate and reopen run in tests/integration/database-lifecycle.test.ts.
*/
+import { PostgresEngine } from '../../../../src/db/engine/postgres/postgres-engine';
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { DatabaseLifecycle } from '../../../../src/nest/database/database-lifecycle.service';
import type { MikroORM } from '@mikro-orm/core';
@@ -15,6 +17,9 @@ import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+/** The settings a Postgres process would read from TREK_DB_URL, given directly rather than through the environment. */
+const POSTGRES = { engine: 'postgres' as const, url: 'postgres://trek:pw@db/trek', poolMax: 10 };
+
const { dbMock, ormMock, calls } = vi.hoisted(() => {
const calls: string[] = [];
return {
@@ -66,15 +71,15 @@ describe('DatabaseLifecycle', () => {
it('DBLIFE-001: open() opens the connection, binds the ORM to later swaps, then brings the schema to head', async () => {
const { orm } = ormStub();
- await new DatabaseLifecycle(orm).open();
+ await new DatabaseLifecycle(orm, new SqliteEngine()).open();
- expect(calls).toEqual(['getRawConnection', 'registerReinitializeHook', 'runSchemaBootstrap:{}']);
- expect(ormMock.runSchemaBootstrap).toHaveBeenCalledWith(orm);
+ expect(calls).toEqual(['getRawConnection', 'registerReinitializeHook', 'runSchemaBootstrap:{"snapshot":true}']);
+ expect(ormMock.runSchemaBootstrap).toHaveBeenCalledWith(orm, { snapshot: true });
});
it('DBLIFE-002: the hook open() registers rebuilds the ORM client and migrates the new file without a pre-migrate copy', async () => {
const { orm, connection } = ormStub();
- await new DatabaseLifecycle(orm).open();
+ await new DatabaseLifecycle(orm, new SqliteEngine()).open();
const hook = dbMock.registerReinitializeHook.mock.calls[0][0];
calls.length = 0;
@@ -86,7 +91,7 @@ describe('DatabaseLifecycle', () => {
it('DBLIFE-007: listeners registered with onReopened run after the schema bootstrap of a reopen, in order', async () => {
const { orm } = ormStub();
- const lifecycle = new DatabaseLifecycle(orm);
+ const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine());
lifecycle.onReopened(async () => {
calls.push('listener:first');
});
@@ -110,7 +115,7 @@ describe('DatabaseLifecycle', () => {
it('DBLIFE-008: a listener that throws fails the reopen, so a listener must contain its own failures', async () => {
const { orm } = ormStub();
- const lifecycle = new DatabaseLifecycle(orm);
+ const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine());
lifecycle.onReopened(async () => {
throw new Error('listener broke');
});
@@ -120,8 +125,33 @@ describe('DatabaseLifecycle', () => {
await expect(hook()).rejects.toThrow('listener broke');
});
+ it('DBLIFE-009: on Postgres a reopen keeps the pool and only migrates, so it never waits on a connection lent to a job', async () => {
+ const bootstrap = vi
+ .spyOn(PostgresEngine.prototype, 'schemaBootstrap')
+ .mockImplementation(async (_orm, options) => {
+ calls.push(`schemaBootstrap:${JSON.stringify(options)}`);
+ });
+ try {
+ const { orm, connection } = ormStub();
+ const lifecycle = new DatabaseLifecycle(orm, new PostgresEngine(POSTGRES));
+ lifecycle.onReopened(async () => {
+ calls.push('listener');
+ });
+ await lifecycle.open();
+ calls.length = 0;
+
+ await lifecycle.reopen();
+
+ expect(connection.close).not.toHaveBeenCalled();
+ expect(connection.connect).not.toHaveBeenCalled();
+ expect(calls).toEqual(['schemaBootstrap:{"snapshot":false}', 'listener']);
+ } finally {
+ bootstrap.mockRestore();
+ }
+ });
+
it('DBLIFE-003: close() and reopen() go through the connection module', async () => {
- const lifecycle = new DatabaseLifecycle(ormStub().orm);
+ const lifecycle = new DatabaseLifecycle(ormStub().orm, new SqliteEngine());
lifecycle.close();
await lifecycle.reopen();
@@ -132,7 +162,9 @@ describe('DatabaseLifecycle', () => {
it('DBLIFE-004: a reopen that fails is not swallowed', async () => {
dbMock.reinitialize.mockRejectedValueOnce(new Error('database is locked'));
- await expect(new DatabaseLifecycle(ormStub().orm).reopen()).rejects.toThrow('database is locked');
+ await expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).reopen()).rejects.toThrow(
+ 'database is locked',
+ );
});
describe('file', () => {
@@ -145,7 +177,7 @@ describe('DatabaseLifecycle', () => {
});
it('DBLIFE-005: is the in-memory database under test', () => {
- expect(new DatabaseLifecycle(ormStub().orm).file).toBe(':memory:');
+ expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(':memory:');
});
it('DBLIFE-006: is TREK_DB_FILE when it is set, the one path the connection, the backup and the restore share', () => {
@@ -154,7 +186,7 @@ describe('DatabaseLifecycle', () => {
vi.stubEnv('NODE_ENV', 'production');
vi.stubEnv('TREK_DB_FILE', file);
- expect(new DatabaseLifecycle(ormStub().orm).file).toBe(file);
+ expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(file);
});
});
});
diff --git a/server/tests/unit/nest/database/unit-of-work.d5.test.ts b/server/tests/unit/nest/database/unit-of-work.d5.test.ts
index a26acc0cf..f4805fd87 100644
--- a/server/tests/unit/nest/database/unit-of-work.d5.test.ts
+++ b/server/tests/unit/nest/database/unit-of-work.d5.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Users } from '../../../../src/db/entities/Users.entity';
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
@@ -29,7 +30,7 @@ let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
diff --git a/server/tests/unit/nest/database/unit-of-work.test.ts b/server/tests/unit/nest/database/unit-of-work.test.ts
index 50775b6a7..4f6fcd286 100644
--- a/server/tests/unit/nest/database/unit-of-work.test.ts
+++ b/server/tests/unit/nest/database/unit-of-work.test.ts
@@ -1,3 +1,4 @@
+import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Days } from '../../../../src/db/entities/Days.entity';
import type { DaysRepository } from '../../../../src/db/repositories/Days.repository';
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
@@ -25,7 +26,7 @@ let days: DaysRepository;
*/
beforeAll(async () => {
t = await createTestOrm(testDb);
- uow = new UnitOfWork(t.em);
+ uow = new UnitOfWork(t.em, new SqliteEngine());
days = t.repo(Days);
});
beforeEach(() => {
diff --git a/server/tests/unit/nest/oauth.service.test.ts b/server/tests/unit/nest/oauth.service.test.ts
index c9388143a..c7fb0b4f8 100644
--- a/server/tests/unit/nest/oauth.service.test.ts
+++ b/server/tests/unit/nest/oauth.service.test.ts
@@ -11,6 +11,7 @@
import { ADDON_IDS } from '../../../src/addons';
import { getMcpSafeUrl } from '../../../src/app-config';
import { db as testDb } from '../../../src/db/database';
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
import { OauthClients } from '../../../src/db/entities/OauthClients.entity';
import { OauthConsents } from '../../../src/db/entities/OauthConsents.entity';
@@ -132,7 +133,7 @@ beforeAll(async () => {
consentsRepo,
addonsStub,
new AuditService(auditLogRepo, usersRepo),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
cimdStub,
);
});
@@ -1446,7 +1447,7 @@ describe('module-scoped OAuth state', () => {
consentsRepo,
addonsStub,
new AuditService(auditLogRepo, usersRepo),
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
cimdStub,
);
expect((await secondInstance.consumeAuthCode(code))?.userId).toBe(42);
diff --git a/server/tests/unit/nest/postgres-database-backup.test.ts b/server/tests/unit/nest/postgres-database-backup.test.ts
new file mode 100644
index 000000000..19a304469
--- /dev/null
+++ b/server/tests/unit/nest/postgres-database-backup.test.ts
@@ -0,0 +1,172 @@
+/**
+ * PostgresDatabaseBackup: the backup port on a Postgres database, over
+ * stand-in pg_dump, pg_restore and psql tools (TREK_DB_PG_BIN_DIR, built by
+ * tests/helpers/fake-pg-tools.ts) and a stand-in DatabaseLifecycle. The tools'
+ * own arguments are pinned in tests/unit/db/engine/postgres/pg-tools.test.ts.
+ */
+import { deriveDb } from '../../../src/app-config/derive';
+import { resolveEngine } from '../../../src/db/engine';
+import { knownMigrationNames } from '../../../src/db/known-migrations';
+import type { DataPathsService } from '../../../src/nest/app-config/data-paths.service';
+import { PostgresDatabaseBackup } from '../../../src/nest/backup/postgres-database-backup';
+import { DomainError } from '../../../src/nest/common/domain-error';
+import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface';
+import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
+import {
+ answersSchema,
+ failsWith,
+ installFakeTool,
+ listsMigrations,
+ writesFile,
+ writesRestrictedScript,
+} from '../../helpers/fake-pg-tools';
+
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const KNOWN = [...knownMigrationNames()].sort();
+let root: string;
+let bin: string;
+let extract: string;
+let reopen: ReturnType;
+let backup: PostgresDatabaseBackup;
+
+/** The adapter over the settings a process reads from these variables, with stand-ins for the lifecycle and the data directory. */
+function postgresBackup(env: Record): PostgresDatabaseBackup {
+ const db = deriveDb(env);
+ return new PostgresDatabaseBackup(
+ { reopen } as unknown as DatabaseLifecycle,
+ { dataDir: root } as unknown as DataPathsService,
+ resolveEngine(db),
+ db,
+ );
+}
+
+beforeEach(() => {
+ root = mkdtempSync(path.join(tmpdir(), 'trek-pg-backup-'));
+ bin = path.join(root, 'bin');
+ extract = path.join(root, 'extract');
+ mkdirSync(bin);
+ mkdirSync(extract);
+ installFakeTool(bin, 'psql', { snippets: [answersSchema()] });
+ reopen = vi.fn(async () => undefined);
+ backup = postgresBackup({ TREK_DB_URL: 'postgresql://trek:pw@db/trek', TREK_DB_PG_BIN_DIR: bin });
+});
+afterEach(() => {
+ vi.unstubAllEnvs();
+ vi.restoreAllMocks();
+ rmSync(root, { recursive: true, force: true });
+});
+
+async function refusal(promise: Promise): Promise<{ status: number; message: string }> {
+ const error = await promise.then(
+ () => null,
+ (err: unknown) => err,
+ );
+ expect(error).toBeInstanceOf(DomainError);
+ return { status: (error as DomainError).getStatus(), message: (error as DomainError).publicMessage };
+}
+
+describe('PostgresDatabaseBackup', () => {
+ it('PGBACKUP-001: a snapshot is a pg_dump; a failed one leaves nothing that could pass for a copy', async () => {
+ expect(backup.archiveEntry).toBe('travel.pgdump');
+ expect(backup.canSnapshot()).toBe(true);
+ await expect(backup.checkpoint()).resolves.toBeUndefined();
+ installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
+ await backup.snapshot(path.join(root, 'a.pgdump'));
+ expect(existsSync(path.join(root, 'a.pgdump'))).toBe(true);
+ installFakeTool(bin, 'pg_dump', { snippets: [writesFile('partial'), failsWith('connection refused')] });
+ await expect(backup.snapshot(path.join(root, 'b.pgdump'))).rejects.toThrow('pg_dump failed: connection refused');
+ expect(existsSync(path.join(root, 'b.pgdump'))).toBe(false);
+ expect(existsSync(path.join(root, 'b.pgdump.partial'))).toBe(false);
+ });
+
+ it('PGBACKUP-002: verify accepts a TREK dump this build knows', async () => {
+ writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
+ installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 3))] });
+ await expect(backup.verify(extract)).resolves.toBeUndefined();
+ });
+
+ it('PGBACKUP-002a: verify refuses an archive without a TREK dump in it', async () => {
+ expect(await refusal(backup.verify(extract))).toEqual({
+ status: 400,
+ message: 'Invalid backup: travel.pgdump not found',
+ });
+ });
+
+ it.each([
+ {
+ id: 'PGBACKUP-002b',
+ setup: () => writeFileSync(path.join(extract, 'travel.db'), 'sqlite'),
+ message: /holds a SQLite database/,
+ },
+ {
+ id: 'PGBACKUP-002c',
+ setup: () => {
+ writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
+ installFakeTool(bin, 'pg_restore', {
+ snippets: [listsMigrations([...KNOWN, 'Migration29990101000000_from_the_future'])],
+ });
+ },
+ message: /made by a newer TREK version/,
+ },
+ {
+ id: 'PGBACKUP-002d',
+ setup: () => {
+ writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
+ installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([])] });
+ },
+ message: /holds no TREK migration history/,
+ },
+ {
+ id: 'PGBACKUP-002e',
+ setup: () => {
+ writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
+ installFakeTool(bin, 'pg_restore', { snippets: [failsWith('pg_restore: error: not a valid archive')] });
+ vi.spyOn(console, 'warn').mockImplementation(() => undefined);
+ },
+ message: 'Uploaded file is not a valid Postgres dump',
+ },
+ ])('$id: verify refuses a dump TREK cannot restore', async ({ setup, message }) => {
+ setup();
+ const refused = await refusal(backup.verify(extract));
+ if (typeof message === 'string') expect(refused.message).toBe(message);
+ else expect(refused.message).toMatch(message);
+ });
+
+ it('PGBACKUP-003: keeps a dump of the database a restore replaces, or says it could not', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => undefined);
+ vi.spyOn(console, 'warn').mockImplementation(() => undefined);
+ installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] });
+ const kept = await backup.keepCopyBeforeRestore();
+ expect(kept).toMatch(/pre-restore-\d+\.pgdump$/);
+ expect(existsSync(kept as string)).toBe(true);
+ installFakeTool(bin, 'pg_dump', { snippets: [failsWith('connection refused')] });
+ await expect(backup.keepCopyBeforeRestore()).resolves.toBeNull();
+ });
+
+ it('PGBACKUP-004: replace restores, then reopens; a reopen failure is reported, not thrown', async () => {
+ installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] });
+ await expect(backup.replace(path.join(extract, 'travel.pgdump'))).resolves.toEqual({ reopenError: null });
+ expect(reopen).toHaveBeenCalledTimes(1);
+ const lost = new Error('reconnect failed');
+ reopen.mockRejectedValueOnce(lost);
+ await expect(backup.replace(path.join(extract, 'travel.pgdump'))).resolves.toEqual({ reopenError: lost });
+ });
+
+ it('PGBACKUP-005: a failed restore still reopens, and both failures surface when the reopen fails too', async () => {
+ installFakeTool(bin, 'pg_restore', { snippets: [failsWith('connection refused')] });
+ const source = path.join(extract, 'travel.pgdump');
+ await expect(backup.replace(source)).rejects.toThrow('pg_restore failed: connection refused');
+ expect(reopen).toHaveBeenCalledTimes(1);
+ reopen.mockRejectedValueOnce(new Error('reconnect failed'));
+ await expect(backup.replace(source)).rejects.toBeInstanceOf(DatabaseConnectionLostError);
+ });
+
+ it('PGBACKUP-006: names the database without its password, and refuses to run without TREK_DB_URL', async () => {
+ expect(backup.location()).toBe('Postgres postgresql://trek:***@db/trek');
+ await expect(postgresBackup({}).snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/);
+ });
+});
diff --git a/server/tests/unit/nest/sqlite-database-backup.test.ts b/server/tests/unit/nest/sqlite-database-backup.test.ts
index eb557b2fa..5699cae5e 100644
--- a/server/tests/unit/nest/sqlite-database-backup.test.ts
+++ b/server/tests/unit/nest/sqlite-database-backup.test.ts
@@ -2,13 +2,14 @@
* SqliteDatabaseBackup: the database backup port for a SQLite file.
*
* The snapshot and the swap run for real here, against a database file in a
- * temp directory: a VACUUM INTO through the real MaintenanceRepository, and the
+ * temp directory: a VACUUM INTO through the real SqliteMaintenance, and the
* copy, sidecar removal and rename of a restore. Only the connection lifecycle
* is a stand-in, because closing and reopening the process's own connection is
* DatabaseLifecycle's business (tests/unit/nest/database/).
*/
-import { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository';
+import { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance';
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup';
+import { DomainError } from '../../../src/nest/common/domain-error';
import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface';
import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
import { withRequestContext } from '../../../src/nest/database/request-context';
@@ -40,7 +41,7 @@ function maintenanceStub() {
function portWith(maintenance: { walCheckpoint: unknown; vacuumInto: unknown }): SqliteDatabaseBackup {
return new SqliteDatabaseBackup(
lifecycle as unknown as DatabaseLifecycle,
- maintenance as unknown as MaintenanceRepository,
+ maintenance as unknown as SqliteMaintenance,
);
}
@@ -116,7 +117,7 @@ describe('SqliteDatabaseBackup', () => {
// test-sql-allow: the marker table is a bare fixture no entity maps; the write has to land in the WAL of this file.
live.prepare('UPDATE marker SET label = ?').run('after');
const target = path.join(dir, 'copy.db');
- const port = portWith(new MaintenanceRepository(orm.em));
+ const port = portWith(new SqliteMaintenance(orm.em));
await withRequestContext(orm.orm, () => port.snapshot(target));
@@ -125,7 +126,7 @@ describe('SqliteDatabaseBackup', () => {
it('SQLBK-005: a VACUUM INTO that fails rejects and leaves nothing at the target', async () => {
const target = path.join(dir, 'missing-dir', 'copy.db');
- const port = portWith(new MaintenanceRepository(orm.em));
+ const port = portWith(new SqliteMaintenance(orm.em));
await expect(withRequestContext(orm.orm, () => port.snapshot(target))).rejects.toThrow();
@@ -146,11 +147,16 @@ describe('SqliteDatabaseBackup', () => {
expect(fs.existsSync(target)).toBe(false);
});
- it('SQLBK-007: verify refuses an unpacked archive without a database', () => {
- expect(portWith(maintenanceStub()).verify(dir)).toEqual({
- error: 'Invalid backup: travel.db not found',
- status: 400,
- });
+ it('SQLBK-007: verify refuses an unpacked archive without a database, as a DomainError the restore route answers', async () => {
+ const refusal = await portWith(maintenanceStub())
+ .verify(dir)
+ .then(
+ () => null,
+ (err: unknown) => err,
+ );
+ expect(refusal).toBeInstanceOf(DomainError);
+ expect((refusal as DomainError).publicMessage).toBe('Invalid backup: travel.db not found');
+ expect((refusal as DomainError).getStatus()).toBe(400);
});
it('SQLBK-008: keeps a copy of the database a restore replaces next to it, and says where', async () => {
diff --git a/server/tests/unit/plugins/discovery.test.ts b/server/tests/unit/plugins/discovery.test.ts
index d8b41e1a6..5d925d3b2 100644
--- a/server/tests/unit/plugins/discovery.test.ts
+++ b/server/tests/unit/plugins/discovery.test.ts
@@ -8,6 +8,7 @@
* bundle), via a real MikroORM over the full migrated schema (`createSnapshotTestDb`
* + `createTestOrm`) rather than a hand-rolled `:memory:` table set.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
@@ -47,7 +48,7 @@ beforeAll(async () => {
actions: t.repo(PluginActions),
settingsFields: t.repo(PluginSettingsFields),
errorLog: t.repo(PluginErrorLog),
- uow: new UnitOfWork(t.em),
+ uow: new UnitOfWork(t.em, new SqliteEngine()),
};
});
beforeEach(() => {
diff --git a/server/tests/unit/plugins/plugin-oauth.service.test.ts b/server/tests/unit/plugins/plugin-oauth.service.test.ts
index d1fc9942f..3e6be8d32 100644
--- a/server/tests/unit/plugins/plugin-oauth.service.test.ts
+++ b/server/tests/unit/plugins/plugin-oauth.service.test.ts
@@ -4,6 +4,7 @@
* https/SSRF guard on the endpoints, the code + refresh token exchanges (mocked fetch),
* tokens encrypted at rest, and a stored refresh token that the plugin never sees.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity';
import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity';
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
@@ -90,7 +91,7 @@ async function makeOauthService(): Promise {
orm.repo(PluginOauthTokens),
orm.repo(PluginOauthState),
orm.repo(PluginSettingsFields),
- new UnitOfWork(orm.em),
+ new UnitOfWork(orm.em, new SqliteEngine()),
);
}
diff --git a/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts b/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts
index 80f8d3f7c..b7dbf28df 100644
--- a/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts
+++ b/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts
@@ -8,6 +8,7 @@
* defensible: dropping the `logError` call, or falling through to `activate`
* anyway, would ship green with no test failing. This file pins it directly.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
@@ -99,7 +100,7 @@ async function buildRuntime(withOrm: boolean): Promise {
t.repo(NotificationChannelPreferences),
// Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead of
// `registry?`/`hostFactory?`, matching the real constructor's own order.
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
undefined,
undefined,
withOrm ? t.orm : undefined,
diff --git a/server/tests/unit/plugins/request-context.test.ts b/server/tests/unit/plugins/request-context.test.ts
index 508a35bb3..c90d697a6 100644
--- a/server/tests/unit/plugins/request-context.test.ts
+++ b/server/tests/unit/plugins/request-context.test.ts
@@ -29,6 +29,7 @@
* shared helpers use to let ordinary unit tests call a repository with no
* request wrapper at all: that default would hide this exact bug.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import { Trips } from '../../../src/db/entities/Trips.entity';
import { Users } from '../../../src/db/entities/Users.entity';
@@ -70,7 +71,7 @@ let userId: number;
beforeAll(async () => {
t = await createTestOrm(testDb, { allowGlobalContext: false });
- permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em));
+ permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em, new SqliteEngine()));
// Plan 3j Task 1: PluginGuards' own role lookup (canCreateAs, PG4) now goes
// through UsersRepository.getRole — a REAL repository bound to `t.em`
// (allowGlobalContext: false, same as `permissions`' AppSettingsRepository
diff --git a/server/tests/unit/plugins/settings-isolation.test.ts b/server/tests/unit/plugins/settings-isolation.test.ts
index db65528b7..456679f49 100644
--- a/server/tests/unit/plugins/settings-isolation.test.ts
+++ b/server/tests/unit/plugins/settings-isolation.test.ts
@@ -10,6 +10,7 @@
* which for a notification channel meant being dispatched to everyone with no credentials.
*/
import { db as testDb } from '../../../src/db/database';
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
@@ -204,7 +205,7 @@ describe('a plugin channel label is bounded by the host', () => {
t.repo(Settings),
t.repo(NotificationChannelPreferences),
// Plan 4 Task 4: `uow` is no longer `@Optional()`.
- new UnitOfWork(t.em),
+ new UnitOfWork(t.em, new SqliteEngine()),
);
// Stand the plugin up as a granted, active notificationChannel provider.
(rt as unknown as { supervisor: { running: Map } }).supervisor.running.set('loud', {
diff --git a/server/tests/unit/scripts/dialect-lint.test.ts b/server/tests/unit/scripts/dialect-lint.test.ts
index d01520ea8..c52d49d8f 100644
--- a/server/tests/unit/scripts/dialect-lint.test.ts
+++ b/server/tests/unit/scripts/dialect-lint.test.ts
@@ -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);
});
diff --git a/server/tests/unit/scripts/engine-dispatch-ratchet.test.ts b/server/tests/unit/scripts/engine-dispatch-ratchet.test.ts
new file mode 100644
index 000000000..a63cec84a
--- /dev/null
+++ b/server/tests/unit/scripts/engine-dispatch-ratchet.test.ts
@@ -0,0 +1,88 @@
+/**
+ * scripts/engine-dispatch-ratchet.mjs (lint:engine-dispatch): an engine check
+ * outside src/db/dialect/ and src/db/engine/ may only shrink. Runs as the real
+ * script against throwaway server roots (--dir) and once on this repository,
+ * so the committed baseline is checked by the unit suite.
+ */
+import { countEngineDispatch } from '../../../scripts/engine-dispatch-ratchet.mjs';
+
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, describe, expect, it } from 'vitest';
+
+const SCRIPT = path.join(__dirname, '../../../scripts/engine-dispatch-ratchet.mjs');
+const SERVER_ROOT = path.join(__dirname, '../../..');
+const BASELINE = 'scripts/engine-dispatch-baseline.json';
+
+const roots: string[] = [];
+
+function serverRoot(files: Record, counts: Record = {}): string {
+ const dir = mkdtempSync(path.join(tmpdir(), 'trek-engine-dispatch-'));
+ roots.push(dir);
+ mkdirSync(path.join(dir, 'src'), { recursive: true });
+ mkdirSync(path.join(dir, 'scripts'), { recursive: true });
+ for (const [file, text] of Object.entries(files)) {
+ mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
+ writeFileSync(path.join(dir, file), text);
+ }
+ writeFileSync(path.join(dir, BASELINE), JSON.stringify({ counts }));
+ return dir;
+}
+
+function run(dir: string, ...args: string[]): { status: number | null; out: string } {
+ try {
+ const stdout = execFileSync(process.execPath, [SCRIPT, `--dir=${dir}`, ...args], {
+ encoding: 'utf8',
+ stdio: ['ignore', 'pipe', 'pipe'],
+ });
+ return { status: 0, out: stdout };
+ } catch (err) {
+ const { status, stdout, stderr } = err as { status: number | null; stdout: string; stderr: string };
+ return { status, out: `${stdout}${stderr}` };
+ }
+}
+
+afterEach(() => {
+ for (const dir of roots.splice(0)) rmSync(dir, { recursive: true, force: true });
+});
+
+// Spelled in pieces so this file does not read as an engine check itself.
+const IS_SQLITE = ['isSql', 'ite(platform)'].join('');
+const IS_POSTGRES = ['isPost', 'gres(p)'].join('');
+const INSTANCE = ['x instanceof ', 'SqlitePlatform'].join('');
+
+describe('lint:engine-dispatch', () => {
+ it('ENGDISP-001: counts the platform checks and instanceof tests, not the ones in comments', () => {
+ expect(countEngineDispatch(`if (${IS_SQLITE}) a(); else if (${IS_POSTGRES}) b();`)).toBe(2);
+ expect(countEngineDispatch(`const y = ${INSTANCE};`)).toBe(1);
+ const BY_NAME = ["if (readEnv().db.engine === 'post", 'gres\') a(); if (engine.id !== "sql', 'ite") b();'].join('');
+ expect(countEngineDispatch(BY_NAME)).toBe(2);
+ expect(countEngineDispatch("if (user.id === 'admin') a();")).toBe(0);
+ expect(countEngineDispatch(`// ${IS_SQLITE}\n/* ${INSTANCE} */\nconst ok = 1;`)).toBe(0);
+ });
+
+ it('ENGDISP-002: the dialect layer and the engine port may dispatch, any other file may not', () => {
+ const text = `export const x = ${IS_SQLITE};\n`;
+ expect(run(serverRoot({ 'src/db/dialect/a.ts': text, 'src/db/engine/sqlite/b.ts': text })).status).toBe(0);
+ const domain = run(serverRoot({ 'src/nest/trips/trips.service.ts': text }));
+ expect(domain.status).toBe(1);
+ expect(domain.out).toContain('src/nest/trips/trips.service.ts');
+ });
+
+ it('ENGDISP-003: a baselined file is held at its count, and --update only lowers it', () => {
+ const text = `export const x = ${IS_SQLITE};\n`;
+ const file = 'src/db/repositories/A.repository.ts';
+ expect(run(serverRoot({ [file]: text }, { [file]: 1 })).status).toBe(0);
+ expect(run(serverRoot({ [file]: `${text}${text}` }, { [file]: 1 })).status).toBe(1);
+ const stale = serverRoot({ [file]: 'export const x = 1;\n' }, { [file]: 1 });
+ expect(run(stale).status).toBe(1);
+ expect(run(stale, '--update').status).toBe(0);
+ expect(JSON.parse(readFileSync(path.join(stale, BASELINE), 'utf8'))).toEqual({ counts: {} });
+ });
+
+ it('ENGDISP-004: the repository holds its committed baseline', () => {
+ expect(run(SERVER_ROOT).status).toBe(0);
+ });
+});
diff --git a/server/tests/unit/scripts/pg-probe/arg-samples.test.ts b/server/tests/unit/scripts/pg-probe/arg-samples.test.ts
index 672c207a9..2f4ff9b48 100644
--- a/server/tests/unit/scripts/pg-probe/arg-samples.test.ts
+++ b/server/tests/unit/scripts/pg-probe/arg-samples.test.ts
@@ -7,13 +7,14 @@
* sample (or a pinned reason it has none).
*/
import {
+ PRESENCE_LABEL,
planRepositories,
readCompilerOptions,
sampleString,
type MethodPlan,
} from '../../../../scripts/pg-probe/arg-samples';
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import ts from 'typescript';
@@ -47,6 +48,7 @@ export class ThingsRepository {
async record(r: Record): Promise {}
async literals(n: bigint, flag: true): Promise {}
async destructured({ id, label }: { id: number; label: string }): Promise {}
+ async presence(write: { name?: readonly [present: boolean, value: string]; flag?: readonly [boolean, number] }): Promise {}
}
export abstract class AbstractRepository {
async skipped(): Promise {}
@@ -102,6 +104,7 @@ describe('pg-probe argument samples', () => {
'literals',
'nullable',
'optionalCallback',
+ 'presence',
'record',
'rest',
'tuple',
@@ -145,6 +148,28 @@ 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-038: the presence label the sampler reads is the one presenceSet declares', () => {
+ // The sampler matches the tuple label by name; a rename in the repository
+ // helper would stop the flag being sampled true, silently unmeasuring every
+ // partial update. Read the real signature, not a copy of it.
+ const file = path.join(__dirname, '../../../../src/db/repositories/_shared/presence-set.ts');
+ const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
+ const labels: string[] = [];
+ const visit = (node: ts.Node): void => {
+ if (ts.isNamedTupleMember(node)) labels.push(node.name.text);
+ ts.forEachChild(node, visit);
+ };
+ visit(source);
+ expect(labels).toContain(PRESENCE_LABEL);
+ });
+
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]);
diff --git a/server/tests/unit/scripts/pg-probe/baseline.test.ts b/server/tests/unit/scripts/pg-probe/baseline.test.ts
index 99db25f11..8dc572ff4 100644
--- a/server/tests/unit/scripts/pg-probe/baseline.test.ts
+++ b/server/tests/unit/scripts/pg-probe/baseline.test.ts
@@ -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', () => {
diff --git a/server/tests/unit/scripts/pg-probe/report.test.ts b/server/tests/unit/scripts/pg-probe/report.test.ts
index ab290e797..c71b99bf8 100644
--- a/server/tests/unit/scripts/pg-probe/report.test.ts
+++ b/server/tests/unit/scripts/pg-probe/report.test.ts
@@ -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'] } }),
);
diff --git a/server/tsconfig.scripts.json b/server/tsconfig.scripts.json
index c9278df77..7fd1ba59e 100644
--- a/server/tsconfig.scripts.json
+++ b/server/tsconfig.scripts.json
@@ -4,5 +4,5 @@
"rootDir": ".",
"noEmit": true
},
- "include": ["scripts/generate-entities.ts", "scripts/generate-db-types.ts", "scripts/migrate-encryption.ts", "scripts/pg-probe.ts", "scripts/pg-probe/*.ts"]
+ "include": ["scripts/generate-entities.ts", "scripts/generate-db-types.ts", "scripts/generate-pg-baseline.ts", "scripts/migrate-encryption.ts", "scripts/pg-probe.ts", "scripts/pg-probe/*.ts"]
}
diff --git a/server/vitest.config.ts b/server/vitest.config.ts
index 78a43107a..ad41cd32b 100644
--- a/server/vitest.config.ts
+++ b/server/vitest.config.ts
@@ -1,5 +1,5 @@
import swc from 'unplugin-swc';
-import { defineConfig } from 'vitest/config';
+import { configDefaults, defineConfig } from 'vitest/config';
export default defineConfig({
// SWC transform so NestJS decorator metadata is emitted in tests
@@ -25,6 +25,8 @@ export default defineConfig({
test: {
root: '.',
include: ['tests/**/*.test.ts'],
+ // The Postgres-only suites run in their own lane (vitest.pg.config.ts, `npm run test:pg`).
+ exclude: [...configDefaults.exclude, 'tests/postgres/**'],
globals: true,
// Migrates one database for the whole run and snapshots it; workers open a
// copy instead of replaying 242 migrations per file. See tests/global-setup.ts.
diff --git a/server/vitest.pg.config.ts b/server/vitest.pg.config.ts
new file mode 100644
index 000000000..c51a7b06c
--- /dev/null
+++ b/server/vitest.pg.config.ts
@@ -0,0 +1,62 @@
+import base from './vitest.config';
+
+import { defineConfig } from 'vitest/config';
+
+/**
+ * The Postgres lane (`npm run test:pg`): the suites that boot the whole app
+ * through `buildApp()` on the real database module, each file on a database
+ * of its own copied from one migrated template, plus the Postgres-only suites
+ * under tests/postgres/. TREK_TEST_DB_URL names a database the lane may create
+ * and drop databases from (`postgres://…/postgres`); without it the lane fails.
+ *
+ * The other suites stay on SQLite: they mock `db/database` with an in-memory
+ * snapshot, seed it with raw SQLite, or are about the SQLite file itself. Two
+ * that boot the app stay there too: tours-hike and tours-plan-types move a
+ * faked setTimeout on by a tenth of a second per turn of the event loop until
+ * a tool call ends, which a synchronous SQLite answers within a few turns and
+ * a database over the network does not (the MCP request times out on the
+ * faked clock).
+ */
+const POSTGRES_LANE = [
+ 'tests/postgres/**/*.test.ts',
+ 'tests/e2e/ai-own-server.e2e.test.ts',
+ 'tests/e2e/assistant-attachments.e2e.test.ts',
+ 'tests/e2e/assistant-book-cards.e2e.test.ts',
+ 'tests/e2e/assistant-book-design.e2e.test.ts',
+ 'tests/e2e/assistant-collections.e2e.test.ts',
+ 'tests/e2e/assistant-home.e2e.test.ts',
+ 'tests/e2e/assistant-journey.e2e.test.ts',
+ 'tests/e2e/assistant-page-images.e2e.test.ts',
+ 'tests/e2e/assistant-renders.e2e.test.ts',
+ 'tests/e2e/assistant-roadtrip.e2e.test.ts',
+ 'tests/e2e/assistant-tour-files.e2e.test.ts',
+ 'tests/e2e/assistant-tours.e2e.test.ts',
+ 'tests/e2e/assistant.e2e.test.ts',
+ 'tests/e2e/collab-mcp.e2e.test.ts',
+ 'tests/e2e/journey-book-design.e2e.test.ts',
+ 'tests/e2e/journey-book.e2e.test.ts',
+ 'tests/e2e/packing-mcp.e2e.test.ts',
+ 'tests/e2e/reservations-read.e2e.test.ts',
+ 'tests/e2e/storage-reclaim.e2e.test.ts',
+ 'tests/integration/book-body-size.test.ts',
+ 'tests/integration/storage-reclaim-sources.test.ts',
+ 'tests/integration/storage-release.test.ts',
+ 'tests/integration/trip-invite-removal.test.ts',
+ 'tests/integration/trip-journal-link.test.ts',
+ 'tests/unit/nest/validate-body-contracts.test.ts',
+ 'tests/unit/nest/validate-managed-routes.test.ts',
+];
+
+export default defineConfig({
+ ...base,
+ test: {
+ ...base.test,
+ include: POSTGRES_LANE,
+ exclude: [],
+ globalSetup: ['tests/postgres/global-setup.ts'],
+ setupFiles: [...(base.test?.setupFiles as string[]), 'tests/postgres/setup.ts'],
+ // Every file boots the app on a database of its own; the lane is about
+ // the engine, so coverage stays the SQLite lane's measure.
+ coverage: { ...base.test?.coverage, enabled: false, thresholds: undefined },
+ },
+});
diff --git a/wiki/Backups.md b/wiki/Backups.md
index 28f501abb..aee4b15af 100644
--- a/wiki/Backups.md
+++ b/wiki/Backups.md
@@ -1,6 +1,6 @@
# Backups
-TREK stores all data in a single SQLite database (`travel.db`) plus an `uploads/` directory of attachments, cover photos, and avatars, by default; storage backends and replication are configured in [[Admin: Storage|Admin-Storage]]. The Backup panel lets you create, download, restore, and schedule backups of both.
+TREK stores all data in a single SQLite database (`travel.db`) plus an `uploads/` directory of attachments, cover photos, and avatars, by default. On a [PostgreSQL database](Database-Postgres) (`TREK_DB_URL` set) the database is `travel.pgdump` instead. Storage backends and replication are configured in [[Admin: Storage|Admin-Storage]]. The Backup panel lets you create, download, restore, and schedule backups of both.
## Where to find it
@@ -14,7 +14,7 @@ A backup is a ZIP archive with these entries:
| Entry | Contents |
|---|---|
-| `travel.db` | The full SQLite database, as a consistent snapshot taken while TREK keeps running (writes during the backup cannot tear it) |
+| `travel.db` | The full SQLite database, as a consistent snapshot taken while TREK keeps running (writes during the backup cannot tear it). On PostgreSQL this entry is `travel.pgdump` instead: a `pg_dump` custom-format file, taken from one consistent snapshot of the database. |
| `uploads/` | All uploaded attachments, covers, and avatars (default location, see [Admin: Storage](Admin-Storage)) |
| `plugins-data/` | Each installed plugin's own database + files (present only if plugins are installed) |
| `plugins-code/` | The installed plugin code, so a restore is self-contained (dev-linked plugins are skipped) |
@@ -38,13 +38,13 @@ You can restore from:
A second limit caps the **decompressed** size of a restore archive at 5 GB by default (`BACKUP_MAX_DECOMPRESSED_MB`). It applies to both routes (a stored backup as well as an uploaded ZIP) and a restore that exceeds it is refused with `Backup exceeds the maximum decompressed size.` Raise it if your own backups legitimately grow past the default, otherwise they become unrestorable.
-Before restoring, TREK runs integrity checks on the uploaded database:
+Before restoring, TREK runs integrity checks on the uploaded database. On PostgreSQL the first two are replaced by the checks in [Database: Postgres](Database-Postgres#backups-and-restore).
1. **SQLite `PRAGMA integrity_check`**: verifies the database file is not corrupt.
2. **Required tables present**: confirms the file contains `users`, `trips`, `trip_members`, `places`, and `days`. Files missing any of these are rejected as not being a valid TREK backup.
3. **Not from a newer TREK**: a backup whose database a newer release migrated is refused with `This backup was made by a newer TREK version. Update TREK before restoring it.` Restoring it would leave this version unable to start.
-The database being replaced is kept as `pre-restore-.db` next to it (in `data/` on a standard install), so a restore of the wrong archive can be undone by hand. Delete these copies once you no longer need them.
+The database being replaced is kept as `pre-restore-.db` next to it (in `data/` on a standard install), so a restore of the wrong archive can be undone by hand. On PostgreSQL the copy is `pre-restore-.pgdump`. Delete these copies once you no longer need them.
The sessions signed in when the restore starts stay signed in, yours included: they are carried into the restored database for every account it holds under the same id and email. An account whose password differs in the backup still has to sign in again, and a session for an account the backup does not have ends with the restore.
@@ -98,7 +98,7 @@ The phone admin has the same lists and settings in its **Backup** section.
Always create a manual backup before updating. See [Updating](Updating).
-TREK also copies the database by itself right before a startup migrates it, to `data/pre-migrate--.db`, and keeps the newest three. That copy is what an older image can be started on again if you have to roll an update back. It holds the database only, never uploads, so it does not replace a backup. See [Updating](Updating#database-migrations).
+TREK also copies the database by itself right before a startup migrates it, to `data/pre-migrate--.db` (`.pgdump` on PostgreSQL), and keeps the newest three. That copy is what an older image can be started on again if you have to roll an update back. It holds the database only, never uploads, so it does not replace a backup. See [Updating](Updating#database-migrations).
## Audit log
diff --git a/wiki/Database-Postgres.md b/wiki/Database-Postgres.md
new file mode 100644
index 000000000..363be8740
--- /dev/null
+++ b/wiki/Database-Postgres.md
@@ -0,0 +1,149 @@
+# Database: Postgres
+
+By default TREK keeps all data in one SQLite file, `travel.db`, in the data directory. Setting `TREK_DB_URL` runs TREK on a PostgreSQL database instead. Without that variable nothing changes, and every page of this wiki that describes `travel.db` still applies to an install on SQLite.
+
+> **Status:** Postgres support is new. It has been checked end to end against PostgreSQL 17 in Docker (first boot, sign-in, trips, backup and restore, the copy before a migration, `RESTORE_FROM_BACKUP`). The same checks have been run against a Supabase project over its direct connection; see [Supabase](#supabase) and the [Manual verification checklist](#manual-verification-checklist) below.
+
+## When to use it
+
+Use Postgres when you already run a PostgreSQL server and want TREK's data there, for example to back it up or monitor it with the tools you use for other databases. A single TREK container on a host with a local disk does not need it.
+
+Postgres does not make TREK scale out. TREK still runs as one process. See [Limitations](#limitations).
+
+## Requirements
+
+- **PostgreSQL 14 to 17.**
+- **An empty database dedicated to TREK.** TREK creates its schema in an empty database. It refuses a database that holds tables it did not create, and a restore replaces everything in the schema it loads into. Do not share the database with another application.
+- **A role that is not a superuser,** which owns the database or can create objects in its schema. A backup archive is a SQL script run by this role during a restore, so a role with fewer privileges limits what a crafted archive can do.
+- **ICU support in the server.** The schema creates the collation `nocase` with `provider = icu`, so the server must be built with ICU. The official `postgres` images include it.
+- **PostgreSQL client tools** (`pg_dump`, `pg_restore`, `psql`) for backups, restores and the copy TREK takes before a migration. The Docker image installs the version 17 client. The client cannot dump a server newer than 17, so a PostgreSQL 18 server needs a client of the same major version, installed on the host or in a custom image, and pointed at with `TREK_DB_PG_BIN_DIR`. See [Configuration](#configuration).
+- **Client tool versions that carry the CVE-2025-8714 fix.** A restore refuses `pg_restore` or `psql` older than 17.6, 16.10, 15.14 or 14.19.
+
+## Configuration
+
+| Variable | What it does | Default |
+|---|---|---|
+| `TREK_DB_URL` | Connection URL, `postgres://user:password@host:5432/database` (or `postgresql://`). Setting it selects Postgres. | unset (SQLite) |
+| `TREK_DB_POOL_MAX` | Most connections TREK opens to the database, from 1 to 100. The pool sets no minimum, so idle connections can close. | `10` |
+| `TREK_DB_PG_BIN_DIR` | Directory holding `pg_dump`, `pg_restore` and `psql`. | see below |
+
+Percent-encode the user name and password inside the URL if they contain characters such as `@`, `:`, `/` or `#`.
+
+### Combining with SQLite settings
+
+`TREK_DB_URL` cannot be combined with `TREK_DB_FILE`, `TREK_DB_JOURNAL_MODE` or `TREK_DB_SYNCHRONOUS`. TREK refuses to start when both are set, because it would not know which database was meant. Unset the SQLite variables. `TREK_DB_PRE_MIGRATE_SNAPSHOT` and `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP` still apply, but the copy they control is a `.pgdump` file. See [Upgrades](#upgrades).
+
+### SSL
+
+The `sslmode` parameter in the URL selects how the connection is secured:
+
+| `sslmode` | Effect |
+|---|---|
+| `disable` | No SSL. |
+| `require`, `verify-ca`, `verify-full` | SSL, with the server certificate verified. |
+| anything else (for example `prefer`, `allow`, `no-verify`, or a misspelling) | TREK refuses to start. |
+
+Without `sslmode`, the connection pool uses no SSL. The client tools use the libpq default for an unset `sslmode`, so set `sslmode` explicitly. For the backup and restore tools, every mode other than `disable` verifies the server, including its host name.
+
+`sslrootcert=` names the file holding the certificate authority that signs the server's certificate. It is needed when the server uses a certificate from a private CA, and it is valid only together with one of the verifying modes. The file must be readable inside the container, so mount it into the container and point the URL at the path inside the container. Without `sslrootcert`, the system's trusted certificate authorities are used.
+
+### Where the client tools are found
+
+TREK runs the tools by their absolute path. It does not search `PATH`. When `TREK_DB_PG_BIN_DIR` is unset, TREK looks in these directories, in order, and uses the first one that holds the tool:
+
+1. `/usr/lib/postgresql/17/bin`
+2. `/usr/lib/postgresql/16/bin`
+3. `/usr/lib/postgresql/15/bin`
+4. `/usr/lib/postgresql/14/bin`
+5. `/usr/local/bin`
+6. `/usr/bin`
+
+The password is passed to the tools in the `PGPASSWORD` environment variable of their process. It never appears on a command line, where other processes could read it.
+
+### Compose example
+
+[`docker-compose.postgres.yml`](https://github.com/liketrek/TREK/blob/main/docker-compose.postgres.yml) runs a `postgres:17` service beside TREK, on a named volume, with the TREK container waiting for the database to pass its health check. Set `POSTGRES_PASSWORD` in a `.env` file beside it before the first start. See [Install: Docker Compose](Install-Docker-Compose#running-on-postgres).
+
+## First boot
+
+On the first start against an empty database, TREK:
+
+1. Creates the schema from its baseline and records every migration the baseline covers as applied, in one transaction.
+2. Applies any migrations after the baseline.
+3. Seeds its default data and creates the admin account, from `ADMIN_EMAIL` and `ADMIN_PASSWORD` when set. Otherwise it prints a random password to the log.
+
+A database that holds tables TREK did not create, and no TREK migration history, is refused. TREK names a few of the tables and writes nothing to the database. Point `TREK_DB_URL` at an empty database.
+
+A database migrated by a newer TREK is refused, and the log says to run that version again or to restore a backup taken with this one.
+
+**Start one TREK process for the first boot and for every upgrade.** Two processes that start at the same time on the same new database are not coordinated. The second one fails its boot, because the first one's schema transaction wins. Once the database is up to date and running, a single container is the supported setup. See [Limitations](#limitations).
+
+### Moving an existing SQLite install
+
+This first Postgres release has no tool that copies a SQLite install into Postgres. A SQLite backup cannot be restored into a Postgres database, and a Postgres backup cannot be restored into SQLite. Moving an existing install to Postgres means starting a new install on Postgres and recreating the data there, or keeping the SQLite install running.
+
+## Upgrades
+
+On a startup that has pending migrations, TREK writes a copy of the database before it migrates anything:
+
+- The copy is a `pg_dump` custom-format file, `data/pre-migrate--.pgdump`, next to the data the container keeps. The log prints the path.
+- The newest `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP` copies are kept (3 by default).
+- If the copy cannot be written, for example because a client tool is missing or the disk is full, TREK refuses to migrate and does not start. Fix the cause, or set `TREK_DB_PRE_MIGRATE_SNAPSHOT=false` to migrate without a copy.
+- A restart with nothing pending, and a first start on an empty database, write no copy.
+
+The copy is a safety net for an update. It does not replace a backup, and it does not hold uploads. To go back to the previous image, follow [Updating: Rolling back a Postgres update](Updating#rolling-back-a-postgres-update).
+
+## Backups and restore
+
+A Postgres install produces the same backup archive as a SQLite install, with one difference: the database is stored as `travel.pgdump`, a `pg_dump` custom-format file, instead of `travel.db`. The uploads, plugin data and the encryption key are in the archive as before. See [Backups](Backups).
+
+Restoring a backup on Postgres follows these rules:
+
+- **The current database is replaced.** TREK first keeps a copy of it as `data/pre-restore-.pgdump`. `pg_restore` then turns the archive's dump into a SQL script, and `psql` clears the current schema, which drops its tables, views, sequences, functions, types and collations, and runs the script. Both steps run in one transaction, so a restore that fails leaves the database as it was.
+- **TREK checks the archive before it replaces anything.** The archive must hold a Postgres dump, not a SQLite file. The dump must carry TREK's migration history, and it must not come from a newer TREK. TREK refuses the restore with a message saying which check failed.
+- **The client tools must be recent enough.** The restore refuses `pg_restore` or `psql` older than 17.6, 16.10, 15.14 or 14.19.
+
+`RESTORE_FROM_BACKUP` works on Postgres too. On the first start, TREK loads the archive's dump into the database when that database holds no tables. When the database already has tables, the variable is ignored and TREK logs a warning. See [Backups: Restoring on a new install, before setup](Backups#restoring-on-a-new-install-before-setup).
+
+## Limitations
+
+- **No migration from SQLite to Postgres** in this first release. Start a new install on Postgres, or keep the SQLite install.
+- **No more than one TREK replica.** TREK keeps state in memory: rate limits, pending one-time codes, the permission cache and the WebSocket connections. Each process has its own copy, so two replicas would disagree with each other. Keep `replicas: 1` in the Helm chart and one `trek` container. Postgres does not change that.
+- **Two processes booting on one database at once are not coordinated.** Start one process for the first boot and for each upgrade, then continue with one.
+- **Client tools for server version 18 and later.** The Docker image's client is version 17, which cannot dump a newer server. Point `TREK_DB_PG_BIN_DIR` at a matching client.
+- **Writes are serialized.** TREK takes a database-wide advisory lock for each write transaction, as the SQLite engine does with its single writer. Heavy concurrent writes wait on that lock.
+
+## Supabase
+
+Supabase runs PostgreSQL and can host TREK's database. The checklist below has been run against a Supabase project over the direct connection: first boot on an empty database, sign-in, trips, backup, restore, restart, and the copy taken before a pending migration. The session pooler has not been checked yet.
+
+- **Use the direct connection or the session pooler.** The direct connection uses port 5432. Its host resolves only to IPv6 unless the project has the IPv4 add-on. A container on a network without IPv6 (Docker Desktop's default network, for example) cannot reach it; use the session pooler there, which also listens on port 5432 on its own host.
+- **Do not use the transaction pooler** (port 6543) for TREK. `pg_dump` and `psql` need a session that lasts for the whole run, and the transaction pooler does not give one.
+- **Verify the server with Supabase's CA.** Supabase's certificate is signed by its own certificate authority, which the system's trusted authorities do not include, so `sslmode=require` alone fails for the backup and restore tools. Download the CA certificate from the project's database settings, mount it into the container, and use `sslmode=verify-full&sslrootcert=/certs/.crt` with the path inside the container.
+- **The `postgres` role works as TREK's role.** It is not a superuser, and it can create the schema and the `nocase` collation.
+- **The database's `public` schema must hold no tables** when TREK first starts, because TREK refuses a database with tables it did not create. A new project's `public` schema is empty.
+- **Backups hold only TREK's schema.** The project's own schemas (`auth`, `storage` and the others Supabase manages) are left out of every backup, and a restore replaces only TREK's schema.
+- **Pick a region close to the TREK server.** Every statement crosses the network, so a distant region makes the first boot and a restore take minutes. During a restore, scheduled jobs that touch the database can log statement timeouts; they run again on their next schedule.
+
+## Manual verification checklist
+
+Run it against a throwaway database before an install relies on this page. On Supabase, steps 1 to 6 have been run over the direct connection, and step 7 by marking the last applied migration as pending instead of starting an older image; step 8 and the session pooler have not.
+
+1. Create an empty database and a non-superuser role that owns it. Set `TREK_DB_URL` with a verifying `sslmode` (on Supabase, `verify-full` with `sslrootcert`), or with the server's own setting otherwise.
+2. Start one TREK process. Confirm the log reports the Postgres engine, the schema is created, and the admin account exists.
+3. Sign in, create a trip, and confirm it appears after a page reload.
+4. Create a backup from the Backup tab. Confirm the archive holds `travel.pgdump` and no `travel.db`.
+5. Restore that backup from the Backup tab. Confirm the trip is still there, and that `data/pre-restore-.pgdump` exists.
+6. Stop and start TREK. Confirm the data is unchanged and no migration runs.
+7. Upgrade: start an older image on a database, then the newer image. Confirm `data/pre-migrate--.pgdump` appears, and that the migration runs.
+8. Roll back with the copy, as described in [Updating](Updating#rolling-back-a-postgres-update), and confirm the older image starts on it.
+9. Record the server version, the client tool versions, and any error text that differs from this page.
+
+## See also
+
+- [Environment Variables](Environment-Variables)
+- [Backups](Backups)
+- [Updating](Updating)
+- [Troubleshooting](Troubleshooting)
+- [Install: Docker Compose](Install-Docker-Compose)
+- [Install: Helm](Install-Helm)
diff --git a/wiki/Environment-Variables.md b/wiki/Environment-Variables.md
index 3819852c4..1fd7a3e87 100644
--- a/wiki/Environment-Variables.md
+++ b/wiki/Environment-Variables.md
@@ -507,12 +507,17 @@ through environment variables. `TREK_PLACE_PHOTO_DIR` below is unaffected.
| `BACKUP_UPLOAD_LIMIT_MB` | Maximum **compressed** size (in MB) of a restore-backup archive that may be uploaded. Raise it if your backups (which include the `uploads/` directory) exceed the default. Non-positive or invalid values abort startup. | `500` |
| `RESTORE_FROM_BACKUP` | Path to a backup ZIP to restore on the **first** start, before any setup. Only acts while no database exists; afterwards it is ignored with a warning. A backup that fails the restore checks stops the start. See [Backups](Backups#restoring-on-a-new-install-before-setup). | none |
| `BACKUP_MAX_DECOMPRESSED_MB` | Maximum **decompressed** size (in MB) of a restore-backup archive — the zip-bomb guard. Independent of `BACKUP_UPLOAD_LIMIT_MB` and enforced on both restore paths, so a restore that fits the upload cap can still be refused with `Backup exceeds the maximum decompressed size.` Raise both when restoring a very large instance. | `5120` (5 GB) |
-| `TREK_DB_FILE` | Explicit path of the database file. Unset, TREK uses `travel.db` in the data directory. | unset |
+| `TREK_DB_URL` | Run TREK on a PostgreSQL database instead of the SQLite file: `postgres://user:password@host:5432/database` (or `postgresql://`). Needs PostgreSQL 14 to 17 and an empty database of its own. Percent-encode special characters in the password. See [Database: Postgres](Database-Postgres). | unset (SQLite) |
+| `TREK_DB_POOL_MAX` | Most connections TREK opens to the PostgreSQL database, from 1 to 100. Unused on SQLite. | `10` |
+| `TREK_DB_PG_BIN_DIR` | Directory holding `pg_dump`, `pg_restore` and `psql`, which back up and restore a PostgreSQL database. Unset, TREK looks in `/usr/lib/postgresql/17` to `14` (`bin`), `/usr/local/bin` and `/usr/bin`. Unused on SQLite. See [Database: Postgres](Database-Postgres#where-the-client-tools-are-found). | unset |
+| `TREK_DB_FILE` | Explicit path of the SQLite database file. Unset, TREK uses `travel.db` in the data directory. Not used with `TREK_DB_URL`. | unset |
| `TREK_DB_JOURNAL_MODE` | SQLite [journal mode](https://sqlite.org/pragma.html#pragma_journal_mode): `DELETE`, `TRUNCATE`, `PERSIST`, `MEMORY`, `WAL` or `OFF`. Set `DELETE` when the data directory lives on network storage — see below. Values SQLite doesn't know log a warning and fall back. | `WAL` |
-| `TREK_DB_PRE_MIGRATE_SNAPSHOT` | Copy the database to `pre-migrate--.db` next to it before a startup applies pending migrations, so the previous image can be started on that copy again. If the copy cannot be written the startup refuses rather than migrate without one. Set `false` only to get past that when you cannot free space and have a backup of your own. See [Updating](Updating#database-migrations). | `true` |
+| `TREK_DB_PRE_MIGRATE_SNAPSHOT` | Copy the database to `pre-migrate--.db` next to it before a startup applies pending migrations, so the previous image can be started on that copy again. On PostgreSQL the copy is a `pg_dump` file, `pre-migrate--.pgdump`, and needs the client tools (see `TREK_DB_PG_BIN_DIR`). If the copy cannot be written the startup refuses rather than migrate without one. Set `false` only to get past that when you cannot free space and have a backup of your own. See [Updating](Updating#database-migrations). | `true` |
| `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP` | How many of those copies stay in the data directory, newest first (1 to 100). | `3` |
| `TREK_DB_SYNCHRONOUS` | SQLite [synchronous](https://sqlite.org/pragma.html#pragma_synchronous) level: `OFF`, `NORMAL`, `FULL` or `EXTRA`. The default follows the journal mode — `NORMAL` under WAL (what SQLite itself uses there), `FULL` otherwise, because a rollback journal at `NORMAL` can lose committed transactions on a power cut. | `NORMAL` / `FULL` |
+With `TREK_DB_URL` set, the SQLite variables (`TREK_DB_FILE`, `TREK_DB_JOURNAL_MODE`, `TREK_DB_SYNCHRONOUS`) do not apply, and setting any of them alongside `TREK_DB_URL` stops the startup. The pre-migration copy settings (`TREK_DB_PRE_MIGRATE_SNAPSHOT` and `TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP`) apply to both engines. The [Database: Postgres](Database-Postgres) page covers the connection, SSL and backups on PostgreSQL.
+
### Running the database on network storage
WAL is the right mode on a local disk and stays the default. It coordinates readers and writers through a shared-memory
diff --git a/wiki/Install-Docker-Compose.md b/wiki/Install-Docker-Compose.md
index 1379a9a9b..fb403a608 100644
--- a/wiki/Install-Docker-Compose.md
+++ b/wiki/Install-Docker-Compose.md
@@ -24,7 +24,7 @@ The compose file ships with several hardening options enabled by default:
| Host path | Container path | Contents |
|---|---|---|
-| `./data` | `/app/data` | SQLite database, logs, `.jwt_secret`, `.encryption_key` |
+| `./data` | `/app/data` | SQLite database (not used with `TREK_DB_URL`), logs, `.jwt_secret`, `.encryption_key`, pre-migration and pre-restore copies |
| `./uploads` | `/app/uploads` | Uploaded files (photos, documents, covers, avatars) |
### Named Volumes
@@ -116,6 +116,21 @@ This compose file is designed for deployments where a reverse proxy (nginx, Cadd
See [Reverse-Proxy](Reverse-Proxy) for complete proxy configuration examples.
+## Running on Postgres
+
+[`docker-compose.postgres.yml`](https://github.com/liketrek/TREK/blob/main/docker-compose.postgres.yml) runs TREK beside a `postgres:17` service instead of the SQLite file. Its `db` service keeps the database in a named volume and publishes no port, and the `trek` service waits for the database's health check before it starts.
+
+Put a password in a `.env` file beside the compose file, then start it:
+
+```bash
+echo 'POSTGRES_PASSWORD=' > .env
+docker compose -f docker-compose.postgres.yml up -d
+```
+
+The password goes into `TREK_DB_URL`, so use URL-safe characters or percent-encode it. Without `POSTGRES_PASSWORD` the database does not initialize and TREK does not start. To run it against a database that already exists elsewhere, use the plain `docker-compose.yml` and set `TREK_DB_URL` yourself.
+
+Read [Database: Postgres](Database-Postgres) before you use it. It covers the requirements, SSL, backups and restores, and the limits of a Postgres install.
+
## Next Steps
- [Environment-Variables](Environment-Variables) — full variable reference
diff --git a/wiki/Install-Helm.md b/wiki/Install-Helm.md
index b51846b9b..9977ca474 100644
--- a/wiki/Install-Helm.md
+++ b/wiki/Install-Helm.md
@@ -177,6 +177,33 @@ Every value the chart writes into its Secret is quoted, so a password such as `1
> **Note:** Without `UNSPLASH_ACCESS_KEY` the server queries Unsplash's unauthenticated endpoint, which many datacenter and VPS IP ranges — including a lot of Kubernetes clusters — are blocked or rate-limited on. Trip-cover and place-image search then fail with **"Unsplash search unavailable"**. A free Access Key switches the server to Unsplash's authenticated API (`api.unsplash.com`), which is not subject to that block. The key can also be set in **Admin → Settings → API Keys**; this value takes priority over that one. See [Environment-Variables](Environment-Variables#image-search-unsplash).
+### Running on Postgres
+
+To run TREK on a PostgreSQL database, set `database.url`. The connection URL carries the password, so the chart writes it into its own Secret as `TREK_DB_URL` rather than into the ConfigMap:
+
+```yaml
+database:
+ url: "postgres://trek:@postgres.example.internal:5432/trek?sslmode=require"
+```
+
+To keep the URL out of your values file, create a Secret yourself and point the chart at it. The key holds the same URL:
+
+```bash
+kubectl create secret generic trek-db --from-literal=TREK_DB_URL='postgres://trek:@postgres.example.internal:5432/trek?sslmode=require'
+```
+
+```yaml
+database:
+ existingSecret: trek-db
+ existingSecretKey: TREK_DB_URL # the default
+```
+
+The chart then reads `TREK_DB_URL` from that Secret. If the key is missing, the pod does not start. It does not fall back to SQLite. The chart refuses to render when `database.url` and the top-level `existingSecret` are both set, because the chart's own Secret, which holds the URL, is not created in that case. Use `database.existingSecret` instead.
+
+The database must be empty and dedicated to TREK, with a role that is not a superuser. The data and uploads volumes are still needed: they hold the data directory, the uploads and the copies TREK takes before migrating. The PostgreSQL requirements, the SSL settings (`sslmode`, `sslrootcert` with a CA file mounted into the pod) and the backups are on [Database: Postgres](Database-Postgres).
+
+Keep `replicas: 1` on Postgres too. TREK keeps rate limits, pending codes, the permission cache and WebSocket connections in memory, so a second pod would not share them. See [Limitations](Database-Postgres#limitations).
+
### Persistent Storage
The data PVC holds the database, the plugins you install and the automatic `pre-migrate--.db` copy TREK writes before an upgrade migrates the database, so the previous image can run on that copy again. See [Updating](Updating).
@@ -339,7 +366,7 @@ Timeout fields are Standard Channel in Gateway API v1.2, though controller suppo
## Upgrade
-The Deployment uses the `Recreate` update strategy (`updateStrategy: Recreate`): the old pod stops before the new one starts. TREK is a single-writer SQLite application, and during a rolling update the new pod would migrate the database while the old one still writes to it. A ReadWriteMany volume does not make that safe, so leave the strategy as it is and keep one replica.
+The Deployment uses the `Recreate` update strategy (`updateStrategy: Recreate`): the old pod stops before the new one starts. The new pod migrates the database when it starts, and during a rolling update it would do that while the old pod still writes to it. On SQLite that is unsafe, and a ReadWriteMany volume does not make it safe either. On Postgres the database is no longer a file, but the strategy and the single replica stay for the in-memory state described above. Leave the strategy as it is and keep one replica.
The in-app Help pages come from the image (`/app/wiki`), so they match the version you upgrade to.
diff --git a/wiki/Troubleshooting.md b/wiki/Troubleshooting.md
index e0585bc54..359feb46a 100644
--- a/wiki/Troubleshooting.md
+++ b/wiki/Troubleshooting.md
@@ -651,3 +651,59 @@ See [Notifications](Notifications#web-push) for the details.
**Cause:** The server kept refusing a queued change (after eight attempts it counts as failed), or an edit you made offline collided with a newer one on the server.
**Fix:** The status pill at the bottom of the window reads **Failed to sync: N** or **Conflicts: N**. Open **Settings → Offline**. Under **Failed changes**, use **Try again** or **Discard** on each item. Under conflicts, choose **Keep mine** or **Keep theirs**. If the local copy itself looks wrong, **Clear cache** empties it and the next sync downloads it again. Unsynced changes are kept when a trip is cleared from the device. See [Offline-Mode-and-PWA](Offline-Mode-and-PWA).
+
+---
+
+## Postgres: TREK refuses to start because the database holds tables
+
+**Cause:** `TREK_DB_URL` points at a database that already has tables, and none of them is TREK's migration history. TREK creates its schema only in an empty database, and it does not adopt tables another application made. The log reads `[DB] Refusing to boot: the database holds N table(s) (...) but no TREK migration history.` Nothing was written to the database.
+
+**Fix:** Create a new, empty database for TREK, or point `TREK_DB_URL` at the one TREK already migrated. If this database is the wrong one, check the URL. See [Database-Postgres](Database-Postgres#first-boot).
+
+---
+
+## Postgres: TREK refuses to start because a copy before migrating could not be written
+
+**Cause:** Before it applies pending migrations, TREK writes a `pg_dump` copy to `data/pre-migrate--.pgdump`. If `pg_dump` is not installed where TREK looks, or the data directory is full or read-only, the copy fails and TREK refuses to migrate. The log says `could not write a copy of the database ... Nothing was migrated.`
+
+**Fix:** Install the PostgreSQL client tools (the Docker image has them) or set `TREK_DB_PG_BIN_DIR` to the directory that holds `pg_dump`. Free disk space or fix the permissions on `data/`, then start again. To migrate without a copy, set `TREK_DB_PRE_MIGRATE_SNAPSHOT=false`, but only when you have a backup of your own. See [Updating](Updating#database-migrations).
+
+---
+
+## Postgres: "pg_dump was not found" when backing up or restoring
+
+**Cause:** The backup, restore or pre-migration copy runs `pg_dump`, `pg_restore` or `psql` by its absolute path. None of them is in the directories TREK searches (`/usr/lib/postgresql/17` down to `14`, `/usr/local/bin`, `/usr/bin`), and `PATH` is not searched.
+
+**Fix:** Install the `postgresql-client` package, or set `TREK_DB_PG_BIN_DIR` to the directory that holds the three tools. The Docker image already includes the version 17 client. See [Database-Postgres](Database-Postgres#where-the-client-tools-are-found).
+
+---
+
+## Postgres: a backup or restore fails with "server version mismatch" or a version error
+
+**Cause:** Two different problems produce messages about versions. `pg_dump` from version 17 cannot dump a server running version 18 or later, and fails with `aborting because of server version mismatch`. A restore also refuses a `pg_restore` or `psql` older than 17.6, 16.10, 15.14 or 14.19, because those versions cannot restore a dump safely (CVE-2025-8714). The error names the version it found.
+
+**Fix:** For a newer server, point `TREK_DB_PG_BIN_DIR` at client tools of the same major version as the server. For an old client, install a newer `postgresql-client` package, or use a newer image. The Docker image's client is version 17, so it works with servers 14 to 17.
+
+---
+
+## Postgres: TREK refuses to start because of the sslmode in TREK_DB_URL
+
+**Cause:** The `sslmode` parameter is not one TREK accepts. TREK accepts `disable`, `require`, `verify-ca` and `verify-full`. A value such as `prefer`, `allow` or `no-verify`, or a misspelling, would connect without verifying the server, so TREK refuses to start. A `sslrootcert` that names a file the container cannot read stops the start the same way.
+
+**Fix:** Set `sslmode` to one of the four accepted values, and mount the CA file and point `sslrootcert` at its path inside the container. See [Database-Postgres](Database-Postgres#ssl).
+
+---
+
+## Postgres: TREK refuses to start because TREK_DB_URL is set with a SQLite variable
+
+**Cause:** `TREK_DB_URL` is set together with `TREK_DB_FILE`, `TREK_DB_JOURNAL_MODE` or `TREK_DB_SYNCHRONOUS`. TREK cannot tell which database those variables mean, so it refuses to start.
+
+**Fix:** Unset the SQLite variables. See [Environment-Variables](Environment-Variables).
+
+---
+
+## Postgres: a restore refuses the archive
+
+**Cause:** The archive is not a Postgres backup, or it cannot be loaded here. The message says which: `This backup holds a SQLite database, which cannot be restored into a Postgres database.` (a backup from a SQLite install), `The uploaded dump holds no TREK migration history.` (a dump that is not from TREK), or `This backup was made by a newer TREK version.` A SQLite backup cannot be restored into PostgreSQL, and a PostgreSQL backup cannot be restored into SQLite.
+
+**Fix:** Restore a backup taken on the same engine. For a newer backup, update TREK first. The database being replaced is kept as `data/pre-restore-.pgdump` before the load starts. An archive that is refused is not loaded, so the database itself is unchanged. See [Backups](Backups#restoring-a-backup).
diff --git a/wiki/Updating.md b/wiki/Updating.md
index ea185c692..36ddb9b65 100644
--- a/wiki/Updating.md
+++ b/wiki/Updating.md
@@ -84,7 +84,7 @@ TREK runs any pending database migrations automatically at startup. No manual mi
Migrations only go forward, so going back to an older image means going back to a copy of the database from before the update. Images from 5.0 on refuse to start on a database a newer TREK has migrated. Images from 4.3.x and earlier do not check this: they start on the newer database without a warning, so restore the copy before you start one of them.
-TREK takes that copy itself. Right before a startup applies pending migrations, it writes the database to `data/pre-migrate--.db` (next to `travel.db`, or next to the file `TREK_DB_FILE` names) and logs the path. `