migrate_data.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package database
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "os"
  8. "path"
  9. "reflect"
  10. "strings"
  11. "time"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  14. "gorm.io/driver/postgres"
  15. "gorm.io/driver/sqlite"
  16. "gorm.io/gorm"
  17. "gorm.io/gorm/logger"
  18. )
  19. // migrationModels is the FK-aware order in which tables are created and copied
  20. // during `x-ui migrate-db --dsn` (SQLite → PostgreSQL data migration) and in
  21. // related tests.
  22. //
  23. // Important: When adding a new top-level model (like OutboundSubscription),
  24. // you must add it here **in addition to** the list in internal/database/db.go:initModels().
  25. // This list is used for:
  26. // - Creating the destination schema during cross-DB migration
  27. // - Truncating tables
  28. // - Copying data row-by-row
  29. // - Resyncing Postgres sequences after bulk insert
  30. //
  31. // DumpSQLite / RestoreSQLite are schema-introspective (they read sqlite_master)
  32. // so they do not need manual updates.
  33. func migrationModels() []any {
  34. return []any{
  35. &model.User{},
  36. &model.Setting{},
  37. &model.HistoryOfSeeders{},
  38. &model.Node{},
  39. &model.ApiToken{},
  40. &model.Inbound{},
  41. &xray.ClientTraffic{},
  42. &model.OutboundTraffics{},
  43. &model.InboundClientIps{},
  44. &model.ClientRecord{},
  45. &model.ClientInbound{},
  46. &model.ClientExternalLink{},
  47. &model.InboundFallback{},
  48. &model.Host{},
  49. &model.NodeClientTraffic{},
  50. &model.NodeClientIp{},
  51. &model.OutboundSubscription{},
  52. }
  53. }
  54. // MigrateData copies every row from the configured SQLite file at srcPath into
  55. // a fresh PostgreSQL database described by dstDSN. The destination tables are
  56. // (re)created with AutoMigrate before the copy. Source data is left untouched.
  57. func MigrateData(srcPath, dstDSN string) error {
  58. if _, err := os.Stat(srcPath); err != nil {
  59. return fmt.Errorf("source sqlite not found at %s: %w", srcPath, err)
  60. }
  61. if dstDSN == "" {
  62. return errors.New("destination DSN is required")
  63. }
  64. if err := os.MkdirAll(path.Dir(srcPath), 0755); err != nil {
  65. return err
  66. }
  67. srcDSN := srcPath + "?_journal_mode=WAL&_busy_timeout=10000"
  68. src, err := gorm.Open(sqlite.Open(srcDSN), &gorm.Config{Logger: logger.Discard})
  69. if err != nil {
  70. return fmt.Errorf("open sqlite source: %w", err)
  71. }
  72. srcSQL, err := src.DB()
  73. if err != nil {
  74. return err
  75. }
  76. defer srcSQL.Close()
  77. dst, err := gorm.Open(postgres.Open(dstDSN), &gorm.Config{Logger: logger.Discard})
  78. if err != nil {
  79. return fmt.Errorf("open postgres destination: %w", err)
  80. }
  81. dstSQL, err := dst.DB()
  82. if err != nil {
  83. return err
  84. }
  85. defer dstSQL.Close()
  86. dstSQL.SetConnMaxLifetime(time.Hour)
  87. log.Println("Creating destination schema...")
  88. for _, m := range migrationModels() {
  89. if err := dst.AutoMigrate(m); err != nil {
  90. return fmt.Errorf("AutoMigrate %T: %w", m, err)
  91. }
  92. }
  93. // AutoMigrate re-creates the legacy client_traffics -> inbounds foreign key,
  94. // but the running panel drops it (see dropLegacyForeignKeys) and tolerates
  95. // client_traffics rows whose inbound was deleted. Drop it here too so copying
  96. // such orphaned rows can't fail with an fk_inbounds_client_stats violation.
  97. if err := dst.Exec("ALTER TABLE client_traffics DROP CONSTRAINT IF EXISTS fk_inbounds_client_stats").Error; err != nil {
  98. return fmt.Errorf("drop legacy foreign key: %w", err)
  99. }
  100. // Empty the destination tables so the migration is idempotent: a fresh
  101. // PostgreSQL DB already holds an auto-seeded admin (id=1) from any prior
  102. // panel start, and a partially-failed earlier run leaves rows behind. Either
  103. // way a plain INSERT with explicit ids would collide on users_pkey, so clear
  104. // our tables (only) before copying.
  105. if err := truncatePostgresTables(dst, migrationModels()); err != nil {
  106. return fmt.Errorf("clear destination tables: %w", err)
  107. }
  108. totalRows := 0
  109. for _, m := range migrationModels() {
  110. n, err := copyTable(src, dst, m)
  111. if err != nil {
  112. return fmt.Errorf("copy %T: %w", m, err)
  113. }
  114. totalRows += n
  115. log.Printf(" %-32s %d rows", reflect.TypeOf(m).Elem().Name(), n)
  116. }
  117. if err := resetPostgresSequences(dst); err != nil {
  118. log.Printf("warning: failed to reset some postgres sequences: %v", err)
  119. }
  120. log.Printf("Migration complete: %d rows across %d tables.", totalRows, len(migrationModels()))
  121. log.Println("Set XUI_DB_TYPE=postgres and XUI_DB_DSN=... in /etc/default/x-ui, then restart x-ui.")
  122. return nil
  123. }
  124. // ExportPostgresToSQLite copies every row from the PostgreSQL database described
  125. // by srcDSN into a fresh SQLite file at dstPath. It is the reverse of
  126. // MigrateData and is used to hand a PostgreSQL-backed panel a portable .db file.
  127. // dstPath is created/overwritten; the PostgreSQL source is left untouched.
  128. func ExportPostgresToSQLite(srcDSN, dstPath string) error {
  129. if srcDSN == "" {
  130. return errors.New("source DSN is required")
  131. }
  132. if err := os.MkdirAll(path.Dir(dstPath), 0755); err != nil {
  133. return err
  134. }
  135. // Start from an empty file so AutoMigrate creates the canonical schema.
  136. if err := os.Remove(dstPath); err != nil && !os.IsNotExist(err) {
  137. return err
  138. }
  139. src, err := gorm.Open(postgres.Open(srcDSN), &gorm.Config{Logger: logger.Discard})
  140. if err != nil {
  141. return fmt.Errorf("open postgres source: %w", err)
  142. }
  143. srcSQL, err := src.DB()
  144. if err != nil {
  145. return err
  146. }
  147. defer srcSQL.Close()
  148. // No WAL: keep all data in the main file so it is complete once closed.
  149. dst, err := gorm.Open(sqlite.Open(dstPath+"?_busy_timeout=10000"), &gorm.Config{Logger: logger.Discard})
  150. if err != nil {
  151. return fmt.Errorf("open sqlite destination: %w", err)
  152. }
  153. dstSQL, err := dst.DB()
  154. if err != nil {
  155. return err
  156. }
  157. defer dstSQL.Close()
  158. return copyAllModels(src, dst)
  159. }
  160. // copyAllModels (re)creates the schema on dst and copies every migrated table
  161. // from src to dst in FK-safe order. src/dst may be any gorm backend.
  162. func copyAllModels(src, dst *gorm.DB) error {
  163. for _, m := range migrationModels() {
  164. if err := dst.AutoMigrate(m); err != nil {
  165. return fmt.Errorf("AutoMigrate %T: %w", m, err)
  166. }
  167. }
  168. for _, m := range migrationModels() {
  169. if _, err := copyTable(src, dst, m); err != nil {
  170. return fmt.Errorf("copy %T: %w", m, err)
  171. }
  172. }
  173. return nil
  174. }
  175. func copyTable(src, dst *gorm.DB, mdl any) (int, error) {
  176. const batchSize = 500
  177. sliceType := reflect.SliceOf(reflect.PointerTo(reflect.TypeOf(mdl).Elem()))
  178. stmt := &gorm.Statement{DB: src}
  179. if err := stmt.Parse(mdl); err != nil {
  180. return 0, err
  181. }
  182. order := strings.Join(stmt.Schema.PrimaryFieldDBNames, ", ")
  183. table := stmt.Schema.Table
  184. columns := stmt.Schema.DBNames
  185. ctx := context.Background()
  186. total := 0
  187. for offset := 0; ; offset += batchSize {
  188. batchPtr := reflect.New(sliceType)
  189. q := src.Model(mdl).Limit(batchSize).Offset(offset)
  190. if order != "" {
  191. q = q.Order(order)
  192. }
  193. if err := q.Find(batchPtr.Interface()).Error; err != nil {
  194. return total, err
  195. }
  196. slice := batchPtr.Elem()
  197. n := slice.Len()
  198. if n == 0 {
  199. break
  200. }
  201. rows := make([]map[string]any, n)
  202. for i := 0; i < n; i++ {
  203. rv := reflect.Indirect(slice.Index(i))
  204. row := make(map[string]any, len(columns))
  205. for _, name := range columns {
  206. value, _ := stmt.Schema.FieldsByDBName[name].ValueOf(ctx, rv)
  207. row[name] = value
  208. }
  209. rows[i] = row
  210. }
  211. if err := dst.Table(table).CreateInBatches(rows, 200).Error; err != nil {
  212. return total, err
  213. }
  214. total += n
  215. if n < batchSize {
  216. break
  217. }
  218. }
  219. return total, nil
  220. }
  221. // truncatePostgresTables empties every migrated table on dst in a single
  222. // statement, resetting identity sequences. CASCADE covers the inbound/client
  223. // foreign keys regardless of insertion order. Only the panel's own tables are
  224. // touched, never the rest of the schema.
  225. func truncatePostgresTables(dst *gorm.DB, models []any) error {
  226. tables := make([]string, 0, len(models))
  227. for _, m := range models {
  228. stmt := &gorm.Statement{DB: dst}
  229. if err := stmt.Parse(m); err != nil {
  230. return err
  231. }
  232. tables = append(tables, `"`+stmt.Schema.Table+`"`)
  233. }
  234. if len(tables) == 0 {
  235. return nil
  236. }
  237. log.Println("Clearing destination tables...")
  238. return dst.Exec("TRUNCATE TABLE " + strings.Join(tables, ", ") + " RESTART IDENTITY CASCADE").Error
  239. }
  240. // resetPostgresSequences advances each migrated table's id sequence past MAX(id),
  241. // otherwise the next INSERT-without-id would clash with copied rows.
  242. func resetPostgresSequences(dst *gorm.DB) error {
  243. return resyncPostgresSequences(dst, migrationModels())
  244. }
  245. // resyncPostgresSequences sets each model's id sequence to MAX(id) so the next
  246. // auto-increment INSERT won't collide with an existing row. Table names are
  247. // resolved from the models themselves (not hardcoded), so they always match the
  248. // migrated tables. The statement is a no-op for tables without an id sequence
  249. // (e.g. composite-PK tables), and idempotent on a healthy DB, so it is safe to
  250. // run both after migration and on every Postgres startup.
  251. func resyncPostgresSequences(db *gorm.DB, models []any) error {
  252. for _, m := range models {
  253. stmt := &gorm.Statement{DB: db}
  254. if err := stmt.Parse(m); err != nil {
  255. continue
  256. }
  257. t := stmt.Table
  258. // t comes from the trusted model set parsed by GORM, not user input, so
  259. // interpolating it as an identifier is safe. We ignore errors per-table.
  260. _ = db.Exec(
  261. `SELECT setval(pg_get_serial_sequence(?, 'id'), COALESCE((SELECT MAX(id) FROM "`+t+`"), 1), true)
  262. WHERE pg_get_serial_sequence(?, 'id') IS NOT NULL`,
  263. t, t,
  264. ).Error
  265. }
  266. return nil
  267. }