Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor: move database admin logic into service (#4893)
Adam Eivy339b55c76c10
diff --git a/server/lib/pgTools.js b/server/lib/pgTools.jsindex 7aa37eb069..e57f777d1e 100644--- a/server/lib/pgTools.js+++ b/server/lib/pgTools.js@@ -10,7 +10,7 @@** Shared by both pg_dump consumers — the backup snapshot path* (`server/services/backup.js`) and the native↔Docker backend-copy / export- * path (`server/routes/database.js`) — so the version-aware selection and the+ * path (`server/services/dbAdmin.js`) — so the version-aware selection and the* `PORTOS_PGDUMP` override behave identically in both. Self-contained: no* imports out to other PortOS services.*/diff --git a/server/lib/pgTools.test.js b/server/lib/pgTools.test.jsindex 3e440a2dba..0dc16a3f9a 100644--- a/server/lib/pgTools.test.js+++ b/server/lib/pgTools.test.js@@ -6,7 +6,7 @@* probes each binary's `--version`.* - resolvePgDump — discovery + pick + the `satisfies` verdict.* - resolvePgDumpBinary — the full override-or-auto decision both consumers- * (backup.js, routes/database.js) share: PORTOS_PGDUMP wins, else auto-select+ * (backup.js, services/dbAdmin.js) share: PORTOS_PGDUMP wins, else auto-select* when the server version is known, else bare `pg_dump`.*/diff --git a/server/lib/spawnCwd.test.js b/server/lib/spawnCwd.test.jsindex bd98e5f8fe..cdd29f5536 100644--- a/server/lib/spawnCwd.test.js+++ b/server/lib/spawnCwd.test.js@@ -281,7 +281,7 @@ describe('every cwd-passing spawn pins PWD', () => {['services/xcodeScripts.js', 'xcodebuild/ls, not an AI CLI'],['services/imageTo3d/laneRunner.js', 'git/bash/python install + render steps inside a target install root, not an AI CLI'],['services/updateExecutor.js', "PortOS's own update scripts, run in the PortOS root by design"],- ['routes/database.js', 'psql/pg_dump, not an AI CLI'],+ ['services/dbAdmin.js', 'psql/pg_dump, not an AI CLI'],['routes/scaffold.js', 'npm/git scaffolding, not an AI CLI'],['routes/scaffoldVite.js', 'npm scaffolding, not an AI CLI'],['routes/scaffoldIOS.js', 'xcodegen, not an AI CLI'],diff --git a/server/routes/database.js b/server/routes/database.jsindex 03a0067d09..3d675253cb 100644--- a/server/routes/database.js+++ b/server/routes/database.js@@ -1,641 +1,63 @@import { Router } from 'express';-import { execFile, spawn } from '../lib/childProcess.js';-import { existsSync, mkdirSync, createReadStream } from 'fs';-import { join } from 'path';-import { asyncHandler, ServerError } from '../lib/errorHandler.js';-import { validateRequest, databaseSwitchSchema, databaseBackendSchema, databaseExportSchema } from '../lib/validation.js';-import { checkHealth, query } from '../lib/db.js';-import { PATHS } from '../lib/fileUtils.js';-import { stripAnsi } from '../lib/ansiStrip.js';-import { resolvePgDumpBinary } from '../lib/pgTools.js';-import { resolveBashBinary, toBashPath } from '../lib/bashResolver.js';--const rootDir = PATHS.root;-const dbScript = toBashPath(join(rootDir, 'scripts', 'db.sh'));-// Resolve the interpreter explicitly. A bare `bash` on Windows often resolves-// (via PM2's PATH) to WSL, which mounts drives at /mnt/h and can't see the-// `H:/...` drive path above — exit 127. resolveBashBinary() prefers Git Bash,-// which both accepts drive paths and runs db.sh against the Windows toolchain.-const bashBinary = resolveBashBinary();+import { asyncHandler } from '../lib/errorHandler.js';+import {+ validateRequest,+ databaseSwitchSchema,+ databaseBackendSchema,+ databaseExportSchema+} from '../lib/validation.js';+import * as dbAdmin from '../services/dbAdmin.js';const router = Router();-/**- * Run a command and return { stdout, stderr, exitCode }.- */-function runCmd(cmd, args, timeout = 120_000, env = process.env) {- return new Promise((resolve) => {- execFile(cmd, args, { cwd: rootDir, timeout, env }, (err, stdout, stderr) => {- const exitCode = err ? (typeof err.code === 'number' ? err.code : 1) : 0;- if (exitCode !== 0) {- const detail = stripAnsi(stderr || stdout || err.message || '').replace(/\s+/g, ' ').trim();- console.error(`🗄️ ${cmd} ${args.join(' ')} exited ${exitCode}: ${detail}`);- }- resolve({- stdout: stripAnsi(stdout || ''),- stderr: stripAnsi(stderr || ''),- exitCode- });- });- });-}--const runDbScript = (args) => runCmd(bashBinary, [dbScript, ...args]);--function parseDbMode(stdout) {- const match = stdout.match(/Current mode:\s*(\w+)/);- if (!match) console.warn('🗄️ Failed to parse mode from db.sh output');- return match?.[1] || 'docker';-}--function emitProgress(io, event, message) {- io?.emit('database:progress', { event, message });-}--/**- * Get Docker container resource stats for portos-db.- */-async function getDockerStats() {- const result = await runCmd('docker', [- 'stats', 'portos-db', '--no-stream', '--format',- '{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}'- ], 10_000);- if (result.exitCode !== 0) return null;- const parts = result.stdout.trim().split('\t');- if (parts.length < 6) return null;- return {- cpu: parts[0],- memUsage: parts[1],- memPercent: parts[2],- netIO: parts[3],- blockIO: parts[4],- pids: parts[5]- };-}--/**- * Get Docker volume disk usage for portos-db data volume.- */-async function getDockerDiskUsage() {- // Get volume name and disk usage in parallel- const [volResult, sizeResult] = await Promise.all([- runCmd('docker', [- 'inspect', 'portos-db', '--format',- '{{ range .Mounts }}{{ if eq .Destination "/var/lib/postgresql/data" }}{{ .Name }}{{ end }}{{ end }}'- ], 5_000),- runCmd('docker', ['system', 'df', '-v'], 10_000)- ]);- if (volResult.exitCode !== 0 || sizeResult.exitCode !== 0) return null;- const volName = volResult.stdout.trim();- if (!volName) return null;- for (const line of sizeResult.stdout.split('\n')) {- if (line.includes(volName)) {- const match = line.match(/(\d+(?:\.\d+)?[KMGT]?B)/i);- return match?.[1] || null;- }- }- return null;-}--/**- * Get native PostgreSQL process stats via ps.- */-async function getNativeStats() {- const result = await runCmd(bashBinary, ['-c',- 'ps aux | grep "[p]ostgres.*-D" | head -1'- ], 5_000);- if (result.exitCode !== 0 || !result.stdout.trim()) return null;- const parts = result.stdout.trim().split(/\s+/);- // ps aux columns: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND- if (parts.length < 6) return null;- const pidRaw = parseInt(parts[1], 10);- if (!Number.isInteger(pidRaw) || pidRaw <= 0) return null;- const pid = String(pidRaw);- // Get all postgres child processes- const childResult = await runCmd(bashBinary, ['-c',- `ps -o pid=,pcpu=,pmem=,rss= -p $(pgrep -P ${pid} | tr '\\n' ',')${pid} 2>/dev/null`- ], 5_000);- let totalCpu = 0, totalMem = 0, totalRss = 0, pids = 0;- if (childResult.exitCode === 0) {- for (const line of childResult.stdout.trim().split('\n')) {- const cols = line.trim().split(/\s+/);- if (cols.length >= 4) {- totalCpu += parseFloat(cols[1]) || 0;- totalMem += parseFloat(cols[2]) || 0;- totalRss += parseInt(cols[3], 10) || 0;- pids++;- }- }- }- return {- cpu: `${totalCpu.toFixed(1)}%`,- memUsage: `${(totalRss / 1024).toFixed(1)} MiB`,- memPercent: `${totalMem.toFixed(1)}%`,- pids: String(pids)- };-}--/**- * Get native PostgreSQL database size via SQL query.- */-async function getNativeDiskUsage() {- const nativePort = process.env.PGPORT || '5432';- const result = await runCmd('psql', [- '-h', 'localhost', '-p', nativePort, '-U', pgUser, '-d', pgDb,- '-tAc', 'SELECT pg_size_pretty(pg_database_size(current_database()))'- ], 5_000, pgEnv(nativePort));- if (result.exitCode !== 0) return null;- return result.stdout.trim() || null;-}-// GET /api/database/status — current mode, connectivity, row counts, resource stats-router.get('/status', asyncHandler(async (req, res) => {- const [scriptResult, health, sizeResult] = await Promise.all([- runDbScript(['status']),- checkHealth(),- query(`SELECT pg_database_size(current_database()) AS db_bytes,- (SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public') AS table_count`)- .then(r => r.rows[0])- .catch(() => null)- ]);-- // Parse mode from script output- const modeMatch = scriptResult.stdout.match(/Current mode:\s*(\w+)/);- const mode = modeMatch?.[1] || 'unknown';-- // Parse Docker status- const dockerRunning = /Container portos-db is running/.test(scriptResult.stdout);- const dockerInstalled = !/Docker not installed/.test(scriptResult.stdout);- const dockerDaemon = !/Docker daemon is not running/.test(scriptResult.stdout);-- // Parse native status- const nativeInstalled = !/Native PostgreSQL not installed/.test(scriptResult.stdout);- const nativeRunning = /System PostgreSQL is running/.test(scriptResult.stdout);- const nativeConfigured = nativeInstalled && /PortOS database exists/.test(scriptResult.stdout);-- // Parse row count- const rowMatch = scriptResult.stdout.match(/Memories table has (\d+|N\/A) rows/);- const memoryCount = rowMatch?.[1] === 'N/A' ? null : parseInt(rowMatch?.[1] || '0', 10);- const connected = /Database is accepting connections/.test(scriptResult.stdout);-- // Fetch resource stats for running backends (in parallel)- const [dockerStats, dockerDisk, nativeStats, nativeDisk] = await Promise.all([- dockerRunning ? getDockerStats() : Promise.resolve(null),- dockerRunning ? getDockerDiskUsage() : Promise.resolve(null),- nativeRunning ? getNativeStats() : Promise.resolve(null),- nativeConfigured ? getNativeDiskUsage() : Promise.resolve(null)- ]);-- res.json({- mode,- connected,- memoryCount,- dbBytes: sizeResult ? parseInt(sizeResult.db_bytes, 10) : null,- tableCount: sizeResult ? parseInt(sizeResult.table_count, 10) : null,- health,- docker: {- installed: dockerInstalled,- daemonRunning: dockerDaemon,- containerRunning: dockerRunning,- stats: dockerStats,- diskUsage: dockerDisk- },- native: {- installed: nativeInstalled,- configured: nativeConfigured,- running: nativeRunning,- stats: nativeStats,- diskUsage: nativeDisk- }- });+router.get('/status', asyncHandler(async (_req, res) => {+ res.json(await dbAdmin.getStatus());}));// POST /api/database/switch — switch mode and optionally migraterouter.post('/switch', asyncHandler(async (req, res) => {- const { target, migrate } = validateRequest(databaseSwitchSchema, req.body);-- const io = req.app.get('io');- const emit = (event, data) => io?.emit('database:progress', { event, ...data });-- if (migrate) {- emit('start', { message: `Migrating data to ${target}...` });- const result = await runDbScript(['migrate']);- if (result.exitCode !== 0) {- emit('error', { message: 'Migration failed' });- throw new ServerError('Migration failed', {- status: 500,- context: { details: result.stderr || result.stdout }- });- }- emit('complete', { message: `Migration to ${target} complete` });- return res.json({ success: true, output: result.stdout });- }-- // Just switch mode without migrating- emit('start', { message: `Switching to ${target}...` });- const switchResult = await runDbScript([target === 'docker' ? 'use-docker' : 'use-native']);- if (switchResult.exitCode !== 0) {- emit('error', { message: 'Switch failed' });- throw new ServerError('Switch failed', {- status: 500,- context: { details: switchResult.stderr || switchResult.stdout }- });- }-- const startResult = await runDbScript(['start']);- if (startResult.exitCode !== 0) {- emit('error', { message: `Failed to start ${target} database` });- throw new ServerError(`Failed to start ${target} database`, {- status: 500,- context: { details: startResult.stderr || startResult.stdout }- });- }-- emit('complete', { message: `Switched to ${target}` });- res.json({ success: true, output: switchResult.stdout + '\n' + startResult.stdout });+ const options = validateRequest(databaseSwitchSchema, req.body);+ res.json(await dbAdmin.switchDatabase(options, req.app.get('io')));}));-const pgUser = process.env.PGUSER || 'portos';-const pgDb = process.env.PGDATABASE || 'portos';-const pgPassword = process.env.PGPASSWORD || 'portos';--// Safely quote a PostgreSQL identifier (table name, role name, db name) by-// doubling any embedded double-quotes and wrapping in double-quotes.-const pgQuoteIdentifier = (name) => `"${String(name).replace(/"/g, '""')}"`;-// Safely escape a PostgreSQL string literal value by doubling single-quotes.-const pgEscapeString = (val) => String(val).replace(/'/g, "''");--// Native (5432) and Docker (5561) use different ports, so both can run simultaneously.-const NATIVE_PORT = '5432';-const DOCKER_PORT = '5561';--// Prefer Homebrew postgresql@17 binaries over system pg (avoids version mismatch)-// Check both arm64 (/opt/homebrew) and Intel (/usr/local) prefixes-const pg17Bin = (() => {- for (const prefix of ['/opt/homebrew/opt/postgresql@17', '/usr/local/opt/postgresql@17']) {- const bin = `${prefix}/bin`;- if (existsSync(`${bin}/psql`)) return bin;- }- return null;-})();--/**- * Build env object with PGPASSWORD for passing to child processes.- * Avoids interpolating password into shell strings (shell metacharacter safety).- */-const pgEnv = (port) => ({- ...process.env,- ...(pg17Bin ? { PATH: `${pg17Bin}:${process.env.PATH}` } : {}),- PGPASSWORD: pgPassword,- PGHOST: 'localhost',- PGPORT: String(port),- PGUSER: pgUser,- PGDATABASE: pgDb-});--// pg17-only directives that older psql chokes on. The legacy import piped the-// dump through `sed -e '/^\restrict /d' …` to strip them; we filter the same-// lines in-process instead (see importDumpFile) so no `sed`/`bash -c` shell is-// involved. Exported for unit testing.-const PG17_ONLY_DIRECTIVES = [/^\\restrict /, /^\\unrestrict /, /^SET transaction_timeout/];-export const isPg17OnlyDirective = (line) => PG17_ONLY_DIRECTIVES.some((re) => re.test(line));--/**- * Import a pg_dump SQL file into the target database WITHOUT invoking a shell.- *- * The legacy path built a `bash -c "sed … \"${dumpFile}\" | psql … -U ${pgUser}- * -d ${pgDb}"` string, interpolating the dump path, port, user, and db name- * into the shell — any shell metacharacter in those values (or in a hostile- * export line surfacing through the pipeline) was injectable. Here psql runs via- * spawn with an argv array (no shell), the dump is streamed on stdin, and the- * pg17-only directives are stripped in-process by isPg17OnlyDirective(),- * replacing the `sed` stage entirely.- *- * Runs outside the Express request lifecycle (spawn/stream callbacks), so every- * async boundary resolves rather than throws — an uncaught throw here would- * crash the Node process (there is no next(err) to bubble to). Returns the same- * { stdout, stderr, exitCode } shape as runCmd().- */-export function importDumpFile(dumpFile, port, env, timeout = 120_000) {- return new Promise((resolve) => {- const psql = spawn('psql', [- '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,- '-v', 'ON_ERROR_STOP=1', '--single-transaction'- ], { cwd: rootDir, env });-- // The dump read stream — declared up front so finish() can tear it down. If- // psql exits early (ON_ERROR_STOP rollback) or the timeout SIGKILLs it, the- // `drain` we may be waiting on never fires, so we must destroy the source- // ourselves or its file descriptor leaks.- //- // Read/write as latin1 (a lossless 1:1 byte↔char mapping) and split lines on- // the raw '\n' byte only: the dump's bytes pass through psql untouched- // regardless of the database's client_encoding, and CR bytes are preserved- // (readline's crlfDelay would silently drop them). A utf8 decode→re-encode- // round-trip would corrupt non-utf8 dumps — this keeps the byte-transparency- // the old `sed | psql` pipe had.- const src = createReadStream(dumpFile, { encoding: 'latin1' });-- // Cap diagnostic capture so a pathologically chatty psql (verbose \echo,- // giant error text) can't exhaust the heap. psql is near-silent on success.- const CAPTURE_LIMIT = 256 * 1024;- let stdout = '';- let stderr = '';- const capture = (buf, chunk) => (buf.length >= CAPTURE_LIMIT ? buf : buf + chunk);- let finished = false;- const finish = (exitCode, extraDetail) => {- if (finished) return;- finished = true;- clearTimeout(timer);- src.destroy();- if (psql.stdin.writable) psql.stdin.end();- if (exitCode !== 0) {- const detail = stripAnsi(stderr || stdout || extraDetail || '').replace(/\s+/g, ' ').trim();- console.error(`🗄️ psql import exited ${exitCode}: ${detail}`);- }- resolve({ stdout: stripAnsi(stdout), stderr: stripAnsi(stderr), exitCode });- };-- const timer = setTimeout(() => {- psql.kill('SIGKILL');- finish(1, `import timed out after ${timeout}ms`);- }, timeout);-- psql.stdout.on('data', (d) => { stdout = capture(stdout, d.toString()); });- psql.stderr.on('data', (d) => { stderr = capture(stderr, d.toString()); });- psql.on('error', (err) => finish(1, err.message));- psql.on('close', (code) => finish(typeof code === 'number' ? code : 1));- // psql may exit early (e.g. ON_ERROR_STOP) while we're still writing — the- // resulting EPIPE would otherwise crash the process.- psql.stdin.on('error', () => {});-- // Byte-transparent line filter: buffer latin1 chars, split on '\n', drop the- // pg17-only directive lines, and forward every other line's original bytes- // (its trailing '\n' re-appended) to psql's stdin. Backpressure pauses the- // source; a drain resumes it and re-drains any buffered lines.- let pending = '';- const writeLine = (line) => {- if (isPg17OnlyDirective(line) || !psql.stdin.writable) return true;- return psql.stdin.write(line + '\n', 'latin1');- };- const pump = () => {- let nl;- while ((nl = pending.indexOf('\n')) !== -1) {- const line = pending.slice(0, nl); // excludes '\n', keeps any trailing '\r'- pending = pending.slice(nl + 1);- if (!writeLine(line)) {- src.pause();- psql.stdin.once('drain', () => { src.resume(); pump(); });- return;- }- }- };-- src.on('data', (chunk) => {- if (finished) return;- pending += chunk;- pump();- });- src.on('error', (err) => finish(1, err.message));- src.on('end', () => {- // Flush a final line that lacked a trailing newline, then close stdin so- // psql runs the script and emits 'close'.- if (pending.length) { writeLine(pending); pending = ''; }- if (psql.stdin.writable) psql.stdin.end();- });- });-}--/**- * Read a specific backend's PostgreSQL major version by probing it on its own- * port (native 5432 / Docker 5561 can run different majors, so the connection- * pool's version isn't authoritative for the export target). `server_version_num`- * is an integer like 170010 → major = floor(n / 10000). Returns null when the- * probe fails — the shared resolver then falls back to bare `pg_dump`.- */-async function probeServerMajor(port, env) {- const result = await runCmd('psql', [- '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,- '-tAc', 'SHOW server_version_num'- ], 5_000, env);- const num = parseInt(result.stdout.trim(), 10);- if (!Number.isFinite(num)) return null;- return Math.floor(num / 10000);-}-// POST /api/database/sync — copy data from active to non-active backend-// Since native (5432) and Docker (5561) use different ports, both can be-// running simultaneously. No need to stop the active backend.router.post('/sync', asyncHandler(async (req, res) => {- const io = req.app.get('io');- const emit = (event, data) => io?.emit('database:progress', { event, ...data });-- const statusResult = await runDbScript(['status']);- const currentMode = parseDbMode(statusResult.stdout);- const targetMode = currentMode === 'docker' ? 'native' : 'docker';- const targetPort = targetMode === 'docker' ? DOCKER_PORT : NATIVE_PORT;-- // Step 1: Export from active database (stays running)- emit('start', { message: 'Exporting from active database...' });- const exportResult = await runDbScript(['export', `sync-${Date.now()}`]);- if (exportResult.exitCode !== 0) {- emit('error', { message: 'Export failed' });- throw new ServerError('Export failed', { status: 500, context: { details: exportResult.stderr || exportResult.stdout } });- }- const exportLines = exportResult.stdout.trim().split('\n');- const dumpFile = exportLines[exportLines.length - 1]?.trim();- console.log(`🗄️ Sync: exported to ${dumpFile}`);-- // Step 2: Ensure target is running and configured- emit('start', { message: `Checking ${targetMode} is running...` });- const readyResult = await runCmd('pg_isready', [- '-h', 'localhost', '-p', targetPort- ], 5_000, pgEnv(targetPort));- if (readyResult.exitCode !== 0) {- emit('error', { message: `${targetMode} database not running on port ${targetPort}. Start it first.` });- throw new ServerError(`${targetMode} database not running on port ${targetPort}. Start it first.`, { status: 400 });- }-- // Ensure target has the portos role and database (native pg may lack the role)- if (targetMode === 'native') {- emit('start', { message: 'Ensuring native database role and schema exist...' });- const sysUser = process.env.USER || 'postgres';- const sysEnv = pgEnv(targetPort);- // Create role if missing (connect as system superuser)- await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',- '-c', `DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname='${pgEscapeString(pgUser)}') THEN CREATE ROLE ${pgQuoteIdentifier(pgUser)} WITH LOGIN PASSWORD '${pgEscapeString(pgPassword)}' CREATEDB SUPERUSER; END IF; END $$;`- ], 10_000, sysEnv);- // Create database if missing- const dbCheck = await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',- '-tAc', `SELECT 1 FROM pg_database WHERE datname='${pgEscapeString(pgDb)}'`- ], 5_000, sysEnv);- if (!dbCheck.stdout.trim().includes('1')) {- await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',- '-c', `CREATE DATABASE ${pgQuoteIdentifier(pgDb)} OWNER ${pgQuoteIdentifier(pgUser)};`- ], 10_000, sysEnv);- }- // Best-effort extensions (non-fatal — dump may handle schema, and pgvector may not be installed)- await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', pgDb,- '-c', 'CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pgcrypto;'- ], 10_000, sysEnv);- }-- // Step 3: Import into target (active backend untouched — different port).- // Streamed via spawn (no shell) with pg17-only directives stripped in-process,- // so no user/env value is ever interpolated into a shell string.- emit('start', { message: `Importing into ${targetMode} (port ${targetPort})...` });- const importResult = await importDumpFile(dumpFile, targetPort, pgEnv(targetPort));-- if (importResult.exitCode !== 0) {- emit('error', { message: `Import into ${targetMode} failed` });- throw new ServerError(`Import into ${targetMode} failed`, { status: 500, context: { details: importResult.stderr || importResult.stdout } });- }-- emit('complete', { message: `Data synced to ${targetMode}` });- res.json({ success: true, dumpFile });+ res.json(await dbAdmin.syncDatabase(req.app.get('io')));}));// POST /api/database/start — start a specific backendrouter.post('/start', asyncHandler(async (req, res) => {const { backend } = validateRequest(databaseBackendSchema, req.body);-- if (backend === 'docker') {- const result = await runCmd('docker', ['compose', 'up', '-d', 'db'], 60_000);- return res.json({ success: result.exitCode === 0, output: result.stdout });- }-- // Native: use db.sh which handles brew services / pg_ctl- const result = await runCmd(bashBinary, [dbScript, 'start'], 30_000);- res.json({ success: result.exitCode === 0, output: result.stdout });+ res.json(await dbAdmin.startDatabase(backend));}));-// POST /api/database/stop — stop the non-active backend+// POST /api/database/stop — stop a specific backendrouter.post('/stop', asyncHandler(async (req, res) => {const { backend } = validateRequest(databaseBackendSchema, req.body);-- if (backend === 'docker') {- const result = await runCmd('docker', ['compose', 'stop', 'db'], 30_000);- return res.json({ success: result.exitCode === 0, output: result.stdout });- }-- // Native stop- const result = await runCmd(bashBinary, [dbScript, 'stop'], 15_000);- res.json({ success: result.exitCode === 0, output: result.stdout });+ res.json(await dbAdmin.stopDatabase(backend));}));// POST /api/database/destroy — destroy the non-active backend's datarouter.post('/destroy', asyncHandler(async (req, res) => {const { backend } = validateRequest(databaseBackendSchema, req.body);-- // Safety: don't destroy the active backend- const statusResult = await runDbScript(['status']);- const currentMode = parseDbMode(statusResult.stdout);- if (backend === currentMode) {- throw new ServerError('Cannot destroy the active backend. Switch to the other backend first.', { status: 400 });- }-- if (backend === 'docker') {- // Stop and remove container + volume- await runCmd('docker', ['compose', 'stop', 'db'], 15_000);- await runCmd('docker', ['compose', 'rm', '-f', 'db'], 15_000);- const result = await runCmd('docker', ['volume', 'rm', '-f', 'portos_portos-pgdata'], 15_000);- // Also try alternate volume name- await runCmd('docker', ['volume', 'rm', '-f', 'portos-pgdata'], 15_000);- return res.json({ success: true, output: result.stdout });- }-- // Native: drop the portos database (system pg stays running)- const sysUser = process.env.USER || 'portos';- const nativePort = process.env.PGPORT || '5432';- const result = await runCmd('psql', [- '-h', 'localhost', '-p', nativePort, '-U', sysUser, '-d', 'postgres',- '-c', `DROP DATABASE IF EXISTS ${pgQuoteIdentifier(pgDb)}`- ], 15_000);- res.json({ success: result.exitCode === 0, output: result.stdout });+ res.json(await dbAdmin.destroyDatabase(backend));}));// POST /api/database/setup-native — install and configure native PostgreSQLrouter.post('/setup-native', asyncHandler(async (req, res) => {- const io = req.app.get('io');- emitProgress(io, 'start', 'Setting up native PostgreSQL...');-- const result = await runDbScript(['setup-native']);- if (result.exitCode !== 0) {- emitProgress(io, 'error', 'Native setup failed');- throw new ServerError('Native PostgreSQL setup failed', {- status: 500,- context: { details: result.stderr || result.stdout }- });- }-- emitProgress(io, 'complete', 'Native PostgreSQL ready');- res.json({ success: true, output: result.stdout });+ res.json(await dbAdmin.setupNativeDatabase(req.app.get('io')));}));-// POST /api/database/export — export database to SQL dump-// Optional body.backend: 'docker' | 'native' to export from a specific backend-// Default: exports from the active backend+// POST /api/database/export — export a specific or active backend to a SQL dumprouter.post('/export', asyncHandler(async (req, res) => {const { backend } = validateRequest(databaseExportSchema, req.body || {});- const label = `backup-${Date.now()}`;-- if (backend) {- // Export from a specific backend by connecting directly to its port- const port = backend === 'docker' ? DOCKER_PORT : NATIVE_PORT;- const env = pgEnv(port);- const readyResult = await runCmd('pg_isready', [- '-h', 'localhost', '-p', port, '-U', pgUser- ], 5_000, env);- if (readyResult.exitCode !== 0) {- throw new ServerError(`${backend} database not running on port ${port}`, { status: 400 });- }- const dumpDir = join(rootDir, 'data', 'db-dumps');- // Use Node's fs (not `mkdir -p`): on Windows `mkdir` is a cmd.exe builtin,- // not a spawnable executable, and `-p` isn't a valid flag — execFile would- // fail with ENOENT. `recursive: true` is the cross-platform equivalent.- mkdirSync(dumpDir, { recursive: true });- const dumpFile = join(dumpDir, `portos-${backend}-${label}.sql`);- // Use -f flag to write output directly — no shell redirect needed, avoids shell injection.- // Resolve a pg_dump whose major is >= the TARGET backend's server (probed- // against this port, not the pool's 5432) via the shared resolver, which- // also honors PORTOS_PGDUMP. Falls back to bare `pg_dump` when the probe- // can't determine the version. (Shared with backup.js — server/lib/pgTools.js.)- const targetMajor = await probeServerMajor(port, env);- const { binary: pgDumpBin, satisfies } = await resolvePgDumpBinary(targetMajor);- if (!satisfies) {- console.warn(`🗄️ No installed pg_dump satisfies ${backend} server major ${targetMajor} (using ${pgDumpBin})`);- }- const result = await runCmd(pgDumpBin, [- '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,- '--no-owner', '--no-privileges', '--if-exists', '--clean', '-f', dumpFile- ], 120_000, env);- if (result.exitCode !== 0) {- throw new ServerError('Export failed', { status: 500, context: { details: result.stderr || result.stdout } });- }- return res.json({ success: true, dumpFile });- }-- // Default: export from active backend via db.sh- const result = await runDbScript(['export']);- if (result.exitCode !== 0) {- throw new ServerError('Export failed', {- status: 500,- context: { details: result.stderr || result.stdout }- });- }- const dumpLines = result.stdout.trim().split('\n');- const dumpFile = dumpLines[dumpLines.length - 1]?.trim();- res.json({ success: true, dumpFile, output: result.stdout });+ res.json(await dbAdmin.exportDatabase(backend));}));// POST /api/database/fix — fix stale pid files-router.post('/fix', asyncHandler(async (req, res) => {- const result = await runDbScript(['fix']);- res.json({- success: result.exitCode === 0,- output: result.stdout,- error: result.exitCode !== 0 ? (result.stderr || result.stdout) : undefined- });+router.post('/fix', asyncHandler(async (_req, res) => {+ res.json(await dbAdmin.fixDatabase());}));export default router;diff --git a/server/routes/database.test.js b/server/routes/database.test.jsindex dd26ccc130..3c2d54af68 100644--- a/server/routes/database.test.js+++ b/server/routes/database.test.js@@ -1,5 +1,5 @@/**- * Tests for POST /api/database/destroy (and the adjacent validation paths).+ * Tests for the database route boundary and destructive admin operations.** Strategy: mock child_process.execFile — which is what runCmd() wraps — so* we can control every shell invocation without touching the real filesystem@@ -46,10 +46,11 @@ vi.mock('../lib/childProcess.js', async (importOriginal) => {import { execFile, spawn } from '../lib/childProcess.js';import { EventEmitter } from 'events';import { PassThrough } from 'stream';-import { writeFileSync, mkdtempSync } from 'fs';+import { writeFileSync, mkdtempSync, readFileSync } from 'fs';import { tmpdir } from 'os';import { join as pathJoin } from 'path';-import databaseRoutes, { isPg17OnlyDirective, importDumpFile } from './database.js';+import databaseRoutes from './database.js';+import { isPg17OnlyDirective, importDumpFile } from '../services/dbAdmin.js';// Helper: make execFile call the callback with controlled outputfunction mockExecFile(responses) {@@ -79,6 +80,16 @@ function makeApp() {return app;}+describe('database route boundary', () => {+ it('delegates subprocess, filesystem, and database work to dbAdmin', () => {+ const source = readFileSync(new URL('./database.js', import.meta.url), 'utf8');++ expect(source).toContain("from '../services/dbAdmin.js'");+ expect(source).not.toMatch(/childProcess|from 'fs'|from 'fs\/promises'|from '\.\.\/lib\/db\.js'/);+ expect(source).not.toMatch(/\b(?:execFile|spawn|query|mkdirSync|createReadStream)\s*\(/);+ });+});+describe('isPg17OnlyDirective (sed-replacement line filter)', () => {it('matches the pg17-only directives the legacy sed stripped', () => {expect(isPg17OnlyDirective('\\restrict abc123')).toBe(true);diff --git a/server/services/dbAdmin.js b/server/services/dbAdmin.jsnew file mode 100644index 0000000000..14d6eb204b--- /dev/null+++ b/server/services/dbAdmin.js@@ -0,0 +1,627 @@+import { execFile, spawn } from '../lib/childProcess.js';+import { existsSync, mkdirSync, createReadStream } from 'fs';+import { join } from 'path';+import { ServerError } from '../lib/errorHandler.js';+import { checkHealth, query } from '../lib/db.js';+import { PATHS } from '../lib/fileUtils.js';+import { stripAnsi } from '../lib/ansiStrip.js';+import { resolvePgDumpBinary } from '../lib/pgTools.js';+import { resolveBashBinary, toBashPath } from '../lib/bashResolver.js';++const rootDir = PATHS.root;+const dbScript = toBashPath(join(rootDir, 'scripts', 'db.sh'));+// Resolve the interpreter explicitly. A bare `bash` on Windows often resolves+// (via PM2's PATH) to WSL, which mounts drives at /mnt/h and can't see the+// `H:/...` drive path above — exit 127. resolveBashBinary() prefers Git Bash,+// which both accepts drive paths and runs db.sh against the Windows toolchain.+const bashBinary = resolveBashBinary();++/**+ * Run a command and return { stdout, stderr, exitCode }.+ */+function runCmd(cmd, args, timeout = 120_000, env = process.env) {+ return new Promise((resolve) => {+ execFile(cmd, args, { cwd: rootDir, timeout, env }, (err, stdout, stderr) => {+ const exitCode = err ? (typeof err.code === 'number' ? err.code : 1) : 0;+ if (exitCode !== 0) {+ const detail = stripAnsi(stderr || stdout || err.message || '').replace(/\s+/g, ' ').trim();+ console.error(`🗄️ ${cmd} ${args.join(' ')} exited ${exitCode}: ${detail}`);+ }+ resolve({+ stdout: stripAnsi(stdout || ''),+ stderr: stripAnsi(stderr || ''),+ exitCode+ });+ });+ });+}++const runDbScript = (args) => runCmd(bashBinary, [dbScript, ...args]);++function parseDbMode(stdout) {+ const match = stdout.match(/Current mode:\s*(\w+)/);+ if (!match) console.warn('🗄️ Failed to parse mode from db.sh output');+ return match?.[1] || 'docker';+}++function emitProgress(io, event, message) {+ io?.emit('database:progress', { event, message });+}++/**+ * Get Docker container resource stats for portos-db.+ */+async function getDockerStats() {+ const result = await runCmd('docker', [+ 'stats', 'portos-db', '--no-stream', '--format',+ '{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}'+ ], 10_000);+ if (result.exitCode !== 0) return null;+ const parts = result.stdout.trim().split('\t');+ if (parts.length < 6) return null;+ return {+ cpu: parts[0],+ memUsage: parts[1],+ memPercent: parts[2],+ netIO: parts[3],+ blockIO: parts[4],+ pids: parts[5]+ };+}++/**+ * Get Docker volume disk usage for portos-db data volume.+ */+async function getDockerDiskUsage() {+ // Get volume name and disk usage in parallel+ const [volResult, sizeResult] = await Promise.all([+ runCmd('docker', [+ 'inspect', 'portos-db', '--format',+ '{{ range .Mounts }}{{ if eq .Destination "/var/lib/postgresql/data" }}{{ .Name }}{{ end }}{{ end }}'+ ], 5_000),+ runCmd('docker', ['system', 'df', '-v'], 10_000)+ ]);+ if (volResult.exitCode !== 0 || sizeResult.exitCode !== 0) return null;+ const volName = volResult.stdout.trim();+ if (!volName) return null;+ for (const line of sizeResult.stdout.split('\n')) {+ if (line.includes(volName)) {+ const match = line.match(/(\d+(?:\.\d+)?[KMGT]?B)/i);+ return match?.[1] || null;+ }+ }+ return null;+}++/**+ * Get native PostgreSQL process stats via ps.+ */+async function getNativeStats() {+ const result = await runCmd(bashBinary, ['-c',+ 'ps aux | grep "[p]ostgres.*-D" | head -1'+ ], 5_000);+ if (result.exitCode !== 0 || !result.stdout.trim()) return null;+ const parts = result.stdout.trim().split(/\s+/);+ // ps aux columns: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND+ if (parts.length < 6) return null;+ const pidRaw = parseInt(parts[1], 10);+ if (!Number.isInteger(pidRaw) || pidRaw <= 0) return null;+ const pid = String(pidRaw);+ // Get all postgres child processes+ const childResult = await runCmd(bashBinary, ['-c',+ `ps -o pid=,pcpu=,pmem=,rss= -p $(pgrep -P ${pid} | tr '\\n' ',')${pid} 2>/dev/null`+ ], 5_000);+ let totalCpu = 0, totalMem = 0, totalRss = 0, pids = 0;+ if (childResult.exitCode === 0) {+ for (const line of childResult.stdout.trim().split('\n')) {+ const cols = line.trim().split(/\s+/);+ if (cols.length >= 4) {+ totalCpu += parseFloat(cols[1]) || 0;+ totalMem += parseFloat(cols[2]) || 0;+ totalRss += parseInt(cols[3], 10) || 0;+ pids++;+ }+ }+ }+ return {+ cpu: `${totalCpu.toFixed(1)}%`,+ memUsage: `${(totalRss / 1024).toFixed(1)} MiB`,+ memPercent: `${totalMem.toFixed(1)}%`,+ pids: String(pids)+ };+}++/**+ * Get native PostgreSQL database size via SQL query.+ */+async function getNativeDiskUsage() {+ const nativePort = process.env.PGPORT || '5432';+ const result = await runCmd('psql', [+ '-h', 'localhost', '-p', nativePort, '-U', pgUser, '-d', pgDb,+ '-tAc', 'SELECT pg_size_pretty(pg_database_size(current_database()))'+ ], 5_000, pgEnv(nativePort));+ if (result.exitCode !== 0) return null;+ return result.stdout.trim() || null;+}++/**+ * Return the current database mode, connectivity, row counts, and resource stats.+ */+export async function getStatus() {+ const [scriptResult, health, sizeResult] = await Promise.all([+ runDbScript(['status']),+ checkHealth(),+ query(`SELECT pg_database_size(current_database()) AS db_bytes,+ (SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public') AS table_count`)+ .then(r => r.rows[0])+ .catch(() => null)+ ]);++ // Parse mode from script output+ const modeMatch = scriptResult.stdout.match(/Current mode:\s*(\w+)/);+ const mode = modeMatch?.[1] || 'unknown';++ // Parse Docker status+ const dockerRunning = /Container portos-db is running/.test(scriptResult.stdout);+ const dockerInstalled = !/Docker not installed/.test(scriptResult.stdout);+ const dockerDaemon = !/Docker daemon is not running/.test(scriptResult.stdout);++ // Parse native status+ const nativeInstalled = !/Native PostgreSQL not installed/.test(scriptResult.stdout);+ const nativeRunning = /System PostgreSQL is running/.test(scriptResult.stdout);+ const nativeConfigured = nativeInstalled && /PortOS database exists/.test(scriptResult.stdout);++ // Parse row count+ const rowMatch = scriptResult.stdout.match(/Memories table has (\d+|N\/A) rows/);+ const memoryCount = rowMatch?.[1] === 'N/A' ? null : parseInt(rowMatch?.[1] || '0', 10);+ const connected = /Database is accepting connections/.test(scriptResult.stdout);++ // Fetch resource stats for running backends (in parallel)+ const [dockerStats, dockerDisk, nativeStats, nativeDisk] = await Promise.all([+ dockerRunning ? getDockerStats() : Promise.resolve(null),+ dockerRunning ? getDockerDiskUsage() : Promise.resolve(null),+ nativeRunning ? getNativeStats() : Promise.resolve(null),+ nativeConfigured ? getNativeDiskUsage() : Promise.resolve(null)+ ]);++ return {+ mode,+ connected,+ memoryCount,+ dbBytes: sizeResult ? parseInt(sizeResult.db_bytes, 10) : null,+ tableCount: sizeResult ? parseInt(sizeResult.table_count, 10) : null,+ health,+ docker: {+ installed: dockerInstalled,+ daemonRunning: dockerDaemon,+ containerRunning: dockerRunning,+ stats: dockerStats,+ diskUsage: dockerDisk+ },+ native: {+ installed: nativeInstalled,+ configured: nativeConfigured,+ running: nativeRunning,+ stats: nativeStats,+ diskUsage: nativeDisk+ }+ };+}++/**+ * Switch database backends, optionally migrating data first.+ */+export async function switchDatabase({ target, migrate }, io) {+ const emit = (event, data) => io?.emit('database:progress', { event, ...data });++ if (migrate) {+ emit('start', { message: `Migrating data to ${target}...` });+ const result = await runDbScript(['migrate']);+ if (result.exitCode !== 0) {+ emit('error', { message: 'Migration failed' });+ throw new ServerError('Migration failed', {+ status: 500,+ context: { details: result.stderr || result.stdout }+ });+ }+ emit('complete', { message: `Migration to ${target} complete` });+ return { success: true, output: result.stdout };+ }++ // Just switch mode without migrating+ emit('start', { message: `Switching to ${target}...` });+ const switchResult = await runDbScript([target === 'docker' ? 'use-docker' : 'use-native']);+ if (switchResult.exitCode !== 0) {+ emit('error', { message: 'Switch failed' });+ throw new ServerError('Switch failed', {+ status: 500,+ context: { details: switchResult.stderr || switchResult.stdout }+ });+ }++ const startResult = await runDbScript(['start']);+ if (startResult.exitCode !== 0) {+ emit('error', { message: `Failed to start ${target} database` });+ throw new ServerError(`Failed to start ${target} database`, {+ status: 500,+ context: { details: startResult.stderr || startResult.stdout }+ });+ }++ emit('complete', { message: `Switched to ${target}` });+ return { success: true, output: switchResult.stdout + '\n' + startResult.stdout };+}++const pgUser = process.env.PGUSER || 'portos';+const pgDb = process.env.PGDATABASE || 'portos';+const pgPassword = process.env.PGPASSWORD || 'portos';++// Safely quote a PostgreSQL identifier (table name, role name, db name) by+// doubling any embedded double-quotes and wrapping in double-quotes.+const pgQuoteIdentifier = (name) => `"${String(name).replace(/"/g, '""')}"`;+// Safely escape a PostgreSQL string literal value by doubling single-quotes.+const pgEscapeString = (val) => String(val).replace(/'/g, "''");++// Native (5432) and Docker (5561) use different ports, so both can run simultaneously.+const NATIVE_PORT = '5432';+const DOCKER_PORT = '5561';++// Prefer Homebrew postgresql@17 binaries over system pg (avoids version mismatch)+// Check both arm64 (/opt/homebrew) and Intel (/usr/local) prefixes+const pg17Bin = (() => {+ for (const prefix of ['/opt/homebrew/opt/postgresql@17', '/usr/local/opt/postgresql@17']) {+ const bin = `${prefix}/bin`;+ if (existsSync(`${bin}/psql`)) return bin;+ }+ return null;+})();++/**+ * Build env object with PGPASSWORD for passing to child processes.+ * Avoids interpolating password into shell strings (shell metacharacter safety).+ */+const pgEnv = (port) => ({+ ...process.env,+ ...(pg17Bin ? { PATH: `${pg17Bin}:${process.env.PATH}` } : {}),+ PGPASSWORD: pgPassword,+ PGHOST: 'localhost',+ PGPORT: String(port),+ PGUSER: pgUser,+ PGDATABASE: pgDb+});++// pg17-only directives that older psql chokes on. The legacy import piped the+// dump through `sed -e '/^\restrict /d' …` to strip them; we filter the same+// lines in-process instead (see importDumpFile) so no `sed`/`bash -c` shell is+// involved. Exported for unit testing.+const PG17_ONLY_DIRECTIVES = [/^\\restrict /, /^\\unrestrict /, /^SET transaction_timeout/];+export const isPg17OnlyDirective = (line) => PG17_ONLY_DIRECTIVES.some((re) => re.test(line));++/**+ * Import a pg_dump SQL file into the target database WITHOUT invoking a shell.+ *+ * The legacy path built a `bash -c "sed … \"${dumpFile}\" | psql … -U ${pgUser}+ * -d ${pgDb}"` string, interpolating the dump path, port, user, and db name+ * into the shell — any shell metacharacter in those values (or in a hostile+ * export line surfacing through the pipeline) was injectable. Here psql runs via+ * spawn with an argv array (no shell), the dump is streamed on stdin, and the+ * pg17-only directives are stripped in-process by isPg17OnlyDirective(),+ * replacing the `sed` stage entirely.+ *+ * Runs outside the Express request lifecycle (spawn/stream callbacks), so every+ * async boundary resolves rather than throws — an uncaught throw here would+ * crash the Node process (there is no next(err) to bubble to). Returns the same+ * { stdout, stderr, exitCode } shape as runCmd().+ */+export function importDumpFile(dumpFile, port, env, timeout = 120_000) {+ return new Promise((resolve) => {+ const psql = spawn('psql', [+ '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,+ '-v', 'ON_ERROR_STOP=1', '--single-transaction'+ ], { cwd: rootDir, env });++ // The dump read stream — declared up front so finish() can tear it down. If+ // psql exits early (ON_ERROR_STOP rollback) or the timeout SIGKILLs it, the+ // `drain` we may be waiting on never fires, so we must destroy the source+ // ourselves or its file descriptor leaks.+ //+ // Read/write as latin1 (a lossless 1:1 byte↔char mapping) and split lines on+ // the raw '\n' byte only: the dump's bytes pass through psql untouched+ // regardless of the database's client_encoding, and CR bytes are preserved+ // (readline's crlfDelay would silently drop them). A utf8 decode→re-encode+ // round-trip would corrupt non-utf8 dumps — this keeps the byte-transparency+ // the old `sed | psql` pipe had.+ const src = createReadStream(dumpFile, { encoding: 'latin1' });++ // Cap diagnostic capture so a pathologically chatty psql (verbose \echo,+ // giant error text) can't exhaust the heap. psql is near-silent on success.+ const CAPTURE_LIMIT = 256 * 1024;+ let stdout = '';+ let stderr = '';+ const capture = (buf, chunk) => (buf.length >= CAPTURE_LIMIT ? buf : buf + chunk);+ let finished = false;+ const finish = (exitCode, extraDetail) => {+ if (finished) return;+ finished = true;+ clearTimeout(timer);+ src.destroy();+ if (psql.stdin.writable) psql.stdin.end();+ if (exitCode !== 0) {+ const detail = stripAnsi(stderr || stdout || extraDetail || '').replace(/\s+/g, ' ').trim();+ console.error(`🗄️ psql import exited ${exitCode}: ${detail}`);+ }+ resolve({ stdout: stripAnsi(stdout), stderr: stripAnsi(stderr), exitCode });+ };++ const timer = setTimeout(() => {+ psql.kill('SIGKILL');+ finish(1, `import timed out after ${timeout}ms`);+ }, timeout);++ psql.stdout.on('data', (d) => { stdout = capture(stdout, d.toString()); });+ psql.stderr.on('data', (d) => { stderr = capture(stderr, d.toString()); });+ psql.on('error', (err) => finish(1, err.message));+ psql.on('close', (code) => finish(typeof code === 'number' ? code : 1));+ // psql may exit early (e.g. ON_ERROR_STOP) while we're still writing — the+ // resulting EPIPE would otherwise crash the process.+ psql.stdin.on('error', () => {});++ // Byte-transparent line filter: buffer latin1 chars, split on '\n', drop the+ // pg17-only directive lines, and forward every other line's original bytes+ // (its trailing '\n' re-appended) to psql's stdin. Backpressure pauses the+ // source; a drain resumes it and re-drains any buffered lines.+ let pending = '';+ const writeLine = (line) => {+ if (isPg17OnlyDirective(line) || !psql.stdin.writable) return true;+ return psql.stdin.write(line + '\n', 'latin1');+ };+ const pump = () => {+ let nl;+ while ((nl = pending.indexOf('\n')) !== -1) {+ const line = pending.slice(0, nl); // excludes '\n', keeps any trailing '\r'+ pending = pending.slice(nl + 1);+ if (!writeLine(line)) {+ src.pause();+ psql.stdin.once('drain', () => { src.resume(); pump(); });+ return;+ }+ }+ };++ src.on('data', (chunk) => {+ if (finished) return;+ pending += chunk;+ pump();+ });+ src.on('error', (err) => finish(1, err.message));+ src.on('end', () => {+ // Flush a final line that lacked a trailing newline, then close stdin so+ // psql runs the script and emits 'close'.+ if (pending.length) { writeLine(pending); pending = ''; }+ if (psql.stdin.writable) psql.stdin.end();+ });+ });+}++/**+ * Read a specific backend's PostgreSQL major version by probing it on its own+ * port (native 5432 / Docker 5561 can run different majors, so the connection+ * pool's version isn't authoritative for the export target). `server_version_num`+ * is an integer like 170010 → major = floor(n / 10000). Returns null when the+ * probe fails — the shared resolver then falls back to bare `pg_dump`.+ */+async function probeServerMajor(port, env) {+ const result = await runCmd('psql', [+ '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,+ '-tAc', 'SHOW server_version_num'+ ], 5_000, env);+ const num = parseInt(result.stdout.trim(), 10);+ if (!Number.isFinite(num)) return null;+ return Math.floor(num / 10000);+}++/**+ * Copy data from the active backend to the inactive backend.+ * Native (5432) and Docker (5561) can run simultaneously, so the active+ * backend remains running throughout the operation.+ */+export async function syncDatabase(io) {+ const emit = (event, data) => io?.emit('database:progress', { event, ...data });++ const statusResult = await runDbScript(['status']);+ const currentMode = parseDbMode(statusResult.stdout);+ const targetMode = currentMode === 'docker' ? 'native' : 'docker';+ const targetPort = targetMode === 'docker' ? DOCKER_PORT : NATIVE_PORT;++ // Step 1: Export from active database (stays running)+ emit('start', { message: 'Exporting from active database...' });+ const exportResult = await runDbScript(['export', `sync-${Date.now()}`]);+ if (exportResult.exitCode !== 0) {+ emit('error', { message: 'Export failed' });+ throw new ServerError('Export failed', { status: 500, context: { details: exportResult.stderr || exportResult.stdout } });+ }+ const exportLines = exportResult.stdout.trim().split('\n');+ const dumpFile = exportLines[exportLines.length - 1]?.trim();+ console.log(`🗄️ Sync: exported to ${dumpFile}`);++ // Step 2: Ensure target is running and configured+ emit('start', { message: `Checking ${targetMode} is running...` });+ const readyResult = await runCmd('pg_isready', [+ '-h', 'localhost', '-p', targetPort+ ], 5_000, pgEnv(targetPort));+ if (readyResult.exitCode !== 0) {+ emit('error', { message: `${targetMode} database not running on port ${targetPort}. Start it first.` });+ throw new ServerError(`${targetMode} database not running on port ${targetPort}. Start it first.`, { status: 400 });+ }++ // Ensure target has the portos role and database (native pg may lack the role)+ if (targetMode === 'native') {+ emit('start', { message: 'Ensuring native database role and schema exist...' });+ const sysUser = process.env.USER || 'postgres';+ const sysEnv = pgEnv(targetPort);+ // Create role if missing (connect as system superuser)+ await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',+ '-c', `DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname='${pgEscapeString(pgUser)}') THEN CREATE ROLE ${pgQuoteIdentifier(pgUser)} WITH LOGIN PASSWORD '${pgEscapeString(pgPassword)}' CREATEDB SUPERUSER; END IF; END $$;`+ ], 10_000, sysEnv);+ // Create database if missing+ const dbCheck = await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',+ '-tAc', `SELECT 1 FROM pg_database WHERE datname='${pgEscapeString(pgDb)}'`+ ], 5_000, sysEnv);+ if (!dbCheck.stdout.trim().includes('1')) {+ await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', 'postgres',+ '-c', `CREATE DATABASE ${pgQuoteIdentifier(pgDb)} OWNER ${pgQuoteIdentifier(pgUser)};`+ ], 10_000, sysEnv);+ }+ // Best-effort extensions (non-fatal — dump may handle schema, and pgvector may not be installed)+ await runCmd('psql', ['-h', 'localhost', '-p', targetPort, '-U', sysUser, '-d', pgDb,+ '-c', 'CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pgcrypto;'+ ], 10_000, sysEnv);+ }++ // Step 3: Import into target (active backend untouched — different port).+ // Streamed via spawn (no shell) with pg17-only directives stripped in-process,+ // so no user/env value is ever interpolated into a shell string.+ emit('start', { message: `Importing into ${targetMode} (port ${targetPort})...` });+ const importResult = await importDumpFile(dumpFile, targetPort, pgEnv(targetPort));++ if (importResult.exitCode !== 0) {+ emit('error', { message: `Import into ${targetMode} failed` });+ throw new ServerError(`Import into ${targetMode} failed`, { status: 500, context: { details: importResult.stderr || importResult.stdout } });+ }++ emit('complete', { message: `Data synced to ${targetMode}` });+ return { success: true, dumpFile };+}++/** Start a specific database backend. */+export async function startDatabase(backend) {+ if (backend === 'docker') {+ const result = await runCmd('docker', ['compose', 'up', '-d', 'db'], 60_000);+ return { success: result.exitCode === 0, output: result.stdout };+ }++ // Native: use db.sh which handles brew services / pg_ctl+ const result = await runCmd(bashBinary, [dbScript, 'start'], 30_000);+ return { success: result.exitCode === 0, output: result.stdout };+}++/** Stop a specific database backend. */+export async function stopDatabase(backend) {+ if (backend === 'docker') {+ const result = await runCmd('docker', ['compose', 'stop', 'db'], 30_000);+ return { success: result.exitCode === 0, output: result.stdout };+ }++ // Native stop+ const result = await runCmd(bashBinary, [dbScript, 'stop'], 15_000);+ return { success: result.exitCode === 0, output: result.stdout };+}++/** Destroy an inactive database backend's data. */+export async function destroyDatabase(backend) {+ // Safety: don't destroy the active backend+ const statusResult = await runDbScript(['status']);+ const currentMode = parseDbMode(statusResult.stdout);+ if (backend === currentMode) {+ throw new ServerError('Cannot destroy the active backend. Switch to the other backend first.', { status: 400 });+ }++ if (backend === 'docker') {+ // Stop and remove container + volume+ await runCmd('docker', ['compose', 'stop', 'db'], 15_000);+ await runCmd('docker', ['compose', 'rm', '-f', 'db'], 15_000);+ const result = await runCmd('docker', ['volume', 'rm', '-f', 'portos_portos-pgdata'], 15_000);+ // Also try alternate volume name+ await runCmd('docker', ['volume', 'rm', '-f', 'portos-pgdata'], 15_000);+ return { success: true, output: result.stdout };+ }++ // Native: drop the portos database (system pg stays running)+ const sysUser = process.env.USER || 'portos';+ const nativePort = process.env.PGPORT || '5432';+ const result = await runCmd('psql', [+ '-h', 'localhost', '-p', nativePort, '-U', sysUser, '-d', 'postgres',+ '-c', `DROP DATABASE IF EXISTS ${pgQuoteIdentifier(pgDb)}`+ ], 15_000);+ return { success: result.exitCode === 0, output: result.stdout };+}++/** Install and configure native PostgreSQL. */+export async function setupNativeDatabase(io) {+ emitProgress(io, 'start', 'Setting up native PostgreSQL...');++ const result = await runDbScript(['setup-native']);+ if (result.exitCode !== 0) {+ emitProgress(io, 'error', 'Native setup failed');+ throw new ServerError('Native PostgreSQL setup failed', {+ status: 500,+ context: { details: result.stderr || result.stdout }+ });+ }++ emitProgress(io, 'complete', 'Native PostgreSQL ready');+ return { success: true, output: result.stdout };+}++/** Export a specific database backend, or the active backend when omitted. */+export async function exportDatabase(backend) {+ const label = `backup-${Date.now()}`;++ if (backend) {+ // Export from a specific backend by connecting directly to its port+ const port = backend === 'docker' ? DOCKER_PORT : NATIVE_PORT;+ const env = pgEnv(port);+ const readyResult = await runCmd('pg_isready', [+ '-h', 'localhost', '-p', port, '-U', pgUser+ ], 5_000, env);+ if (readyResult.exitCode !== 0) {+ throw new ServerError(`${backend} database not running on port ${port}`, { status: 400 });+ }+ const dumpDir = join(rootDir, 'data', 'db-dumps');+ // Use Node's fs (not `mkdir -p`): on Windows `mkdir` is a cmd.exe builtin,+ // not a spawnable executable, and `-p` isn't a valid flag — execFile would+ // fail with ENOENT. `recursive: true` is the cross-platform equivalent.+ mkdirSync(dumpDir, { recursive: true });+ const dumpFile = join(dumpDir, `portos-${backend}-${label}.sql`);+ // Use -f flag to write output directly — no shell redirect needed, avoids shell injection.+ // Resolve a pg_dump whose major is >= the TARGET backend's server (probed+ // against this port, not the pool's 5432) via the shared resolver, which+ // also honors PORTOS_PGDUMP. Falls back to bare `pg_dump` when the probe+ // can't determine the version. (Shared with backup.js — server/lib/pgTools.js.)+ const targetMajor = await probeServerMajor(port, env);+ const { binary: pgDumpBin, satisfies } = await resolvePgDumpBinary(targetMajor);+ if (!satisfies) {+ console.warn(`🗄️ No installed pg_dump satisfies ${backend} server major ${targetMajor} (using ${pgDumpBin})`);+ }+ const result = await runCmd(pgDumpBin, [+ '-h', 'localhost', '-p', String(port), '-U', pgUser, '-d', pgDb,+ '--no-owner', '--no-privileges', '--if-exists', '--clean', '-f', dumpFile+ ], 120_000, env);+ if (result.exitCode !== 0) {+ throw new ServerError('Export failed', { status: 500, context: { details: result.stderr || result.stdout } });+ }+ return { success: true, dumpFile };+ }++ // Default: export from active backend via db.sh+ const result = await runDbScript(['export']);+ if (result.exitCode !== 0) {+ throw new ServerError('Export failed', {+ status: 500,+ context: { details: result.stderr || result.stdout }+ });+ }+ const dumpLines = result.stdout.trim().split('\n');+ const dumpFile = dumpLines[dumpLines.length - 1]?.trim();+ return { success: true, dumpFile, output: result.stdout };+}++/** Fix stale PostgreSQL pid files. */+export async function fixDatabase() {+ const result = await runDbScript(['fix']);+ return {+ success: result.exitCode === 0,+ output: result.stdout,+ error: result.exitCode !== 0 ? (result.stderr || result.stdout) : undefined+ };+}