Files
siftlode/scripts/backup.ps1
T
peter 358d722ce9 fix(e12): close what round one broke, and revert two fixes that overreached
Round two of the review loop, on round one's own diff. Two of its findings were
regressions the fixes themselves introduced, which is exactly what the loop is for:

**A per-user refusal was writing onto a shared row.** MediaAssets are deduped across
users, and the generic failure handler puts the message on the asset — right for an
upstream failure, wrong for "your storage is full". Two users queuing the same video share
one asset, so the first user hitting their cap failed the second user's job with the first
one's private reason, telling someone with an empty library to free up space. The refusal
now raises `_OwnerRefused`, caught separately: the job fails, the asset goes back to
`pending` for whoever else holds it. Pinned by a test that drives `_download` directly —
the refusal happens before extraction, so nothing needs stubbing.

**The restore scripts restarted the app, undoing the restore.** `docker compose start`
re-runs the container command, and that runs `alembic upgrade head` — so on the rollback
path this documents as recommended (restore the dump, THEN pin the older tag) the newer
image immediately re-applied the migrations the restore had just undone, and on a FAILED
restore it put the app back on a half-restored schema. Both are the opposite of why the
stop was added. Nothing is restarted automatically now; the scripts say what to run and
when, and the docs' snippets say the same.

**Reverted rather than patched:** the OnboardingWizard toast (the wizard already renders
that failure inline, next to a Retry button — three recoveries for one failure), and
`useVerifyEntry`'s "each new link clears the other". The latter blanked the signed-out
card's only banner, where nothing renders the reset flag to replace it. The two states are
independent again; what changes is that `dismiss` peels ONE layer, which is what the
original finding was actually about.

Also: `Stats` uses the YouTube helper rather than the bare one, so the route's English 403
detail no longer beats the translated fallback under a Hungarian UI; `urlparse` guarded
alongside `.port` (an unclosed bracket raised just as late), the reason renamed `malformed`
since "bad port" misdescribes it; three docstrings narrowed to what the code actually does
(the worker's byte check is per-job, not an aggregate ledger, and not on the edit path;
the redirect residual leaks a reachability oracle, which the note had denied; the
invalidation helper does not cover suspend); the pasted Windows recipe wrapped so `throw`
can stop it; the bash one-liner's failure branch no longer exits 0; and the tests that were
missing for the round-one fixes — the account panel's error paths, the invalidation set,
and the shared-asset case above.

Gate green: vitest 685, pytest 950.
2026-09-05 04:02:57 +02:00

57 lines
3.1 KiB
PowerShell

# Dumps the Siftlode Postgres database to backups\siftlode-<timestamp>.dump (custom format).
# Run from the project root: .\scripts\backup.ps1
#
# A dump is only HALF a backup. The other half is your .env — it holds TOKEN_ENCRYPTION_KEY, without
# which every encrypted value in the restored database (Google credentials, SMTP password, Plex
# token, stored refresh tokens) is unreadable. See docs/self-hosting.md § Backups.
$ErrorActionPreference = "Stop"
# The dump is moved with `docker compose cp`, never through the PowerShell pipeline. Piping a binary
# stream corrupts it on Windows PowerShell 5.1 (text decoding), and on PowerShell 7 the old
# `-Encoding Byte` workaround does not even exist any more — it throws "'Byte' is not a supported
# encoding name", so this script simply failed there.
$remote = "/tmp/siftlode-backup.dump"
# Best-effort cleanup of the in-container dump. The try/catch is the load-bearing part: on Windows
# PowerShell 5.1 ANY redirected native stderr (`2>$null` and `2>&1` alike) becomes an ErrorRecord,
# and the $ErrorActionPreference above makes it TERMINATING — so one stray Compose warning would
# kill the script here, masking the real diagnostic below or reporting a finished backup as a
# failure. Do not remove the empty catch. A cleanup step must never decide the verdict.
function Remove-RemoteDump($path) {
try { docker compose exec -T db rm -f $path 2>&1 | Out-Null } catch { }
}
# `docker compose` finds docker-compose.yml on its own and honours $env:COMPOSE_FILE. The documented
# self-host layout (docs/self-hosting.md § Get the files) has NEITHER — just the selfhost file.
if (-not $env:COMPOSE_FILE -and -not (Test-Path "docker-compose.yml") -and (Test-Path "docker-compose.selfhost.yml")) {
$env:COMPOSE_FILE = "docker-compose.selfhost.yml"
}
$user = if ($env:POSTGRES_USER) { $env:POSTGRES_USER } else { "siftlode" }
$dbname = if ($env:POSTGRES_DB) { $env:POSTGRES_DB } else { "siftlode" }
New-Item -ItemType Directory -Force -Path "backups" | Out-Null
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
$out = "backups\siftlode-$ts.dump"
docker compose exec -T db pg_dump -U $user -Fc $dbname -f $remote
# Capture BEFORE the cleanup call: any native command overwrites $LASTEXITCODE, so interpolating it
# after Remove-RemoteDump would report the cleanup's code (0) inside a branch that only runs when
# pg_dump failed — a message contradicting itself. Same reason $cpCode is captured below.
$dumpCode = $LASTEXITCODE
if ($dumpCode -ne 0) {
Remove-RemoteDump $remote
throw "BACKUP FAILED — pg_dump returned $dumpCode; no file was written."
}
docker compose cp "db:$remote" $out
$cpCode = $LASTEXITCODE
Remove-RemoteDump $remote
if ($cpCode -ne 0) { throw "BACKUP FAILED — could not copy the dump out of the container (exit $cpCode)." }
if (-not (Test-Path $out) -or (Get-Item $out).Length -eq 0) {
if (Test-Path $out) { Remove-Item $out }
throw "BACKUP FAILED — the dump came out empty; no backup was written."
}
Write-Output "Wrote $out"
Write-Output "Remember: back up your .env too (TOKEN_ENCRYPTION_KEY) — this dump alone cannot be fully restored."