From 2c48c75387804dea4bfdaf08257dcbb90cccb173 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 2 Apr 2026 14:26:13 +0200 Subject: [PATCH] fix: use FindDataRoot in FindOrInitSQLiteStore for consistent database location --- src/store/graph_store_sqlite.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/store/graph_store_sqlite.go b/src/store/graph_store_sqlite.go index fbbfae0..0c31515 100644 --- a/src/store/graph_store_sqlite.go +++ b/src/store/graph_store_sqlite.go @@ -74,31 +74,20 @@ func FindAndOpenSQLiteStore() (GraphStore, error) { } // FindOrInitSQLiteStore is like FindAndOpenSQLiteStore but intended for server -// mode: if no .ax.db is found it creates and initialises one in the current -// working directory instead of returning an error. +// mode: if no database is found it creates and initialises one in the +// ~/.local/share/ax/ directory instead of returning an error. func FindOrInitSQLiteStore() (GraphStore, error) { - dir, err := filepath.Abs(".") + dataRoot, err := FindDataRoot(".local", "share") if err != nil { - return nil, err + return nil, fmt.Errorf("failed to find data dir: %w", err) } - for { - dbpath := filepath.Join(dir, ".ax.db") - if _, err := os.Stat(dbpath); err == nil { - return NewSQLiteStore(dbpath) - } - if parent := filepath.Dir(dir); parent == dir { - break - } else { - dir = parent + dbPath := filepath.Join(dataRoot, "ax.db") + if _, err := os.Stat(dbPath); err != nil { + if err := InitSQLiteStore(dbPath); err != nil { + return nil, err } } - // Not found — create and initialise in CWD. - cwd, _ := filepath.Abs(".") - dbpath := filepath.Join(cwd, ".ax.db") - if err := InitSQLiteStore(dbpath); err != nil { - return nil, err - } - return NewSQLiteStore(dbpath) + return NewSQLiteStore(dbPath) } // NewSQLiteStore opens a SQLite database at the given path, runs a one-time