From 52f87580a0f4bc2adf847bded534355d23d2cd4a Mon Sep 17 00:00:00 2001 From: mikestefanello <552328+mikestefanello@users.noreply.github.com> Date: Tue, 22 Apr 2025 09:09:56 -0400 Subject: [PATCH] Use memdb for in-memory sqlite dbs. --- config/config.yaml | 5 ++++- pkg/services/container.go | 15 ++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index b5a57f7..f87c556 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -32,7 +32,10 @@ cache: database: driver: "sqlite3" connection: "dbs/main.db?_journal=WAL&_timeout=5000&_fk=true" - testConnection: ":memory:?_journal=WAL&_timeout=5000&_fk=true" + # $RAND will be automatically replaced with a random value. + # memdb is more robust for an in-memory database rather than :memory: because the latter has the potential + # retain data even after you close and re-open the connection. + testConnection: "file:/$RAND?vfs=memdb&_timeout=1000&_fk=true" files: directory: "uploads" diff --git a/pkg/services/container.go b/pkg/services/container.go index 625acb8..0c0c631 100644 --- a/pkg/services/container.go +++ b/pkg/services/container.go @@ -5,6 +5,7 @@ import ( "database/sql" "fmt" "log/slog" + "math/rand" "os" "path" "path/filepath" @@ -241,18 +242,22 @@ func (c *Container) initTasks() { // openDB opens a database connection. func openDB(driver, connection string) (*sql.DB, error) { - // Helper to automatically create the directories that the specified sqlite file - // should reside in, if one. if driver == "sqlite3" { + // Helper to automatically create the directories that the specified sqlite file + // should reside in, if one. d := strings.Split(connection, "/") - if len(d) > 1 { - path := strings.Join(d[:len(d)-1], "/") + dirpath := strings.Join(d[:len(d)-1], "/") - if err := os.MkdirAll(path, 0755); err != nil { + if err := os.MkdirAll(dirpath, 0755); err != nil { return nil, err } } + + // Check if a random value is required, which is often used for in-memory test databases. + if strings.Contains(connection, "$RAND") { + connection = strings.Replace(connection, "$RAND", fmt.Sprint(rand.Int()), 1) + } } return sql.Open(driver, connection)