detached 82a9e9c

perf(db): enable WAL mode and performance PRAGMAs for SQLite

commit
82a9e9c5e88f73ef466ded5e12cfdd09148c3260
author
bnema · · 2025-10-23 21:52 UTC
committer
bnema · · 2025-10-24 17:33 UTC
parent
eedaf29
signature
unsigned
1 file · +22 -8
M internal/db/init.go +22 -8
@@ -38,20 +38,34 @@ func InitDB(dbPath string) (*sql.DB, error) {
3838 return nil, fmt.Errorf("failed to connect to database: %w", err)
3939 }
4040
41- // Run embedded migrations - single source of truth for schema initialization
42- if err := migrations.RunEmbeddedMigrations(database); err != nil {
43- if err := database.Close(); err != nil {
44- log.Printf("Warning: failed to close database: %v", err)
41+ // Configure SQLite for optimal performance
42+ pragmas := []string{
43+ "PRAGMA journal_mode = WAL", // Write-Ahead Logging: 70k reads/sec, 3.6k writes/sec, concurrent access
44+ "PRAGMA synchronous = NORMAL", // Safe in WAL mode, only checkpoints wait for FSYNC
45+ "PRAGMA cache_size = -64000", // 64MB cache (default is only ~2MB)
46+ "PRAGMA temp_store = MEMORY", // Temporary tables in RAM
47+ "PRAGMA mmap_size = 30000000000", // 30GB memory-mapped I/O (virtual memory, OS-managed)
48+ "PRAGMA busy_timeout = 5000", // Wait 5 seconds on lock contention
49+ }
50+
51+ for _, pragma := range pragmas {
52+ if _, err := database.Exec(pragma); err != nil {
53+ if err := database.Close(); err != nil {
54+ log.Printf("Warning: failed to close database: %v", err)
55+ }
56+ return nil, fmt.Errorf("failed to set pragma %q: %w", pragma, err)
4557 }
46- return nil, fmt.Errorf("failed to run database migrations: %w", err)
4758 }
4859
49- // Verify all migrations are applied
50- if err := migrations.VerifyAllMigrationsApplied(database); err != nil {
60+ log.Printf("SQLite configured with WAL mode and performance optimizations")
61+
62+ // Run embedded migrations - single source of truth for schema initialization
63+ // RunEmbeddedMigrations is idempotent and checks if migrations are already applied
64+ if err := migrations.RunEmbeddedMigrations(database); err != nil {
5165 if err := database.Close(); err != nil {
5266 log.Printf("Warning: failed to close database: %v", err)
5367 }
54- return nil, fmt.Errorf("migration verification failed: %w", err)
68+ return nil, fmt.Errorf("failed to run database migrations: %w", err)
5569 }
5670
5771 return database, nil
@@ -38,20 +38,34 @@ func InitDB(dbPath string) (*sql.DB, error) {
38 return nil, fmt.Errorf("failed to connect to database: %w", err)38 return nil, fmt.Errorf("failed to connect to database: %w", err)
39 }39 }
40 40
41- // Run embedded migrations - single source of truth for schema initialization41+ // Configure SQLite for optimal performance
42- if err := migrations.RunEmbeddedMigrations(database); err != nil {42+ pragmas := []string{
43- if err := database.Close(); err != nil {43+ "PRAGMA journal_mode = WAL", // Write-Ahead Logging: 70k reads/sec, 3.6k writes/sec, concurrent access
44- log.Printf("Warning: failed to close database: %v", err)44+ "PRAGMA synchronous = NORMAL", // Safe in WAL mode, only checkpoints wait for FSYNC
45+ "PRAGMA cache_size = -64000", // 64MB cache (default is only ~2MB)
46+ "PRAGMA temp_store = MEMORY", // Temporary tables in RAM
47+ "PRAGMA mmap_size = 30000000000", // 30GB memory-mapped I/O (virtual memory, OS-managed)
48+ "PRAGMA busy_timeout = 5000", // Wait 5 seconds on lock contention
49+ }
50+
51+ for _, pragma := range pragmas {
52+ if _, err := database.Exec(pragma); err != nil {
53+ if err := database.Close(); err != nil {
54+ log.Printf("Warning: failed to close database: %v", err)
55+ }
56+ return nil, fmt.Errorf("failed to set pragma %q: %w", pragma, err)
45 }57 }
46- return nil, fmt.Errorf("failed to run database migrations: %w", err)
47 }58 }
48 59
49- // Verify all migrations are applied60+ log.Printf("SQLite configured with WAL mode and performance optimizations")
50- if err := migrations.VerifyAllMigrationsApplied(database); err != nil {61+
62+ // Run embedded migrations - single source of truth for schema initialization
63+ // RunEmbeddedMigrations is idempotent and checks if migrations are already applied
64+ if err := migrations.RunEmbeddedMigrations(database); err != nil {
51 if err := database.Close(); err != nil {65 if err := database.Close(); err != nil {
52 log.Printf("Warning: failed to close database: %v", err)66 log.Printf("Warning: failed to close database: %v", err)
53 }67 }
54- return nil, fmt.Errorf("migration verification failed: %w", err)68+ return nil, fmt.Errorf("failed to run database migrations: %w", err)
55 }69 }
56 70
57 return database, nil71 return database, nil

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close