|
|
@@ -942,13 +942,26 @@ func (s *ServerService) ImportDB(file multipart.File) error {
|
|
|
return common.NewErrorf("Error saving db: %v", err)
|
|
|
}
|
|
|
|
|
|
- // Check if we can init the db or not
|
|
|
- if err = database.InitDB(tempPath); err != nil {
|
|
|
- return common.NewErrorf("Error checking db: %v", err)
|
|
|
+ // Close temp file before opening via sqlite
|
|
|
+ if err = tempFile.Close(); err != nil {
|
|
|
+ return common.NewErrorf("Error closing temporary db file: %v", err)
|
|
|
}
|
|
|
+ tempFile = nil
|
|
|
|
|
|
- // Stop Xray
|
|
|
- s.StopXrayService()
|
|
|
+ // Validate integrity (no migrations / side effects)
|
|
|
+ if err = database.ValidateSQLiteDB(tempPath); err != nil {
|
|
|
+ return common.NewErrorf("Invalid or corrupt db file: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Stop Xray (ignore error but log)
|
|
|
+ if errStop := s.StopXrayService(); errStop != nil {
|
|
|
+ logger.Warningf("Failed to stop Xray before DB import: %v", errStop)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Close existing DB to release file locks (especially on Windows)
|
|
|
+ if errClose := database.CloseDB(); errClose != nil {
|
|
|
+ logger.Warningf("Failed to close existing DB before replacement: %v", errClose)
|
|
|
+ }
|
|
|
|
|
|
// Backup the current database for fallback
|
|
|
fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath())
|
|
|
@@ -983,7 +996,7 @@ func (s *ServerService) ImportDB(file multipart.File) error {
|
|
|
return common.NewErrorf("Error moving db file: %v", err)
|
|
|
}
|
|
|
|
|
|
- // Migrate DB
|
|
|
+ // Open & migrate new DB
|
|
|
if err = database.InitDB(config.GetDBPath()); err != nil {
|
|
|
if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil {
|
|
|
return common.NewErrorf("Error migrating db and restoring fallback: %v", errRename)
|