db: clear blobs when using --clear-db or --force-clear-db (#13605)

* Call Close() as part of ClearDB

* Add method to clear blob storage

* Clear blob storage when clearing DB
This commit is contained in:
Preston Van Loon
2024-02-09 21:52:30 -06:00
committed by GitHub
parent 5df8b83a05
commit 03068ba781
7 changed files with 44 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package kv
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
@@ -84,10 +85,12 @@ func (s *Store) view(fn func(*bolt.Tx) error) error {
// ClearDB removes any previously stored data at the configured data directory.
func (s *Store) ClearDB() error {
if err := s.Close(); err != nil {
return fmt.Errorf("failed to close db: %w", err)
}
if _, err := os.Stat(s.databasePath); os.IsNotExist(err) {
return nil
}
prometheus.Unregister(createBoltCollector(s.db))
return os.Remove(filepath.Join(s.databasePath, ProtectionDbFileName))
}

View File

@@ -18,9 +18,6 @@ func SetupDB(t testing.TB, pubkeys [][fieldparams.BLSPubkeyLength]byte) iface.Va
t.Fatalf("Failed to instantiate DB: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
if err := db.ClearDB(); err != nil {
t.Fatalf("Failed to clear database: %v", err)
}