Skip to content

Commit

Permalink
refactor: use gobuffalo/pop for SQL abstraction (#2059)
Browse files Browse the repository at this point in the history
This patch replaces the existing SQL and memory managers with a pop based persister. Existing SQL migrations are compatible as they have been migrated to the new SQL abstraction in version 1.7.x. As a goodie, ORY Hydra now supports SQLite for both in-memory as well as on-disk (useful for development and very small deployments) databases!

Closes #1730

Co-authored-by: aeneasr <aeneas@ory.sh>
Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 20, 2020
1 parent 8f87c1f commit 56bce67
Show file tree
Hide file tree
Showing 176 changed files with 2,408 additions and 3,916 deletions.
13 changes: 1 addition & 12 deletions .circleci/config.yml
Expand Up @@ -55,7 +55,7 @@ jobs:
paths:
- "/go/pkg/mod"
- ".bin"
- run: .bin/go-acc -o coverage.txt ./... -- -failfast -timeout=20m
- run: .bin/go-acc -o coverage.txt ./... -- -failfast -timeout=20m -tags sqlite
# Running race conditions requires parallel tests, otherwise it's worthless (which is the case)
# - run: go test -race -short $(go list ./... | grep -v cmd)
- run: test -z "$CIRCLE_PR_NUMBER" && goveralls -service=circle-ci -coverprofile=coverage.txt -repotoken=$COVERALLS_REPO_TOKEN || echo "forks are not allowed to push to coveralls"
Expand Down Expand Up @@ -207,14 +207,6 @@ workflows:
filters:
tags:
only: /.*/
- test-e2e:
name: test-e2e-plugin
requires:
- test-e2e-memory
flavor: plugin
filters:
tags:
only: /.*/
- changelog/generate:
requires:
- nancy/test
Expand All @@ -223,7 +215,6 @@ workflows:
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
- test-e2e-plugin
- docs/build
# - test-legacy-migrations-mysql
# - test-legacy-migrations-cockroach
Expand All @@ -242,7 +233,6 @@ workflows:
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
- test-e2e-plugin
# - test-legacy-migrations-mysql
# - test-legacy-migrations-cockroach
filters:
Expand Down Expand Up @@ -295,7 +285,6 @@ workflows:
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
- test-e2e-plugin
- changelog/generate
# - test-legacy-migrations-mysql
# - test-legacy-migrations-cockroach
Expand Down
4 changes: 2 additions & 2 deletions .schema/api.swagger.json
Expand Up @@ -2503,7 +2503,7 @@
"type": "string"
},
"challenge": {
"description": "Challenge is the identifier (\"authorization challenge\") of the consent authorization request. It is used to\nidentify the session.",
"description": "ID is the identifier (\"authorization challenge\") of the consent authorization request. It is used to\nidentify the session.",
"type": "string"
},
"client": {
Expand Down Expand Up @@ -2657,7 +2657,7 @@
],
"properties": {
"challenge": {
"description": "Challenge is the identifier (\"login challenge\") of the login request. It is used to\nidentify the session.",
"description": "ID is the identifier (\"login challenge\") of the login request. It is used to\nidentify the session.",
"type": "string"
},
"client": {
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
@@ -1,6 +1,5 @@
# To compile this image manually run:
#
# $ GO111MODULE=on GOOS=linux GOARCH=amd64 go build && docker build -t oryd/hydra:v1.0.0-rc.7_oryOS.10 . && rm hydra
FROM alpine:3.12

RUN apk add -U --no-cache ca-certificates
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-e2e
Expand Up @@ -12,7 +12,7 @@ ENV GO111MODULE=on
ADD . .

RUN go install .
RUN go build -buildmode=plugin -o /memtest.so ./test/e2e/plugin/memtest.go
RUN go build -buildmode=plugin -tags sqlite -o /memtest.so ./test/e2e/plugin/memtest.go

ENTRYPOINT ["hydra"]

Expand Down
13 changes: 6 additions & 7 deletions Makefile
Expand Up @@ -40,7 +40,7 @@ lint: .bin/golangci-lint
# Runs full test suite including tests where databases are enabled
.PHONY: test-legacy-migrations
test-legacy-migrations: test-resetdb sqlbin
source scripts/test-env.sh && go test -tags legacy_migration_test -failfast -timeout=20m ./internal/fizzmigrate
source scripts/test-env.sh && go test -tags legacy_migration_test sqlite -failfast -timeout=20m ./internal/fizzmigrate
docker rm -f hydra_test_database_mysql
docker rm -f hydra_test_database_postgres
docker rm -f hydra_test_database_cockroach
Expand All @@ -49,7 +49,7 @@ test-legacy-migrations: test-resetdb sqlbin
.PHONY: test
test: .bin/go-acc
make test-resetdb
source scripts/test-env.sh && go-acc ./... -- -failfast -timeout=20m
source scripts/test-env.sh && go-acc ./... -- -failfast -timeout=20m -tags sqlite
docker rm -f hydra_test_database_mysql
docker rm -f hydra_test_database_postgres
docker rm -f hydra_test_database_cockroach
Expand All @@ -71,7 +71,7 @@ test-resetdb: node_modules
.PHONY: docker
docker: .bin/packr2
packr2
CGO_ENABLED=0 GO111MODULE=on GOOS=linux GOARCH=amd64 go build
GO111MODULE=on GOOS=linux GOARCH=amd64 go build -tags sqlite
packr2 clean
docker build -t oryd/hydra:latest .
docker build -f Dockerfile-alpine -t oryd/hydra:latest-alpine .
Expand All @@ -88,13 +88,11 @@ e2e: node_modules test-resetdb
./test/e2e/circle-ci.bash mysql-jwt
./test/e2e/circle-ci.bash cockroach
./test/e2e/circle-ci.bash cockroach-jwt
./test/e2e/circle-ci.bash plugin
./test/e2e/circle-ci.bash plugin-jwt

# Runs tests in short mode, without database adapters
.PHONY: quicktest
quicktest:
go test -failfast -short ./...
go test -failfast -short -tags sqlite ./...

# Formats the code
.PHONY: format
Expand Down Expand Up @@ -138,6 +136,7 @@ install-stable: .bin/packr2
git checkout $$HYDRA_LATEST
packr2
GO111MODULE=on go install \
-tags sqlite \
-ldflags "-X github.com/ory/hydra/cmd.Version=$$HYDRA_LATEST -X github.com/ory/hydra/cmd.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'` -X github.com/ory/hydra/cmd.Commit=`git rev-parse HEAD`" \
.
packr2 clean
Expand All @@ -146,5 +145,5 @@ install-stable: .bin/packr2
.PHONY: install
install: .bin/packr2
packr2
GO111MODULE=on go install .
GO111MODULE=on go install -tags sqlite .
packr2 clean
32 changes: 32 additions & 0 deletions client/client.go
Expand Up @@ -24,6 +24,8 @@ import (
"strings"
"time"

"github.com/gobuffalo/pop/v5"

jose "gopkg.in/square/go-jose.v2" // Naming the dependency jose is important for go-swagger to work, see https://github.com/go-swagger/go-swagger/issues/1587

"github.com/ory/fosite"
Expand Down Expand Up @@ -202,6 +204,36 @@ func (Client) TableName() string {
return "hydra_client"
}

func (c *Client) BeforeSave(_ *pop.Connection) error {
if c.JSONWebKeys == nil {
c.JSONWebKeys = new(x.JoseJSONWebKeySet)
}

if c.Metadata == nil {
c.Metadata = []byte("{}")
}

if c.Audience == nil {
c.Audience = sqlxx.StringSlicePipeDelimiter{}
}

if c.AllowedCORSOrigins == nil {
c.AllowedCORSOrigins = sqlxx.StringSlicePipeDelimiter{}
}

if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now()
}
c.CreatedAt = c.CreatedAt.UTC()

if c.UpdatedAt.IsZero() {
c.UpdatedAt = time.Now()
}
c.UpdatedAt = c.UpdatedAt.UTC()

return nil
}

func (c *Client) GetID() string {
return c.ID
}
Expand Down
2 changes: 0 additions & 2 deletions client/manager.go
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/ory/fosite"
)

var _, _ Manager = new(SQLManager), new(MemoryManager)

type Manager interface {
Storage

Expand Down
149 changes: 0 additions & 149 deletions client/manager_memory.go

This file was deleted.

0 comments on commit 56bce67

Please sign in to comment.