Skip to content

Commit 02df3c4

Browse files
authored
Only apply Litestream pragmas when Litestream is enabled (#342)
* Only apply Litestream pragmas when Litestream is enabled Litestream has a set of SQLite pragmas it recommends, but we should only apply them when we're actually using Litestream. Otherwise, we should stick with the SQLite defaults. * Use WAL mode regardless of Litestream
1 parent 68ad95a commit 02df3c4

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

cmd/picoshare/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232

3333
ensureDirExists(filepath.Dir(*dbPath))
3434

35-
store := sqlite.New(*dbPath)
35+
store := sqlite.New(*dbPath, isLitestreamEnabled())
3636

3737
collector := garbagecollect.NewCollector(store, *vacuumDb)
3838
gc := garbagecollect.NewScheduler(&collector, 7*time.Hour)
@@ -68,3 +68,7 @@ func ensureDirExists(dir string) {
6868
}
6969
}
7070
}
71+
72+
func isLitestreamEnabled() bool {
73+
return os.Getenv("LITESTREAM_BUCKET") != ""
74+
}

store/sqlite/sqlite.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,37 @@ type (
3131
}
3232
)
3333

34-
func New(path string) store.Store {
35-
return NewWithChunkSize(path, defaultChunkSize)
34+
func New(path string, optimizeForLitestream bool) store.Store {
35+
return NewWithChunkSize(path, defaultChunkSize, optimizeForLitestream)
3636
}
3737

3838
// NewWithChunkSize creates a SQLite-based datastore with the user-specified
3939
// chunk size for writing files. Most callers should just use New().
40-
func NewWithChunkSize(path string, chunkSize int) store.Store {
40+
func NewWithChunkSize(path string, chunkSize int, optimizeForLitestream bool) store.Store {
4141
log.Printf("reading DB from %s", path)
4242
ctx, err := sql.Open("sqlite3", path)
4343
if err != nil {
4444
log.Fatalln(err)
4545
}
4646

4747
if _, err := ctx.Exec(`
48-
PRAGMA temp_store = FILE;
49-
50-
-- Apply Litestream recommendations: https://litestream.io/tips/
51-
PRAGMA busy_timeout = 5000;
52-
PRAGMA synchronous = NORMAL;
53-
PRAGMA journal_mode = WAL;
54-
PRAGMA wal_autocheckpoint = 0;
48+
PRAGMA temp_store = FILE;
49+
PRAGMA journal_mode = WAL;
5550
`); err != nil {
5651
log.Fatalf("failed to set pragmas: %v", err)
5752
}
5853

54+
if optimizeForLitestream {
55+
if _, err := ctx.Exec(`
56+
-- Apply Litestream recommendations: https://litestream.io/tips/
57+
PRAGMA busy_timeout = 5000;
58+
PRAGMA synchronous = NORMAL;
59+
PRAGMA wal_autocheckpoint = 0;
60+
`); err != nil {
61+
log.Fatalf("failed to set Litestream compatibility pragmas: %v", err)
62+
}
63+
}
64+
5965
applyMigrations(ctx)
6066

6167
return &db{

store/test_sqlite/db.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"github.com/mtlynch/picoshare/v2/store/sqlite"
99
)
1010

11+
const optimizeForLitestream = false
12+
1113
func New() store.Store {
12-
return sqlite.New(ephemeralDbURI())
14+
return sqlite.New(ephemeralDbURI(), optimizeForLitestream)
1315
}
1416

1517
func NewWithChunkSize(chunkSize int) store.Store {
16-
return sqlite.NewWithChunkSize(ephemeralDbURI(), chunkSize)
18+
return sqlite.NewWithChunkSize(ephemeralDbURI(), chunkSize, optimizeForLitestream)
1719
}
1820

1921
func ephemeralDbURI() string {

0 commit comments

Comments
 (0)