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

deps: switch to modernc.org/sqlite for pure go builds #221

Open
wants to merge 3 commits 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
3 changes: 1 addition & 2 deletions Dockerfile.local
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
FROM golang:alpine as builder
RUN apk add --no-cache gcc g++

RUN mkdir /build
RUN mkdir /dist
WORKDIR build
COPY go.mod go.mod
RUN go mod download
COPY . .
RUN go build -ldflags "-extldflags -static -X main.VERSION=${VERSION##*/v}" -o /dist/authn
RUN CGO_ENABLED=0 go build -ldflags "-extldflags -static -X main.VERSION=${VERSION##*/v}" -o /dist/authn

FROM alpine
RUN apk add --no-cache ca-certificates
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ test: init
TEST_POSTGRES_URL=postgres://$(DB_USERNAME):$(DB_PASSWORD)@127.0.0.1:8703/postgres?sslmode=disable \
go test -race ./...

.PHONY: test-nocgo
test-nocgo: init
docker-compose up -d redis mysql postgres
TEST_REDIS_URL=redis://127.0.0.1:8701/12 \
TEST_MYSQL_URL=mysql://[email protected]:8702/authnservertest \
TEST_POSTGRES_URL=postgres://$(DB_USERNAME):$(DB_PASSWORD)@127.0.0.1:8703/postgres?sslmode=disable \
CGO_ENABLED=0 \
go test ./...

# Run benchmarks
.PHONY: benchmarks
benchmarks:
Expand Down
6 changes: 3 additions & 3 deletions app/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"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"
sq3 "modernc.org/sqlite"
)

func NewDB(url *url.URL) (*sqlx.DB, error) {
Expand Down Expand Up @@ -61,8 +61,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 *sq3.Error:
return i.Code() == 2067 // SQLITE_CONSTRAINT_UNIQUE
case *my.MySQLError:
return i.Number == 1062
case *pq.Error:
Expand Down
4 changes: 2 additions & 2 deletions app/data/sqlite3/blob_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/jmoiron/sqlx"
"github.com/keratin/authn-server/ops"
sq3 "github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
sq3 "modernc.org/sqlite"
)

var placeholder = "generating"
Expand Down Expand Up @@ -43,7 +43,7 @@ func (s *BlobStore) Read(name string) ([]byte, error) {

func (s *BlobStore) WriteNX(name string, blob []byte) (bool, error) {
_, err := s.DB.Exec("INSERT INTO blobs (name, blob, expires_at) VALUES (?, ?, ?)", name, blob, time.Now().Add(s.TTL))
if i, ok := err.(sq3.Error); ok && i.ExtendedCode == sq3.ErrConstraintUnique {
if i, ok := err.(*sq3.Error); ok && i.Code() == 2067 {
return false, nil
}
if err != nil {
Expand Down
19 changes: 13 additions & 6 deletions app/data/sqlite3/db.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package sqlite3

import (
"database/sql"
"fmt"
"strings"

"github.com/jmoiron/sqlx"
"modernc.org/sqlite"

// load sqlite3 library with side effects
_ "github.com/mattn/go-sqlite3"
_ "modernc.org/sqlite"
)

func init() {
sql.Register("sqlite3", &sqlite.Driver{})
}

func NewDB(env string) (*sqlx.DB, error) {
// https://github.com/mattn/go-sqlite3/issues/274#issuecomment-232942571
// https://modernc.org/sqlite/issues/274#issuecomment-232942571
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty sure this ain't right 😅

// 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
8 changes: 5 additions & 3 deletions app/data/sqlite3/migrations.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package sqlite3

import (
"errors"
"strings"

"github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
"modernc.org/sqlite"
)

// MigrateDB is committed to doing the work necessary to converge the database
Expand All @@ -29,8 +30,9 @@ func MigrateDB(db *sqlx.DB) error {
}

func isDuplicateError(e error) bool {
sqliteError, ok := e.(sqlite3.Error)
return ok && sqliteError.Code == 1 && strings.Contains(sqliteError.Error(), "duplicate column name")
var sqliteError *sqlite.Error
ok := errors.As(e, &sqliteError)
return ok && sqliteError.Code() == 1 && strings.Contains(sqliteError.Error(), "duplicate column name")
}

func createAccounts(db *sqlx.DB) error {
Expand Down
6 changes: 4 additions & 2 deletions app/services/account_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestAccountCreatorSuccess(t *testing.T) {
{app.Config{UsernameIsEmail: true, UsernameDomains: []string{"rightdomain.com"}}, "[email protected]", "PASSword"},
}

for _, tc := range testCases {
for i := range testCases {
tc := testCases[i] // avoid implicit capture
acc, err := services.AccountCreator(store, &tc.config, tc.username, tc.password)
require.NoError(t, err)
assert.NotEqual(t, 0, acc.ID)
Expand Down Expand Up @@ -60,7 +61,8 @@ func TestAccountCreatorFailure(t *testing.T) {
{app.Config{UsernameIsEmail: true}, "[email protected]", "[email protected]", services.FieldErrors{{"password", "INSECURE"}}},
}

for _, tc := range testCases {
for i := range testCases {
tc := testCases[i] // avoid implicit capture
t.Run(tc.username, func(t *testing.T) {
acc, err := services.AccountCreator(store, &tc.config, tc.username, tc.password)
if assert.Equal(t, tc.errors, err) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/jmoiron/sqlx v0.0.0-20170430194603-d9bd385d68c0
github.com/joho/godotenv v1.2.0
github.com/lib/pq v1.10.0
github.com/mattn/go-sqlite3 v1.6.0
github.com/mattn/go-sqlite3 v1.14.18 // indirect
Copy link
Contributor Author

Choose a reason for hiding this comment

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

github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
github.com/sirupsen/logrus v1.6.0
Expand All @@ -28,4 +28,5 @@ require (
golang.org/x/crypto v0.1.0
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
gopkg.in/square/go-jose.v2 v2.3.1
modernc.org/sqlite v1.27.0
)
Loading
Loading