chore(db): stop wiping databases with an older schema version
All installations are past schema version 2, so the one-time reset is gone; data is never deleted on start (demo mode still resets). user_version is kept for future migrations. Legal test uses an in-memory DB (cleanup race). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1df32ece92
commit
108d166990
5 files changed
+12
-25
No files matched your search
@@ -6,7 +6,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -58,22 +57,11 @@ func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// schemaVersion is stored in PRAGMA user_version. Databases from an older version are wiped and rebuilt
|
||||
// on start: MindSift is not in production yet, so breaking changes need no migration path.
|
||||
// schemaVersion is stored in PRAGMA user_version so later releases can tell which migrations a database
|
||||
// already has. (Version 2 once wiped older databases on start; all installations are past that now.)
|
||||
const schemaVersion = 2
|
||||
|
||||
func (s *Store) resetOutdatedSchema() error {
|
||||
var version int
|
||||
if err := s.db.QueryRow(`PRAGMA user_version`).Scan(&version); err != nil {
|
||||
return err
|
||||
}
|
||||
if version >= schemaVersion {
|
||||
return nil
|
||||
}
|
||||
slog.Warn("Veraltetes Datenbankschema: alle Daten werden gelöscht und neu angelegt", "version", version, "neu", schemaVersion)
|
||||
return s.dropAllTables()
|
||||
}
|
||||
|
||||
// dropAllTables removes every table; used by the demo mode reset.
|
||||
func (s *Store) dropAllTables() error {
|
||||
conn, err := s.db.Conn(context.Background())
|
||||
if err != nil {
|
||||
@@ -107,9 +95,6 @@ func (s *Store) dropAllTables() error {
|
||||
}
|
||||
|
||||
func (s *Store) migrate() error {
|
||||
if err := s.resetOutdatedSchema(); err != nil {
|
||||
return fmt.Errorf("reset outdated schema: %w", err)
|
||||
}
|
||||
schema := `
|
||||
PRAGMA journal_mode = WAL;
|
||||
PRAGMA synchronous = NORMAL;
|
||||
|
||||
@@ -144,8 +144,9 @@ func TestVisibilityFollowsOrganizations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutdatedSchemaIsWiped(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "old.db")
|
||||
// Existing data must survive a restart, whatever user_version the database carries.
|
||||
func TestDataSurvivesRestart(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "keep.db")
|
||||
store, err := NewStore(path)
|
||||
if err != nil {
|
||||
t.Fatalf("NewStore failed: %v", err)
|
||||
@@ -159,8 +160,8 @@ func TestOutdatedSchemaIsWiped(t *testing.T) {
|
||||
t.Fatalf("reopen failed: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
if n, _ := store.CountUsers(); n != 0 {
|
||||
t.Fatalf("outdated database should start empty, found %d users", n)
|
||||
if n, _ := store.CountUsers(); n != 1 {
|
||||
t.Fatalf("data must be kept on restart, found %d users", n)
|
||||
}
|
||||
var v int
|
||||
_ = store.db.QueryRow(`PRAGMA user_version`).Scan(&v)
|
||||
|
||||
Reference in new issue
Block a user