2022-07-07 05:19:05 +08:00
|
|
|
//go:build !cgo
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-02 02:29:07 +08:00
|
|
|
"time"
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
"github.com/pocketbase/dbx"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func connectDB(dbPath string) (*dbx.DB, error) {
|
2022-11-02 02:29:07 +08:00
|
|
|
// 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(100000)&_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)&_pragma=synchronous(NORMAL)&_pragma=journal_size_limit(100000000)"
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2022-11-02 02:29:07 +08:00
|
|
|
db, err := dbx.MustOpen("sqlite", fmt.Sprintf("%s?%s", dbPath, pragmas))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a fixed connection pool to limit the SQLITE_BUSY errors
|
|
|
|
//
|
|
|
|
// @see https://gitlab.com/cznic/sqlite/-/issues/115
|
|
|
|
db.DB().SetMaxOpenConns(200)
|
|
|
|
db.DB().SetMaxIdleConns(200)
|
|
|
|
db.DB().SetConnMaxIdleTime(5 * time.Minute)
|
|
|
|
|
|
|
|
return db, nil
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|