forked from tangled.org/core
this repo has no description

*/db: set sqlite options across all connections in the pool

This does result in the removal of some options that aren't supported by
the SQLite driver [0], but IMO we can stick with the defaults for these.

[0]: https://github.com/mattn/go-sqlite3#connection-string

Signed-off-by: Winter <winter@winter.cafe>

Changed files
+59 -40
appview
db
knotserver
db
spindle
db
+31 -22
appview/db/db.go
···
}
func Make(dbPath string) (*DB, error) {
-
db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1")
+
// https://github.com/mattn/go-sqlite3#connection-string
+
opts := []string{
+
"_foreign_keys=1",
+
"_journal_mode=WAL",
+
"_synchronous=NORMAL",
+
"_auto_vacuum=incremental",
+
}
+
+
db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&"))
+
if err != nil {
+
return nil, err
+
}
+
+
ctx := context.Background()
+
+
conn, err := db.Conn(ctx)
if err != nil {
return nil, err
}
-
_, err = db.Exec(`
-
pragma journal_mode = WAL;
-
pragma synchronous = normal;
-
pragma temp_store = memory;
-
pragma mmap_size = 30000000000;
-
pragma page_size = 32768;
-
pragma auto_vacuum = incremental;
-
pragma busy_timeout = 5000;
+
defer conn.Close()
+
_, err = conn.ExecContext(ctx, `
create table if not exists registrations (
id integer primary key autoincrement,
domain text not null unique,
···
}
// run migrations
-
runMigration(db, "add-description-to-repos", func(tx *sql.Tx) error {
+
runMigration(conn, "add-description-to-repos", func(tx *sql.Tx) error {
tx.Exec(`
alter table repos add column description text check (length(description) <= 200);
`)
return nil
})
-
runMigration(db, "add-rkey-to-pubkeys", func(tx *sql.Tx) error {
+
runMigration(conn, "add-rkey-to-pubkeys", func(tx *sql.Tx) error {
// add unconstrained column
_, err := tx.Exec(`
alter table public_keys
···
return nil
})
-
runMigration(db, "add-rkey-to-comments", func(tx *sql.Tx) error {
+
runMigration(conn, "add-rkey-to-comments", func(tx *sql.Tx) error {
_, err := tx.Exec(`
alter table comments drop column comment_at;
alter table comments add column rkey text;
···
return err
})
-
runMigration(db, "add-deleted-and-edited-to-issue-comments", func(tx *sql.Tx) error {
+
runMigration(conn, "add-deleted-and-edited-to-issue-comments", func(tx *sql.Tx) error {
_, err := tx.Exec(`
alter table comments add column deleted text; -- timestamp
alter table comments add column edited text; -- timestamp
···
return err
})
-
runMigration(db, "add-source-info-to-pulls-and-submissions", func(tx *sql.Tx) error {
+
runMigration(conn, "add-source-info-to-pulls-and-submissions", func(tx *sql.Tx) error {
_, err := tx.Exec(`
alter table pulls add column source_branch text;
alter table pulls add column source_repo_at text;
···
return err
})
-
runMigration(db, "add-source-to-repos", func(tx *sql.Tx) error {
+
runMigration(conn, "add-source-to-repos", func(tx *sql.Tx) error {
_, err := tx.Exec(`
alter table repos add column source text;
`)
···
// NOTE: this cannot be done in a transaction, so it is run outside [0]
//
// [0]: https://sqlite.org/pragma.html#pragma_foreign_keys
-
db.Exec("pragma foreign_keys = off;")
-
runMigration(db, "recreate-pulls-column-for-stacking-support", func(tx *sql.Tx) error {
+
conn.ExecContext(ctx, "pragma foreign_keys = off;")
+
runMigration(conn, "recreate-pulls-column-for-stacking-support", func(tx *sql.Tx) error {
_, err := tx.Exec(`
create table pulls_new (
-- identifiers
···
`)
return err
})
-
db.Exec("pragma foreign_keys = on;")
+
conn.ExecContext(ctx, "pragma foreign_keys = on;")
// run migrations
-
runMigration(db, "add-spindle-to-repos", func(tx *sql.Tx) error {
+
runMigration(conn, "add-spindle-to-repos", func(tx *sql.Tx) error {
tx.Exec(`
alter table repos add column spindle text;
`)
···
})
// recreate and add rkey + created columns with default constraint
-
runMigration(db, "rework-collaborators-table", func(tx *sql.Tx) error {
+
runMigration(conn, "rework-collaborators-table", func(tx *sql.Tx) error {
// create new table
// - repo_at instead of repo integer
// - rkey field
···
type migrationFn = func(*sql.Tx) error
-
func runMigration(d *sql.DB, name string, migrationFn migrationFn) error {
-
tx, err := d.Begin()
+
func runMigration(c *sql.Conn, name string, migrationFn migrationFn) error {
+
tx, err := c.BeginTx(context.Background(), nil)
if err != nil {
return err
}
+14 -9
knotserver/db/init.go
···
import (
"database/sql"
+
"strings"
_ "github.com/mattn/go-sqlite3"
)
···
}
func Setup(dbPath string) (*DB, error) {
-
db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1")
+
// https://github.com/mattn/go-sqlite3#connection-string
+
opts := []string{
+
"_foreign_keys=1",
+
"_journal_mode=WAL",
+
"_synchronous=NORMAL",
+
"_auto_vacuum=incremental",
+
}
+
+
db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&"))
if err != nil {
return nil, err
}
+
// NOTE: If any other migration is added here, you MUST
+
// copy the pattern in appview: use a single sql.Conn
+
// for every migration.
+
_, err = db.Exec(`
-
pragma journal_mode = WAL;
-
pragma synchronous = normal;
-
pragma temp_store = memory;
-
pragma mmap_size = 30000000000;
-
pragma page_size = 32768;
-
pragma auto_vacuum = incremental;
-
pragma busy_timeout = 5000;
-
create table if not exists known_dids (
did text primary key
);
+14 -9
spindle/db/db.go
···
import (
"database/sql"
+
"strings"
_ "github.com/mattn/go-sqlite3"
)
···
}
func Make(dbPath string) (*DB, error) {
-
db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1")
+
// https://github.com/mattn/go-sqlite3#connection-string
+
opts := []string{
+
"_foreign_keys=1",
+
"_journal_mode=WAL",
+
"_synchronous=NORMAL",
+
"_auto_vacuum=incremental",
+
}
+
+
db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&"))
if err != nil {
return nil, err
}
+
// NOTE: If any other migration is added here, you MUST
+
// copy the pattern in appview: use a single sql.Conn
+
// for every migration.
+
_, err = db.Exec(`
-
pragma journal_mode = WAL;
-
pragma synchronous = normal;
-
pragma temp_store = memory;
-
pragma mmap_size = 30000000000;
-
pragma page_size = 32768;
-
pragma auto_vacuum = incremental;
-
pragma busy_timeout = 5000;
-
create table if not exists _jetstream (
id integer primary key autoincrement,
last_time_us integer not null