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
|
||||
|
||||
Reference in New Issue
Block a user