Phase 6 part 2: restore from a backup
A restore is staged (checked, the current database backed up) and swapped in when the app next starts. Settings → Backups restores with a self-restart, cancels a waiting restore, and takes uploads; supermarket restore takes a backup name or a file; supermarket backup --with-secrets makes an archive with the keys, for moving machines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -52,3 +56,62 @@ func TestBackupsPage(t *testing.T) {
|
||||
t.Errorf("bad delete: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreFromThePage(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
s.DB.PutJSON("test:value", "before")
|
||||
b, err := backup.Make(s.DB.DB, s.backupDir(), backup.Manual, "good", time.Now().Add(-time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.DB.PutJSON("test:value", "after")
|
||||
var restarts atomic.Int32
|
||||
s.Restart = func() { restarts.Add(1) }
|
||||
|
||||
rec := postForm(h, "/settings/backups/restore", url.Values{"name": {b.Name}})
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, "Restoring "+b.Name) || !strings.Contains(body, "this page reloads by itself") ||
|
||||
!strings.Contains(body, "A restore is waiting") || rec.Header().Get("Refresh") == "" {
|
||||
t.Errorf("restore: %v %s", rec.Header(), body)
|
||||
}
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
if restarts.Load() != 1 || !backup.Pending(s.DataDir) {
|
||||
t.Errorf("restarts %d, pending %v", restarts.Load(), backup.Pending(s.DataDir))
|
||||
}
|
||||
// Cancelled before the restart: nothing happens.
|
||||
body = postForm(h, "/settings/backups/cancel", nil).Body.String()
|
||||
if !strings.Contains(body, "The restore is cancelled") || backup.Pending(s.DataDir) {
|
||||
t.Errorf("cancel: %s", body)
|
||||
}
|
||||
if body := postForm(h, "/settings/backups/restore", url.Values{"name": {"../app.db"}}).Body.String(); !strings.Contains(body, "There's no backup called") {
|
||||
t.Errorf("bad name: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupUpload(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
b, _ := backup.Make(s.DB.DB, t.TempDir(), backup.Manual, "elsewhere", time.Now())
|
||||
raw, _ := os.ReadFile(b.Path)
|
||||
upload := func(name string, content []byte) string {
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
fw, _ := mw.CreateFormFile("file", name)
|
||||
fw.Write(content)
|
||||
mw.Close()
|
||||
return do(h, http.MethodPost, "/settings/backups/upload", &buf, map[string]string{"Content-Type": mw.FormDataContentType()}).Body.String()
|
||||
}
|
||||
body := upload("app-from-laptop.db", raw)
|
||||
if !strings.Contains(body, "Uploaded as app-") || !strings.Contains(body, "-manual-uploaded-app-from-laptop.db") {
|
||||
t.Errorf("upload: %s", body)
|
||||
}
|
||||
if body := upload("notes.txt", []byte("hello")); !strings.Contains(body, "isn't a backup Supermarket can use") {
|
||||
t.Errorf("bad upload: %s", body)
|
||||
}
|
||||
if bs, _ := backup.List(s.backupDir()); len(bs) != 1 {
|
||||
t.Errorf("backups: %v", bs)
|
||||
}
|
||||
left, _ := filepath.Glob(filepath.Join(s.backupDir(), ".upload-*"))
|
||||
if len(left) != 0 {
|
||||
t.Errorf("temporary files left: %v", left)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user