package main import ( "context" "database/sql" "database/sql/driver" moderncsqlite "modernc.org/sqlite" ) func init() { for _, name := range sql.Drivers() { if name == "sqlite3" { return } } d := &moderncsqlite.Driver{} d.RegisterConnectionHook(sqlitePragmaHook) sql.Register("sqlite3", d) } // sqlitePragmaHook sets WAL journal mode and a 5-second busy timeout on // every new SQLite connection. This prevents SQLITE_BUSY errors when // multiple goroutines write concurrently (e.g. mautrix crypto sync + // memory/knowledge stores). func sqlitePragmaHook(conn moderncsqlite.ExecQuerierContext, _ string) error { ctx := context.Background() pragmas := []string{ "PRAGMA journal_mode=WAL", "PRAGMA busy_timeout=5000", } for _, p := range pragmas { if _, err := conn.ExecContext(ctx, p, []driver.NamedValue{}); err != nil { return err } } return nil }