mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 10:28:49 -07:00
Fix reset DB
The reset of all folders failed when there was no data for a given folder, as it was not returned by db.ListFolders then. But we don't really care about that, we can "reset" it anyway...
This commit is contained in:
parent
0f1b40da71
commit
2b2cae2d50
@ -570,26 +570,26 @@ func (s *apiSvc) postSystemRestart(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *apiSvc) postSystemReset(w http.ResponseWriter, r *http.Request) {
|
||||
var qs = r.URL.Query()
|
||||
folder := qs.Get("folder")
|
||||
var err error
|
||||
if len(folder) == 0 {
|
||||
for folder := range cfg.Folders() {
|
||||
err = s.model.ResetFolder(folder)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = s.model.ResetFolder(folder)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
||||
if len(folder) > 0 {
|
||||
if _, ok := cfg.Folders()[folder]; !ok {
|
||||
http.Error(w, "Invalid folder ID", 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(folder) == 0 {
|
||||
// Reset all folders.
|
||||
for folder := range cfg.Folders() {
|
||||
s.model.ResetFolder(folder)
|
||||
}
|
||||
s.flushResponse(`{"ok": "resetting database"}`, w)
|
||||
} else {
|
||||
// Reset a specific folder, assuming it's supposed to exist.
|
||||
s.model.ResetFolder(folder)
|
||||
s.flushResponse(`{"ok": "resetting folder `+folder+`"}`, w)
|
||||
}
|
||||
|
||||
go restart()
|
||||
}
|
||||
|
||||
|
@ -1732,15 +1732,9 @@ func (m *Model) CheckFolderHealth(id string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Model) ResetFolder(folder string) error {
|
||||
for _, f := range db.ListFolders(m.db) {
|
||||
if f == folder {
|
||||
func (m *Model) ResetFolder(folder string) {
|
||||
l.Infof("Cleaning data for folder %q", folder)
|
||||
db.DropFolder(m.db, folder)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Unknown folder %q", folder)
|
||||
}
|
||||
|
||||
func (m *Model) String() string {
|
||||
|
@ -9,6 +9,7 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -67,17 +68,17 @@ func TestReset(t *testing.T) {
|
||||
|
||||
// Reset indexes of the default folder
|
||||
log.Println("Reset indexes of default folder")
|
||||
_, err = p.Post("/rest/system/reset?folder=default", nil)
|
||||
bs, err := p.Post("/rest/system/reset?folder=default", nil)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to reset indexes of the default folder:", err)
|
||||
t.Fatalf("Failed to reset indexes (default): %v (%s)", err, bytes.TrimSpace(bs))
|
||||
}
|
||||
|
||||
// Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So
|
||||
// we wait for it to exit, then do a stop so the rc.Process is happy and
|
||||
// restart it again.
|
||||
time.Sleep(time.Second)
|
||||
checkedStop(t, p)
|
||||
p = startInstance(t, 1)
|
||||
defer checkedStop(t, p)
|
||||
|
||||
m, err = p.Model("default")
|
||||
if err != nil {
|
||||
@ -108,17 +109,14 @@ func TestReset(t *testing.T) {
|
||||
|
||||
// Reset all indexes
|
||||
log.Println("Reset DB...")
|
||||
_, err = p.Post("/rest/system/reset?folder=default", nil)
|
||||
bs, err = p.Post("/rest/system/reset", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to reset indexes", err)
|
||||
t.Fatalf("Failed to reset indexes (all): %v (%s)", err, bytes.TrimSpace(bs))
|
||||
}
|
||||
|
||||
// Syncthing restarts on reset. But we set STNORESTART=1 for the tests. So
|
||||
// we wait for it to exit, then do a stop so the rc.Process is happy and
|
||||
// restart it again.
|
||||
// we wait for it to exit, then restart it again.
|
||||
time.Sleep(time.Second)
|
||||
checkedStop(t, p)
|
||||
|
||||
p = startInstance(t, 1)
|
||||
defer checkedStop(t, p)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user