Files
base-ha/pragma_dsn_test.go
84d68a4ab5 fix(sqlite): drop direct modernc.org/sqlite import — carry pragmas on the DSN (#4)
base-ha's only direct modernc.org/sqlite reference was naming the parameter type
(sqlite.ExecQuerierContext) of litesql/go-sqlite-ha's Driver.ConnectionHook field.
That field is typed modernc.org/sqlite.ConnectionHookFn; hanzoai/sqlite's identically-
shaped ExecQuerierContext is a DISTINCT defined type (and !cgo-only), so a func using
it is not assignable to the field — the naive import swap does not compile. Rebuilding
go-sqlite-ha against hanzoai/sqlite is out of scope (it also needs modernc's
SQLitePreUpdateData / Backup / lib constants, which hanzoai/sqlite does not re-export).

Instead, drop the connection hook and carry the same 7 pragmas on the DSN via
_pragma=name(value). go-ha forwards unrecognized DSN query params verbatim to its
underlying pure-Go driver, which runs each as PRAGMA name=value on every new
connection — behaviour-identical to the old hook. base-ha now imports no low-level
SQLite driver directly; modernc drops to an indirect (litesql) dependency.

The data.db connector lookup is made query-tolerant since ListDSN now carries the
_pragma suffix. New round-trip test asserts all 7 pragmas survive go-ha DSN parsing.

Co-authored-by: hanzo-dev <[email protected]>
2026-07-18 09:14:59 -07:00

44 lines
1.5 KiB
Go

package main
import (
"strings"
"testing"
ha "github.com/litesql/go-ha"
)
// TestWithPragmasForwardsToDriverDSN proves the tuning base-ha used to apply via
// a driver-typed connection hook is now carried on the DSN and forwarded, intact,
// by litesql/go-ha to its underlying pure-Go SQLite driver. go-ha splits an HA
// name into (driver DSN, HA options) and passes unrecognized query params —
// our _pragma= set — straight through to the driver, which runs each as
// PRAGMA name=value on every new connection. This is what lets base-ha drop its
// direct low-level SQLite driver import while keeping identical per-connection tuning.
func TestWithPragmasForwardsToDriverDSN(t *testing.T) {
const path = "/var/base/data.db"
dsn, opts, err := ha.NameToOptions(withPragmas(path))
if err != nil {
t.Fatalf("NameToOptions: %v", err)
}
if len(opts) != 0 {
t.Fatalf("pragmas must not be consumed as HA options, got %d", len(opts))
}
if !strings.HasPrefix(dsn, path+"?") {
t.Fatalf("driver DSN lost the db path: %q", dsn)
}
for _, p := range sqlitePragmas {
if want := "_pragma=" + p; !strings.Contains(dsn, want) {
t.Errorf("driver DSN missing %q (dsn=%s)", want, dsn)
}
}
}
// TestWithPragmasAppendsToExistingQuery ensures withPragmas extends, not clobbers,
// a path that already carries a query string.
func TestWithPragmasAppendsToExistingQuery(t *testing.T) {
if got := withPragmas("data.db?_txlock=immediate"); !strings.HasPrefix(got, "data.db?_txlock=immediate&_pragma=") {
t.Fatalf("existing query not preserved: %q", got)
}
}