From c37dfa34d605e43df01e00a006486ebbd5eb843f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 5 Jan 2026 07:48:28 +0100 Subject: [PATCH] fix(sqlite): verify connection in OpenDB Adds a Ping() call to sqliteutil.OpenDB to ensure the database connection is valid and the file can be opened. This ensures that file permission or missing directory errors are caught and diagnosed immediately. Assisted-By: cagent --- pkg/memory/database/sqlite/sqlite.go | 2 +- pkg/session/store.go | 4 ---- pkg/sqliteutil/sqlite.go | 9 +++++++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/memory/database/sqlite/sqlite.go b/pkg/memory/database/sqlite/sqlite.go index f0295a99c..515d0df56 100644 --- a/pkg/memory/database/sqlite/sqlite.go +++ b/pkg/memory/database/sqlite/sqlite.go @@ -19,7 +19,7 @@ func NewMemoryDatabase(path string) (database.Database, error) { } // Ensure we close the connection if table creation fails // Note: We don't defer close here because we return the db on success - + _, err = db.ExecContext(context.Background(), "CREATE TABLE IF NOT EXISTS memories (id TEXT PRIMARY KEY, created_at TEXT, memory TEXT)") if err != nil { db.Close() diff --git a/pkg/session/store.go b/pkg/session/store.go index 4f1770830..3ceb5f902 100644 --- a/pkg/session/store.go +++ b/pkg/session/store.go @@ -8,8 +8,6 @@ import ( "strconv" "time" - - "github.com/docker/cagent/pkg/concurrent" "github.com/docker/cagent/pkg/sqliteutil" ) @@ -28,8 +26,6 @@ func convertMessagesToItems(messages []Message) []Item { return items } - - // Store defines the interface for session storage type Store interface { AddSession(ctx context.Context, session *Session) error diff --git a/pkg/sqliteutil/sqlite.go b/pkg/sqliteutil/sqlite.go index b463d2e68..862dfbed9 100644 --- a/pkg/sqliteutil/sqlite.go +++ b/pkg/sqliteutil/sqlite.go @@ -34,6 +34,15 @@ func OpenDB(path string) (*sql.DB, error) { db.SetMaxIdleConns(1) db.SetConnMaxLifetime(0) + // Verify connection works (this will trigger file creation/open) + if err := db.Ping(); err != nil { + db.Close() + if IsCantOpenError(err) { + return nil, DiagnoseDBOpenError(path, err) + } + return nil, err + } + return db, nil }