Skip to content

Commit fa088e9

Browse files
aeneasrory-bot
authored andcommitted
feat: migrate SQLite driver from mattn/go-sqlite3 to modernc.org/sqlite and remove CGO dep
GitOrigin-RevId: 4b06aa7218156efb69b9edf0ff6c4a8e5e8de046
1 parent 9465d41 commit fa088e9

File tree

29 files changed

+250
-144
lines changed

29 files changed

+250
-144
lines changed

.docker/Dockerfile-local-build

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ RUN apt-get update && apt-get upgrade -y &&\
1212
COPY go.mod go.sum ./
1313
COPY internal/httpclient/go.* ./internal/httpclient/
1414

15-
ENV CGO_ENABLED=1
16-
1715
RUN go mod download
1816

1917
COPY . .
20-
RUN go build -ldflags="-extldflags=-static" -tags sqlite,sqlite_omit_load_extension -o /usr/bin/hydra
18+
RUN go build -ldflags="-extldflags=-static" -o /usr/bin/hydra
2119

2220
#########################
2321

.docker/Dockerfile-test-hsm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ENV CGO_ENABLED=1
1717
RUN go mod download
1818
COPY . .
1919

20-
RUN go build -tags sqlite,hsm -o /usr/bin/hydra
20+
RUN go build -tags hsm -o /usr/bin/hydra
2121

2222
ENV HSM_ENABLED=true
2323
ENV HSM_LIBRARY=/usr/lib/softhsm/libsofthsm2.so
@@ -33,4 +33,4 @@ RUN go test -p 1 -failfast -short -tags=sqlite,hsm ./...
3333

3434
FROM builder AS test-refresh-hsm
3535

36-
RUN UPDATE_SNAPSHOTS=true go test -p 1 -failfast -short -tags=sqlite,hsm,refresh ./...
36+
RUN UPDATE_SNAPSHOTS=true go test -p 1 -failfast -short -tags=hsm,refresh ./...

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
only-new-issues: "true"
8585
- name: Run go tests
8686
run: |
87-
go test -coverprofile coverage.out -failfast -timeout=20m -tags sqlite,sqlite_omit_load_extension ./...
87+
go test -coverprofile coverage.out -failfast -timeout=20m ./...
8888
- name: Submit to Codecov
8989
run: |
9090
bash <(curl -s https://codecov.io/bash)

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ request, go through this checklist:
165165
1. Create a feature branch off of `master` so that changes do not get mixed up.
166166
1. [Rebase](http://git-scm.com/book/en/Git-Branching-Rebasing) your local
167167
changes against the `master` branch.
168-
1. Run the full project test suite with the `go test -tags sqlite ./...` (or
169-
equivalent) command and confirm that it passes.
168+
1. Run the full project test suite with the `go test ./...` (or equivalent)
169+
command and confirm that it passes.
170170
1. Run `make format`
171171
1. Add a descriptive prefix to commits. This ensures a uniform commit history
172172
and helps structure the changelog. Please refer to this

DEVELOP.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,24 @@ There are three types of tests:
5858
Short tests run fairly quickly and use SQLite in-memory.
5959

6060
All tests run against a sqlite in-memory database, thus it is required to use
61-
the `-tags sqlite` build tag.
61+
the `` build tag.
6262

6363
Run all short tests:
6464

6565
```shell
66-
go test -v -failfast -short -tags sqlite ./...
66+
go test -v -failfast -short ./...
6767
```
6868

6969
Run short tests in a specific module:
7070

7171
```shell
72-
go test -v -failfast -short -tags sqlite ./client
72+
go test -v -failfast -short ./client
7373
```
7474

7575
Run a specific test:
7676

7777
```shell
78-
go test -v -failfast -short -tags sqlite -run ^TestName$ ./...
78+
go test -v -failfast -short -run ^TestName$ ./...
7979
```
8080

8181
### Regular tests

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ lint: .bin/golangci-lint
3636
.PHONY: test
3737
test:
3838
make test-resetdb
39-
source scripts/test-env.sh && go test -failfast -timeout=20m -tags sqlite,sqlite_omit_load_extension ./...
39+
source scripts/test-env.sh && go test -failfast -timeout=20m ./...
4040
docker rm -f hydra_test_database_mysql
4141
docker rm -f hydra_test_database_postgres
4242
docker rm -f hydra_test_database_cockroach
@@ -68,15 +68,15 @@ e2e: node_modules test-resetdb
6868
# Runs tests in short mode, without database adapters
6969
.PHONY: quicktest
7070
quicktest:
71-
go test -failfast -short -tags sqlite,sqlite_omit_load_extension ./...
71+
go test -failfast -short ./...
7272

7373
.PHONY: quicktest-hsm
7474
quicktest-hsm:
7575
DOCKER_CONTENT_TRUST=1 docker build --progress=plain -f .docker/Dockerfile-test-hsm --target test-hsm -t oryd/hydra:${IMAGE_TAG} --target test-hsm .
7676

7777
.PHONY: test-refresh
7878
test-refresh:
79-
UPDATE_SNAPSHOTS=true go test -short -tags sqlite,sqlite_omit_load_extension ./...
79+
UPDATE_SNAPSHOTS=true go test -short ./...
8080
DOCKER_CONTENT_TRUST=1 docker build --progress=plain -f .docker/Dockerfile-test-hsm --target test-refresh-hsm -t oryd/hydra:${IMAGE_TAG} --target test-refresh-hsm .
8181

8282
authors: # updates the AUTHORS file
@@ -169,14 +169,13 @@ install-stable:
169169
HYDRA_LATEST=$$(git describe --abbrev=0 --tags)
170170
git checkout $$HYDRA_LATEST
171171
go install \
172-
-tags sqlite,sqlite_omit_load_extension \
173172
-ldflags "-X github.com/ory/hydra/v2/driver/config.Version=$$HYDRA_LATEST -X github.com/ory/hydra/v2/driver/config.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'` -X github.com/ory/hydra/v2/driver/config.Commit=`git rev-parse HEAD`" \
174173
.
175174
git checkout master
176175

177176
.PHONY: install
178177
install:
179-
go install -tags sqlite,sqlite_omit_load_extension .
178+
go install .
180179

181180
.PHONY: pre-release
182181
pre-release:

cmd/server/helper_cert_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
"github.com/stretchr/testify/require"
1919

2020
"github.com/ory/x/configx"
21+
"github.com/ory/x/dbal"
2122
"github.com/ory/x/logrusx"
2223
"github.com/ory/x/servicelocatorx"
2324
"github.com/ory/x/tlsx"
2425

2526
"github.com/ory/hydra/v2/cmd/server"
2627
"github.com/ory/hydra/v2/driver"
27-
"github.com/ory/hydra/v2/driver/config"
2828
"github.com/ory/hydra/v2/internal/testhelpers"
2929
"github.com/ory/hydra/v2/jwk"
3030
)
@@ -35,11 +35,12 @@ func TestGetOrCreateTLSCertificate(t *testing.T) {
3535
logger.Logger.ExitFunc = func(code int) { t.Fatalf("Logger called os.Exit(%v)", code) }
3636
d, err := driver.New(t.Context(),
3737
driver.WithConfigOptions(configx.WithValues(map[string]interface{}{
38-
"dsn": config.DSNMemory,
38+
"dsn": dbal.NewSQLiteTestDatabase(t),
3939
"serve.tls.enabled": true,
4040
"serve.tls.cert.path": certPath,
4141
"serve.tls.key.path": keyPath,
4242
})),
43+
driver.WithAutoMigrate(),
4344
driver.WithServiceLocatorOptions(servicelocatorx.WithLogger(logger)),
4445
)
4546
require.NoError(t, err)
@@ -107,12 +108,15 @@ func TestGetOrCreateTLSCertificateBase64(t *testing.T) {
107108
require.NoError(t, err)
108109
keyBase64 := base64.StdEncoding.EncodeToString(keyPEM)
109110

110-
d, err := driver.New(t.Context(), driver.WithConfigOptions(configx.WithValues(map[string]interface{}{
111-
"dsn": config.DSNMemory,
112-
"serve.tls.enabled": true,
113-
"serve.tls.cert.base64": certBase64,
114-
"serve.tls.key.base64": keyBase64,
115-
})))
111+
d, err := driver.New(t.Context(),
112+
driver.WithConfigOptions(configx.WithValues(map[string]interface{}{
113+
"dsn": dbal.NewSQLiteTestDatabase(t),
114+
"serve.tls.enabled": true,
115+
"serve.tls.cert.base64": certBase64,
116+
"serve.tls.key.base64": keyBase64,
117+
})),
118+
driver.WithAutoMigrate(),
119+
)
116120
require.NoError(t, err)
117121
getCert := server.GetOrCreateTLSCertificate(t.Context(), d, d.Config().ServeAdmin(t.Context()).TLS, "admin")
118122
require.NotNil(t, getCert)

driver/config/provider.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import (
1111
"net/http"
1212
"net/url"
1313
"strings"
14+
"sync"
1415
"testing"
1516
"time"
1617

1718
"github.com/stretchr/testify/require"
1819

19-
"github.com/gofrs/uuid"
2020
"github.com/pkg/errors"
2121

2222
"github.com/ory/hydra/v2/spec"
2323
"github.com/ory/hydra/v2/x"
2424
"github.com/ory/x/configx"
2525
"github.com/ory/x/contextx"
26-
"github.com/ory/x/dbal"
2726
"github.com/ory/x/hasherx"
2827
"github.com/ory/x/logrusx"
2928
"github.com/ory/x/otelx"
29+
3030
"github.com/ory/x/randx"
3131
"github.com/ory/x/stringslice"
3232
"github.com/ory/x/urlx"
@@ -130,9 +130,11 @@ var (
130130
)
131131

132132
type DefaultProvider struct {
133-
l *logrusx.Logger
134-
p *configx.Provider
135-
c contextx.Contextualizer
133+
l *logrusx.Logger
134+
p *configx.Provider
135+
c contextx.Contextualizer
136+
dsnOnce sync.Once
137+
sqliteInMemoryDSN string
136138
}
137139

138140
func (p *DefaultProvider) GetHasherAlgorithm(ctx context.Context) string {
@@ -299,7 +301,15 @@ func (p *DefaultProvider) DSN() string {
299301
dsn := p.getProvider(contextx.RootContext).String(KeyDSN)
300302

301303
if dsn == DSNMemory {
302-
return dbal.NewSQLiteInMemoryDatabase(uuid.Must(uuid.NewV4()).String())
304+
p.dsnOnce.Do(func() {
305+
fn, err := randx.RuneSequence(12, randx.AlphaNum)
306+
if err != nil {
307+
p.l.Fatal("could not create temp dir for SQLite database")
308+
}
309+
310+
p.sqliteInMemoryDSN = fmt.Sprintf("sqlite://file:%s?_fk=true&mode=memory&cache=shared&_busy_timeout=100000&_time_format=sqlite", string(fn))
311+
})
312+
return p.sqliteInMemoryDSN
303313
}
304314

305315
if len(dsn) > 0 {
@@ -513,6 +523,7 @@ func (p *DefaultProvider) KratosAdminURL(ctx context.Context) (*url.URL, bool) {
513523

514524
return u, u != nil
515525
}
526+
516527
func (p *DefaultProvider) KratosPublicURL(ctx context.Context) (*url.URL, bool) {
517528
u := p.getProvider(ctx).RequestURIF(KeyIdentityProviderPublicURL, nil)
518529

driver/registry_sql_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func init() {
3939
func TestGetJWKSFetcherStrategyHostEnforcement(t *testing.T) {
4040
t.Parallel()
4141

42-
r, err := New(t.Context(), WithConfigOptions(
42+
r, err := New(t.Context(), WithAutoMigrate(), WithConfigOptions(
4343
configx.WithValues(map[string]any{
44-
config.KeyDSN: "memory",
44+
config.KeyDSN: dbal.NewSQLiteTestDatabase(t),
4545
config.HSMEnabled: "false",
4646
config.KeyClientHTTPNoPrivateIPRanges: true,
4747
}),
@@ -146,7 +146,7 @@ func TestDefaultKeyManager_HsmDisabled(t *testing.T) {
146146
WithConfigOptions(
147147
configx.SkipValidation(),
148148
configx.WithValues(map[string]any{
149-
config.KeyDSN: "memory",
149+
config.KeyDSN: dbal.NewSQLiteTestDatabase(t),
150150
config.HSMEnabled: false,
151151
}),
152152
),

fosite/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ request, go through this checklist:
151151
1. Create a feature branch off of `master` so that changes do not get mixed up.
152152
1. [Rebase](http://git-scm.com/book/en/Git-Branching-Rebasing) your local
153153
changes against the `master` branch.
154-
1. Run the full project test suite with the `go test -tags sqlite ./...` (or
155-
equivalent) command and confirm that it passes.
154+
1. Run the full project test suite with the `go test ./...` (or equivalent)
155+
command and confirm that it passes.
156156
1. Run `make format`
157157
1. Add a descriptive prefix to commits. This ensures a uniform commit history
158158
and helps structure the changelog. Please refer to this

0 commit comments

Comments
 (0)