mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-14 23:31:00 +00:00
fix(database): create SQLite backup snapshots online (#6137)
* fix(database): snapshot SQLite backups online Use SQLite's online backup API for downloadable backups and SQLite migration exports instead of checkpointing then reading the live database file. The regression test validates a backup made while writes continue. * style(database): group SQLite driver imports * fix(database): bound online backup retries Use a single backup step and a bounded connection-acquisition/retry context. Tighten temporary-file cleanup and regression assertions while removing the unused checkpoint helper. * test(database): cover existing backup destinations * fix(database): harden SQLite snapshot lifecycle Sweep interrupted snapshot directories at SQLite startup, keep rollback-journal backups incremental, and make caller-owned cleanup explicit. Reuse one scheduled Telegram snapshot across administrators and make the direct SQLite driver dependency explicit. --------- Co-authored-by: PathGao <gaoyanbo@gaoyanbodeMacBook-Air.local>
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
func journalModeOf(t *testing.T) string {
|
||||
@@ -43,40 +39,3 @@ func TestSqliteJournalModeEnvOverrideDelete(t *testing.T) {
|
||||
t.Fatalf("journal_mode = %q, want delete", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalCheckpointMakesRawFileBackupComplete(t *testing.T) {
|
||||
t.Setenv("XUI_DB_JOURNAL_MODE", "")
|
||||
dbDir := t.TempDir()
|
||||
dbPath := filepath.Join(dbDir, "x-ui.db")
|
||||
if err := InitDB(dbPath); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = CloseDB() })
|
||||
|
||||
if err := db.Create(&model.Setting{Key: "walBackupProbe", Value: "42"}).Error; err != nil {
|
||||
t.Fatalf("write setting: %v", err)
|
||||
}
|
||||
if err := Checkpoint(); err != nil {
|
||||
t.Fatalf("Checkpoint: %v", err)
|
||||
}
|
||||
|
||||
raw, err := os.ReadFile(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read db file: %v", err)
|
||||
}
|
||||
copyPath := filepath.Join(t.TempDir(), "copy.db")
|
||||
if err := os.WriteFile(copyPath, raw, 0o600); err != nil {
|
||||
t.Fatalf("write copy: %v", err)
|
||||
}
|
||||
if err := ValidateSQLiteDB(copyPath); err != nil {
|
||||
t.Fatalf("checkpointed raw copy must be a valid sqlite db: %v", err)
|
||||
}
|
||||
|
||||
dump, err := DumpSQLiteToBytes(copyPath)
|
||||
if err != nil {
|
||||
t.Fatalf("dump copy: %v", err)
|
||||
}
|
||||
if !bytes.Contains(dump, []byte("walBackupProbe")) {
|
||||
t.Fatal("raw-file backup taken after Checkpoint must contain the latest write")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user