From a0323d6d218ff8165d75d23dbd3edbe2a9853a66 Mon Sep 17 00:00:00 2001 From: aeneasr <3372410+aeneasr@users.noreply.github.com> Date: Mon, 19 Oct 2020 13:38:58 +0200 Subject: [PATCH] Load SQLite instrumentation only when build tag present --- connection_instrumented.go | 19 +++++++++++++------ connection_instrumented_nosqlite_test.go | 15 +++++++++++++++ dialect_nosqlite_test.go | 14 ++++++++++++++ dialect_sqlite.go | 6 ++++++ dialect_sqlite_shim.go | 5 +++++ dialect_sqlite_test.go | 5 +++++ 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 connection_instrumented_nosqlite_test.go create mode 100644 dialect_nosqlite_test.go diff --git a/connection_instrumented.go b/connection_instrumented.go index e27fee2d..84cf7820 100644 --- a/connection_instrumented.go +++ b/connection_instrumented.go @@ -8,13 +8,12 @@ import ( pgx "github.com/jackc/pgx/v4/stdlib" "github.com/jmoiron/sqlx" "github.com/luna-duclos/instrumentedsql" - "github.com/mattn/go-sqlite3" "github.com/pkg/errors" ) const instrumentedDriverName = "instrumented-sql-driver" -func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (driverName, dialect string) { +func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (driverName, dialect string, err error) { driverName = defaultDriverName if deets.Driver != "" { driverName = deets.Driver @@ -27,7 +26,7 @@ func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (drive } // If instrumentation is disabled, we just return the driver name we got (e.g. "pgx"). - return driverName, dialect + return driverName, dialect, nil } if len(deets.InstrumentedDriverOptions) == 0 { @@ -48,7 +47,11 @@ func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (drive dr = mysqld.MySQLDriver{} newDriverName = instrumentedDriverName + "-" + nameMySQL case nameSQLite3: - dr = new(sqlite3.SQLiteDriver) + var err error + dr, err = newSQLiteDriver() + if err != nil { + return "", "", err + } newDriverName = instrumentedDriverName + "-" + nameSQLite3 } @@ -64,7 +67,7 @@ func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (drive sql.Register(newDriverName, instrumentedsql.WrapDriver(dr, deets.InstrumentedDriverOptions...)) } - return newDriverName, dialect + return newDriverName, dialect, nil } // openPotentiallyInstrumentedConnection first opens a raw SQL connection and then wraps it with `sqlx`. @@ -74,7 +77,11 @@ func instrumentDriver(deets *ConnectionDetails, defaultDriverName string) (drive // a custom driver name when using instrumentation, this detection would fail // otherwise. func openPotentiallyInstrumentedConnection(c dialect, dsn string) (*sqlx.DB, error) { - driverName, dialect := instrumentDriver(c.Details(), c.DefaultDriver()) + driverName, dialect, err := instrumentDriver(c.Details(), c.DefaultDriver()) + if err != nil { + return nil, err + } + con, err := sql.Open(driverName, dsn) if err != nil { return nil, errors.Wrap(err, "could not open database connection") diff --git a/connection_instrumented_nosqlite_test.go b/connection_instrumented_nosqlite_test.go new file mode 100644 index 00000000..715a92ab --- /dev/null +++ b/connection_instrumented_nosqlite_test.go @@ -0,0 +1,15 @@ +// +build !sqlite + +package pop + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestInstrumentation_WithoutSqlite(t *testing.T) { + _, _, err := instrumentDriver(&ConnectionDetails{ + URL: "sqlite://:memory:", + }, "sqlite") + require.NoError(t, err) +} diff --git a/dialect_nosqlite_test.go b/dialect_nosqlite_test.go new file mode 100644 index 00000000..92fc29ca --- /dev/null +++ b/dialect_nosqlite_test.go @@ -0,0 +1,14 @@ +// +build !sqlite + +package pop + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSqlite_NewDriver(t *testing.T) { + _, err := newSQLiteDriver() + require.Error(t, err) +} diff --git a/dialect_sqlite.go b/dialect_sqlite.go index f67c14ee..b3045090 100644 --- a/dialect_sqlite.go +++ b/dialect_sqlite.go @@ -3,7 +3,9 @@ package pop import ( + "database/sql/driver" "fmt" + "github.com/mattn/go-sqlite3" "io" "net/url" "os" @@ -290,3 +292,7 @@ func finalizerSQLite(cd *ConnectionDetails) { } // or fix user specified url? } } + +func newSQLiteDriver() (driver.Driver, error) { + return new(sqlite3.SQLiteDriver), nil +} diff --git a/dialect_sqlite_shim.go b/dialect_sqlite_shim.go index c394eaad..0c51baa1 100644 --- a/dialect_sqlite_shim.go +++ b/dialect_sqlite_shim.go @@ -3,6 +3,7 @@ package pop import ( + "database/sql/driver" "errors" ) @@ -16,3 +17,7 @@ func init() { func newSQLite(deets *ConnectionDetails) (dialect, error) { return nil, errors.New("sqlite3 support was not compiled into the binary") } + +func newSQLiteDriver() (driver.Driver, error) { + return nil, errors.New("sqlite3 support was not compiled into the binary") +} diff --git a/dialect_sqlite_test.go b/dialect_sqlite_test.go index a31315e6..16796f6d 100644 --- a/dialect_sqlite_test.go +++ b/dialect_sqlite_test.go @@ -163,3 +163,8 @@ func TestSqlite_CreateDB(t *testing.T) { // Creating DB twice should produce an error r.EqualError(dialect.CreateDB(), fmt.Sprintf("could not create SQLite database '%s'; database exists", p)) } + +func TestSqlite_NewDriver(t *testing.T) { + _, err := newSQLiteDriver() + require.NoError(t, err) +}