Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SQLite without built tag #662

Merged
merged 2 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions dialect_sqlite.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build sqlite
// +build sqlite

package pop

import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"net/url"
Expand All @@ -21,8 +20,6 @@ import (
"github.com/gobuffalo/pop/v6/internal/defaults"
"github.com/gobuffalo/pop/v6/logging"
"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3" // Load SQLite3 CGo driver
)

const nameSQLite3 = "sqlite3"
Expand All @@ -43,6 +40,15 @@ type sqlite struct {
smGil *sync.Mutex
}

func requireSQLite3() error {
for _, driverName := range sql.Drivers() {
if driverName == nameSQLite3 {
return nil
}
}
return errors.New("sqlite3 support was not compiled into the binary")
}

func (m *sqlite) Name() string {
return nameSQLite3
}
Expand Down Expand Up @@ -263,6 +269,10 @@ func (m *sqlite) TruncateAll(tx *Connection) error {
}

func newSQLite(deets *ConnectionDetails) (dialect, error) {
err := requireSQLite3()
if err != nil {
return nil, err
}
deets.URL = fmt.Sprintf("sqlite3://%s", deets.Database)
cd := &sqlite{
gil: &sync.Mutex{},
Expand Down Expand Up @@ -328,5 +338,13 @@ func finalizerSQLite(cd *ConnectionDetails) {
}

func newSQLiteDriver() (driver.Driver, error) {
return new(sqlite3.SQLiteDriver), nil
err := requireSQLite3()
if err != nil {
return nil, err
}
db, err := sql.Open(nameSQLite3, ":memory:?cache=newSQLiteDriver_temporary")
if err != nil {
return nil, err
}
return db.Driver(), db.Close()
}
23 changes: 0 additions & 23 deletions dialect_sqlite_shim.go

This file was deleted.

8 changes: 8 additions & 0 deletions dialect_sqlite_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build sqlite
// +build sqlite
duckbrain marked this conversation as resolved.
Show resolved Hide resolved

package pop

import (
_ "github.com/mattn/go-sqlite3" // Load SQLite3 CGo driver
)