[#2570] fixed default PRAGMAs not being applied for new connections

This commit is contained in:
Gani Georgiev
2023-05-27 09:04:01 +03:00
parent 833a84b3d7
commit f6a616b7e8
36 changed files with 99 additions and 73 deletions
-20
View File
@@ -1,20 +0,0 @@
package core
import (
"github.com/pocketbase/dbx"
)
func initPragmas(db *dbx.DB) error {
// note: the busy_timeout pragma must be first because
// the connection needs to be set to block on busy before WAL mode
// is set in case it hasn't been already set by another connection
_, err := db.NewQuery(`
PRAGMA busy_timeout = 10000;
PRAGMA journal_mode = WAL;
PRAGMA journal_size_limit = 200000000;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = ON;
`).Execute()
return err
}
+32 -8
View File
@@ -3,18 +3,42 @@
package core
import (
"database/sql"
"github.com/mattn/go-sqlite3"
"github.com/pocketbase/dbx"
_ "github.com/mattn/go-sqlite3"
)
func connectDB(dbPath string) (*dbx.DB, error) {
db, err := dbx.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
func init() {
// Registers the sqlite3 driver with a ConnectHook so that we can
// initialize the default PRAGMAs.
//
// Note 1: we don't define the PRAGMA as part of the dsn string
// because not all pragmas are available.
//
// Note 2: the busy_timeout pragma must be first because
// the connection needs to be set to block on busy before WAL mode
// is set in case it hasn't been already set by another connection.
sql.Register("sqlite3_with_connect_hook",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
conn.Exec(`
PRAGMA busy_timeout = 10000;
PRAGMA journal_mode = WAL;
PRAGMA journal_size_limit = 200000000;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = ON;
`, nil)
if err := initPragmas(db); err != nil {
db.Close()
return nil
},
},
)
}
func connectDB(dbPath string) (*dbx.DB, error) {
db, err := dbx.Open("sqlite3_with_connect_hook", dbPath)
if err != nil {
return nil, err
}
+6 -6
View File
@@ -8,13 +8,13 @@ import (
)
func connectDB(dbPath string) (*dbx.DB, error) {
db, err := dbx.Open("sqlite", dbPath)
if err != nil {
return nil, err
}
// Note: the busy_timeout pragma must be first because
// the connection needs to be set to block on busy before WAL mode
// is set in case it hasn't been already set by another connection.
pragmas := "?_pragma=busy_timeout(1000)&_pragma=journal_mode(WAL)&_pragma=journal_size_limit(200000000)&_pragma=synchronous(NORMAL)&_pragma=foreign_keys(ON)"
if err := initPragmas(db); err != nil {
db.Close()
db, err := dbx.Open("sqlite", dbPath+pragmas)
if err != nil {
return nil, err
}