1
0

db_settled_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package database
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. // Locks the #5665 guard: composite-PK client_inbounds has no id column, so the
  8. // sequence-reset SQL must never be issued for it.
  9. func TestTableWithIdColumn_SkipsCompositeKeyModels(t *testing.T) {
  10. if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  11. t.Fatalf("InitDB: %v", err)
  12. }
  13. t.Cleanup(func() { _ = CloseDB() })
  14. if table, ok := tableWithIdColumn(db, &model.ClientInbound{}); ok {
  15. t.Errorf("ClientInbound (table %q) has no id column but was not skipped", table)
  16. }
  17. table, ok := tableWithIdColumn(db, &model.Inbound{})
  18. if !ok {
  19. t.Fatal("Inbound has an id column but was reported as skippable")
  20. }
  21. if table != "inbounds" {
  22. t.Errorf("Inbound table = %q, want inbounds", table)
  23. }
  24. }
  25. // Exercises the #5665 AutoMigrate skip on SQLite (the check is dialect-agnostic):
  26. // settled after InitDB, not settled with a missing column or table.
  27. func TestPostgresModelSettled_TracksSchemaPresence(t *testing.T) {
  28. if err := InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  29. t.Fatalf("InitDB: %v", err)
  30. }
  31. t.Cleanup(func() { _ = CloseDB() })
  32. for _, mdl := range []any{&model.ClientRecord{}, &model.ClientGroup{}, &model.ClientInbound{}} {
  33. if !postgresModelSettled(mdl) {
  34. t.Errorf("%T not settled right after InitDB", mdl)
  35. }
  36. }
  37. if err := db.Migrator().DropColumn(&model.ClientGroup{}, "reset_up"); err != nil {
  38. t.Fatalf("drop column: %v", err)
  39. }
  40. if postgresModelSettled(&model.ClientGroup{}) {
  41. t.Error("ClientGroup settled despite missing reset_up column")
  42. }
  43. if err := db.Migrator().DropTable(&model.ClientGroup{}); err != nil {
  44. t.Fatalf("drop table: %v", err)
  45. }
  46. if postgresModelSettled(&model.ClientGroup{}) {
  47. t.Error("ClientGroup settled despite missing table")
  48. }
  49. }