Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 144 additions & 5 deletions nixos/home/pm-dashboard-backup-pmp-links.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,30 @@
# at the default state path: run completes and uploads; the
# archive's projector/pmp_links.sql restores into a scratch DB
# with content identical to the source, contains NO pmp_items
# data, and the manifest records the member required + present.
# data, and the manifest records the member required + present;
# the stub log shows copyto calls to BOTH offsite legs (R2 and
# Drive) for the same archive.
# * env-url — the env file's database URL points at a fixture DB
# elsewhere: the export comes from THAT database.
# * env-last — two DATABASE_URL assignments in the env file: the
# LAST one selects the source (the stale first one must not).
# * env-quoted — the last assignment is quoted, CR-terminated and
# percent-encoded, and its path contains a space: the quote pair,
# CR and edge whitespace are trimmed, the %XX escapes decoded, and
# interior spaces preserved.
# * env-remote — DATABASE_URL names a non-file DSN: the run fails
# explicitly (nothing is uploaded, the cursor is unchanged, the
# failure names pmp_links and does not leak the DSN).
# * env-relative — DATABASE_URL names a relative path and
# PM_PROJECTOR_DIR is set: the DB resolves under that directory.
# * projector-dir — no DATABASE_URL, PM_PROJECTOR_DIR names the
# projector working dir: the active DB is data/projector.db under
# it (the projector own default), and it wins over a stale
# projector.db at the dir root and over the state-dir default.
# * env-encoded-q — DATABASE_URL is file:...projector%3Fprod.db:
# the decoded path names a real file with a literal ? in its name,
# and that exact file is exported (the decoded ? must not turn
# into SQLite URI query syntax).
# * no-db — no projector DB anywhere: run completes, manifest
# records the member optional + missing.
# * corrupt-db — the configured DB file is garbage: run fails,
Expand Down Expand Up @@ -139,10 +160,15 @@ SQL

run_backup() {
local name="$1"
HOME="$RUN_HOME" PATH="$BIN:$PATH" BWS_ACCESS_TOKEN="test-token" \
shift
# PM_PROJECTOR_* are cleared so an ambient value can never pick the
# source for a scenario; scenarios pass their own as env(1) arguments.
env -u PM_PROJECTOR_DIR -u PM_PROJECTOR_DB -u PM_PROJECTOR_ENV \
HOME="$RUN_HOME" PATH="$BIN:$PATH" BWS_ACCESS_TOKEN="test-token" \
RCLONE_STUB_DIR="$TMP_ROOT/$name/rclone-store" \
RCLONE_CALLS_LOG="$TMP_ROOT/$name/rclone-calls.log" \
PM_DB_DUMP_SCRIPT="$RUN_HOME/no-such-dump-script.sh" \
"$@" \
bash "$SCRIPT" 2026-10-28 >"$TMP_ROOT/$name.log" 2>&1
RC=$?
MARKER_AFTER="$(cat "$RUN_HOME/backups/dashboard/.last-run-epoch" 2>/dev/null || echo missing)"
Expand Down Expand Up @@ -178,6 +204,13 @@ no_upload() { # $1 scenario name
|| ! grep -q '^copyto' "$TMP_ROOT/$1/rclone-calls.log"
}

copied_both() { # $1 scenario name — copyto recorded for BOTH offsite legs
local log="$TMP_ROOT/$1/rclone-calls.log"
[[ -f "$log" ]] || return 1
grep -qE '^copyto .*/dashboard-2026-10-28\.tar\.zst r2bak:.*/dashboard-2026-10-28\.tar\.zst$' "$log" &&
grep -qE '^copyto .*/dashboard-2026-10-28\.tar\.zst drive:.*/dashboard-2026-10-28\.tar\.zst$' "$log"
}

canonical_links() { # $1 = sqlite db path; canonical pmp_links content
sqlite3 "file:$1?mode=ro" '.dump pmp_links'
}
Expand Down Expand Up @@ -218,10 +251,10 @@ else
fail "with-db: restored pmp_links content differs from source"
fi
assert_member with-db required present
if ! no_upload with-db; then
ok "with-db: archive uploaded to the offsite legs (stub rclone copyto recorded)"
if copied_both with-db; then
ok "with-db: stub log records copyto to both R2 and Drive"
else
fail "with-db: expected uploads to the offsite legs"
fail "with-db: expected copyto to BOTH R2 and Drive; calls: $(cat "$TMP_ROOT/with-db/rclone-calls.log" 2>/dev/null || echo none)"
fi

# --- env-url: the env file's database URL selects the source DB ----------------
Expand All @@ -239,6 +272,112 @@ else
fi
assert_member env-url required present

# --- env-last: the LAST DATABASE_URL assignment selects the source -------------
setup_home env-last
mkdir -p "$RUN_HOME/elsewhere" "$RUN_HOME/.local/state/pm-projector"
make_fixture_db "$RUN_HOME/elsewhere/stale.db" 'stale-first-link-note'
make_fixture_db "$RUN_HOME/elsewhere/live.db" 'last-assignment-link-note'
{
printf '%s=file:%s\n' 'DATABASE_URL' "$RUN_HOME/elsewhere/stale.db"
printf '%s=file:%s\n' 'DATABASE_URL' "$RUN_HOME/elsewhere/live.db"
} >"$RUN_HOME/.local/state/pm-projector/.env"
run_backup env-last
D="$(archive_dir env-last)"
if [[ "$RC" -eq 0 ]] \
&& grep -q 'last-assignment-link-note' "$D/projector/pmp_links.sql" 2>/dev/null \
&& ! grep -q 'stale-first-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then
ok "env-last: export sourced from the last DATABASE_URL assignment"
else
fail "env-last: export did not come from the last assignment (rc=$RC); log: $(cat "$TMP_ROOT/env-last.log")"
fi

# --- env-quoted: quotes, CR, edge space trimmed; %XX decoded -------------------
setup_home env-quoted
mkdir -p "$RUN_HOME/with space" "$RUN_HOME/.local/state/pm-projector"
make_fixture_db "$RUN_HOME/with space/quoted db.db" 'quoted-encoded-link-note'
printf 'DATABASE_URL = "file:%s/with%%20space/quoted%%20db.db" \r\n' "$RUN_HOME" \
>"$RUN_HOME/.local/state/pm-projector/.env"
run_backup env-quoted
D="$(archive_dir env-quoted)"
if [[ "$RC" -eq 0 ]] && grep -q 'quoted-encoded-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then
ok "env-quoted: quoted, CR-terminated, percent-encoded URL resolved to the real DB"
else
fail "env-quoted: quoted/encoded URL was not resolved (rc=$RC); log: $(cat "$TMP_ROOT/env-quoted.log")"
fi

# --- env-remote: a non-file DSN fails explicitly, no local fallback -----------
setup_home env-remote
mkdir -p "$RUN_HOME/.local/state/pm-projector"
# A local DB exists at the fallback path: it must NOT be silently archived.
make_fixture_db "$RUN_HOME/.local/state/pm-projector/projector.db" 'fallback-must-not-be-used'
printf '%s=postgres://user:sup3rsecret@db.example.invalid/app\n' 'DATABASE_URL' \
>"$RUN_HOME/.local/state/pm-projector/.env"
run_backup env-remote
if [[ "$RC" -ne 0 ]]; then
ok "env-remote: remote DATABASE_URL fails the run ($RC)"
else
fail "env-remote: remote DATABASE_URL silently accepted (exit 0)"
fi
if grep -q 'pmp_links' "$TMP_ROOT/env-remote.log" \
&& ! grep -q 'sup3rsecret' "$TMP_ROOT/env-remote.log"; then
ok "env-remote: failure names pmp_links and does not leak the DSN"
else
fail "env-remote: failure output missing pmp_links or leaked the DSN: $(cat "$TMP_ROOT/env-remote.log")"
fi
if no_upload env-remote; then
ok "env-remote: nothing uploaded"
else
fail "env-remote: an upload was attempted"
fi
if [[ "$MARKER_AFTER" == "$PRESET" ]]; then
ok "env-remote: journal marker unchanged"
else
fail "env-remote: marker advanced — was $PRESET, now $MARKER_AFTER"
fi

# --- env-relative: relative DB resolves under PM_PROJECTOR_DIR -----------------
setup_home env-relative
mkdir -p "$RUN_HOME/projector" "$RUN_HOME/.local/state/pm-projector"
make_fixture_db "$RUN_HOME/projector/rel.db" 'relative-dir-link-note'
printf '%s=file:rel.db\n' 'DATABASE_URL' >"$RUN_HOME/.local/state/pm-projector/.env"
run_backup env-relative PM_PROJECTOR_DIR="$RUN_HOME/projector"
D="$(archive_dir env-relative)"
if [[ "$RC" -eq 0 ]] && grep -q 'relative-dir-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then
ok "env-relative: relative DB resolved under PM_PROJECTOR_DIR"
else
fail "env-relative: relative DB not resolved under PM_PROJECTOR_DIR (rc=$RC); log: $(cat "$TMP_ROOT/env-relative.log")"
fi

# --- projector-dir: PM_PROJECTOR_DIR default wins over the state-dir fallback --
setup_home projector-dir
mkdir -p "$RUN_HOME/projector/data" "$RUN_HOME/.local/state/pm-projector"
make_fixture_db "$RUN_HOME/projector/data/projector.db" 'projector-dir-link-note'
make_fixture_db "$RUN_HOME/projector/projector.db" 'stale-root-link-note'
make_fixture_db "$RUN_HOME/.local/state/pm-projector/projector.db" 'state-dir-fallback-link-note'
run_backup projector-dir PM_PROJECTOR_DIR="$RUN_HOME/projector"
D="$(archive_dir projector-dir)"
if [[ "$RC" -eq 0 ]] \
&& grep -q 'projector-dir-link-note' "$D/projector/pmp_links.sql" 2>/dev/null \
&& ! grep -q 'stale-root-link-note' "$D/projector/pmp_links.sql" 2>/dev/null && ! grep -q 'state-dir-fallback-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then
ok "projector-dir: PM_PROJECTOR_DIR/data DB chosen before stale-root and state-dir DBs"
else
fail "projector-dir: PM_PROJECTOR_DIR/data DB not chosen (rc=$RC); log: $(cat "$TMP_ROOT/projector-dir.log")"
fi

# --- env-encoded-q: decoded ? in the filename stays a filename character -----
setup_home env-encoded-q
mkdir -p "$RUN_HOME/elsewhere" "$RUN_HOME/.local/state/pm-projector"
make_fixture_db "$RUN_HOME/elsewhere/projector?prod.db" 'encoded-q-link-note'
printf '%s=file:%s
' 'DATABASE_URL' "$RUN_HOME/elsewhere/projector%3Fprod.db" >"$RUN_HOME/.local/state/pm-projector/.env"
run_backup env-encoded-q
D="$(archive_dir env-encoded-q)"
if [[ "$RC" -eq 0 ]] && grep -q 'encoded-q-link-note' "$D/projector/pmp_links.sql" 2>/dev/null; then
ok "env-encoded-q: percent-decoded ? filename exported as-is"
else
fail "env-encoded-q: decoded ? filename not exported (rc=$RC); log: $(cat "$TMP_ROOT/env-encoded-q.log")"
fi

# --- no-db: not applicable, recorded optional + missing ------------------------
setup_home no-db
run_backup no-db
Expand Down
108 changes: 93 additions & 15 deletions nixos/home/pm-dashboard-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -379,41 +379,119 @@ if [ -d "$DUMP_DIR" ]; then cp -a "$DUMP_DIR" "$STAGE/db-dumps" ||
fail "dataset copy failed: $DUMP_DIR"; fi

# --- pmp_links export (code#1744 P0) ----------------------------------
# Source: the ACTUAL configured projector database — DATABASE_URL in the
# pm-projector env file when it names a file: URL, else the deployed
# default ~/.local/state/pm-projector/projector.db (a relative file:
# value resolves against the projector unit's working directory).
# Source: the ACTUAL configured projector database — the LAST DATABASE_URL
# assignment in the pm-projector env file when it names a file: URL
# (percent-decoded) or a local path, else <PM_PROJECTOR_DIR>/data/projector.db
# when that directory is named, else the deployed default
# ~/.local/state/pm-projector/projector.db (a relative value resolves
# against PM_PROJECTOR_DIR). A remote (non-file) DATABASE_URL fails the
# run: exporting a stale local DB instead would make the archive evidence
# claim a source it never read.
# PM_PROJECTOR_DB / PM_PROJECTOR_ENV override both (tests, relocation).
# Only the pmp_links table is exported (schema + rows as SQL): the rest
# of projector.db is a rebuildable projection and credentials never
# enter the archive — the .env file itself is NOT read beyond the
# DATABASE_URL line and is never staged.
# Env-file value -> path/URL: strip one trailing CR, edge whitespace and a
# single matching quote pair, leaving interior characters (spaces, quotes)
# untouched — only the value's edges are syntax, the path itself is data.
pmp_trim_value() {
local v="$1"
v="${v%$'\r'}"
v="${v#"${v%%[![:space:]]*}"}"
v="${v%"${v##*[![:space:]]}"}"
if ((${#v} >= 2)); then
case "$v" in
\"*\") v="${v:1:${#v}-2}" ;;
\'*\') v="${v:1:${#v}-2}" ;;
esac
fi
printf '%s' "$v"
}
# Percent-decode a file: URL path (%20 -> space, %2F -> /, ...). Malformed
# escapes are kept literally rather than dropped.
pmp_url_decode() {
local in="$1" out="" byte
while [[ "$in" == *%* ]]; do
out+="${in%%\%*}"
in="${in#*\%}"
if [[ "${in:0:2}" =~ ^[0-9A-Fa-f]{2}$ ]]; then
printf -v byte "\\x${in:0:2}"
out+="$byte"
in="${in:2}"
else
out+="%"
fi
done
printf '%s' "$out$in"
}
PMP_DB="${PM_PROJECTOR_DB:-}"
if [ -z "$PMP_DB" ]; then
PMP_ENV="${PM_PROJECTOR_ENV:-$HOME/.local/state/pm-projector/.env}"
PMP_URL=""
if [ -f "$PMP_ENV" ]; then
_url="$(grep -m1 '^DATABASE_URL=' "$PMP_ENV" 2>/dev/null | cut -d= -f2- | tr -d '"'"'"' \r')"
case "$_url" in
file:*) PMP_DB="${_url#file:}"; PMP_DB="${PMP_DB%%\?*}" ;;
# The LAST assignment wins: a later line is what the service loads.
PMP_URL="$(grep -E '^[[:space:]]*(export[[:space:]]+)?DATABASE_URL[[:space:]]*=' "$PMP_ENV" 2>/dev/null |
tail -n 1 | sed -E 's/^[[:space:]]*(export[[:space:]]+)?DATABASE_URL[[:space:]]*=//')"
fi
PMP_URL="$(pmp_trim_value "$PMP_URL")"
PMP_DIR="${PM_PROJECTOR_DIR:-}"
if [ -n "$PMP_URL" ]; then
case "$PMP_URL" in
file:*)
PMP_DB="${PMP_URL#file:}"
PMP_DB="${PMP_DB%%\?*}"
PMP_DB="${PMP_DB%%#*}"
# file://host/path -> /path: drop an empty or localhost
# authority, then decode the path before any existence check.
case "$PMP_DB" in
//localhost/*) PMP_DB="/${PMP_DB#//localhost/}" ;;
///*) PMP_DB="/${PMP_DB#///}" ;;
esac
PMP_DB="$(pmp_url_decode "$PMP_DB")"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve URI escaping when opening the decoded database path

For a configured URL such as file:/path/projector%3Fprod.db, decoding produces the correct filesystem path for the existence check, but the subsequent SQLite calls interpolate it into file:$PMP_DB?mode=ro again. The decoded ? becomes query syntax, so SQLite opens /path/projector instead of /path/projector?prod.db; encoded # and percent escapes have similar problems. Consequently, these newly supported URLs can fail the entire backup or export a different database. Re-escape the resolved path before constructing the SQLite URI, or pass the decoded filename with SQLite's documented -readonly option (sqlite3 --help: “open the database read-only”).

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in afda12d: the export no longer interpolates the decoded path into a file:...?mode=ro URI. All three SQLite calls now use sqlite3 -readonly with the decoded path as a plain filename, so a literal ? or # in the name stays a filename character. New env-encoded-q test case: DATABASE_URL=file:...projector%3Fprod.db exports the real projector?prod.db file; the full pmp-links suite passes.

;;
*://*)
# Remote DSN (postgres://, libsql://, ...): this export cannot
# read it, and silently archiving a local file instead would make
# the archive claim a source it never read. Name the scheme only
# — a DSN may embed credentials.
fail "pmp_links export failed: DATABASE_URL in $PMP_ENV uses '${PMP_URL%%://*}://', which is not supported (only file: URLs or local paths); remote export is not implemented"
;;
*)
PMP_DB="$PMP_URL" # local path, absolute or relative
;;
esac
fi
if [ -z "$PMP_DB" ]; then
# Nothing configured: the projector's own default is
# file:data/projector.db relative to its working directory
# (pm-stack/projector/src/store.ts), so the active DB is
# <PM_PROJECTOR_DIR>/data/projector.db when that dir is named,
# else the deployed state-directory default. A projector.db at
# the directory root is NOT the active DB and is never picked.
if [ -n "$PMP_DIR" ] && [ -f "$PMP_DIR/data/projector.db" ]; then
PMP_DB="$PMP_DIR/data/projector.db"
else
PMP_DB="$HOME/.local/state/pm-projector/projector.db"
fi
else
case "$PMP_DB" in
/*) ;;
*) PMP_DB="${PMP_DIR:-$HOME/git/code/pm-stack/projector}/$PMP_DB" ;;
esac
fi
[ -n "$PMP_DB" ] || PMP_DB="$HOME/.local/state/pm-projector/projector.db"
case "$PMP_DB" in
/*) ;;
*) PMP_DB="${PM_PROJECTOR_DIR:-$HOME/git/code/pm-stack/projector}/$PMP_DB" ;;
esac
fi
if [ -f "$PMP_DB" ]; then
command -v sqlite3 >/dev/null 2>&1 ||
fail "sqlite3 not found — cannot export pmp_links from $PMP_DB"
PMP_ROWS="$(sqlite3 "file:$PMP_DB?mode=ro" 'SELECT COUNT(*) FROM pmp_links;' 2>/dev/null)" ||
PMP_ROWS="$(sqlite3 -readonly "$PMP_DB" 'SELECT COUNT(*) FROM pmp_links;' 2>/dev/null)" ||
fail "pmp_links export failed: cannot read pmp_links in $PMP_DB"
mkdir -p "$STAGE/projector"
sqlite3 "file:$PMP_DB?mode=ro" '.dump pmp_links' >"$STAGE/projector/pmp_links.sql" ||
sqlite3 -readonly "$PMP_DB" '.dump pmp_links' >"$STAGE/projector/pmp_links.sql" ||
fail "pmp_links export failed: .dump of $PMP_DB"
# .dump omits the table's indexes on this sqlite build — append them
# from sqlite_master so a restore reproduces the full table shape.
sqlite3 "file:$PMP_DB?mode=ro" \
sqlite3 -readonly "$PMP_DB" \
"SELECT sql || ';' FROM sqlite_master WHERE type = 'index' AND tbl_name = 'pmp_links' AND sql IS NOT NULL;" \
>>"$STAGE/projector/pmp_links.sql" ||
fail "pmp_links export failed: index definitions of $PMP_DB"
Expand Down
Loading