Files
mirror-3x-ui/internal/database/node_client_traffic_dip_migration_test.go
T
Hamed c7f5850e5f test(node): cover counter resets on PostgreSQL (#7272)
The three regressions for a node counter that restarts below its baseline, and the migration test for the column that fix added, shared fixed node ids and inbound tags. Run as a group against PostgreSQL, the database in the report, the second one failed on a duplicate key before reaching the code under test.

Give each case its own PostgreSQL schema and run all four in the PostgreSQL CI job, requiring four passes. No panel code changes; the fix itself is already on main in 408fd682.

Refs #7271
2026-11-29 18:55:46 +03:30

59 lines
2.0 KiB
Go

package database
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
)
func TestMigrateNodeClientTrafficDippedWhileFrozenColumn(t *testing.T) {
cleanup, err := testpg.IsolatePackage("node_frozen_dip_migration")
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanup)
initMigrateDB(t)
tx := GetDB().Begin()
if tx.Error != nil {
t.Fatal(tx.Error)
}
t.Cleanup(func() { tx.Rollback() })
// Recreate the previous schema in this transaction on SQLite or PostgreSQL.
if err := tx.Exec("ALTER TABLE node_client_traffics DROP COLUMN dipped_while_frozen").Error; err != nil {
t.Fatal(err)
}
const email = "legacy-baseline@x"
if err := tx.Exec("INSERT INTO node_client_traffics (node_id, email, up, down) VALUES (7, ?, 30, 50)", email).Error; err != nil {
t.Fatal(err)
}
if err := migrateNodeClientTrafficDippedWhileFrozenColumn(tx); err != nil {
t.Fatal(err)
}
var stored model.NodeClientTraffic
if err := tx.Where("email = ?", email).First(&stored).Error; err != nil {
t.Fatal(err)
}
if stored.NodeId != 7 || stored.Up != 30 || stored.Down != 50 || stored.DippedWhileFrozen {
t.Fatalf("legacy baseline after migration: node=%d up=%d down=%d dipped=%t, want 7/30/50/false",
stored.NodeId, stored.Up, stored.Down, stored.DippedWhileFrozen)
}
var nulls int64
if err := tx.Raw("SELECT COUNT(*) FROM node_client_traffics WHERE dipped_while_frozen IS NULL").Scan(&nulls).Error; err != nil {
t.Fatal(err)
}
if nulls != 0 {
t.Fatalf("migration left %d NULL dipped_while_frozen cell(s), want 0", nulls)
}
if err := tx.Model(&stored).Update("dipped_while_frozen", true).Error; err != nil {
t.Fatal(err)
}
if err := migrateNodeClientTrafficDippedWhileFrozenColumn(tx); err != nil {
t.Fatalf("repeat migration: %v", err)
}
if err := tx.First(&stored, stored.Id).Error; err != nil || !stored.DippedWhileFrozen {
t.Fatalf("repeat migration lost the pending dip: dipped=%t, err=%v", stored.DippedWhileFrozen, err)
}
}