Files
petal/internal/db/backup_test.go
prosolis 8410b6315b Phase 15: containerize Petal for the parodia.dev VPS
Deploy plumbing so Petal can run on the public VPS behind the Traefik
already on that box, with vLLM reached over headscale.

- Dockerfile: node build -> go build -> alpine runtime. CGO stays off
  (modernc SQLite is pure Go), so the runtime layer exists only for
  ffmpeg (read-aloud transcodes Piper's WAV) and tzdata (the companion's
  bedtime nag and night mode read the local clock). Runs as uid 10001
  with /data as the single writable mount.

- docker-compose.yml: Traefik labels following this host's convention
  (external `traefik` network, `web-secure` entrypoint, `default` cert
  resolver). Petal publishes no host port. ./data is a bind mount, not a
  named volume, so the nightly backup and a restore are reachable from
  the host.

- Piper runs as two sibling containers rather than host systemd units.
  The plan assumed Piper was already installed on the VPS; it is not,
  the host has no lingering user session to keep user units alive, and
  containers keep the TTS ports on an internal network unreachable from
  anywhere but Petal. One image, voice chosen per service, model cached
  in a shared volume -- so the pt-PT voice is a new service, not a new
  image.

- db.Backup + a `-backup` flag: VACUUM INTO, not a file copy. Petal runs
  in WAL mode, so the newest committed pages may live in petal.db-wal;
  copying the three files separately can capture a torn mid-checkpoint
  state. VACUUM INTO reads one coherent snapshot without taking a write
  lock, and emits a single file with no -wal/-shm companions. Refuses an
  existing destination so a failed run can't destroy the last good
  backup.

- deploy/backup-petal.sh: nightly snapshot, compress, push to millenia
  over headscale with a post-transfer size check, prune both sides.

- deploy/petal.env.example: LLM_TIMEOUT raised 30s -> 90s for the
  WAN+VPN round trip, since the voice and collocation passes send a
  whole document and the timeout is a hard deadline on Complete.
2026-07-26 23:08:22 -07:00

109 lines
2.9 KiB
Go

package db
import (
"database/sql"
"os"
"path/filepath"
"testing"
)
// The point of VACUUM INTO over a file copy is that it captures rows still
// sitting in the WAL. This writes with the source connection open (so the WAL
// is hot and unlikely to have been checkpointed) and asserts the backup has
// them.
func TestBackupCapturesLiveWrites(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "petal.db")
dest := filepath.Join(dir, "backups", "petal-backup.db")
d, err := Open(src)
if err != nil {
t.Fatalf("open source: %v", err)
}
defer d.Close()
if _, err := d.Exec(
`INSERT INTO documents (id, user_id, title, content_text) VALUES ('d1', ?, '春天', 'hello 春天')`,
LocalUserID,
); err != nil {
t.Fatalf("insert: %v", err)
}
if err := Backup(src, dest); err != nil {
t.Fatalf("backup: %v", err)
}
// VACUUM INTO emits a single self-contained file — no -wal/-shm to ship
// alongside it.
for _, suffix := range []string{"-wal", "-shm"} {
if _, err := os.Stat(dest + suffix); err == nil {
t.Errorf("backup left a %s companion file behind", suffix)
}
}
copyConn, err := sql.Open("sqlite", dsn(dest))
if err != nil {
t.Fatalf("open backup: %v", err)
}
defer copyConn.Close()
var title string
if err := copyConn.QueryRow(`SELECT title FROM documents WHERE id = 'd1'`).Scan(&title); err != nil {
t.Fatalf("row missing from backup: %v", err)
}
if title != "春天" {
t.Errorf("title = %q, want 春天", title)
}
// The seeded user has to come across too, or a restore would orphan every
// document's foreign key.
var users int
if err := copyConn.QueryRow(`SELECT COUNT(*) FROM users WHERE id = ?`, LocalUserID).Scan(&users); err != nil {
t.Fatalf("count users: %v", err)
}
if users != 1 {
t.Errorf("users in backup = %d, want 1", users)
}
}
// A second run to the same path must fail loudly rather than clobber or
// half-write the previous good backup.
func TestBackupRefusesExistingDestination(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "petal.db")
dest := filepath.Join(dir, "petal-backup.db")
d, err := Open(src)
if err != nil {
t.Fatalf("open source: %v", err)
}
defer d.Close()
if err := Backup(src, dest); err != nil {
t.Fatalf("first backup: %v", err)
}
before, err := os.ReadFile(dest)
if err != nil {
t.Fatalf("read backup: %v", err)
}
if err := Backup(src, dest); err == nil {
t.Fatal("second backup to the same path succeeded; want an error")
}
after, err := os.ReadFile(dest)
if err != nil {
t.Fatalf("re-read backup: %v", err)
}
if len(before) != len(after) {
t.Errorf("existing backup was modified: %d bytes → %d", len(before), len(after))
}
}
func TestBackupMissingSource(t *testing.T) {
dir := t.TempDir()
if err := Backup(filepath.Join(dir, "nope.db"), filepath.Join(dir, "out.db")); err == nil {
t.Fatal("backup of a nonexistent database succeeded; want an error")
}
}