@@ -4,14 +4,13 @@ import (
4
4
"context"
5
5
"database/sql"
6
6
_ "embed"
7
+ "fmt"
8
+ "strings"
7
9
)
8
10
9
11
//go:embed schema.sql
10
12
var initscript string
11
13
12
- // global, hardcoded sqlite config
13
- var sqliteConfig string = "?_foreign_keys=true"
14
-
15
14
// Temporary solution. We don't actually have migrations right now.
16
15
// We just execute the content of schema.sql every time
17
16
func ApplyMigrations (db * sql.DB , ctx context.Context ) error {
@@ -26,6 +25,8 @@ func ApplyMigrations(db *sql.DB, ctx context.Context) error {
26
25
// TODO: runtime pragmas, as seen here
27
26
// https://github.com/mtlynch/picoshare/blob/master/store/sqlite/sqlite.go
28
27
func NewTestConn (sqlitePath string , ctx context.Context ) (* sql.DB , error ) {
28
+ // global, hardcoded sqlite config
29
+ var sqliteConfig string = "?_foreign_keys=true"
29
30
db , err := sql .Open ("sqlite3" , sqlitePath + sqliteConfig )
30
31
if err != nil {
31
32
return nil , err
@@ -35,58 +36,35 @@ func NewTestConn(sqlitePath string, ctx context.Context) (*sql.DB, error) {
35
36
}
36
37
37
38
// Return a readonly connection to the sqlite database
38
- // TODO: proper config
39
39
// TODO: ping()
40
40
func NewReadPool (sqlitePath string , ctx context.Context ) (* sql.DB , error ) {
41
- return sql .Open ("sqlite3" , sqlitePath + sqliteConfig )
41
+ // https://github.com/mattn/go-sqlite3/issues/1022#issuecomment-1067353980
42
+ // https://github.com/mattn/go-sqlite3?tab=readme-ov-file#connection-string
43
+ options := []string {
44
+ "_foreign_keys=true" ,
45
+ "_journal_mode=wal" ,
46
+ "_busy_timeout=5000" ,
47
+ "mode=ro" ,
48
+ }
49
+ connString := fmt .Sprintf ("file:%s?%s" , sqlitePath , strings .Join (options , "&" ))
50
+ return sql .Open ("sqlite3" , connString )
42
51
}
43
52
44
53
// Return a read+write connection to the sqlite database
45
- // TODO: proper config
46
54
func NewWritePool (sqlitePath string , ctx context.Context ) (conn * sql.DB , err error ) {
47
- conn , err = sql .Open ("sqlite3" , sqlitePath + sqliteConfig )
55
+ // https://github.com/mattn/go-sqlite3/issues/1022#issuecomment-1067353980
56
+ // https://github.com/mattn/go-sqlite3?tab=readme-ov-file#connection-string
57
+ options := []string {
58
+ "_foreign_keys=true" ,
59
+ "_journal_mode=wal" ,
60
+ "_txlock=immediate" ,
61
+ "_busy_timeout=5000" ,
62
+ "mode=rwc" ,
63
+ }
64
+ connString := fmt .Sprintf ("file:%s?%s" , sqlitePath , strings .Join (options , "&" ))
65
+ conn , err = sql .Open ("sqlite3" , connString )
48
66
//sqlite does not support cuncurrent write
49
- //TODO: test performance implications
50
- // conn.SetMaxOpenConns(1)
67
+ conn .SetMaxOpenConns (1 )
51
68
return
52
69
}
53
70
54
- // func SQLite(t *testing.T, migrations []string) (*sql.DB, func()) {
55
- // t.Helper()
56
- // // For each test, pick a new database name at random.
57
- // source, err := os.CreateTemp("", "sqltest_sqlite_")
58
- // if err != nil {
59
- // t.Fatal(err)
60
- // }
61
- // return CreateSQLiteDatabase(t, source.Name(), migrations)
62
- // }
63
-
64
- // func CreateSQLiteDatabase(t *testing.T, path string, migrations []string) (*sql.DB, func()) {
65
- // t.Helper()
66
-
67
- // t.Logf("open %s\n", path)
68
- // sdb, err := sql.Open("sqlite", path)
69
- // if err != nil {
70
- // t.Fatal(err)
71
- // }
72
-
73
- // files, err := sqlpath.Glob(migrations)
74
- // if err != nil {
75
- // t.Fatal(err)
76
- // }
77
- // for _, f := range files {
78
- // blob, err := os.ReadFile(f)
79
- // if err != nil {
80
- // t.Fatal(err)
81
- // }
82
- // if _, err := sdb.Exec(string(blob)); err != nil {
83
- // t.Fatalf("%s: %s", filepath.Base(f), err)
84
- // }
85
- // }
86
-
87
- // return sdb, func() {
88
- // if _, err := os.Stat(path); err == nil {
89
- // os.Remove(path)
90
- // }
91
- // }
92
- // }
0 commit comments