forked from mystiq/dex
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:
parent
71bbbee075
commit
19d7edd530
6 changed files with 69 additions and 46 deletions
|
@ -14,7 +14,6 @@ import (
|
||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
"github.com/go-sql-driver/mysql"
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
sqlite3 "github.com/mattn/go-sqlite3"
|
|
||||||
|
|
||||||
"github.com/dexidp/dex/pkg/log"
|
"github.com/dexidp/dex/pkg/log"
|
||||||
"github.com/dexidp/dex/storage"
|
"github.com/dexidp/dex/storage"
|
||||||
|
@ -32,47 +31,6 @@ const (
|
||||||
mysqlErrUnknownSysVar = 1193
|
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
|
// nolint
|
||||||
const (
|
const (
|
||||||
// postgres SSL modes
|
// 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 {
|
func getenv(key, defaultVal string) string {
|
||||||
if val := os.Getenv(key); val != "" {
|
if val := os.Getenv(key); val != "" {
|
||||||
return val
|
return val
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
// +build cgo
|
||||||
|
|
||||||
package sql
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
// +build cgo
|
||||||
|
|
||||||
package sql
|
package sql
|
||||||
|
|
||||||
import (
|
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)
|
||||||
|
}
|
Loading…
Reference in a new issue