Don't try to build sqlite when cgo isn't enabled

Signed-off-by: A Gardner <3100188+actgardner@users.noreply.github.com>
This commit is contained in:
A Gardner 2020-11-17 17:44:20 -05:00
parent 71bbbee075
commit 19d7edd530
6 changed files with 69 additions and 46 deletions

View File

@ -14,7 +14,6 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/dexidp/dex/pkg/log"
"github.com/dexidp/dex/storage"
@ -32,47 +31,6 @@ const (
mysqlErrUnknownSysVar = 1193
)
// SQLite3 options for creating an SQL db.
type SQLite3 struct {
// File to
File string `json:"file"`
}
// Open creates a new storage implementation backed by SQLite3
func (s *SQLite3) Open(logger log.Logger) (storage.Storage, error) {
conn, err := s.open(logger)
if err != nil {
return nil, err
}
return conn, nil
}
func (s *SQLite3) open(logger log.Logger) (*conn, error) {
db, err := sql.Open("sqlite3", s.File)
if err != nil {
return nil, err
}
if s.File == ":memory:" {
// sqlite3 uses file locks to coordinate concurrent access. In memory
// doesn't support this, so limit the number of connections to 1.
db.SetMaxOpenConns(1)
}
errCheck := func(err error) bool {
sqlErr, ok := err.(sqlite3.Error)
if !ok {
return false
}
return sqlErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey
}
c := &conn{db, &flavorSQLite3, logger, errCheck}
if _, err := c.migrate(); err != nil {
return nil, fmt.Errorf("failed to perform migrations: %v", err)
}
return c, nil
}
// nolint
const (
// postgres SSL modes

View File

@ -85,10 +85,6 @@ func testDB(t *testing.T, o opener, withTransactions bool) {
}
}
func TestSQLite3(t *testing.T) {
testDB(t, &SQLite3{":memory:"}, false)
}
func getenv(key, defaultVal string) string {
if val := os.Getenv(key); val != "" {
return val

View File

@ -1,3 +1,5 @@
// +build cgo
package sql
import (

View File

@ -1,3 +1,5 @@
// +build cgo
package sql
import (

54
storage/sql/sqlite.go Normal file
View File

@ -0,0 +1,54 @@
// +build cgo
package sql
import (
"database/sql"
"fmt"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/dexidp/dex/pkg/log"
"github.com/dexidp/dex/storage"
)
// SQLite3 options for creating an SQL db.
type SQLite3 struct {
// File to
File string `json:"file"`
}
// Open creates a new storage implementation backed by SQLite3
func (s *SQLite3) Open(logger log.Logger) (storage.Storage, error) {
conn, err := s.open(logger)
if err != nil {
return nil, err
}
return conn, nil
}
func (s *SQLite3) open(logger log.Logger) (*conn, error) {
db, err := sql.Open("sqlite3", s.File)
if err != nil {
return nil, err
}
if s.File == ":memory:" {
// sqlite3 uses file locks to coordinate concurrent access. In memory
// doesn't support this, so limit the number of connections to 1.
db.SetMaxOpenConns(1)
}
errCheck := func(err error) bool {
sqlErr, ok := err.(sqlite3.Error)
if !ok {
return false
}
return sqlErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey
}
c := &conn{db, &flavorSQLite3, logger, errCheck}
if _, err := c.migrate(); err != nil {
return nil, fmt.Errorf("failed to perform migrations: %v", err)
}
return c, nil
}

View File

@ -0,0 +1,11 @@
// +build cgo
package sql
import (
"testing"
)
func TestSQLite3(t *testing.T) {
testDB(t, &SQLite3{":memory:"}, false)
}