mirror of
https://github.com/hanzoai/orm.git
synced 2026-10-30 15:01:22 +00:00
NewEngine normalized its argument before opening, so a caller asking for "sqlite" — the one name this estate's driver registers on both builds — was rewritten to "sqlite3", which resolves only under cgo. Measured: a CGO_ENABLED=0 binary answered `unknown driver "sqlite3"` and could not open a database at all, while the same code passed under cgo. It would equally have sent a pgx caller to a "postgres" driver it never registered. Two questions were sharing one answer. The driver name belongs to the caller, who alone knows what it registered; the dialect names the SQL grammar this engine generates — quoting, autoincrement, upsert — and stays "sqlite3" because that is what the generator's own branches compare against. normalizeDriver is normalizeDialect, and it no longer decides how the database opens.
38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package engine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
_ "github.com/hanzoai/sqlite"
|
|
)
|
|
|
|
// TestNewEngineOpensTheCallersDriver proves the engine opens the name it was
|
|
// given. It used to normalize "sqlite" to "sqlite3", which is registered only
|
|
// under cgo — so a CGO_ENABLED=0 binary could not open a database at all, and
|
|
// the failure was invisible on a developer's machine.
|
|
//
|
|
// The dialect is a separate answer and stays "sqlite3": it names the SQL grammar
|
|
// this engine generates, not a driver.
|
|
func TestNewEngineOpensTheCallersDriver(t *testing.T) {
|
|
e, err := NewEngine("sqlite", "file:"+t.TempDir()+"/t.db")
|
|
if err != nil {
|
|
t.Fatalf(`NewEngine("sqlite"): %v`, err)
|
|
}
|
|
t.Cleanup(func() { _ = e.Close() })
|
|
|
|
if got := e.driver; got != "sqlite3" {
|
|
t.Errorf("dialect = %q, want %q — the grammar is a different answer from the driver", got, "sqlite3")
|
|
}
|
|
if err := e.DB().Ping(); err != nil {
|
|
t.Fatalf("ping: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestUnregisteredDriverIsRefused is the other half: the engine must not quietly
|
|
// substitute a name the caller never registered.
|
|
func TestUnregisteredDriverIsRefused(t *testing.T) {
|
|
if _, err := NewEngine("sqlite3", "file:"+t.TempDir()+"/t.db"); err == nil {
|
|
t.Skip(`"sqlite3" resolves on this build (cgo), so there is nothing to refuse`)
|
|
}
|
|
}
|