Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

*/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>

authored by

Winter and committed by
Tangled
ea126347 4e27d297

+59 -40
+31 -22
appview/db/db.go
··· 27 27 } 28 28 29 29 func Make(dbPath string) (*DB, error) { 30 - db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1") 30 + // https://github.com/mattn/go-sqlite3#connection-string 31 + opts := []string{ 32 + "_foreign_keys=1", 33 + "_journal_mode=WAL", 34 + "_synchronous=NORMAL", 35 + "_auto_vacuum=incremental", 36 + } 37 + 38 + db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&")) 31 39 if err != nil { 32 40 return nil, err 33 41 } 34 - _, err = db.Exec(` 35 - pragma journal_mode = WAL; 36 - pragma synchronous = normal; 37 - pragma temp_store = memory; 38 - pragma mmap_size = 30000000000; 39 - pragma page_size = 32768; 40 - pragma auto_vacuum = incremental; 41 - pragma busy_timeout = 5000; 42 42 43 + ctx := context.Background() 44 + 45 + conn, err := db.Conn(ctx) 46 + if err != nil { 47 + return nil, err 48 + } 49 + defer conn.Close() 50 + 51 + _, err = conn.ExecContext(ctx, ` 43 52 create table if not exists registrations ( 44 53 id integer primary key autoincrement, 45 54 domain text not null unique, ··· 476 467 } 477 468 478 469 // run migrations 479 - runMigration(db, "add-description-to-repos", func(tx *sql.Tx) error { 470 + runMigration(conn, "add-description-to-repos", func(tx *sql.Tx) error { 480 471 tx.Exec(` 481 472 alter table repos add column description text check (length(description) <= 200); 482 473 `) 483 474 return nil 484 475 }) 485 476 486 - runMigration(db, "add-rkey-to-pubkeys", func(tx *sql.Tx) error { 477 + runMigration(conn, "add-rkey-to-pubkeys", func(tx *sql.Tx) error { 487 478 // add unconstrained column 488 479 _, err := tx.Exec(` 489 480 alter table public_keys ··· 506 497 return nil 507 498 }) 508 499 509 - runMigration(db, "add-rkey-to-comments", func(tx *sql.Tx) error { 500 + runMigration(conn, "add-rkey-to-comments", func(tx *sql.Tx) error { 510 501 _, err := tx.Exec(` 511 502 alter table comments drop column comment_at; 512 503 alter table comments add column rkey text; ··· 514 505 return err 515 506 }) 516 507 517 - runMigration(db, "add-deleted-and-edited-to-issue-comments", func(tx *sql.Tx) error { 508 + runMigration(conn, "add-deleted-and-edited-to-issue-comments", func(tx *sql.Tx) error { 518 509 _, err := tx.Exec(` 519 510 alter table comments add column deleted text; -- timestamp 520 511 alter table comments add column edited text; -- timestamp ··· 522 513 return err 523 514 }) 524 515 525 - runMigration(db, "add-source-info-to-pulls-and-submissions", func(tx *sql.Tx) error { 516 + runMigration(conn, "add-source-info-to-pulls-and-submissions", func(tx *sql.Tx) error { 526 517 _, err := tx.Exec(` 527 518 alter table pulls add column source_branch text; 528 519 alter table pulls add column source_repo_at text; ··· 531 522 return err 532 523 }) 533 524 534 - runMigration(db, "add-source-to-repos", func(tx *sql.Tx) error { 525 + runMigration(conn, "add-source-to-repos", func(tx *sql.Tx) error { 535 526 _, err := tx.Exec(` 536 527 alter table repos add column source text; 537 528 `) ··· 542 533 // NOTE: this cannot be done in a transaction, so it is run outside [0] 543 534 // 544 535 // [0]: https://sqlite.org/pragma.html#pragma_foreign_keys 545 - db.Exec("pragma foreign_keys = off;") 546 - runMigration(db, "recreate-pulls-column-for-stacking-support", func(tx *sql.Tx) error { 536 + conn.ExecContext(ctx, "pragma foreign_keys = off;") 537 + runMigration(conn, "recreate-pulls-column-for-stacking-support", func(tx *sql.Tx) error { 547 538 _, err := tx.Exec(` 548 539 create table pulls_new ( 549 540 -- identifiers ··· 598 589 `) 599 590 return err 600 591 }) 601 - db.Exec("pragma foreign_keys = on;") 592 + conn.ExecContext(ctx, "pragma foreign_keys = on;") 602 593 603 594 // run migrations 604 - runMigration(db, "add-spindle-to-repos", func(tx *sql.Tx) error { 595 + runMigration(conn, "add-spindle-to-repos", func(tx *sql.Tx) error { 605 596 tx.Exec(` 606 597 alter table repos add column spindle text; 607 598 `) ··· 609 600 }) 610 601 611 602 // recreate and add rkey + created columns with default constraint 612 - runMigration(db, "rework-collaborators-table", func(tx *sql.Tx) error { 603 + runMigration(conn, "rework-collaborators-table", func(tx *sql.Tx) error { 613 604 // create new table 614 605 // - repo_at instead of repo integer 615 606 // - rkey field ··· 668 659 669 660 type migrationFn = func(*sql.Tx) error 670 661 671 - func runMigration(d *sql.DB, name string, migrationFn migrationFn) error { 672 - tx, err := d.Begin() 662 + func runMigration(c *sql.Conn, name string, migrationFn migrationFn) error { 663 + tx, err := c.BeginTx(context.Background(), nil) 673 664 if err != nil { 674 665 return err 675 666 }
+14 -9
knotserver/db/init.go
··· 2 2 3 3 import ( 4 4 "database/sql" 5 + "strings" 5 6 6 7 _ "github.com/mattn/go-sqlite3" 7 8 ) ··· 12 11 } 13 12 14 13 func Setup(dbPath string) (*DB, error) { 15 - db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1") 14 + // https://github.com/mattn/go-sqlite3#connection-string 15 + opts := []string{ 16 + "_foreign_keys=1", 17 + "_journal_mode=WAL", 18 + "_synchronous=NORMAL", 19 + "_auto_vacuum=incremental", 20 + } 21 + 22 + db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&")) 16 23 if err != nil { 17 24 return nil, err 18 25 } 19 26 20 - _, err = db.Exec(` 21 - pragma journal_mode = WAL; 22 - pragma synchronous = normal; 23 - pragma temp_store = memory; 24 - pragma mmap_size = 30000000000; 25 - pragma page_size = 32768; 26 - pragma auto_vacuum = incremental; 27 - pragma busy_timeout = 5000; 27 + // NOTE: If any other migration is added here, you MUST 28 + // copy the pattern in appview: use a single sql.Conn 29 + // for every migration. 28 30 31 + _, err = db.Exec(` 29 32 create table if not exists known_dids ( 30 33 did text primary key 31 34 );
+14 -9
spindle/db/db.go
··· 2 2 3 3 import ( 4 4 "database/sql" 5 + "strings" 5 6 6 7 _ "github.com/mattn/go-sqlite3" 7 8 ) ··· 12 11 } 13 12 14 13 func Make(dbPath string) (*DB, error) { 15 - db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=1") 14 + // https://github.com/mattn/go-sqlite3#connection-string 15 + opts := []string{ 16 + "_foreign_keys=1", 17 + "_journal_mode=WAL", 18 + "_synchronous=NORMAL", 19 + "_auto_vacuum=incremental", 20 + } 21 + 22 + db, err := sql.Open("sqlite3", dbPath+"?"+strings.Join(opts, "&")) 16 23 if err != nil { 17 24 return nil, err 18 25 } 19 26 20 - _, err = db.Exec(` 21 - pragma journal_mode = WAL; 22 - pragma synchronous = normal; 23 - pragma temp_store = memory; 24 - pragma mmap_size = 30000000000; 25 - pragma page_size = 32768; 26 - pragma auto_vacuum = incremental; 27 - pragma busy_timeout = 5000; 27 + // NOTE: If any other migration is added here, you MUST 28 + // copy the pattern in appview: use a single sql.Conn 29 + // for every migration. 28 30 31 + _, err = db.Exec(` 29 32 create table if not exists _jetstream ( 30 33 id integer primary key autoincrement, 31 34 last_time_us integer not null