Files
sqlite/pragma_test.go
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

48 lines
1.3 KiB
Go

package sqlite
import (
"database/sql"
"path/filepath"
"strings"
"testing"
)
// TestPragmaDSNApplies proves PragmaDSN encodes the pragma set in the ACTIVE
// backend's DSN syntax such that the pragmas actually take effect — not merely
// that the string is well-formed. This is the regression that guards the
// modernc/mattn DSN-format split: it runs green under BOTH build tags.
func TestPragmaDSNApplies(t *testing.T) {
db, err := sql.Open("sqlite", PragmaDSN(filepath.Join(t.TempDir(), "p.db"), DefaultPragmas))
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
t.Fatalf("ping: %v", err)
}
var journal string
if err := db.QueryRow(`PRAGMA journal_mode`).Scan(&journal); err != nil {
t.Fatalf("query journal_mode: %v", err)
}
if !strings.EqualFold(journal, "wal") {
t.Fatalf("journal_mode = %q, want wal (PragmaDSN did not apply on this backend)", journal)
}
var busy int
if err := db.QueryRow(`PRAGMA busy_timeout`).Scan(&busy); err != nil {
t.Fatalf("query busy_timeout: %v", err)
}
if busy != 10000 {
t.Fatalf("busy_timeout = %d, want 10000", busy)
}
var fk int
if err := db.QueryRow(`PRAGMA foreign_keys`).Scan(&fk); err != nil {
t.Fatalf("query foreign_keys: %v", err)
}
if fk != 1 {
t.Fatalf("foreign_keys = %d, want 1", fk)
}
}