Switch back to CGO SQLite driver

- Replace modernc.org/sqlite with github.com/mattn/go-sqlite3
- Update connection string for go-sqlite3 syntax
- Keep all performance optimizations and pragmas

The CGO driver may provide better performance for write-heavy
workloads compared to the pure Go implementation.
This commit is contained in:
2025-07-27 22:57:53 +02:00
parent d328fb0942
commit b49d3ce88c
3 changed files with 10 additions and 61 deletions

View File

@@ -12,7 +12,7 @@ import (
"time"
"github.com/google/uuid"
_ "modernc.org/sqlite" // Pure Go SQLite driver
_ "github.com/mattn/go-sqlite3" // CGO SQLite driver
)
//go:embed schema.sql
@@ -94,10 +94,10 @@ func NewWithConfig(config Config, logger *slog.Logger) (*Database, error) {
return nil, fmt.Errorf("failed to create database directory: %w", err)
}
// Add connection parameters for modernc.org/sqlite
// Add connection parameters for go-sqlite3
// Enable WAL mode and other performance optimizations
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL&_cache_size=-512000", config.Path)
db, err := sql.Open("sqlite", dsn)
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_journal_mode=WAL&_synchronous=NORMAL&cache=shared", config.Path)
db, err := sql.Open("sqlite3", dsn)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
@@ -126,7 +126,7 @@ func (d *Database) Initialize() error {
pragmas := []string{
"PRAGMA journal_mode=WAL", // Already set in connection string
"PRAGMA synchronous=NORMAL", // Faster than FULL, still safe
"PRAGMA cache_size=-524288", // 512MB cache
"PRAGMA cache_size=-524288", // 512MB cache (negative = KB)
"PRAGMA temp_store=MEMORY", // Use memory for temp tables
"PRAGMA mmap_size=268435456", // 256MB memory-mapped I/O
"PRAGMA wal_autocheckpoint=1000", // Checkpoint every 1000 pages