Skip to content

Commit

Permalink
sql: improve sql migration routine and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeneas Rekkas (arekkas) authored and arekkas committed Jan 2, 2017
1 parent 971d7ba commit 4f931cd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions client/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (d *sqlData) ToClient() *Client {
}

func (s *SQLManager) CreateSchemas() error {
migrate.SetTable("hydra_client_migration")
n, err := migrate.Exec(s.DB.DB, s.DB.DriverName(), migrations, migrate.Up)
if err != nil {
return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/handler_jwk_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func injectJWKManager(c *config.Config) {
m := &jwk.RedisManager{
DB: con.RedisSession(),
Cipher: &jwk.AEAD{
c.GetSystemSecret(),
Key: c.GetSystemSecret(),
},
}
ctx.KeyManager = m
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/ory-am/hydra/warden/group"
"github.com/ory-am/ladon"
"github.com/pkg/errors"
"github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
Expand Down Expand Up @@ -172,13 +171,11 @@ func (c *Config) Context() *Context {
break
case *SQLConnection:
m := ladon.NewSQLManager(con.GetDatabase(), nil)
migrate.SetTable("hydra_policy_migration")
if err := m.CreateSchemas(); err != nil {
logrus.Fatalf("Could not create policy schema: %s", err)
}
manager = m

migrate.SetTable("hydra_groups_migration")
gm := &group.SQLManager{DB: con.GetDatabase()}
if err := gm.CreateSchemas(); err != nil {
logrus.Fatalf("Could not create group schema: %s", err)
Expand Down
43 changes: 43 additions & 0 deletions integration/sql_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package integration

import (
"testing"
"github.com/ory-am/hydra/client"
"github.com/ory-am/fosite"
"github.com/stretchr/testify/require"
"github.com/ory-am/hydra/policy"
"github.com/ory-am/hydra/jwk"
"github.com/ory-am/hydra/oauth2"
"github.com/ory-am/ladon"
"github.com/ory-am/hydra/warden/group"
)

func TestSQLSchema(t *testing.T) {
var testGenerator = &jwk.RS256Generator{}
ks, _ := testGenerator.Generate("")
p1 := ks.Key("private")
r := fosite.NewRequest()
r.ID = "foo"
db := ConnectToMySQL()

cm := &client.SQLManager{DB: db, Hasher: &fosite.BCrypt{}}
gm := group.SQLManager{DB: db}
jm := jwk.SQLManager{DB: db, Cipher: &jwk.AEAD{Key: []byte("11111111111111111111111111111111")}}
om := oauth2.FositeSQLStore{Manager: cm, DB: db }
pm, err := policy.NewSQLManager(db)
require.Nil(t, err)

require.Nil(t, cm.CreateSchemas())
require.Nil(t, gm.CreateSchemas())
require.Nil(t, jm.CreateSchemas())
require.Nil(t, om.CreateSchemas())

require.Nil(t, jm.AddKey("foo", jwk.First(p1)))
require.Nil(t, pm.Create(&ladon.DefaultPolicy{ID: "foo"}))
require.Nil(t, cm.CreateClient(&client.Client{ID: "foo"}))
require.Nil(t, om.CreateAccessTokenSession(nil, "asdfasdf", r))
require.Nil(t, gm.CreateGroup(&group.Group{
ID: "asdfas",
Members: []string{"asdf"},
}))
}
15 changes: 14 additions & 1 deletion policy/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package policy

import "github.com/ory-am/ladon"
import (
"github.com/ory-am/ladon"
"github.com/jmoiron/sqlx"
"github.com/rubenv/sql-migrate"
)

// Manager is responsible for managing and persisting policies.
type Manager interface {
Expand All @@ -9,3 +13,12 @@ type Manager interface {
// Update a policy.
Update(policy ladon.Policy) error
}

func NewSQLManager(db *sqlx.DB) (ladon.Manager, error) {
m := ladon.NewSQLManager(db, nil)
migrate.SetTable("hydra_policy_migration")
if err := m.CreateSchemas(); err != nil {
return nil, err
}
return m, nil
}
1 change: 1 addition & 0 deletions warden/group/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type SQLManager struct {
}

func (s *SQLManager) CreateSchemas() error {
migrate.SetTable("hydra_groups_migration")
n, err := migrate.Exec(s.DB.DB, s.DB.DriverName(), migrations, migrate.Up)
if err != nil {
return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)
Expand Down

0 comments on commit 4f931cd

Please sign in to comment.