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:
|
database:
|
||||||
driver: "sqlite3"
|
driver: "sqlite3"
|
||||||
connection: "dbs/main.db?_journal=WAL&_timeout=5000&_fk=true"
|
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:
|
files:
|
||||||
directory: "uploads"
|
directory: "uploads"
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -241,18 +242,22 @@ func (c *Container) initTasks() {
|
||||||
|
|
||||||
// openDB opens a database connection.
|
// openDB opens a database connection.
|
||||||
func openDB(driver, connection string) (*sql.DB, error) {
|
func openDB(driver, connection string) (*sql.DB, error) {
|
||||||
|
if driver == "sqlite3" {
|
||||||
// Helper to automatically create the directories that the specified sqlite file
|
// Helper to automatically create the directories that the specified sqlite file
|
||||||
// should reside in, if one.
|
// should reside in, if one.
|
||||||
if driver == "sqlite3" {
|
|
||||||
d := strings.Split(connection, "/")
|
d := strings.Split(connection, "/")
|
||||||
|
|
||||||
if len(d) > 1 {
|
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
|
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)
|
return sql.Open(driver, connection)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue