From ffc499cff42203e07088c6d81102b6d866dadef0 Mon Sep 17 00:00:00 2001 From: Daniel Simmons Date: Mon, 21 Jul 2025 15:40:19 +0200 Subject: [PATCH] fix: Race condition with tests and sqlite init Should fix the error: ``` panic: SQL logic error: table `entities` already exists (1) [recovered] panic: SQL logic error: table `entities` already exists (1) ``` By using in-memory sqlite databases for tests instead of sharing one on the filesystem. --- config.go | 1 + connection.go | 7 ++++++- testutil/testutil.go | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 99bc646..7def826 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ const configFilename = "hal.yaml" type Config struct { HomeAssistant HomeAssistantConfig `yaml:"homeAssistant"` Location LocationConfig `yaml:"location"` + DatabasePath string `yaml:"databasePath"` } type HomeAssistantConfig struct { diff --git a/connection.go b/connection.go index 55c95d6..ae23c96 100644 --- a/connection.go +++ b/connection.go @@ -40,7 +40,12 @@ type ConnectionBinder interface { } func NewConnection(cfg Config) *Connection { - db, err := store.Open("sqlite.db") + dbPath := cfg.DatabasePath + if dbPath == "" { + dbPath = "sqlite.db" + } + + db, err := store.Open(dbPath) if err != nil { panic(err) } diff --git a/testutil/testutil.go b/testutil/testutil.go index 4ce5c38..555517c 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -32,6 +32,7 @@ func NewClientServer(t *testing.T) (*hal.Connection, *hassws.Server, func()) { Token: "test-token", UserID: TestUserID, }, + DatabasePath: ":memory:", }) // Create test entity and register it