The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(db): reset the admin on Postgres and keep the key rotation off it
Maurice693acd5580c2
diff --git a/server/reset-admin.js b/server/reset-admin.jsindex d5061a983..e90204729 100644--- a/server/reset-admin.js+++ b/server/reset-admin.js@@ -7,13 +7,18 @@** Defaults to [email protected] with a generated password (printed below). The* account is flagged must_change_password, so you are prompted to set a new one- * on first login. Honours TREK_DB_FILE, TREK_DB_JOURNAL_MODE and- * TREK_DB_SYNCHRONOUS the same way the server does.+ * on first login.+ *+ * It works on the database the server runs on. With TREK_DB_URL set, that is+ * the Postgres database, reached through the server's own engine (its pool,+ * SSL settings and write lock, from the built server in dist/); no SQLite file+ * is opened or created then. Otherwise it is the SQLite file, honouring+ * TREK_DB_FILE, TREK_DB_JOURNAL_MODE and TREK_DB_SYNCHRONOUS the same way the+ * server does.*/const fs = require('fs');const path = require('path');const crypto = require('crypto');-const Database = require('better-sqlite3');const bcrypt = require('bcryptjs');// Kept in sync with the seeder/authService cost factor.@@ -23,91 +28,174 @@ const email = process.env.RESET_ADMIN_EMAIL || '[email protected]';const password = process.env.RESET_ADMIN_PASSWORD || crypto.randomBytes(12).toString('base64url');const generated = !process.env.RESET_ADMIN_PASSWORD;-// journal_mode is stored in the database file header, not per connection, so-// opening the file here decides the mode for the server too. Plain JS outside-// the Nest context can't reach src/app-config, hence the direct read — the-// defaults must match parsers.resolveDurability() (WAL + NORMAL, FULL once a-// rollback journal is in play). Run this via `docker exec` and the container's-// environment applies, same as for the server.-const journalModes = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'];-const syncLevels = ['OFF', 'NORMAL', 'FULL', 'EXTRA'];--const wantedJournal = (process.env.TREK_DB_JOURNAL_MODE || '').trim().toUpperCase();-const journalMode = journalModes.includes(wantedJournal) ? wantedJournal : 'WAL';-if (wantedJournal && journalMode !== wantedJournal) {- console.warn(`TREK_DB_JOURNAL_MODE="${process.env.TREK_DB_JOURNAL_MODE}" is not a SQLite journal mode — using ${journalMode}.`);+/** What the script prints once the account is reset or created. */+function report(outcome) {+ if (outcome.created) console.log(`\n✓ Admin account created: ${email} (username: ${outcome.username})`);+ else console.log(`\n✓ Admin password reset: ${email}`);+ if (generated) console.log(` Password: ${password}`);+ console.log(' You will be asked to change the password on first login.\n');}-const wantedSync = (process.env.TREK_DB_SYNCHRONOUS || '').trim().toUpperCase();-const defaultSync = journalMode === 'WAL' ? 'NORMAL' : 'FULL';-const synchronous = syncLevels.includes(wantedSync) ? wantedSync : defaultSync;-if (wantedSync && synchronous !== wantedSync) {- console.warn(`TREK_DB_SYNCHRONOUS="${process.env.TREK_DB_SYNCHRONOUS}" is not a SQLite synchronous level — using ${synchronous}.`);-}+/** The SQLite file the server runs on, reset with better-sqlite3. */+function resetOnSqlite() {+ const Database = require('better-sqlite3');-const dbPath = process.env.TREK_DB_FILE || path.join(__dirname, 'data/travel.db');-// SQLite silently opens an unwritable file read-only (#2933); db.readonly can't tell.-const notWritable = [dbPath, path.dirname(dbPath)].find((p) => {- if (!fs.existsSync(p)) return false;- try {- fs.accessSync(p, fs.constants.W_OK);- return false;- } catch {- return true;- }-});-if (notWritable) {- console.error(`Cannot write to ${notWritable} as uid ${process.getuid?.() ?? '?'}.`);- console.error('Run this script as the user that owns the data directory. In the Docker image that is node:');- console.error(' docker exec -it -u node trek node server/reset-admin.js');- process.exit(1);-}+ // journal_mode is stored in the database file header, not per connection, so+ // opening the file here decides the mode for the server too. Plain JS outside+ // the Nest context can't reach src/app-config, hence the direct read — the+ // defaults must match parsers.resolveDurability() (WAL + NORMAL, FULL once a+ // rollback journal is in play). Run this via `docker exec` and the container's+ // environment applies, same as for the server.+ const journalModes = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'];+ const syncLevels = ['OFF', 'NORMAL', 'FULL', 'EXTRA'];-const db = new Database(dbPath);-// Spelled out rather than left to better-sqlite3's default, so switching the-// journal mode below waits for a busy server instead of failing on the spot.-// Five seconds, same as src/db/database.ts.-db.exec('PRAGMA busy_timeout = 5000');-// Temp files in memory, like every connection src/db/connection.ts opens: a-// container with a read-only root and no tmpfs on /tmp has nowhere to put them-// (#2518).-db.exec('PRAGMA temp_store = MEMORY');-db.exec(`PRAGMA journal_mode = ${journalMode}`);-db.exec(`PRAGMA synchronous = ${synchronous}`);+ const wantedJournal = (process.env.TREK_DB_JOURNAL_MODE || '').trim().toUpperCase();+ const journalMode = journalModes.includes(wantedJournal) ? wantedJournal : 'WAL';+ if (wantedJournal && journalMode !== wantedJournal) {+ console.warn(`TREK_DB_JOURNAL_MODE="${process.env.TREK_DB_JOURNAL_MODE}" is not a SQLite journal mode — using ${journalMode}.`);+ }-const hash = bcrypt.hashSync(password, BCRYPT_COST);-// Case-insensitive like login, so [email protected] is found as [email protected].-const existing = db.prepare('SELECT id FROM users WHERE LOWER(email) = LOWER(?)').get(email);+ const wantedSync = (process.env.TREK_DB_SYNCHRONOUS || '').trim().toUpperCase();+ const defaultSync = journalMode === 'WAL' ? 'NORMAL' : 'FULL';+ const synchronous = syncLevels.includes(wantedSync) ? wantedSync : defaultSync;+ if (wantedSync && synchronous !== wantedSync) {+ console.warn(`TREK_DB_SYNCHRONOUS="${process.env.TREK_DB_SYNCHRONOUS}" is not a SQLite synchronous level — using ${synchronous}.`);+ }-if (existing) {- // password_version moves on, as on every password change in the app: it is- // what ends the sessions still signed in to an account that needed resetting.- db.prepare('UPDATE users SET password_hash = ?, role = ?, must_change_password = 1, password_version = COALESCE(password_version, 0) + 1 WHERE id = ?')- .run(hash, 'admin', existing.id);- // Its sessions end too, so the account's session list shows none of them- // as live. The table is missing on a database that no server tracking- // sessions has booted yet; the password_version bump above already refuses- // every token there.- const sessionsTable = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'user_sessions'").get();- if (sessionsTable) {- db.prepare('UPDATE user_sessions SET revoked_at = CURRENT_TIMESTAMP WHERE user_id = ? AND revoked_at IS NULL')- .run(existing.id);+ const dbPath = process.env.TREK_DB_FILE || path.join(__dirname, 'data/travel.db');+ // SQLite silently opens an unwritable file read-only (#2933); db.readonly can't tell.+ const notWritable = [dbPath, path.dirname(dbPath)].find((p) => {+ if (!fs.existsSync(p)) return false;+ try {+ fs.accessSync(p, fs.constants.W_OK);+ return false;+ } catch {+ return true;+ }+ });+ if (notWritable) {+ console.error(`Cannot write to ${notWritable} as uid ${process.getuid?.() ?? '?'}.`);+ console.error('Run this script as the user that owns the data directory. In the Docker image that is node:');+ console.error(' docker exec -it -u node trek node server/reset-admin.js');+ process.exit(1);}- console.log(`\n✓ Admin password reset: ${email}`);-} else {- // 'admin' is usually taken by the first-run seed — pick the first free username- // so the insert can't trip the UNIQUE(username) constraint.- let username = 'admin';- let n = 1;- while (db.prepare('SELECT 1 FROM users WHERE username = ?').get(username)) {- username = `admin${n++}`;++ const db = new Database(dbPath);+ // Spelled out rather than left to better-sqlite3's default, so switching the+ // journal mode below waits for a busy server instead of failing on the spot.+ // Five seconds, same as src/db/database.ts.+ db.exec('PRAGMA busy_timeout = 5000');+ // Temp files in memory, like every connection src/db/connection.ts opens: a+ // container with a read-only root and no tmpfs on /tmp has nowhere to put them+ // (#2518).+ db.exec('PRAGMA temp_store = MEMORY');+ db.exec(`PRAGMA journal_mode = ${journalMode}`);+ db.exec(`PRAGMA synchronous = ${synchronous}`);++ const hash = bcrypt.hashSync(password, BCRYPT_COST);+ // Case-insensitive like login, so [email protected] is found as [email protected].+ const existing = db.prepare('SELECT id FROM users WHERE LOWER(email) = LOWER(?)').get(email);++ let outcome;+ if (existing) {+ // password_version moves on, as on every password change in the app: it is+ // what ends the sessions still signed in to an account that needed resetting.+ db.prepare('UPDATE users SET password_hash = ?, role = ?, must_change_password = 1, password_version = COALESCE(password_version, 0) + 1 WHERE id = ?')+ .run(hash, 'admin', existing.id);+ // Its sessions end too, so the account's session list shows none of them+ // as live. The table is missing on a database that no server tracking+ // sessions has booted yet; the password_version bump above already refuses+ // every token there.+ const sessionsTable = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'user_sessions'").get();+ if (sessionsTable) {+ db.prepare('UPDATE user_sessions SET revoked_at = CURRENT_TIMESTAMP WHERE user_id = ? AND revoked_at IS NULL')+ .run(existing.id);+ }+ outcome = { created: false };+ } else {+ // 'admin' is usually taken by the first-run seed — pick the first free username+ // so the insert can't trip the UNIQUE(username) constraint.+ let username = 'admin';+ let n = 1;+ while (db.prepare('SELECT 1 FROM users WHERE username = ?').get(username)) {+ username = `admin${n++}`;+ }+ db.prepare('INSERT INTO users (username, email, password_hash, role, must_change_password) VALUES (?, ?, ?, ?, 1)')+ .run(username, email, hash, 'admin');+ outcome = { created: true, username };}- db.prepare('INSERT INTO users (username, email, password_hash, role, must_change_password) VALUES (?, ?, ?, ?, 1)')- .run(username, email, hash, 'admin');- console.log(`\n✓ Admin account created: ${email} (username: ${username})`);++ report(outcome);+ db.close();}-if (generated) console.log(` Password: ${password}`);-console.log(' You will be asked to change the password on first login.\n');+/**+ * The Postgres database in TREK_DB_URL, reset through the built server's+ * engine (dist/db/engine): the same pool options and SSL verification as the+ * server, and the same write lock, taken in one transaction with the reset.+ * The statements are the SQLite branch's, but for the session end, whose time+ * is written as the canonical `YYYY-MM-DD HH:MM:SS` UTC text the column holds.+ */+async function resetOnPostgres() {+ if ((process.env.TREK_DB_FILE || '').trim()) {+ console.error('TREK_DB_URL and TREK_DB_FILE are both set, so it is not clear which database is meant; nothing was changed.');+ process.exit(1);+ }+ let engineModule;+ try {+ engineModule = require('./dist/db/engine');+ } catch (err) {+ console.error(`The built server (${path.join(__dirname, 'dist')}) is needed to reach a Postgres database: ${err.message}`);+ console.error('Run this inside the TREK container, or run `npm run build --workspace=server` first.');+ process.exit(1);+ }+ const { MikroORM } = require('@mikro-orm/core');+ const engine = engineModule.resolveEngine();+ const orm = await MikroORM.init({ ...engineModule.buildOrmConfig(engine), pool: { min: 0, max: 1 } });+ try {+ const em = orm.em.fork();+ const hash = bcrypt.hashSync(password, BCRYPT_COST);+ const now = new Date().toISOString().slice(0, 19).replace('T', ' ');+ const outcome = await em.transactional(async (tx) => {+ await engine.serializeWrites(tx);+ const connection = tx.getConnection();+ const query = (sql, params = []) => connection.execute(sql, params, 'all', tx.getTransactionContext());+ const [existing] = await query('SELECT id FROM users WHERE LOWER(email) = LOWER(?)', [email]);+ if (existing) {+ await query(+ 'UPDATE users SET password_hash = ?, role = ?, must_change_password = 1, password_version = COALESCE(password_version, 0) + 1 WHERE id = ?',+ [hash, 'admin', existing.id],+ );+ const [sessions] = await query("SELECT to_regclass('user_sessions') IS NOT NULL AS present");+ if (sessions && sessions.present) {+ await query('UPDATE user_sessions SET revoked_at = ? WHERE user_id = ? AND revoked_at IS NULL', [now, existing.id]);+ }+ return { created: false };+ }+ let username = 'admin';+ let n = 1;+ while ((await query('SELECT 1 FROM users WHERE username = ?', [username])).length > 0) {+ username = `admin${n++}`;+ }+ await query('INSERT INTO users (username, email, password_hash, role, must_change_password) VALUES (?, ?, ?, ?, 1)', [+ username,+ email,+ hash,+ 'admin',+ ]);+ return { created: true, username };+ });+ report(outcome);+ } finally {+ await orm.close(true);+ }+}-db.close();+if ((process.env.TREK_DB_URL || '').trim()) {+ resetOnPostgres().catch((err) => {+ console.error(`Could not reset the admin account on the Postgres database: ${err && err.message ? err.message : err}`);+ process.exit(1);+ });+} else {+ resetOnSqlite();+}diff --git a/server/scripts/migrate-encryption.ts b/server/scripts/migrate-encryption.tsindex 8838f65d0..b09a96bfa 100644--- a/server/scripts/migrate-encryption.ts+++ b/server/scripts/migrate-encryption.ts@@ -141,6 +141,19 @@ async function main() {console.log('This script re-encrypts all stored secrets under a new ENCRYPTION_KEY.');console.log('A backup of the database will be created before any changes are made.\n');+ // A Postgres install keeps its secrets in the database TREK_DB_URL names,+ // which this script cannot rotate yet. It stops before it opens, creates or+ // backs up any file: rotating a SQLite file left over from before the move+ // would rewrite the key file while the secrets in Postgres stay under the+ // old key.+ if (process.env.TREK_DB_URL?.trim()) {+ rl.close();+ console.error('TREK_DB_URL is set: this install keeps its secrets in Postgres, which this script cannot');+ console.error('re-encrypt yet. Nothing was changed. Keep the current ENCRYPTION_KEY; see the wiki page');+ console.error('Encryption-Key-Rotation.');+ process.exit(1);+ }+// Resolve DB pathconst dbPath = path.resolve(process.env.DB_PATH ?? path.join(__dirname, '../data/travel.db'));diff --git a/server/tests/unit/db/maintenance-scripts-postgres.test.ts b/server/tests/unit/db/maintenance-scripts-postgres.test.tsnew file mode 100644index 000000000..a413dbf3a--- /dev/null+++ b/server/tests/unit/db/maintenance-scripts-postgres.test.ts@@ -0,0 +1,66 @@+/**+ * reset-admin.js and migrate-encryption.ts on an install whose database is+ * Postgres (TREK_DB_URL): neither opens or creates a SQLite file there. The+ * key rotation refuses outright, since it cannot re-encrypt the secrets in+ * Postgres yet; the reset refuses a TREK_DB_FILE beside the URL, as the server+ * does. The reset itself on Postgres goes through the built server's engine,+ * which these SQLite-lane tests do not have.+ */+import { execFileSync } from 'child_process';+import fs from 'fs';+import os from 'os';+import path from 'path';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++const SERVER_ROOT = path.resolve(__dirname, '../../..');+/** Never reached: both scripts stop before they connect. */+const PG_URL = 'postgres://trek:[email protected]:1/trek?sslmode=disable';++let tmpDir: string;++beforeEach(() => {+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-scripts-pg-'));+});++afterEach(() => {+ fs.rmSync(tmpDir, { recursive: true, force: true });+});++function run(args: string[], env: Record<string, string>, input = ''): { status: number; stderr: string } {+ try {+ execFileSync(process.execPath, args, {+ cwd: SERVER_ROOT,+ env: { ...process.env, ...env },+ input,+ stdio: 'pipe',+ encoding: 'utf8',+ });+ return { status: 0, stderr: '' };+ } catch (err) {+ const e = err as { status: number; stderr: string };+ return { status: e.status, stderr: e.stderr };+ }+}++describe('maintenance scripts with TREK_DB_URL', () => {+ it('PGSCRIPTS-001: migrate-encryption.ts refuses before it asks for a key, and touches no file', () => {+ const dbPath = path.join(tmpDir, 'travel.db');+ const r = run(+ ['--import', 'tsx', 'scripts/migrate-encryption.ts'],+ { TREK_DB_URL: PG_URL, DB_PATH: dbPath },+ 'old-key\nnew-key\nyes\n',+ );+ expect(r.status).toBe(1);+ expect(r.stderr).toContain('TREK_DB_URL is set');+ expect(r.stderr).toContain('Nothing was changed');+ expect(fs.readdirSync(tmpDir)).toEqual([]);+ }, 60000);++ it('PGSCRIPTS-002: reset-admin.js refuses TREK_DB_FILE beside TREK_DB_URL and creates no SQLite file', () => {+ const dbPath = path.join(tmpDir, 'travel.db');+ const r = run(['reset-admin.js'], { TREK_DB_URL: PG_URL, TREK_DB_FILE: dbPath, RESET_ADMIN_PASSWORD: 'x' });+ expect(r.status).toBe(1);+ expect(r.stderr).toContain('TREK_DB_URL and TREK_DB_FILE are both set');+ expect(fs.readdirSync(tmpDir)).toEqual([]);+ }, 60000);+});diff --git a/wiki/Database-Postgres.md b/wiki/Database-Postgres.mdindex 4783595ea..9f5077257 100644--- a/wiki/Database-Postgres.md+++ b/wiki/Database-Postgres.md@@ -135,6 +135,11 @@ Restoring a backup on Postgres follows these rules:`RESTORE_FROM_BACKUP` works on Postgres too. On the first start, TREK loads the archive's dump into the database when that database holds no tables. When the database already has tables, the variable is ignored and TREK logs a warning. See [Backups: Restoring on a new install, before setup](Backups#restoring-on-a-new-install-before-setup).+## Maintenance tools++- **Resetting the admin account** works as on SQLite: `docker exec -it -u node trek node server/reset-admin.js` resets or creates the account in the Postgres database, with the server's connection settings and its write lock. It never opens or creates a `travel.db`, and it refuses to run when `TREK_DB_FILE` is set beside `TREK_DB_URL`. See [Troubleshooting](Troubleshooting).+- **Rotating the encryption key** is not available on Postgres yet. `scripts/migrate-encryption.ts` stops before it changes anything when `TREK_DB_URL` is set. Keep the current `ENCRYPTION_KEY`. See [Encryption Key Rotation](Encryption-Key-Rotation).+## Limitations- **No migration from SQLite to Postgres** in this first release. Start a new install on Postgres, or keep the SQLite install.diff --git a/wiki/Encryption-Key-Rotation.md b/wiki/Encryption-Key-Rotation.mdindex 1aac8632f..b2a9ba23b 100644--- a/wiki/Encryption-Key-Rotation.md+++ b/wiki/Encryption-Key-Rotation.md@@ -58,6 +58,8 @@ To find your current key: check the `ENCRYPTION_KEY` environment variable or reaUse `scripts/migrate-encryption.ts` to re-encrypt the stored secrets listed below without downtime or manual re-entry.+The script works on the SQLite database only. On a [Postgres](Database-Postgres) install (`TREK_DB_URL` set) it stops before it changes anything, because it cannot re-encrypt the secrets held in Postgres yet. Keep the current key there; rotating a `travel.db` left over from before the move would rewrite the key file while the secrets in Postgres stay under the old key.+**Docker:**```bashdiff --git a/wiki/Environment-Variables.md b/wiki/Environment-Variables.mdindex 4ff51278d..e0096dfcd 100644--- a/wiki/Environment-Variables.md+++ b/wiki/Environment-Variables.md@@ -536,7 +536,7 @@ from the next boot onward. The startup log prints what is actually in effect:[DB] journal_mode=DELETE, synchronous=FULL```-The maintenance tools that open the same file — `reset-admin.js` and `scripts/migrate-encryption.ts` — read the same two+The maintenance tools that open the same file (on SQLite; with `TREK_DB_URL` they never open it) — `reset-admin.js` and `scripts/migrate-encryption.ts` — read the same twovariables, so run them with the same environment (`docker exec` into the container does this for you). Otherwise thenext key rotation or admin reset would quietly switch the file back to WAL.diff --git a/wiki/Troubleshooting.md b/wiki/Troubleshooting.mdindex 0ab02681c..18e4b36b2 100644--- a/wiki/Troubleshooting.md+++ b/wiki/Troubleshooting.md@@ -43,7 +43,7 @@ Log in with what it shows; you will be asked to set a new password.docker exec -it -u node trek node server/reset-admin.js```-This resets (or creates) `[email protected]` and prints a generated password. Override with `-e [email protected] -e RESET_ADMIN_PASSWORD=yourpass`. You will be asked to change it on first login. Resetting an existing account also signs it out of every session it still had.+This resets (or creates) `[email protected]` and prints a generated password. Override with `-e [email protected] -e RESET_ADMIN_PASSWORD=yourpass`. You will be asked to change it on first login. Resetting an existing account also signs it out of every session it still had. On a [Postgres](Database-Postgres) install (`TREK_DB_URL` set) the same command resets the account in the Postgres database, through the same connection settings as the server; it does not open or create a `travel.db`.**Start over with chosen credentials** (fresh install, no data to keep):