Skip to content

Commit

Permalink
feat: allow using SQLite without built tag via include (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckbrain committed Apr 23, 2022
1 parent d722117 commit 6abef98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
30 changes: 24 additions & 6 deletions dialect_sqlite.go
@@ -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
@@ -0,0 +1,8 @@
//go:build sqlite
// +build sqlite

package pop

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

0 comments on commit 6abef98

Please sign in to comment.