Use memdb for in-memory sqlite dbs.

This commit is contained in:
mikestefanello 2025-04-22 09:09:56 -04:00
parent a53bdf9a1b
commit 52f87580a0
2 changed files with 14 additions and 6 deletions

View file

@ -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)