Files
3x-ui/internal/web/service/server_pg_import_sniff_test.go
T
MHSanaei 30b611614b feat: import SQLite migration dumps through the PostgreSQL panel restore
The SQLite panel's Download Migration produces a portable SQL text dump
advertised as seeding a PostgreSQL panel, but the PostgreSQL Restore only
accepted pg_dump custom archives, so the migration file was rejected with
'Invalid file' even though the upload picker asked for .dump. importDB now
sniffs the upload header: PGDMP archives keep the pg_restore path, while
raw SQLite databases (.db) and SQL text migration dumps are rebuilt,
integrity-checked, and copied into PostgreSQL with the same MigrateData
engine as 'x-ui migrate-db --dsn'. The restore picker accepts .dump/.db on
PostgreSQL and the backup modal texts describe the accepted formats in
every locale.
2026-07-12 18:04:38 +02:00

46 lines
1.4 KiB
Go

package service
import (
"path/filepath"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
)
func TestSniffPgImportKind(t *testing.T) {
cases := []struct {
name string
header []byte
want int
}{
{"pg custom archive", []byte("PGDMP\x01\x10\x04"), pgImportPgDump},
{"raw sqlite database", []byte("SQLite format 3\x00rest of header"), pgImportSQLiteDB},
{"sqlite cli dump without pragma", []byte("BEGIN TRANSACTION;\nCREATE TABLE t(i);"), pgImportSQLiteDump},
{"bom and whitespace before pragma", []byte("\xef\xbb\xbf\r\n PRAGMA foreign_keys=OFF;"), pgImportSQLiteDump},
{"plain-format postgres dump", []byte("--\n-- PostgreSQL database dump\n--"), pgImportUnknown},
{"empty file", nil, pgImportUnknown},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := sniffPgImportKind(tc.header); got != tc.want {
t.Errorf("sniffPgImportKind(%q) = %d, want %d", tc.header, got, tc.want)
}
})
}
t.Run("panel migration dump", func(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "x-ui.db")
if err := database.InitDB(dbPath); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dump, err := database.DumpSQLiteToBytes(dbPath)
if err != nil {
t.Fatalf("DumpSQLiteToBytes: %v", err)
}
if got := sniffPgImportKind(dump[:64]); got != pgImportSQLiteDump {
t.Errorf("sniffPgImportKind(real migration dump) = %d, want %d", got, pgImportSQLiteDump)
}
})
}