Merge pull request #120 from ClusterCockpit/optimize-sqlite-db-setup

Enable SQL hooks for statement debugging
This commit is contained in:
Jan Eitzinger 2023-06-05 16:41:52 +02:00 committed by GitHub
commit 797d5da80b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,11 +5,14 @@
package repository package repository
import ( import (
"database/sql"
"log" "log"
"sync" "sync"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
"github.com/qustavo/sqlhooks/v2"
) )
var ( var (
@ -45,15 +48,15 @@ func Connect(driver string, db string) {
switch driver { switch driver {
case "sqlite3": case "sqlite3":
// sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{})) sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{}))
// - Set WAL mode (not strictly necessary each time because it's persisted in the database, but good for first run) // - Set WAL mode (not strictly necessary each time because it's persisted in the database, but good for first run)
// - Set busy timeout, so concurrent writers wait on each other instead of erroring immediately // - Set busy timeout, so concurrent writers wait on each other instead of erroring immediately
// - Enable foreign key checks // - Enable foreign key checks
opts.URL += "?_journal=WAL&_timeout=5000&_fk=true" opts.URL += "?_journal=WAL&_timeout=5000&_fk=true"
// dbHandle, err = sqlx.Open("sqlite3WithHooks", fmt.Sprintf("%s?_foreign_keys=on", db)) dbHandle, err = sqlx.Open("sqlite3WithHooks", opts.URL)
dbHandle, err = sqlx.Open("sqlite3", opts.URL) // dbHandle, err = sqlx.Open("sqlite3", opts.URL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }