mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
Merge remote-tracking branch 'origin/main' into codex/nodes-fleet-7342
This commit is contained in:
@@ -91,6 +91,7 @@ func allModels() []any {
|
||||
&model.NodeClientIp{},
|
||||
&model.ClientGlobalTraffic{},
|
||||
&model.NodePendingReset{},
|
||||
&model.NodePendingClientDelete{},
|
||||
&model.OutboundSubscription{},
|
||||
&model.SubBalancer{},
|
||||
&model.TuicTrafficReceipt{},
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -59,6 +60,7 @@ func migrationModels() []any {
|
||||
&model.NodeClientIp{},
|
||||
&model.ClientGlobalTraffic{},
|
||||
&model.NodePendingReset{},
|
||||
&model.NodePendingClientDelete{},
|
||||
&model.OutboundSubscription{},
|
||||
&model.SubBalancer{},
|
||||
&model.TuicTrafficReceipt{},
|
||||
@@ -225,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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
// NodePendingClientDelete tracks a permanent delete that the node has not confirmed.
|
||||
type NodePendingClientDelete struct {
|
||||
NodeId int `gorm:"primaryKey;autoIncrement:false;index:idx_node_delete_order,priority:1"`
|
||||
Email string `gorm:"primaryKey;index:idx_node_delete_order,priority:3"`
|
||||
ClientId int `gorm:"not null"`
|
||||
Generation string `gorm:"not null"`
|
||||
LastAttemptAt int64 `gorm:"not null;index:idx_node_delete_order,priority:2"`
|
||||
}
|
||||
Reference in New Issue
Block a user