Skip to content

Commit

Permalink
sql: add sql migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeneas Rekkas (arekkas) committed Dec 20, 2016
1 parent f612172 commit 18cd409
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
24 changes: 16 additions & 8 deletions client/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/pborman/uuid"
"github.com/pkg/errors"
"strings"
"github.com/rubenv/sql-migrate"
)

var sqlSchema = []string{
`CREATE TABLE IF NOT EXISTS hydra_client (
var migrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "1",
Up: []string{`CREATE TABLE IF NOT EXISTS hydra_client (
id varchar(255) NOT NULL PRIMARY KEY,
client_name text NOT NULL,
client_secret text NOT NULL,
Expand All @@ -28,7 +32,12 @@ var sqlSchema = []string{
logo_uri text NOT NULL,
contacts text NOT NULL,
public boolean NOT NULL
)`,
)`},
Down: []string{
"DROP TABLE hydra_client",
},
},
},
}

type SQLManager struct {
Expand Down Expand Up @@ -109,10 +118,9 @@ func (d *sqlData) ToClient() *Client {
}

func (s *SQLManager) CreateSchemas() error {
for _, query := range sqlSchema {
if _, err := s.DB.Exec(query); err != nil {
return errors.Wrapf(err, "Could not create schema:\n%s", query)
}
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)
}
return nil
}
Expand Down Expand Up @@ -191,7 +199,7 @@ func (m *SQLManager) CreateClient(c *Client) error {
if _, err := m.DB.NamedExec(fmt.Sprintf(
"INSERT INTO hydra_client (%s) VALUES (%s)",
strings.Join(sqlParams, ", "),
":"+strings.Join(sqlParams, ", :"),
":" + strings.Join(sqlParams, ", :"),
), data); err != nil {
return errors.Wrap(err, "")
}
Expand Down
16 changes: 10 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import:
- token/hmac
- token/jwt
- package: github.com/ory-am/ladon
version: ^0.3.4
version: ^0.4.0
- package: github.com/pborman/uuid
version: ^1.0.0
- package: github.com/pkg/errors
Expand Down
22 changes: 16 additions & 6 deletions jwk/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@ import (
"github.com/ory-am/hydra/pkg"
"github.com/pkg/errors"
"github.com/square/go-jose"
"github.com/rubenv/sql-migrate"
)

type SQLManager struct {
DB *sqlx.DB
Cipher *AEAD
}

var sqlSchema = []string{
`CREATE TABLE IF NOT EXISTS hydra_jwk (
var migrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "1",
Up: []string{
`CREATE TABLE IF NOT EXISTS hydra_jwk (
sid varchar(255) NOT NULL,
kid varchar(255) NOT NULL,
version int NOT NULL DEFAULT 0,
keydata text NOT NULL,
PRIMARY KEY (sid, kid)
)`,
},
Down: []string{
"DROP TABLE hydra_jwk",
},
},
},
}

type sqlData struct {
Expand All @@ -32,10 +43,9 @@ type sqlData struct {
}

func (s *SQLManager) CreateSchemas() error {
for _, query := range sqlSchema {
if _, err := s.DB.Exec(query); err != nil {
return errors.Wrapf(err, "Could not create schema:\n%s", query)
}
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)
}
return nil
}
Expand Down
39 changes: 26 additions & 13 deletions oauth2/fosite_store_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"strings"
"time"
"github.com/rubenv/sql-migrate"
)

type FositeSQLStore struct {
Expand All @@ -34,17 +35,30 @@ func sqlTemplate(table string) string {
}

const (
sqlTableOpenID = "oidc"
sqlTableAccess = "access"
sqlTableOpenID = "oidc"
sqlTableAccess = "access"
sqlTableRefresh = "refresh"
sqlTableCode = "code"
sqlTableCode = "code"
)

var sqlSchema = []string{
sqlTemplate(sqlTableAccess),
sqlTemplate(sqlTableRefresh),
sqlTemplate(sqlTableCode),
sqlTemplate(sqlTableOpenID),
var migrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "1",
Up: []string{
sqlTemplate(sqlTableAccess),
sqlTemplate(sqlTableRefresh),
sqlTemplate(sqlTableCode),
sqlTemplate(sqlTableOpenID),
},
Down: []string{
fmt.Sprintf("DROP TABLE %s", sqlTableAccess),
fmt.Sprintf("DROP TABLE %s", sqlTableRefresh),
fmt.Sprintf("DROP TABLE %s", sqlTableCode),
fmt.Sprintf("DROP TABLE %s", sqlTableOpenID),
},
},
},
}

var sqlParams = []string{
Expand Down Expand Up @@ -133,7 +147,7 @@ func (s *FositeSQLStore) createSession(signature string, requester fosite.Reques
"INSERT INTO hydra_oauth2_%s (%s) VALUES (%s)",
table,
strings.Join(sqlParams, ", "),
":"+strings.Join(sqlParams, ", :"),
":" + strings.Join(sqlParams, ", :"),
)
if _, err := s.DB.NamedExec(query, data); err != nil {
return errors.Wrap(err, "")
Expand All @@ -160,10 +174,9 @@ func (s *FositeSQLStore) deleteSession(signature string, table string) error {
}

func (s *FositeSQLStore) CreateSchemas() error {
for _, query := range sqlSchema {
if _, err := s.DB.Exec(query); err != nil {
return errors.Wrapf(err, "Could not create schema:\n%s", query)
}
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)
}
return nil
}
Expand Down

0 comments on commit 18cd409

Please sign in to comment.