Phase 6 part 1: database backups
Backups with SQLite's VACUUM INTO into <data>/backups: on request (Settings → Backups, supermarket backup [note]), before every schema migration, and daily, keeping 7 dailies and 4 weeklies. Listed with size and schema; downloadable and deletable. The old app.db.pre-* copies are moved in, with their -wal files folded in first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/backup"
|
||||
)
|
||||
|
||||
func TestBackupsPage(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
if body := do(h, http.MethodGet, "/settings/backups", nil, nil).Body.String(); !strings.Contains(body, "No backups yet") {
|
||||
t.Errorf("empty: %s", body)
|
||||
}
|
||||
body := postForm(h, "/settings/backups/now", url.Values{"note": {"Before the import"}}).Body.String()
|
||||
if !strings.Contains(body, "Backed up to app-") || !strings.Contains(body, "-manual-before-the-import.db") ||
|
||||
!strings.Contains(body, "By you") || !strings.Contains(body, "before-the-import") {
|
||||
t.Errorf("after backing up: %s", body)
|
||||
}
|
||||
bs, _ := backup.List(s.backupDir())
|
||||
if len(bs) != 1 {
|
||||
t.Fatalf("backups: %v", bs)
|
||||
}
|
||||
rec := do(h, http.MethodGet, "/settings/backups/file/"+bs[0].Name, nil, nil)
|
||||
if rec.Code != http.StatusOK || !strings.HasPrefix(rec.Body.String(), "SQLite format 3") ||
|
||||
!strings.Contains(rec.Header().Get("Content-Disposition"), bs[0].Name) {
|
||||
t.Errorf("download: %d %v", rec.Code, rec.Header())
|
||||
}
|
||||
for _, bad := range []string{"..%2Fapp.db", "secrets.json", "app.db"} {
|
||||
if rec := do(h, http.MethodGet, "/settings/backups/file/"+bad, nil, nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("%s: %d", bad, rec.Code)
|
||||
}
|
||||
}
|
||||
// The daily one, once a day.
|
||||
s.dailyBackup(time.Now())
|
||||
s.dailyBackup(time.Now())
|
||||
if bs, _ := backup.List(s.backupDir()); len(bs) != 2 {
|
||||
t.Errorf("after the daily: %d", len(bs))
|
||||
}
|
||||
body = postForm(h, "/settings/backups/delete", url.Values{"name": {bs[0].Name}}).Body.String()
|
||||
if !strings.Contains(body, "Deleted "+bs[0].Name) {
|
||||
t.Errorf("delete: %s", body)
|
||||
}
|
||||
if _, err := os.Stat(bs[0].Path); err == nil {
|
||||
t.Error("still there")
|
||||
}
|
||||
if body := postForm(h, "/settings/backups/delete", url.Values{"name": {"../app.db"}}).Body.String(); !strings.Contains(body, "There's no backup called") {
|
||||
t.Errorf("bad delete: %s", body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user