fix(database): migrate backups that predate the delete queue

Treat the new queue as empty when it is absent from a legacy source. Keep copying queued operations from current backups and preserve strict handling for other tables.
This commit is contained in:
Hamed Sanaei
2026-12-02 13:37:12 +03:30
parent 6fe0bd86dd
commit 6bac85b829
2 changed files with 56 additions and 1 deletions
+11
View File
@@ -8,6 +8,7 @@ import (
"os"
"path"
"reflect"
"slices"
"strings"
"time"
@@ -226,6 +227,16 @@ func copyTable(src, dst *gorm.DB, mdl any) (int, error) {
}
order := strings.Join(stmt.Schema.PrimaryFieldDBNames, ", ")
table := stmt.Schema.Table
if _, optional := mdl.(*model.NodePendingClientDelete); optional {
// Backups predating the outbox have no pending deletes to copy.
tables, err := src.Migrator().GetTables()
if err != nil {
return 0, err
}
if !slices.Contains(tables, table) {
return 0, nil
}
}
columns := stmt.Schema.DBNames
ctx := context.Background()
+45 -1
View File
@@ -18,13 +18,16 @@ func TestMigrateData_CompositeKeyTableLargerThanBatch(t *testing.T) {
t.Skip("set XUI_TEST_PG_DSN to a reachable Postgres to run this test")
}
// Seed a SQLite source with the full schema and >500 client_inbounds rows.
// Seed a pre-outbox SQLite source with >500 client_inbounds rows.
srcPath := t.TempDir() + "/x-ui.db"
src, err := gorm.Open(sqlite.Open(srcPath), &gorm.Config{Logger: logger.Discard})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
for _, m := range migrationModels() {
if _, pending := m.(*model.NodePendingClientDelete); pending {
continue
}
if err := src.AutoMigrate(m); err != nil {
t.Fatalf("automigrate %T: %v", m, err)
}
@@ -61,6 +64,9 @@ func TestMigrateData_CompositeKeyTableLargerThanBatch(t *testing.T) {
if got != n {
t.Fatalf("client_inbounds rows = %d, want %d", got, n)
}
if err := dst.Model(&model.NodePendingClientDelete{}).Count(&got).Error; err != nil || got != 0 {
t.Fatalf("migrated pending delete rows = %d, want 0 (%v)", got, err)
}
}
func TestMigrateData_PreservesFalseDefaultedColumns(t *testing.T) {
@@ -207,3 +213,41 @@ func TestMigrateData_FailedCopyLeavesDestinationUntouched(t *testing.T) {
t.Fatalf("failed migration leaked partial rows: evil count = %d, want 0", evil)
}
}
func TestCopyPendingClientDeletesAcceptsLegacySourceAndPreservesNewRows(t *testing.T) {
openDB := func(path string) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{Logger: logger.Discard})
if err != nil {
t.Fatal(err)
}
conn, err := db.DB()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = conn.Close() })
return db
}
src := openDB(t.TempDir() + "/source.db")
dst := openDB(t.TempDir() + "/destination.db")
pending := &model.NodePendingClientDelete{NodeId: 1, Email: "pending@example.test", ClientId: 3, Generation: "queue-generation", LastAttemptAt: 17}
if err := dst.AutoMigrate(pending); err != nil {
t.Fatal(err)
}
if n, err := copyTable(src, dst, pending); err != nil || n != 0 {
t.Fatalf("copy from source without delete queue: %d rows, %v", n, err)
}
if err := src.AutoMigrate(pending); err != nil {
t.Fatal(err)
}
if err := src.Create(pending).Error; err != nil {
t.Fatal(err)
}
if n, err := copyTable(src, dst, pending); err != nil || n != 1 {
t.Fatalf("copy persisted delete queue: %d rows, %v", n, err)
}
var got model.NodePendingClientDelete
if err := dst.Take(&got).Error; err != nil || got != *pending {
t.Fatalf("copied delete intent = %+v, want %+v (%v)", got, pending, err)
}
}