mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-05 20:09:34 +00:00
feat(migrate-db): SQLite <-> .dump conversion and Download Migration in Overview
Binary: extend the migrate-db subcommand with --dump and --restore so a SQLite database can be exported to a portable SQL text dump and rebuilt from one, alongside the existing --dsn PostgreSQL copy. Implemented in Go via the bundled sqlite driver (new database/dump_sqlite.go); no external sqlite3 client is required. Add ExportPostgresToSQLite (reverse of MigrateData) to build a SQLite .db from live PostgreSQL data, reusing the shared copyAllModels helper. Overview: add a "Download Migration" item to Backup & Restore plus a getMigration endpoint/service that returns a .dump on SQLite or a .db on PostgreSQL, so the data can seed a panel on the other backend. Document the endpoint in api-docs and translate the three new strings across all locales. Tests: cover the destination-side copy (AutoMigrate + copyTable into SQLite) and the dump/restore round-trip including quoted values. Ignore *.dump. The x-ui.sh helper that drives this from the CLI is in PR #4910.
This commit is contained in:
137
database/dump_sqlite_test.go
Normal file
137
database/dump_sqlite_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/xray"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// TestCopyAllModelsIntoSQLite exercises the same AutoMigrate + copyTable
|
||||
// machinery that ExportPostgresToSQLite relies on, but with a SQLite source so
|
||||
// it needs no external database. The Postgres source path uses identical gorm
|
||||
// reads (see MigrateData), so this validates the destination-side copy.
|
||||
func TestCopyAllModelsIntoSQLite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
srcPath := filepath.Join(dir, "src.db")
|
||||
dstPath := filepath.Join(dir, "dst.db")
|
||||
|
||||
src, err := gorm.Open(sqlite.Open(srcPath), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open src: %v", err)
|
||||
}
|
||||
defer closeGorm(src)
|
||||
for _, m := range migrationModels() {
|
||||
if err := src.AutoMigrate(m); err != nil {
|
||||
t.Fatalf("automigrate src %T: %v", m, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Seed a few rows across parent/child tables and a composite-PK table.
|
||||
if err := src.Create(&model.User{Username: "admin", Password: "x"}).Error; err != nil {
|
||||
t.Fatalf("seed user: %v", err)
|
||||
}
|
||||
if err := src.Create(&model.Inbound{UserId: 1, Remark: "in", Port: 443, Protocol: "vless", Tag: "inbound-443"}).Error; err != nil {
|
||||
t.Fatalf("seed inbound: %v", err)
|
||||
}
|
||||
if err := src.Create(&xray.ClientTraffic{InboundId: 1, Email: "a@b.c", Enable: true, Up: 10, Down: 20}).Error; err != nil {
|
||||
t.Fatalf("seed traffic: %v", err)
|
||||
}
|
||||
|
||||
dst, err := gorm.Open(sqlite.Open(dstPath), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open dst: %v", err)
|
||||
}
|
||||
defer closeGorm(dst)
|
||||
if err := copyAllModels(src, dst); err != nil {
|
||||
t.Fatalf("copyAllModels: %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
model any
|
||||
want int64
|
||||
}{
|
||||
{&model.User{}, 1},
|
||||
{&model.Inbound{}, 1},
|
||||
{&xray.ClientTraffic{}, 1},
|
||||
} {
|
||||
var got int64
|
||||
if err := dst.Model(tc.model).Count(&got).Error; err != nil {
|
||||
t.Fatalf("count %T: %v", tc.model, err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("%T: got %d rows, want %d", tc.model, got, tc.want)
|
||||
}
|
||||
}
|
||||
|
||||
// Spot-check a copied value survived the round-trip.
|
||||
var ct xray.ClientTraffic
|
||||
if err := dst.Where("email = ?", "a@b.c").First(&ct).Error; err != nil {
|
||||
t.Fatalf("read back traffic: %v", err)
|
||||
}
|
||||
if ct.Up != 10 || ct.Down != 20 || !ct.Enable {
|
||||
t.Errorf("traffic mismatch: %+v", ct)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDumpAndRestoreSQLiteRoundTrip dumps a seeded SQLite db to .dump text and
|
||||
// rebuilds it, asserting the row survives.
|
||||
func TestDumpAndRestoreSQLiteRoundTrip(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
srcPath := filepath.Join(dir, "src.db")
|
||||
dumpPath := filepath.Join(dir, "out.dump")
|
||||
dstPath := filepath.Join(dir, "rebuilt.db")
|
||||
|
||||
src, err := gorm.Open(sqlite.Open(srcPath), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open src: %v", err)
|
||||
}
|
||||
if err := src.AutoMigrate(&model.Setting{}); err != nil {
|
||||
t.Fatalf("automigrate: %v", err)
|
||||
}
|
||||
if err := src.Create(&model.Setting{Key: "secret", Value: "o'brien \"quote\""}).Error; err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
if sqlDB, _ := src.DB(); sqlDB != nil {
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
if err := DumpSQLite(srcPath, dumpPath); err != nil {
|
||||
t.Fatalf("DumpSQLite: %v", err)
|
||||
}
|
||||
if fi, err := os.Stat(dumpPath); err != nil || fi.Size() == 0 {
|
||||
t.Fatalf("dump missing/empty: %v", err)
|
||||
}
|
||||
if err := RestoreSQLite(dumpPath, dstPath); err != nil {
|
||||
t.Fatalf("RestoreSQLite: %v", err)
|
||||
}
|
||||
|
||||
dst, err := gorm.Open(sqlite.Open(dstPath), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open dst: %v", err)
|
||||
}
|
||||
defer closeGorm(dst)
|
||||
var s model.Setting
|
||||
if err := dst.Where("key = ?", "secret").First(&s).Error; err != nil {
|
||||
t.Fatalf("read back: %v", err)
|
||||
}
|
||||
if s.Value != "o'brien \"quote\"" {
|
||||
t.Errorf("value mismatch after round-trip: %q", s.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// closeGorm closes the underlying *sql.DB so Windows can delete the temp file.
|
||||
func closeGorm(db *gorm.DB) {
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
if s, err := db.DB(); err == nil {
|
||||
s.Close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user