fix: Bug when opening another SQLite

This commit is contained in:
2026-07-06 09:50:23 +02:00
parent 06183bd5d4
commit 7b0a1ac832
5 changed files with 109 additions and 90 deletions
+30 -11
View File
@@ -9,6 +9,7 @@ import (
"io"
"math"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"sort"
@@ -620,17 +621,15 @@ func (a *App) startup(ctx context.Context) {
// Windows reinstall. It lives OUTSIDE the DB since we must know the path
// before opening it.
if custom := readDBPointer(dataDir); custom != "" {
// Portability guard: a pointer that is merely ANOTHER folder's default DB
// location ("…/<other>/data/opslog.db") means the portable folder was
// renamed or copied — its config.json still points at the original. Ignore
// it and use THIS folder's own data (and clear the stale pointer so it
// stops happening). A genuine custom location — another drive, a different
// filename — is NOT default-style, so it's still honoured.
stale := strings.EqualFold(filepath.Base(custom), "opslog.db") &&
strings.EqualFold(filepath.Base(filepath.Dir(custom)), "data") &&
!strings.EqualFold(filepath.Clean(filepath.Dir(custom)), filepath.Clean(dataDir))
if stale {
fmt.Printf("OpsLog: ignoring stale DB pointer %q (folder moved) — using %s\n", custom, a.dbPath)
// Honour any chosen database that still exists on disk. Only fall back to
// this folder's own default when the pointed-to file is genuinely GONE —
// the portable folder was copied to a machine without the original drive,
// or the path was deleted — and clear the now-dangling pointer so it stops
// trying. (The old heuristic guessed "folder moved" from the file name and
// wrongly discarded valid logbooks named …/data/opslog.db, sending the user
// back to the default every launch — the save appeared not to stick.)
if _, err := os.Stat(custom); err != nil {
fmt.Printf("OpsLog: chosen database %q not found (%v) — using %s\n", custom, err, a.dbPath)
_ = writeDBPointer(dataDir, "")
} else {
a.dbPath = custom
@@ -1527,6 +1526,26 @@ func (a *App) QuitApp() {
}
}
// RestartApp relaunches OpsLog and closes this instance, so a database change
// (the DB pointer is read once at startup) applies without the user manually
// reopening the app. The new process opens the SQLite files while this one is
// closing; SQLite's busy_timeout(5000) covers that brief overlap.
func (a *App) RestartApp() error {
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("locate executable: %w", err)
}
cmd := exec.Command(exe)
cmd.Dir = filepath.Dir(exe)
if err := cmd.Start(); err != nil {
return fmt.Errorf("relaunch OpsLog: %w", err)
}
if a.ctx != nil {
wruntime.Quit(a.ctx)
}
return nil
}
// reloadLookupProviders rebuilds the lookup chain from current settings.
// Called at startup and after the user saves new credentials.
//