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

Add the nocgo build flag to enable a build without sqlite3 #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions app/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"fmt"
"net/url"

"github.com/lib/pq"

my "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/keratin/authn-server/app/data/mock"
"github.com/keratin/authn-server/app/data/mysql"
"github.com/keratin/authn-server/app/data/postgres"
"github.com/keratin/authn-server/app/data/sqlite3"
sq3 "github.com/mattn/go-sqlite3"
"github.com/lib/pq"
)

func NewDB(url *url.URL) (*sqlx.DB, error) {
Expand Down Expand Up @@ -61,8 +59,8 @@ func MigrateDB(url *url.URL) error {

func IsUniquenessError(err error) bool {
switch i := err.(type) {
case sq3.Error:
return i.ExtendedCode == sq3.ErrConstraintUnique
case sqlite3.Error:
return i.ExtendedCode == sqlite3.ErrConstraintUnique
case *my.MySQLError:
return i.Number == 1062
case *pq.Error:
Expand Down
2 changes: 2 additions & 0 deletions app/data/sqlite3/blob_store.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nocgo

package sqlite3

import (
Expand Down
33 changes: 33 additions & 0 deletions app/data/sqlite3/blob_store_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build nocgo

// This stub package allows authn-server to be build without CGO, i.e. with CGO_ENABLED=0. This has numerous advantages
// including on toolchains where static linking is impossible. In order to use it run with `go build -tags nocgo`.

package sqlite3

import (
"time"

"github.com/jmoiron/sqlx"
"github.com/keratin/authn-server/ops"
)

var placeholder = "generating"

type BlobStore struct {
TTL time.Duration
LockTime time.Duration
DB sqlx.Ext
}

func (s *BlobStore) Clean(reporter ops.ErrorReporter) {
panic("Cannot use sqlite3 BlobStore because building with nocgo build tag")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
panic("Cannot use sqlite3 BlobStore because building with nocgo build tag")
panic("Cannot use sqlite3 BlobStore without cgo build")

}

func (s *BlobStore) Read(name string) ([]byte, error) {
panic("Cannot use sqlite3 BlobStore because building with nocgo build tag")
}

func (s *BlobStore) WriteNX(name string, blob []byte) (bool, error) {
panic("Cannot use sqlite3 BlobStore because building with nocgo build tag")
}
2 changes: 2 additions & 0 deletions app/data/sqlite3/blob_store_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nocgo

package sqlite3_test

import (
Expand Down
16 changes: 11 additions & 5 deletions app/data/sqlite3/db.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nocgo

package sqlite3

import (
Expand All @@ -6,19 +8,23 @@ import (

"github.com/jmoiron/sqlx"
// load sqlite3 library with side effects
_ "github.com/mattn/go-sqlite3"
sq3 "github.com/mattn/go-sqlite3"
)

type Error = sq3.Error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing these aliases here just means we can isolate the CGO requiring files and wont' need another _nocgo.go file elsewhere


var ErrConstraintUnique = sq3.ErrConstraintUnique

func NewDB(env string) (*sqlx.DB, error) {
// https://github.com/mattn/go-sqlite3/issues/274#issuecomment-232942571
// enable a busy timeout for concurrent load. keep it short. the busy timeout can be harmful
// under sustained load, but helpful during short bursts.
// this block used to keep backward compatibility

// this block used to keep backward compatibility
if !strings.Contains(env, ".") {
env = "./"+ env +".db"
env = "./" + env + ".db"
}

return sqlx.Connect("sqlite3", fmt.Sprintf("%v?cache=shared&_busy_timeout=200", env))
}

Expand Down
28 changes: 28 additions & 0 deletions app/data/sqlite3/db_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build nocgo

// This stub package allows authn-server to be build without CGO, i.e. with CGO_ENABLED=0. This has numerous advantages
// including on toolchains where static linking is impossible. In order to use it run with `go build -tags nocgo`.

package sqlite3

import (
"github.com/jmoiron/sqlx"
)

type Error struct {
ExtendedCode int
}

func (Error) Error() string {
panic("Cannot use sqlite3 Error because building with nocgo build tag")
}

var ErrConstraintUnique int

func NewDB(env string) (*sqlx.DB, error) {
panic("Cannot use sqlite3 DB because building with nocgo build tag")
}

func TestDB() (*sqlx.DB, error) {
panic("Cannot use sqlite3 DB because building with nocgo build tag")
}