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
fix: stop database migration when its dump command fails (#8781)
[._.]/ Adam Eivybcc724e8e933
diff --git a/scripts/db-migrate.test.js b/scripts/db-migrate.test.jsnew file mode 100644index 0000000000..cf58fe766e--- /dev/null+++ b/scripts/db-migrate.test.js@@ -0,0 +1,140 @@+// Runs the real scripts/db.sh control flow against stubbed executables in a+// temporary root, so a failed pg_dump is proven to stop `migrate` before any+// import, mode change, or source shutdown (#8781). No real database is touched:+// every host command db.sh reaches (docker, psql, pg_dump, pg_isready, uname)+// is a stub placed ahead of the system directories on PATH.+import { describe, it, expect, beforeEach, afterEach } from 'vitest';+import { spawnSync } from 'node:child_process';+import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { dirname, join } from 'node:path';+import { fileURLToPath } from 'node:url';++const here = dirname(fileURLToPath(import.meta.url));+const FULL_DUMP = 'DROP TABLE IF EXISTS example_record;\nCREATE TABLE example_record (id int);\n';++// One `docker` stub covers every Docker call db.sh makes in docker mode:+// `ps` (running check), `exec … pg_dump` (export), `exec -i … psql` (import),+// and `compose stop db` (source shutdown). DUMP_MODE picks the dump outcome.+const DOCKER_STUB = `#!/bin/sh+echo "docker $*" >> "$STUB_LOG"+case "$1" in+ ps) echo "Up 5 minutes"; exit 0 ;;+ info) exit 0 ;;+ compose)+ case "$*" in *" stop "*) echo SOURCE_STOP >> "$STUB_LOG" ;; esac+ exit 0 ;;+ exec)+ case "$*" in+ *pg_dump*)+ case "$DUMP_MODE" in+ partial) printf 'DROP TABLE example_record;\\n'; exit 1 ;;+ empty) exit 1 ;;+ *) printf '%s' "$FULL_DUMP"; exit 0 ;;+ esac ;;+ *psql*) echo IMPORT >> "$STUB_LOG"; cat >> "$IMPORT_LOG"; exit 0 ;;+ esac ;;+esac+exit 0+`;++const PSQL_STUB = `#!/bin/sh+echo "psql $*" >> "$STUB_LOG"+case "$*" in *count*) echo 3 ;; esac+exit 0+`;++function writeStub(binDir, name, body) {+ const path = join(binDir, name);+ writeFileSync(path, body);+ chmodSync(path, 0o755);+}++describe.skipIf(process.platform === 'win32')('scripts/db.sh export + migrate', () => {+ let root;+ let stubLog;+ let importLog;+ let envFile;+ let dumpDir;++ beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'portos-db-sh-'));+ mkdirSync(join(root, 'scripts'));+ copyFileSync(join(here, 'db.sh'), join(root, 'scripts', 'db.sh'));+ const binDir = join(root, 'bin');+ mkdirSync(binDir);+ writeStub(binDir, 'docker', DOCKER_STUB);+ writeStub(binDir, 'psql', PSQL_STUB);+ writeStub(binDir, 'pg_isready', '#!/bin/sh\nexit 0\n');+ // Not Darwin, so db.sh skips prepending Homebrew's real Postgres to PATH.+ writeStub(binDir, 'uname', '#!/bin/sh\necho Linux\n');+ stubLog = join(root, 'stub.log');+ importLog = join(root, 'import.log');+ envFile = join(root, '.env');+ dumpDir = join(root, 'data', 'db-dumps');+ writeFileSync(envFile, 'PGMODE=docker\n');+ writeFileSync(stubLog, '');+ });++ afterEach(() => {+ rmSync(root, { recursive: true, force: true });+ });++ const run = (args, dumpMode) => spawnSync('bash', [join(root, 'scripts', 'db.sh'), ...args], {+ cwd: root,+ encoding: 'utf8',+ env: {+ PATH: `${join(root, 'bin')}:/usr/bin:/bin`,+ HOME: root,+ STUB_LOG: stubLog,+ IMPORT_LOG: importLog,+ DUMP_MODE: dumpMode,+ FULL_DUMP,+ PGPASSWORD: 'test-only',+ PGHOST: '127.0.0.1'+ }+ });++ const tempArtifacts = () => (existsSync(dumpDir) ? readdirSync(dumpDir).filter(f => f.startsWith('portos-export.')) : []);+ const dumps = () => (existsSync(dumpDir) ? readdirSync(dumpDir).filter(f => f.endsWith('.sql')) : []);++ it.each(['partial', 'empty'])('aborts migrate on a %s failed dump before import, mode change, or shutdown', (mode) => {+ const result = run(['migrate'], mode);++ expect(result.status).not.toBe(0);+ const log = readFileSync(stubLog, 'utf8');+ expect(log).not.toContain('IMPORT');+ expect(log).not.toContain('SOURCE_STOP');+ expect(existsSync(importLog)).toBe(false);+ expect(readFileSync(envFile, 'utf8')).toBe('PGMODE=docker\n');+ expect(dumps()).toEqual([]);+ expect(tempArtifacts()).toEqual([]);+ });++ it('keeps an existing dump intact when a re-export under the same label fails', () => {+ mkdirSync(dumpDir, { recursive: true });+ const existing = join(dumpDir, 'portos-keep.sql');+ writeFileSync(existing, FULL_DUMP);++ const result = run(['export', 'keep'], 'partial');++ expect(result.status).not.toBe(0);+ expect(result.stdout).not.toContain('portos-keep.sql');+ expect(readFileSync(existing, 'utf8')).toBe(FULL_DUMP);+ expect(tempArtifacts()).toEqual([]);+ });++ it('imports the complete dump and switches mode once on success', () => {+ const result = run(['migrate'], 'ok');++ expect(result.status).toBe(0);+ expect(readFileSync(importLog, 'utf8')).toBe(FULL_DUMP);+ const log = readFileSync(stubLog, 'utf8');+ expect(log.match(/^IMPORT$/gm)).toHaveLength(1);+ expect(log.match(/^SOURCE_STOP$/gm)).toHaveLength(1);+ expect(readFileSync(envFile, 'utf8')).toBe('PGMODE=native\n');+ const [dump] = dumps();+ expect(readFileSync(join(dumpDir, dump), 'utf8')).toBe(FULL_DUMP);+ expect(tempArtifacts()).toEqual([]);+ });+});diff --git a/scripts/db.sh b/scripts/db.shindex 86aef82093..a3a51f10b0 100755--- a/scripts/db.sh+++ b/scripts/db.sh@@ -539,31 +539,50 @@ run_pg_dump() {elif command -v pg_dump >/dev/null 2>&1; thenPGPASSWORD="$PGPASSWORD" pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" "$@"else- err "pg_dump not found on host and Docker DB is not running"- exit 1+ err "pg_dump not found on host and Docker DB is not running" >&2+ # return (not exit) so cmd_export can clean up its temp file+ return 1fi}-# Export database to SQL dump+# Export database to SQL dump. Prints the dump path on stdout ONLY on success.+# Every step is checked explicitly: cmd_migrate calls this inside a command+# substitution, where set -e is not inherited (and inherit_errexit is missing+# from older supported Bash), so a failed pg_dump must not fall through to the+# rename + success echo (#8781).cmd_export() {local label="${1:-$(date +%Y%m%d-%H%M%S)}"# Sanitize label to prevent path traversalif echo "$label" | grep -qE '[^A-Za-z0-9._-]'; then- err "Invalid label: only alphanumeric, dots, hyphens, and underscores are allowed"- exit 1+ err "Invalid label: only alphanumeric, dots, hyphens, and underscores are allowed" >&2+ return 1fi- mkdir -p "$DUMP_DIR"+ if ! mkdir -p "$DUMP_DIR"; then+ err "Could not create dump directory: $DUMP_DIR" >&2+ return 1+ filocal dumpfile="$DUMP_DIR/portos-$label.sql"info "Exporting database to $dumpfile..." >&2- # Dump to a temp file first to avoid leaving a partial/corrupt dump on failure+ # Dump to a temp file first so a failed dump never replaces an existing onelocal tmpfile- tmpfile="$(mktemp "$DUMP_DIR/portos-export.XXXXXX")"- run_pg_dump --no-owner --no-privileges --if-exists --clean > "$tmpfile"- mv "$tmpfile" "$dumpfile"+ if ! tmpfile="$(mktemp "$DUMP_DIR/portos-export.XXXXXX")"; then+ err "Could not create a temporary dump file in $DUMP_DIR" >&2+ return 1+ fi+ if ! run_pg_dump --no-owner --no-privileges --if-exists --clean > "$tmpfile"; then+ rm -f "$tmpfile"+ err "pg_dump failed — no dump was written" >&2+ return 1+ fi+ if ! mv "$tmpfile" "$dumpfile"; then+ rm -f "$tmpfile"+ err "Could not move the dump into place: $dumpfile" >&2+ return 1+ filog "Exported to: $dumpfile" >&2echo "$dumpfile"@@ -636,8 +655,12 @@ cmd_migrate() {info "Source has $count memories"# Export from source+ # Abort before touching the target or the mode when the export fails (#8781)local dumpfile- dumpfile=$(cmd_export "migrate-$(date +%Y%m%d-%H%M%S)")+ if ! dumpfile=$(cmd_export "migrate-$(date +%Y%m%d-%H%M%S)") || [ ! -f "$dumpfile" ]; then+ err "Export from $current_mode failed — migration aborted; $target_mode was not modified and mode remains $current_mode"+ exit 1+ fi# Determine target portlocal target_port