Files
zeekayandHanzo Dev 9fec90146d feat: unified commit-hook + PragmaDSN so consumers drop direct modernc imports
Adds the surface Hanzo Base needs to import ONLY github.com/hanzoai/sqlite and
never register modernc.org/sqlite directly — so a CGO consumer that needs
SQLCipher (commerce's per-tenant money DBs) no longer hits `panic: sql: Register
called twice for driver sqlite` from base's modernc registration colliding with
hanzoai/sqlite's mattn one. Complements the v0.1.5 sqlite_purego opt-out, which
serves the other case (a pure-Go cgo consumer wanting neither SQLCipher nor to
drop modernc); commerce needs SQLCipher, so it takes THIS path.

- hooks.go + hooks_{cgo,nocgo}.go: CommitHookRegisterer bridges the backends'
  divergent native commit-hook signatures (mattn func() int / modernc func()
  int32) to one CommitHookFn. Same cgo&&!sqlite_purego / !cgo||sqlite_purego
  split as the drivers, so all four tag/cgo combos stay exclusive + exhaustive.
- pragma.go + pragma_{cgo,nocgo}.go: PragmaDSN + DefaultPragmas encode one pragma
  set in the ACTIVE backend's DSN syntax (mattn _name=value / modernc
  _pragma=name(value)); a single-form DSN is silently ignored by the other
  backend, so busy_timeout/WAL would otherwise not apply when the binary is built
  for the other backend.
- hooks_test.go + pragma_test.go: prove (all pure-Go combos) the hook
  fires/aborts/clears and the pragmas take effect (journal_mode=WAL,
  busy_timeout=10000, FK=ON).

Exactly one "sqlite" registration per build preserved (drivers unchanged);
encrypted-at-rest path untouched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-03 21:03:46 -07:00

37 lines
1.7 KiB
Go

package sqlite
// Connection-tuning surface — one pragma set, encoded per backend.
//
// The two backends express connection pragmas with DIFFERENT DSN syntax:
//
// modernc (!cgo): ?_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)&...
// mattn (cgo): ?_busy_timeout=10000&_journal_mode=WAL&...
//
// A DSN written for one backend is silently IGNORED by the other (mattn drops
// unknown `_pragma=` params; modernc drops unknown `_busy_timeout=` params), so
// a hardcoded single-form DSN loses its pragmas — no WAL, no busy_timeout —
// whenever the binary is built for the other backend (e.g. Base is CGO=0 but is
// embedded into commerce's CGO=1 build). PragmaDSN encodes the pragmas in the
// ACTIVE backend's syntax (pragma_cgo.go / pragma_nocgo.go) so the same pragma
// set applies under both.
// Pragma is one SQLite PRAGMA (name + value) applied at connection-open time via
// the DSN.
type Pragma struct{ Name, Value string }
// DefaultPragmas is the canonical Hanzo embedded-SQLite connection tuning. Order
// matters: busy_timeout MUST lead so a connection blocks on a busy database
// before journal_mode=WAL is set (WAL cannot be enabled while another
// connection holds the database). This is the single source of truth for the
// unencrypted default-connect DSN used across Hanzo Base (core + multitenant
// store) — treat it as read-only.
var DefaultPragmas = []Pragma{
{Name: "busy_timeout", Value: "10000"},
{Name: "journal_mode", Value: "WAL"},
{Name: "journal_size_limit", Value: "200000000"},
{Name: "synchronous", Value: "NORMAL"},
{Name: "foreign_keys", Value: "ON"},
{Name: "temp_store", Value: "MEMORY"},
{Name: "cache_size", Value: "-32000"},
}