From d10f6893139ff34fedc8d3f92c7736e11aaa2b5d Mon Sep 17 00:00:00 2001 From: Johannes Rauh Date: Tue, 10 Dec 2024 13:30:36 +0100 Subject: [PATCH] Allow multiple instances in memory --- database/db.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/database/db.go b/database/db.go index 9ba102f..05ebfd9 100644 --- a/database/db.go +++ b/database/db.go @@ -219,9 +219,19 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry } db = sqlx.NewDb(sql.OpenDB(NewConnector(connector, logger, connectorCallbacks)), PostgreSQL) case "sqlite": - addr = c.Database + var ( + name = c.Database + mode = "" + ) + + if strings.Contains(c.Database, ":memory:") && c.Database != ":memory:" { + name = strings.Split(c.Database, ":memory:")[1] + mode = "mode=memory&" + } + + addr = fmt.Sprintf("file:%s?%scache=shared", name, mode) - liteDb, err := sql.Open(SQLite, fmt.Sprintf("file:%s?cache=shared", c.Database)) + liteDb, err := sql.Open(SQLite, addr) if err != nil { return nil, errors.Wrap(err, "can't open sqlite database") }