Use memdb for in-memory sqlite dbs.
This commit is contained in:
parent
a53bdf9a1b
commit
52f87580a0
2 changed files with 14 additions and 6 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
if driver == "sqlite3" {
|
||||
// Helper to automatically create the directories that the specified sqlite file
|
||||
// should reside in, if one.
|
||||
if driver == "sqlite3" {
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue