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:
@@ -618,6 +618,14 @@ type DB struct {
|
||||
}
|
||||
|
||||
// Open opens (creating if needed) the database at path and migrates it.
|
||||
// BeforeMigrate, if set, runs before an existing database is migrated to
|
||||
// a newer schema (from and to are the schema versions): the app backs it
|
||||
// up. A new, empty database isn't backed up.
|
||||
var BeforeMigrate func(d *sql.DB, from, to int) error
|
||||
|
||||
// Schema is the schema version this build migrates databases to.
|
||||
func Schema() int { return len(migrations) }
|
||||
|
||||
func Open(path string) (*DB, error) {
|
||||
// WAL lets the web UI read while a background job writes; busy_timeout
|
||||
// waits out short write locks instead of failing.
|
||||
@@ -634,6 +642,19 @@ func Open(path string) (*DB, error) {
|
||||
return nil, fmt.Errorf("opening %s: %w", path, err)
|
||||
}
|
||||
d := &DB{sqldb}
|
||||
if BeforeMigrate != nil {
|
||||
var version int
|
||||
if err := d.QueryRow(`PRAGMA user_version`).Scan(&version); err != nil {
|
||||
sqldb.Close()
|
||||
return nil, err
|
||||
}
|
||||
if version > 0 && version < len(migrations) {
|
||||
if err := BeforeMigrate(sqldb, version, len(migrations)); err != nil {
|
||||
sqldb.Close()
|
||||
return nil, fmt.Errorf("backing up before migrating to schema %d: %w", len(migrations), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := d.migrate(); err != nil {
|
||||
sqldb.Close()
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBeforeMigrate(t *testing.T) {
|
||||
defer func() { BeforeMigrate = nil }()
|
||||
var calls []string
|
||||
BeforeMigrate = func(d *sql.DB, from, to int) error {
|
||||
var n int
|
||||
d.QueryRow(`PRAGMA user_version`).Scan(&n)
|
||||
calls = append(calls, fmt.Sprintf("%d→%d at %d", from, to, n))
|
||||
return nil
|
||||
}
|
||||
dir := t.TempDir()
|
||||
// A new database: nothing to back up.
|
||||
d, err := Open(filepath.Join(dir, "new.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.Close()
|
||||
// Already current: nothing either.
|
||||
d, _ = Open(filepath.Join(dir, "new.db"))
|
||||
d.Close()
|
||||
if len(calls) != 0 {
|
||||
t.Fatalf("called for a new or current database: %v", calls)
|
||||
}
|
||||
// One migration behind: backed up first, before the migration runs.
|
||||
old := filepath.Join(dir, "old.db")
|
||||
raw, _ := sql.Open("sqlite", "file:"+old)
|
||||
for _, m := range migrations[:len(migrations)-1] {
|
||||
if _, err := raw.Exec(m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, len(migrations)-1))
|
||||
raw.Close()
|
||||
d, err = Open(old)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.Close()
|
||||
want := fmt.Sprintf("%d→%d at %d", len(migrations)-1, len(migrations), len(migrations)-1)
|
||||
if len(calls) != 1 || calls[0] != want || Schema() != len(migrations) {
|
||||
t.Errorf("calls: %v, want %s", calls, want)
|
||||
}
|
||||
// A failed backup stops the migration.
|
||||
raw, _ = sql.Open("sqlite", "file:"+old)
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, len(migrations)-1))
|
||||
raw.Close()
|
||||
BeforeMigrate = func(*sql.DB, int, int) error { return errors.New("disk full") }
|
||||
if _, err := Open(old); err == nil || !strings.Contains(err.Error(), "disk full") {
|
||||
t.Errorf("open: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user