Skip to content

Commit

Permalink
Introduces surrogate_id to SQLManager (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Nov 12, 2017
1 parent 4ee776c commit fbe272f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions rule/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

type sqlRule struct {
ID string `db:"id"`
ID string `db:"surrogate_id"`
InternalID int64 `db:"id"`
MatchesMethods string `db:"matches_methods"`
MatchesURL string `db:"matches_url"`
RequiredScopes string `db:"required_scopes"`
Expand Down Expand Up @@ -76,9 +77,10 @@ var migrations = &migrate.MemoryMigrationSource{
{
Id: "1",
Up: []string{`CREATE TABLE IF NOT EXISTS oathkeeper_rule (
id varchar(64) NOT NULL PRIMARY KEY,
id SERIAL PRIMARY KEY,
surrogate_id varchar(190) NOT NULL UNIQUE,
matches_methods varchar(64) NOT NULL,
matches_url text NOT NULL,
matches_url text NOT NULL,
required_scopes text NOT NULL,
required_action text NOT NULL,
required_resource text NOT NULL,
Expand All @@ -95,7 +97,7 @@ var migrations = &migrate.MemoryMigrationSource{
}

var sqlParams = []string{
"id",
"surrogate_id",
"matches_methods",
"matches_url",
"required_scopes",
Expand Down Expand Up @@ -146,7 +148,7 @@ func (s *SQLManager) ListRules() ([]Rule, error) {

func (s *SQLManager) GetRule(id string) (*Rule, error) {
var d sqlRule
if err := s.db.Get(&d, s.db.Rebind("SELECT * FROM oathkeeper_rule WHERE id=?"), id); err == sql.ErrNoRows {
if err := s.db.Get(&d, s.db.Rebind("SELECT * FROM oathkeeper_rule WHERE surrogate_id=?"), id); err == sql.ErrNoRows {
return nil, errors.WithStack(helper.ErrResourceNotFound)
} else if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -175,14 +177,14 @@ func (s *SQLManager) UpdateRule(rule *Rule) error {
for _, param := range sqlParams {
query = append(query, fmt.Sprintf("%s=:%s", param, param))
}
if _, err := s.db.NamedExec(fmt.Sprintf(`UPDATE oathkeeper_rule SET %s WHERE id=:id`, strings.Join(query, ", ")), sr); err != nil {
if _, err := s.db.NamedExec(fmt.Sprintf(`UPDATE oathkeeper_rule SET %s WHERE surrogate_id=:surrogate_id`, strings.Join(query, ", ")), sr); err != nil {
return errors.WithStack(err)
}
return nil
}

func (s *SQLManager) DeleteRule(id string) error {
if _, err := s.db.Exec(s.db.Rebind(`DELETE FROM oathkeeper_rule WHERE id=?`), id); err != nil {
if _, err := s.db.Exec(s.db.Rebind(`DELETE FROM oathkeeper_rule WHERE surrogate_id=?`), id); err != nil {
return errors.WithStack(err)
}
return nil
Expand Down

0 comments on commit fbe272f

Please sign in to comment.