Skip to content
Snippets Groups Projects
Commit 880a2c5a authored by ale's avatar ale
Browse files

Add more default SQLite options

Set _fk, _cache_size, and _auto_vacuum to better default values.
parent 43960816
No related branches found
No related tags found
No related merge requests found
Pipeline #77287 passed
......@@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"log"
"net/url"
"strings"
_ "github.com/mattn/go-sqlite3"
......@@ -13,11 +14,24 @@ import (
// DebugMigrations can be set to true to dump statements to stderr.
var DebugMigrations bool
const defaultOptions = "?cache=shared&_busy_timeout=10000&_journal=WAL&_sync=OFF"
// See https://github.com/mattn/go-sqlite3/issues/209 for details on
// why these default parameters were chosen. WAL mode is mandatory for
// external litestream support.
func defaultOptions() url.Values {
v := make(url.Values)
v.Set("cache", "shared")
v.Set("_journal", "WAL")
v.Set("_sync", "OFF")
v.Set("_busy_timeout", "999999")
v.Set("_fk", "true")
v.Set("_cache_size", "268435456")
v.Set("_auto_vacuum", "incremental")
return v
}
type sqlOptions struct {
migrations []func(*sql.Tx) error
sqlopts string
sqlopts url.Values
}
type Option func(*sqlOptions)
......@@ -28,16 +42,16 @@ func WithMigrations(migrations []func(*sql.Tx) error) Option {
}
}
func WithSqliteOptions(sqlopts string) Option {
func WithSqliteOption(opt, value string) Option {
return func(opts *sqlOptions) {
opts.sqlopts = sqlopts
opts.sqlopts.Set(opt, value)
}
}
// OpenDB opens a SQLite database and runs the database migrations.
func OpenDB(dburi string, options ...Option) (*sql.DB, error) {
var opts sqlOptions
opts.sqlopts = defaultOptions
opts.sqlopts = defaultOptions()
for _, o := range options {
o(&opts)
}
......@@ -45,7 +59,7 @@ func OpenDB(dburi string, options ...Option) (*sql.DB, error) {
// Add sqlite3-specific parameters if none are already
// specified in the connection URI.
if !strings.Contains(dburi, "?") {
dburi += opts.sqlopts
dburi = fmt.Sprintf("%s?%s", dburi, opts.sqlopts.Encode())
}
db, err := sql.Open("sqlite3", dburi)
......@@ -56,6 +70,7 @@ func OpenDB(dburi string, options ...Option) (*sql.DB, error) {
// Limit the pool to a single connection.
// https://github.com/mattn/go-sqlite3/issues/209
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
if err = migrate(db, opts.migrations); err != nil {
db.Close() // nolint
......
......@@ -3,7 +3,6 @@ package sqlutil
import (
"context"
"database/sql"
"io/ioutil"
"os"
"testing"
)
......@@ -13,7 +12,7 @@ func init() {
}
func TestOpenDB(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......@@ -44,7 +43,7 @@ func checkTestValue(t *testing.T, db *sql.DB) {
}
func TestOpenDB_Migrations_MultipleStatements(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......@@ -64,7 +63,7 @@ func TestOpenDB_Migrations_MultipleStatements(t *testing.T) {
}
func TestOpenDB_Migrations_SingleStatement(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......@@ -86,7 +85,7 @@ func TestOpenDB_Migrations_SingleStatement(t *testing.T) {
}
func TestOpenDB_Migrations_Versions(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......@@ -114,7 +113,7 @@ func TestOpenDB_Migrations_Versions(t *testing.T) {
}
func TestOpenDB_Write(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......@@ -143,7 +142,7 @@ func TestOpenDB_Write(t *testing.T) {
}
func TestOpenDB_Migrations_Legacy(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment