mirror of
https://github.com/docker/docker-agent.git
synced 2026-11-04 18:30:09 +00:00
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
This commit is contained in:
3 files changed
+10
-5
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user