Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormhsanaei <ho3ein.sanaei@gmail.com>2025-10-14 23:03:17 +0300
committermhsanaei <ho3ein.sanaei@gmail.com>2025-10-14 23:03:17 +0300
commitd8523bbdac4a65e3e33bca6d1a50a58f33804f52 (patch)
treeaed89949c074e3fa25e82c2881c967b620a560ef /database
parent8afa39144ec68714aa584ad34925dde4bbdb14ca (diff)
fix(import): prevent sqlite disk I/O error by validating temp DB then swapping
Diffstat (limited to 'database')
-rw-r--r--database/db.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/database/db.go b/database/db.go
index 6de81d79..6b579dd9 100644
--- a/database/db.go
+++ b/database/db.go
@@ -4,6 +4,7 @@ package database
import (
"bytes"
+ "errors"
"io"
"io/fs"
"log"
@@ -199,3 +200,29 @@ func Checkpoint() error {
}
return nil
}
+
+// ValidateSQLiteDB opens the provided sqlite DB path with a throw-away connection
+// and runs a PRAGMA integrity_check to ensure the file is structurally sound.
+// It does not mutate global state or run migrations.
+func ValidateSQLiteDB(dbPath string) error {
+ if _, err := os.Stat(dbPath); err != nil { // file must exist
+ return err
+ }
+ gdb, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{Logger: logger.Discard})
+ if err != nil {
+ return err
+ }
+ sqlDB, err := gdb.DB()
+ if err != nil {
+ return err
+ }
+ defer sqlDB.Close()
+ var res string
+ if err := gdb.Raw("PRAGMA integrity_check;").Scan(&res).Error; err != nil {
+ return err
+ }
+ if res != "ok" {
+ return errors.New("sqlite integrity check failed: " + res)
+ }
+ return nil
+}