feat(db): run TREK on PostgreSQL

TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before
otherwise: an engine port under src/db/engine, the dialect helpers under
src/db/dialect, a Postgres baseline for the migrations up to the cut,
pg_dump/pg_restore backups, a probe of every repository method against a
real server and a Postgres test lane in CI.
This commit is contained in:
Isaias Tavares
2026-11-05 01:15:21 -03:00
committed by Maurice
parent 17b47b38b9
commit 2e3762afe1
176 changed files with 6728 additions and 777 deletions
+126 -4
View File
@@ -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
+4 -1
View File
@@ -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.
+9 -3
View File
@@ -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
+4
View File
@@ -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.
<br />
<h2 id="helm-kubernetes">Helm (Kubernetes)</h2>
+1
View File
@@ -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.
+17 -4
View File
@@ -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:
+6
View File
@@ -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 }}
+20 -4
View File
@@ -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:<password>@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.
+87
View File
@@ -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=<a long random value>
# 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:
+1 -19
View File
@@ -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",
+5 -2
View File
@@ -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-<schema>-<time>.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-<schema>-<time>.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.
+11 -5
View File
File diff suppressed because one or more lines are too long
+338
View File
@@ -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"
]
}
+825
View File
@@ -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();
+5 -1
View File
@@ -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",
-3
View File
@@ -26,9 +26,6 @@
"src/db/entities/PluginUserErasureQueue.entity.ts": {
"datetime": 1
},
"src/db/repositories/MaintenanceRepository.ts": {
"pragma": 1
},
"src/nest/backup/backup-archive.ts": {
"pragma": 1
}
+9 -2
View File
@@ -124,9 +124,16 @@ export function classify(file) {
return 'source';
}
/** PRAGMA is fine in src/db/*.ts (connection, durability, baseline, snapshot) and the migrations. */
/**
* PRAGMA is fine in src/db/*.ts (connection, durability, baseline, snapshot),
* the migrations and the SQLite engine's own folder (src/db/engine/sqlite/).
*/
export function pragmaAllowed(file) {
return /^src\/db\/[^/]+\.ts$/.test(file) || file.startsWith('src/db/migrations/');
return (
/^src\/db\/[^/]+\.ts$/.test(file) ||
file.startsWith('src/db/migrations/') ||
file.startsWith('src/db/engine/sqlite/')
);
}
function isSqlTag(node) {
@@ -0,0 +1,3 @@
{
"counts": {}
}
@@ -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);
+229
View File
@@ -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<string> = knownMigrationNames()): Promise<BaselinePass> {
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<string, unknown>;
/** 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<MigratedSchema> {
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<void> {
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<void> {
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<void> {
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);
});
}
+2 -19
View File
@@ -1,21 +1,4 @@
{
"failing": {
"DayAccommodationsRepository.listForTripWithPlaceAndBooking": 1,
"DayAccommodationsRepository.listRoadtripStays": 1,
"GoogleApiUsageRepository.recordCall": 1,
"MaintenanceRepository.vacuumInto": 1,
"MaintenanceRepository.walCheckpoint": 1,
"ReservationDayPositionsRepository.upsertScoped": 1,
"ReservationsRepository.findWithJoins": 1,
"ReservationsRepository.listForCalendar": 1,
"ReservationsRepository.listForTrip": 1,
"ReservationsRepository.listPublicStaysForCalendar": 1,
"RouteUsageDailyRepository.record": 1
},
"uncovered": [
"BucketListRepository.update",
"BudgetItemsRepository.update",
"PackingBagsRepository.update",
"TodoItemsRepository.update"
]
"failing": {},
"uncovered": []
}
+6 -5
View File
@@ -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
+19 -4
View File
@@ -4,7 +4,8 @@
* The probe has no fixtures: it reads every repository class with the
* TypeScript checker and builds one sample argument list per public method
* from the declared parameter types (a number is 1, a string is "probe" or a
* date-shaped string when the name says so, an object gets every property
* date-shaped string when the name says so, a boolean false unless it is the
* `present` flag of a partial update's pair, an object gets every property
* it can sample, an array one element). The values only have to get the
* method to send its SQL; what the probe measures is whether Postgres
* accepts that SQL against an empty schema. A sample the column cannot take
@@ -24,6 +25,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);
}
+11 -7
View File
@@ -11,9 +11,9 @@
* `--update` lowers entries and drops the ones that reach zero or whose
* method is gone; it never raises or adds one.
*
* Only statements Postgres refuses as SQL count here (SQLSTATE classes 42
* and 0A, see `countsTowardRatchet`). A value the sample arguments got wrong
* (class 22, a sample string in a date column) says nothing about the
* Statements Postgres refuses as SQL or over a value's type count here
* (SQLSTATE classes 42, 0A and 22, see `countsTowardRatchet`). A constraint
* the sample values break (class 23) or a timeout says nothing about the
* dialect and is reported without being held.
*
* `uncovered` lists the methods whose SQL the probe did not see: the ones it
@@ -56,12 +56,16 @@ export interface BaselineVerdict {
/**
* Whether a refused statement counts toward the ratchet: SQLSTATE class 42
* (syntax error, unknown function, operator or column, datatype mismatch)
* and class 0A (feature not supported) are what a SQLite-only spelling gets.
* Anything else (a data exception such as 22P02 or 22007, a constraint, a
* timeout) comes from the sample values or the empty tables, not the SQL.
* and class 0A (feature not supported) are what a SQLite-only spelling gets,
* and class 22 (a data exception such as 22P02 or 22007) is a value of the
* wrong type reaching a column, which SQLite compares where Postgres refuses
* (a query-string id bound to an integer column). The sampler sends what
* each parameter declares, so a class 22 refusal is the code's, not the
* sample's. A constraint (class 23) or a timeout comes from the sample values
* or the empty tables, not the SQL.
*/
export function countsTowardRatchet(code: string): boolean {
return /^(42|0A)/.test(code);
return /^(42|0A|22)/.test(code);
}
const METHOD_KEY = /^[A-Za-z_$][\w$]*\.[A-Za-z_$][\w$]*$/;
+4 -3
View File
@@ -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);
+13
View File
@@ -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' },
+8 -6
View File
@@ -22,7 +22,7 @@ export interface FailedStatement {
code: string;
message: string;
sql: string;
/** A dialect refusal (SQLSTATE class 42 or 0A) the ratchet holds; anything else is reported only. */
/** A refusal the ratchet holds (SQLSTATE class 42, 0A or 22); anything else is reported only. */
counted: boolean;
}
@@ -38,8 +38,9 @@ export interface ProbeSummary {
/** Every distinct statement Postgres refused, counted toward the ratchet or not. */
failedStatements: FailedStatement[];
/**
* `Class.method` → distinct statements Postgres refused as SQL (SQLSTATE
* class 42 or 0A); every called method appears, zeros included.
* `Class.method` → distinct statements Postgres refused as SQL or over a
* value's type (SQLSTATE class 42, 0A or 22); every called method appears,
* zeros included.
*/
failingByMethod: Map<string, number>;
/**
@@ -192,7 +193,8 @@ export function formatConsole(input: ReportInput): string {
const refusals = splitRefusals(summary);
lines.push(
`Statements: ${summary.statements} distinct, ${summary.failedStatements.length} refused by Postgres ` +
`(${refusals.counted.length} as SQL, held to the baseline; ${refusals.reported.length} over a sample value or the empty tables, reported only).`,
`(${refusals.counted.length} as SQL or over a value's type, held to the baseline; ` +
`${refusals.reported.length} over a constraint, a timeout or the empty tables, reported only).`,
);
for (const [code, count] of [...summary.failuresByCode.entries()].sort((a, b) => b[1] - a[1])) {
lines.push(` ${code}: ${count}${countsTowardRatchet(code) ? '' : ' (not held)'}`);
@@ -254,8 +256,8 @@ export function formatMarkdown(input: ReportInput): string {
);
out.push(`| Methods whose SQL the probe did not see | ${summary.uncovered.size} |`);
out.push(`| Distinct statements | ${summary.statements} |`);
out.push(`| Refused by Postgres as SQL (held) | ${refusals.counted.length} |`);
out.push(`| Refused over a sample value or the empty tables (reported only) | ${refusals.reported.length} |`);
out.push(`| Refused by Postgres as SQL or over a value's type (held) | ${refusals.counted.length} |`);
out.push(`| Refused over a constraint, a timeout or the empty tables (reported only) | ${refusals.reported.length} |`);
out.push('');
if (probedNothing(summary))
out.push('No repository method was called, so there is nothing to hold to the baseline.', '');
-106
View File
@@ -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;
}
+3 -3
View File
@@ -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,
@@ -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,
+14
View File
@@ -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;
+17
View File
@@ -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<typeof deriveDb>;
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,
+14
View File
@@ -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
+21 -1
View File
@@ -33,6 +33,7 @@ export const SECRET_ENV_KEYS: ReadonlySet<string> = 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.
*
+1
View File
@@ -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,
+37
View File
@@ -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';
+11
View File
@@ -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
+14 -5
View File
@@ -18,13 +18,22 @@ export async function ensureEmailCaseIndex(connection: Connection): Promise<void
const pending: unknown[] = await connection.execute(`SELECT value FROM app_settings WHERE key = ?`, [PENDING_KEY]);
if (pending.length === 0) return;
const duplicates: Array<{ ids: string }> = 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<string, number[]>();
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;
+49
View File
@@ -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<Options>;
/**
* 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>): 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<void>;
/**
* 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<void>;
/**
* Runs first in every `UnitOfWork` transaction, on its EntityManager: makes
* the transactions that write run one at a time, as SQLite's single writer
* does, so a `MAX(...) + 1` read inside one cannot race another.
*/
serializeWrites(em: EntityManager): Promise<void>;
}
+58
View File
@@ -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;
}
+93
View File
@@ -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;
}
@@ -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$;
`;
+119
View File
@@ -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<string, string> {
const parsed = new URL(url);
const env: Record<string, string> = {
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 `<tool> failed: <its stderr, summarized>`, with the original error as
* the cause.
*/
export async function run(
tool: PgTool,
binDir: string | undefined,
args: string[],
env: Record<string, string>,
): Promise<string> {
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 });
}
}
+206
View File
@@ -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<PgSchema> {
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<void> {
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<boolean> {
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<void> {
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<string[]> {
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<string[]> {
const script = await run(
'pg_restore',
binDir,
['--data-only', '--table=mikro_orm_migrations', '--file=-', '--', source],
{},
);
return migrationNamesFromCopy(script);
}
@@ -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<PostgreSqlConnection['mapOptions']>[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);
}
}
@@ -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<DbEngineSettings, { engine: 'postgres' }>;
/** 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<void>) | 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>): 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<void> {
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<void> {
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<void> {
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 },
};
}
}
@@ -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<Record<string, readonly string[]>> = {
// 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()',
],
};
@@ -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 <random key>`, 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<Partial<Record<number, number>>> = { 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<void> {
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.',
);
}
@@ -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;
}
@@ -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<string>;
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<string[]> {
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<Migrator['getStorage']>,
tables: readonly string[],
historyTable: string,
baseline: () => PostgresBaseline,
): Promise<void> {
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<void> {
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<void> {
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
* `<data>/pre-migrate-<schema>-<time>.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) };
}
@@ -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));
}
+42
View File
@@ -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 });
}
}
+17
View File
@@ -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 '<unparseable URL>';
}
if (parsed.password) parsed.password = '***';
parsed.search = '';
parsed.hash = '';
return parsed.toString();
}
@@ -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>): void {
getRawConnection();
registerReinitializeHook(async () => {
const connection = orm.em.getConnection();
await connection.close(true);
await connection.connect();
await onReopened();
});
}
close(): void {
closeDb();
}
reopen(): Promise<void> {
return reinitialize();
}
schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void> {
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<void> {
return Promise.resolve();
}
}
@@ -0,0 +1,49 @@
import type { EntityManager } from '@mikro-orm/core';
/**
* The SQLite engine's whole-file maintenance: the two statements a backup,
* a restore and the demo reset need that are about the database file rather
* than any table (`PRAGMA wal_checkpoint`, `VACUUM INTO`). Neither has a
* Postgres spelling, so they live under the engine that owns the file instead
* of in `MaintenanceRepository`, whose remaining statements run on every
* engine (and which the Postgres probe calls method by method).
*
* Provided through Nest DI by `MaintenanceModule` (`nest/database/`), a
* factory over the context-resolving global `EntityManager`, and injected by
* the SQLite backup port (`SqliteDatabaseBackup`). Free of Nest decorators
* like every other file under `db/`, so a test can build one over its own
* `EntityManager`.
*/
export class SqliteMaintenance {
constructor(private readonly em: EntityManager) {}
/**
* MikroORM's own validating `getContext()`: throws outside a request
* context, the fail-closed check `TrekRepository` applies to every
* repository, since a raw `connection.execute()` has no such guard.
*/
private validateRequestContext(): void {
this.em.getContext();
}
/**
* `PRAGMA wal_checkpoint(TRUNCATE)`: flushes the WAL into the main file
* before a snapshot or a swap. Reached through the backup port's
* `checkpoint()`; every caller treats it as best effort.
*/
async walCheckpoint(): Promise<void> {
this.validateRequestContext();
await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
}
/**
* `` VACUUM INTO '<path>' ``: a consistent point-in-time copy of the file
* under concurrent writers, reached through the backup port's
* `snapshot()`. Single quotes in `path` are escaped by doubling.
*/
async vacuumInto(path: string): Promise<void> {
this.validateRequestContext();
const escaped = path.replaceAll("'", "''");
await this.em.getConnection().execute(`VACUUM INTO '${escaped}'`);
}
}
+21 -11
View File
@@ -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<void> {
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<boolean> {
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<string | null> {
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<Set<string>> {
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,
+15 -35
View File
@@ -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<void> {
export async function runSchemaBootstrap(orm: AnyOrm, { snapshot }: { snapshot: boolean }): Promise<void> {
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<void> {
// 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<AnyOrm> {
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 },
});
}
+7 -8
View File
@@ -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<boolean> {
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) {
@@ -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<DayAccommodation
* parity keeps the duplicates, no `DISTINCT`.
*/
async listForTripWithPlaceAndBooking(trip_id: number | string): Promise<DayAccommodationListRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<DayAccommodationListKyselyDB>()
.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<DayAccommodation
* this table (see {@link DayAccommodationListKyselyDB}).
*/
async listRoadtripStays(trip_id: number): Promise<RoadtripStayRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<RoadtripStaysKyselyDB>()
.selectFrom('day_accommodations as a')
.leftJoin('places as p', 'p.id', 'a.place_id')
@@ -485,7 +490,7 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
eb
.selectFrom('reservations as r')
.select((eb2) => eb2.fn.min<number | null>('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)
@@ -43,7 +43,9 @@ export class GoogleApiUsageRepository extends TrekRepository<GoogleApiUsage> {
await this.kysely<GoogleApiUsageKyselyDB>()
.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();
}
@@ -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<Entity>` — 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<Entity>`, 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<void> {
this.validateRequestContext();
await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
}
/**
* `` VACUUM INTO '<path>' `` — 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<void> {
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<void> {
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;
}
}
}
@@ -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<Places> {
*/
async listForTrip(trip_id: string, filters: {
searchPattern?: string;
category?: string;
tag?: string;
category?: number;
tag?: number;
assignment?: 'all' | 'unassigned' | 'assigned';
}): Promise<PlaceListRow[]> {
const platform = this.getEntityManager().getPlatform();
@@ -680,15 +680,13 @@ export class PlacesRepository extends TrekRepository<Places> {
.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') {
@@ -52,10 +52,14 @@ export class ReservationDayPositionsRepository extends TrekRepository<Reservatio
* position) SELECT r.id, d.id, ? FROM reservations r JOIN days d ON
* d.trip_id = r.trip_id WHERE r.id = ? AND d.id = ? AND r.trip_id = ?`.
*
* Kysely `insertInto().orReplace().columns().expression(selectFrom…)`
* (the MikroORM QueryBuilder's `insertFrom` has no `OR REPLACE` flag —
* `.onConflict().merge()` is semantically different, a real UPDATE rather
* than REPLACE's delete-then-insert). The trip scoping lives IN the
* Kysely `insertInto().columns().expression(selectFrom…)` with an
* `ON CONFLICT (reservation_id, day_id) DO UPDATE SET position`, which both
* engines read (`INSERT OR REPLACE` is SQLite only). The pair is the
* table's whole primary key and `position` its only other column, so the
* update leaves the same row REPLACE's delete-then-insert did, and no
* foreign key points at this table for the delete to have cascaded to.
* The SELECT keeps its WHERE, which SQLite needs to read the trailing
* ON CONFLICT as the upsert clause. The trip scoping lives IN the
* SELECT (a foreign `reservation_id`/`day_id` materialises zero rows to
* insert, never a foreign-key error) — preserved exactly, not hoisted
* into a pre-check.
@@ -68,7 +72,6 @@ export class ReservationDayPositionsRepository extends TrekRepository<Reservatio
): Promise<void> {
await this.kysely<ReservationDayPositionsKyselyDB>()
.insertInto('reservation_day_positions')
.orReplace()
.columns(['reservation_id', 'day_id', 'position'])
.expression((eb) =>
eb
@@ -79,6 +82,9 @@ export class ReservationDayPositionsRepository extends TrekRepository<Reservatio
.where('d.id', '=', day_id)
.where('r.trip_id', '=', trip_id as number),
)
.onConflict((oc) =>
oc.columns(['reservation_id', 'day_id']).doUpdateSet((eb) => ({ position: eb.ref('excluded.position') })),
)
.execute();
}
}
@@ -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<Reservations> {
findPassContext(id: number, tripId: number) { return passContextQuery(this.kysely<PassContextDB>(), id, tripId); }
private joinedQuery() {
return joinedReservationQuery(this.kysely<ReservationJoinDB>());
return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());
}
/**
@@ -875,10 +864,13 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
/** CL2 — see {@link CalendarReservationRow}'s docstring. */
async listForCalendar(trip_id: number | string): Promise<CalendarReservationRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<CalendarReservationKyselyDB>()
.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<Reservations> {
eb
.selectFrom('reservations as r2')
.select((eb2) => eb2.fn.min<number | null>('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<Reservations> {
/** CL7 — see {@link CalendarStayRow}'s docstring. */
async listPublicStaysForCalendar(trip_id: number | string): Promise<CalendarStayRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<CalendarStayKyselyDB>()
.selectFrom('day_accommodations as a')
.where('a.trip_id', '=', trip_id as number)
@@ -953,7 +946,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
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<Reservations> {
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<Reservations> {
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),
@@ -80,12 +80,14 @@ export class RouteUsageDailyRepository extends TrekRepository<RouteUsageDaily> {
await this.kysely<RouteUsageDailyKyselyDB>()
.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();
@@ -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<Trips> {
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.
@@ -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(<reservation ref> AS INTEGER) = <stay ref>`. 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<DB, TB extends keyof DB>(
platform: Platform,
eb: ExpressionBuilder<DB, TB>,
reservationAccommodationId: StringReference<DB, TB>,
stayId: StringReference<DB, TB>,
): ExpressionWrapper<DB, TB, SqlBool> {
return eb(castIntegerKysely(platform, eb, reservationAccommodationId), '=', eb.ref(stayId).$castTo<number>());
}
@@ -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<DB, 'reservations' | 'days' | 'places' | 'd
* A booking with its day number, its place's name and its stay's place and
* times: the projection `ReservationsRepository.listForTrip` and
* `findWithJoins` narrow with their own `WHERE`. Kept out of the repository
* file, which is at its size baseline.
* file, which is at its size baseline. `accommodation_id` is TEXT and `ap.id`
* INTEGER, so the stay is matched through accommodationIdMatches: SQLite
* coerces the bare comparison, Postgres refuses it.
*/
export function joinedReservationQuery(kysely: Kysely<ReservationJoinDB>) {
export function joinedReservationQuery(kysely: Kysely<ReservationJoinDB>, 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([
+32
View File
@@ -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<AnyOrm> {
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 },
});
}
+2
View File
@@ -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,
});
}
+13 -29
View File
@@ -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());
+40 -19
View File
@@ -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<string>,
unrun: Set<string>,
): Promise<void> {
if (unrun.size === 0) return;
const pending = new Set(unrun);
unrun.clear();
const outcomes = new Map<string, Outcome>();
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<void>,
settle?: () => Promise<void>,
): Promise<void> {
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();
}
}
}
+10 -4
View File
@@ -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<RestoreResult> {
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 };
+39 -9
View File
@@ -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<string | null> {
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<string | null> {
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<void> {
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<BootRestoreOutcome> {
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<BootRes
const refused = await extractBackupArchive(plan.archive, staging);
if (refused) throw new BootRestoreError(`RESTORE_FROM_BACKUP: ${refused.error}`);
const unreadable = checkBackupDatabase(staging);
const unreadable = await checkArchiveDatabase(plan, staging);
if (unreadable) {
fs.rmSync(staging, { recursive: true, force: true });
throw new BootRestoreError(`RESTORE_FROM_BACKUP: ${unreadable.error}`);
throw new BootRestoreError(`RESTORE_FROM_BACKUP: ${unreadable}`);
}
// Storage reclaim drops what the restored database knew about its storage on
// the start that follows, even when the backup is this installation's own.
markReclaimResetPending(plan.reclaimResetFile);
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);
await putDatabaseInPlace(plan, staging);
// The key the archive's secrets were encrypted with. An ENCRYPTION_KEY in the
// environment still wins over the file, exactly as on any other start.
@@ -1,11 +1,15 @@
import { DATABASE_BACKUP } from '../database/database-backup.interface';
import type { DatabaseEngine } from '../../db/engine';
import { DATABASE_BACKUP, type DatabaseBackupStrategy } from '../database/database-backup.interface';
import { DatabaseEngineModule, DATABASE_ENGINE } from '../database/database-engine.module';
import { DatabaseLifecycleModule } from '../database/database-lifecycle.module';
import { MaintenanceModule } from '../database/maintenance.module';
import { PostgresDatabaseBackup } from './postgres-database-backup';
import { SqliteDatabaseBackup } from './sqlite-database-backup';
import { Global, Module } from '@nestjs/common';
/**
* Binds the database backup port (`DATABASE_BACKUP`) to the engine in use.
* Binds the database backup port (`DATABASE_BACKUP`) to the engine in use
* (`TREK_DB_URL`, the one `DATABASE_ENGINE` provides).
*
* Global because its consumers sit in two domains: the backup domain itself
* and the admin domain (the demo reset job and the baseline route). Admin
@@ -19,8 +23,20 @@ import { Global, Module } from '@nestjs/common';
*/
@Global()
@Module({
imports: [DatabaseLifecycleModule, MaintenanceModule],
providers: [SqliteDatabaseBackup, { provide: DATABASE_BACKUP, useExisting: SqliteDatabaseBackup }],
imports: [DatabaseEngineModule, DatabaseLifecycleModule, MaintenanceModule],
providers: [
SqliteDatabaseBackup,
PostgresDatabaseBackup,
{
provide: DATABASE_BACKUP,
useFactory: (
engine: DatabaseEngine,
sqlite: SqliteDatabaseBackup,
postgres: PostgresDatabaseBackup,
): DatabaseBackupStrategy => ({ sqlite, postgres })[engine.id],
inject: [DATABASE_ENGINE, SqliteDatabaseBackup, PostgresDatabaseBackup],
},
],
exports: [DATABASE_BACKUP],
})
export class DatabaseBackupModule {}
+58
View File
@@ -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<void>): Promise<Outcome> {
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<void>,
reopen: () => Promise<void>,
): 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<void>,
): Promise<string | null> {
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;
}
}
@@ -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<string | null> {
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<typeof postgresTarget>;
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<void> {
// 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<void> {
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<void> {
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<string | null> {
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(),
);
}
}
@@ -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<void> {
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<string | null> {
/** A copy of the database a restore is about to replace, next to it: the swap deletes the current file. */
keepCopyBeforeRestore(): Promise<string | null> {
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);
}
+2
View File
@@ -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));
@@ -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<void>;
/**
* 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<void>;
/**
* 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
@@ -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 {}
@@ -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 {}
@@ -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<void>> = [];
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<void> {
// 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<void> {
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<void> {
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<void> {
await this.engine.schemaBootstrap(this.orm, { snapshot: false });
for (const listener of this.reopenListeners) await listener();
}
}
+12 -5
View File
@@ -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 {}
+6 -4
View File
@@ -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 {}
+16 -3
View File
@@ -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<T>(fn: () => Promise<T>): Promise<T> {
return this.em.transactional(() => fn());
return this.em.transactional(async (em) => {
await this.engine.serializeWrites(em);
return fn();
});
}
}
+17
View File
@@ -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
+7 -9
View File
@@ -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);
+3 -2
View File
@@ -18,14 +18,15 @@ async function main(): Promise<void> {
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 {
+27
View File
@@ -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);
+124
View File
@@ -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 `<bin>/<name>.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<string, string>;
}
/** 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<string, string> = {};
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));
}
+1 -1
View File
@@ -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')),
+2 -1
View File
@@ -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<Ad
t.repo(PhotoProviderFields),
t.repo(AppSettings),
t.repo(Users),
new UnitOfWork(t.em),
new UnitOfWork(t.em, new SqliteEngine()),
);
}
+2 -1
View File
@@ -1,3 +1,4 @@
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
import { AssignmentParticipants } from '../../src/db/entities/AssignmentParticipants.entity';
import { Categories } from '../../src/db/entities/Categories.entity';
@@ -143,7 +144,7 @@ export function createTestUnitOfWork(db: Database.Database): Promise<UnitOfWork>
// 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;
}
+2 -1
View File
@@ -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),
);
});
@@ -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),
);
});
+2 -1
View File
@@ -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),
);
});
+17
View File
@@ -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([]);
}
});
});
// ─────────────────────────────────────────────────────────────────────────────
@@ -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,
@@ -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<PluginRun
const audit = new AuditService(t.repo(AuditLog), t.repo(Users));
const addons = await createTestAddonsService(testDb);
const userSettings = new PluginUserSettingsService(t.repo(PluginSettingsFields), t.repo(PluginUserConfig));
const uow = new UnitOfWork(t.em);
const uow = new UnitOfWork(t.em, new SqliteEngine());
return new PluginRuntimeService(
audit,
addons,
@@ -218,7 +219,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
t.repo(PluginCapabilityAudit),
t.repo(Settings),
t.repo(NotificationChannelPreferences),
new UnitOfWork(t.em),
new UnitOfWork(t.em, new SqliteEngine()),
fakeRegistry(['db:own']),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -254,7 +255,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
t.repo(PluginCapabilityAudit),
t.repo(Settings),
t.repo(NotificationChannelPreferences),
new UnitOfWork(t.em),
new UnitOfWork(t.em, new SqliteEngine()),
fakeRegistry(['db:own', 'db:read:trips']),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -303,7 +304,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
t.repo(PluginCapabilityAudit),
t.repo(Settings),
t.repo(NotificationChannelPreferences),
new UnitOfWork(t.em),
new UnitOfWork(t.em, new SqliteEngine()),
registry,
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -342,7 +343,7 @@ describe('Plan 3j Task 2 — R-install-gates named accept+refuse pairs (PR17/PR2
t.repo(PluginCapabilityAudit),
t.repo(Settings),
t.repo(NotificationChannelPreferences),
new UnitOfWork(t.em),
new UnitOfWork(t.em, new SqliteEngine()),
registry,
);
await expect(rtR.retrust('pr28-refuse', '2.0.0', 'NEWKEY', { userId: 1 })).rejects.toThrow(/nothing to re-trust/);

Some files were not shown because too many files have changed in this diff Show More