mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Restore: a server's database with users refused before anything else
A restore copies the archive's database into a PostgreSQL or MySQL one, which must be new; one with users was refused only after the whole archive was unpacked, with Copy's bare message. It is now checked first (Store.Empty), and the refusal says what to do: make a new database, point bbs.conf at it, restore again; nothing has been changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
4 files changed
+66
-5
No files matched your search
@@ -51,7 +51,9 @@ puts it back:
|
||||
away when all is well);
|
||||
- an SQLite database is put in the database's place;
|
||||
- a PostgreSQL, MySQL or MariaDB database must be new and empty (made as
|
||||
for `bbs db copy`): the archive's is copied into it;
|
||||
for `bbs db copy`): the archive's is copied into it. One with users is
|
||||
refused before anything is unpacked or changed: make a new one, point
|
||||
`database` at it, and restore again;
|
||||
- what the archive does not have (the files, with `files = no`) is left
|
||||
as it is.
|
||||
|
||||
|
||||
@@ -161,3 +161,25 @@ func TestClean(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmptyServer: a server's database is restored only into a new, empty
|
||||
// one: one with users is refused, saying what to do and that nothing was
|
||||
// changed (checked through SQLite, which Open takes the same way).
|
||||
func TestEmptyServer(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
fresh := "sqlite:" + filepath.Join(dir, "new.db")
|
||||
if err := emptyServer(fresh); err != nil {
|
||||
t.Errorf("a new database: %v", err)
|
||||
}
|
||||
used := filepath.Join(dir, "used.db")
|
||||
st, err := store.Open(used)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st.CreateUser("alice", "alicepass1", "", false)
|
||||
st.Close()
|
||||
err = emptyServer("sqlite:" + used)
|
||||
if err == nil || !strings.Contains(err.Error(), "nothing has been changed") || !strings.Contains(err.Error(), "new, empty") {
|
||||
t.Errorf("one with users: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ func Restore(archive string, to Targets, now time.Time, say func(string)) error
|
||||
return err
|
||||
}
|
||||
// first: every name in it one that stays where it is put
|
||||
hasDB := false
|
||||
if err := walk(archive, func(h *tar.Header, r io.Reader) error {
|
||||
if !clean(h.Name) {
|
||||
return fmt.Errorf("%s: %q: not a name a backup has", archive, h.Name)
|
||||
@@ -56,10 +57,18 @@ func Restore(archive string, to Targets, now time.Time, say func(string)) error
|
||||
if h.Typeflag != tar.TypeReg && h.Typeflag != tar.TypeDir {
|
||||
return fmt.Errorf("%s: %q: not a file or a directory", archive, h.Name)
|
||||
}
|
||||
hasDB = hasDB || h.Name == "database.db"
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// a server's database is copied into, so it must be new: said before
|
||||
// anything is unpacked or changed
|
||||
if hasDB && to.Database != "" && !store.IsFile(to.Database) {
|
||||
if err := emptyServer(to.Database); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
stamp := ".before-restore-" + now.Format("20060102-150405")
|
||||
work, err := os.MkdirTemp("", "bbs-restore-")
|
||||
if err != nil {
|
||||
@@ -191,6 +200,26 @@ func aside(p, stamp string, say func(string)) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// emptyServer checks that the database server's database at addr is new and
|
||||
// empty, as a restore into it needs: what to do if it is not.
|
||||
func emptyServer(addr string) error {
|
||||
db, err := store.Open(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the database: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
empty, err := db.Empty()
|
||||
if err != nil {
|
||||
return fmt.Errorf("the database: %w", err)
|
||||
}
|
||||
if !empty {
|
||||
return fmt.Errorf("the database %s has users: a server's database is restored into a new, empty one "+
|
||||
"(made as for bbs db copy). Make one, point bbs.conf's database at it, and restore again; "+
|
||||
"nothing has been changed", store.Describe(addr))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// restoreDB puts the snapshot back as the database at addr: a SQLite file
|
||||
// (with its -wal and -shm) set aside and the snapshot copied in its place;
|
||||
// any other engine's, copied into, so it must be empty.
|
||||
|
||||
+12
-4
@@ -41,13 +41,21 @@ func Copy(src, dst *Store, progress func(table string, rows int)) error {
|
||||
return copyTables(src.db.Query, dst, progress)
|
||||
}
|
||||
|
||||
// Empty reports whether the database is new: no users yet, as Copy needs
|
||||
// of the one it copies into.
|
||||
func (s *Store) Empty() (bool, error) {
|
||||
var n int
|
||||
if err := s.db.QueryRow(`SELECT COUNT(*) FROM users`).Scan(&n); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n == 0, nil
|
||||
}
|
||||
|
||||
// copyTables copies every table read by query into dst.
|
||||
func copyTables(query func(string, ...any) (*sql.Rows, error), dst *Store, progress func(table string, rows int)) error {
|
||||
var n int
|
||||
if err := dst.db.QueryRow(`SELECT COUNT(*) FROM users`).Scan(&n); err != nil {
|
||||
if empty, err := dst.Empty(); err != nil {
|
||||
return err
|
||||
}
|
||||
if n > 0 {
|
||||
} else if !empty {
|
||||
return errors.New("the database to copy into has users already: it must be new")
|
||||
}
|
||||
t, err := dst.db.Begin()
|
||||
|
||||
Reference in new issue
Block a user