Merge pull request #1866 from pachyderm/actgardner/split-sqlite
Don't try to build sqlite storage when cgo isn't enabled
This commit is contained in:
commit
6ca0cbc857
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,3 +1,5 @@
|
||||
// +build cgo
|
||||
|
||||
package sql
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,5 @@
|
||||
// +build cgo
|
||||
|
||||
package sql
|
||||
|
||||
import (
|
||||
|
54
storage/sql/sqlite.go
Normal file
54
storage/sql/sqlite.go
Normal 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
|
||||
}
|
11
storage/sql/sqlite_test.go
Normal file
11
storage/sql/sqlite_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
// +build cgo
|
||||
|
||||
package sql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSQLite3(t *testing.T) {
|
||||
testDB(t, &SQLite3{":memory:"}, false)
|
||||
}
|
Reference in New Issue
Block a user