Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
880a2c5a
Commit
880a2c5a
authored
4 months ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add more default SQLite options
Set _fk, _cache_size, and _auto_vacuum to better default values.
parent
43960816
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#77287
passed
4 months ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sqlutil/db.go
+21
-6
21 additions, 6 deletions
sqlutil/db.go
sqlutil/db_test.go
+6
-7
6 additions, 7 deletions
sqlutil/db_test.go
with
27 additions
and
13 deletions
sqlutil/db.go
+
21
−
6
View file @
880a2c5a
...
...
@@ -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
WithSqliteOption
s
(
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
...
...
This diff is collapsed.
Click to expand it.
sqlutil/db_test.go
+
6
−
7
View file @
880a2c5a
...
...
@@ -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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
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
.
Temp
Dir
(
""
,
""
)
dir
,
err
:=
os
.
Mkdir
Temp
(
""
,
""
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment