fix: use FindDataRoot in FindOrInitSQLiteStore for consistent database location

This commit is contained in:
2026-04-02 14:26:13 +02:00
parent e04a44cdcf
commit fed2e56e5f

View File

@@ -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