migrate_data.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package database
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path"
  8. "reflect"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/database/model"
  11. "github.com/mhsanaei/3x-ui/v3/xray"
  12. "gorm.io/driver/postgres"
  13. "gorm.io/driver/sqlite"
  14. "gorm.io/gorm"
  15. "gorm.io/gorm/logger"
  16. )
  17. // migrationModels is the FK-aware order in which tables are created and copied.
  18. // Parents come before their children so foreign-key constraints stay satisfied
  19. // even when checks are not explicitly disabled.
  20. func migrationModels() []any {
  21. return []any{
  22. &model.User{},
  23. &model.Setting{},
  24. &model.HistoryOfSeeders{},
  25. &model.CustomGeoResource{},
  26. &model.Node{},
  27. &model.ApiToken{},
  28. &model.Inbound{},
  29. &xray.ClientTraffic{},
  30. &model.OutboundTraffics{},
  31. &model.InboundClientIps{},
  32. &model.ClientRecord{},
  33. &model.ClientInbound{},
  34. &model.InboundFallback{},
  35. }
  36. }
  37. // MigrateData copies every row from the configured SQLite file at srcPath into
  38. // a fresh PostgreSQL database described by dstDSN. The destination tables are
  39. // (re)created with AutoMigrate before the copy. Source data is left untouched.
  40. func MigrateData(srcPath, dstDSN string) error {
  41. if _, err := os.Stat(srcPath); err != nil {
  42. return fmt.Errorf("source sqlite not found at %s: %w", srcPath, err)
  43. }
  44. if dstDSN == "" {
  45. return errors.New("destination DSN is required")
  46. }
  47. if err := os.MkdirAll(path.Dir(srcPath), 0755); err != nil {
  48. return err
  49. }
  50. srcDSN := srcPath + "?_journal_mode=WAL&_busy_timeout=10000"
  51. src, err := gorm.Open(sqlite.Open(srcDSN), &gorm.Config{Logger: logger.Discard})
  52. if err != nil {
  53. return fmt.Errorf("open sqlite source: %w", err)
  54. }
  55. srcSQL, err := src.DB()
  56. if err != nil {
  57. return err
  58. }
  59. defer srcSQL.Close()
  60. dst, err := gorm.Open(postgres.Open(dstDSN), &gorm.Config{Logger: logger.Discard})
  61. if err != nil {
  62. return fmt.Errorf("open postgres destination: %w", err)
  63. }
  64. dstSQL, err := dst.DB()
  65. if err != nil {
  66. return err
  67. }
  68. defer dstSQL.Close()
  69. dstSQL.SetConnMaxLifetime(time.Hour)
  70. log.Println("Creating destination schema...")
  71. for _, m := range migrationModels() {
  72. if err := dst.AutoMigrate(m); err != nil {
  73. return fmt.Errorf("AutoMigrate %T: %w", m, err)
  74. }
  75. }
  76. totalRows := 0
  77. for _, m := range migrationModels() {
  78. n, err := copyTable(src, dst, m)
  79. if err != nil {
  80. return fmt.Errorf("copy %T: %w", m, err)
  81. }
  82. totalRows += n
  83. log.Printf(" %-32s %d rows", reflect.TypeOf(m).Elem().Name(), n)
  84. }
  85. if err := resetPostgresSequences(dst); err != nil {
  86. log.Printf("warning: failed to reset some postgres sequences: %v", err)
  87. }
  88. log.Printf("Migration complete: %d rows across %d tables.", totalRows, len(migrationModels()))
  89. log.Println("Set XUI_DB_TYPE=postgres and XUI_DB_DSN=... in /etc/default/x-ui, then restart x-ui.")
  90. return nil
  91. }
  92. // copyTable streams every row of `mdl` from src to dst in batches.
  93. func copyTable(src, dst *gorm.DB, mdl any) (int, error) {
  94. sliceType := reflect.SliceOf(reflect.PointerTo(reflect.TypeOf(mdl).Elem()))
  95. batchPtr := reflect.New(sliceType)
  96. batchPtr.Elem().Set(reflect.MakeSlice(sliceType, 0, 0))
  97. total := 0
  98. err := src.Model(mdl).FindInBatches(batchPtr.Interface(), 500, func(tx *gorm.DB, _ int) error {
  99. batch := batchPtr.Elem()
  100. if batch.Len() == 0 {
  101. return nil
  102. }
  103. if err := dst.CreateInBatches(batchPtr.Interface(), 200).Error; err != nil {
  104. return err
  105. }
  106. total += batch.Len()
  107. return nil
  108. }).Error
  109. return total, err
  110. }
  111. // resetPostgresSequences advances each table's id sequence past MAX(id),
  112. // otherwise the next INSERT-without-id would clash with copied rows.
  113. func resetPostgresSequences(dst *gorm.DB) error {
  114. tables := []string{
  115. "users", "inbounds", "outbound_traffics", "settings", "inbound_client_ips",
  116. "client_traffics", "history_of_seeders", "custom_geo_resources", "nodes",
  117. "api_tokens", "client_records", "client_inbounds", "inbound_fallback_children",
  118. }
  119. for _, t := range tables {
  120. // setval is a no-op if the table or its id sequence doesn't exist; we ignore errors per-table.
  121. _ = dst.Exec(fmt.Sprintf(
  122. `SELECT setval(pg_get_serial_sequence('%s','id'), COALESCE((SELECT MAX(id) FROM "%s"), 1), true)
  123. WHERE pg_get_serial_sequence('%s','id') IS NOT NULL`,
  124. t, t, t,
  125. )).Error
  126. }
  127. return nil
  128. }