First pass implementation of an SQLite backing store for fetched opportunities

This commit is contained in:
bwilliams committed 2026-10-13 12:52:17 +01:00
1 parent 9b0e6dccfe
commit 045492a148
29 files changed
+3132 -9

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
package store
import (
"database/sql"
"fmt"
)
// Option configures Open.
type Option func(*options)
type options struct {
autoMigrate bool
}
// OpenDB opens the SQLite database at path with the store's connection
// settings (foreign keys, WAL, busy timeout, immediate transactions). The
// file is created on first use. Use it for anything that works on the store's
// database directly, such as running migrations.
func OpenDB(path string) (*sql.DB, error) {
return sql.Open("sqlite3", dsn(path))
}
// WithAutoMigrate makes Open apply pending migrations instead of returning
// ErrNeedsMigration. Intended for tests and throwaway databases; applications
// should migrate explicitly with the migrate command.
func WithAutoMigrate() Option {
return func(o *options) { o.autoMigrate = true }
}
// dsn builds the mattn/go-sqlite3 connection string.
//
// - _foreign_keys: SQLite ignores REFERENCES clauses unless this is on.
// - _journal_mode=WAL: readers don't block the writer or each other.
// - _busy_timeout: wait for a lock rather than failing immediately.
// - _txlock=immediate: transactions take the write lock at BEGIN, so a
// read-then-write transaction (as in SaveOpportunity) can't fail with
// SQLITE_BUSY halfway through.
func dsn(path string) string {
return fmt.Sprintf(
"file:%s?_foreign_keys=on&_journal_mode=WAL&_busy_timeout=5000&_txlock=immediate",
path,
)
}