Skip to content

Commit

Permalink
fix: insert relation tuples without fmt.Sprintf (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Feb 11, 2021
1 parent c7d2770 commit fe507bb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
6 changes: 3 additions & 3 deletions internal/persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func (p *Persister) newConnection(options map[string]string) (c *pop.Connection,
}

func (p *Persister) MigrateUp(_ context.Context) error {
return p.mb.Up()
return errors.WithStack(p.mb.Up())
}

func (p *Persister) MigrateDown(_ context.Context, steps int) error {
return p.mb.Down(steps)
return errors.WithStack(p.mb.Down(steps))
}

func (p *Persister) MigrationStatus(_ context.Context, w io.Writer) error {
return p.mb.Status(w)
return errors.WithStack(p.mb.Status(w))
}

func (p *Persister) connection(ctx context.Context) *pop.Connection {
Expand Down
60 changes: 37 additions & 23 deletions internal/persistence/sql/relationtuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package sql

import (
"context"
"fmt"
"time"

"github.com/ory/x/sqlcon"

"github.com/gobuffalo/pop/v5"

"github.com/pkg/errors"
Expand All @@ -17,7 +18,8 @@ import (

type (
relationTuple struct {
ShardID string `db:"shard_id"`
// An ID field is required to make pop happy. The actual ID is a composite primary key.
ID string `db:"shard_id"`
Object string `db:"object"`
Relation string `db:"relation"`
Subject string `db:"subject"`
Expand All @@ -35,31 +37,20 @@ const (
namespaceContextKey contextKeys = "namespace"
)

func (relationTuples) TableName(ctx context.Context) string {
func namespaceTableFromContext(ctx context.Context) string {
n, ok := ctx.Value(namespaceContextKey).(*namespace.Namespace)
if n == nil || !ok {
panic("namespace context key not set")
}
return tableFromNamespace(n)
}

func (p *Persister) insertRelationTuple(ctx context.Context, rel *relationtuple.InternalRelationTuple) error {
commitTime := time.Now()

n, err := p.namespaces.GetNamespace(ctx, rel.Namespace)
if err != nil {
return err
}

// TODO sharding
shardID := "default"

p.l.WithFields(rel.ToLoggerFields()).Trace("creating in database")
func (relationTuples) TableName(ctx context.Context) string {
return namespaceTableFromContext(ctx)
}

return p.connection(ctx).RawQuery(fmt.Sprintf(
"INSERT INTO %s (shard_id, object, relation, subject, commit_time) VALUES (?, ?, ?, ?, ?)", tableFromNamespace(n)),
shardID, rel.Object, rel.Relation, rel.Subject.String(), commitTime,
).Exec()
func (relationTuple) TableName(ctx context.Context) string {
return namespaceTableFromContext(ctx)
}

func (r *relationTuple) toInternal() (*relationtuple.InternalRelationTuple, error) {
Expand All @@ -68,12 +59,38 @@ func (r *relationTuple) toInternal() (*relationtuple.InternalRelationTuple, erro
}

sub, err := relationtuple.SubjectFromString(r.Subject)
if err != nil {
return nil, err
}

return &relationtuple.InternalRelationTuple{
Relation: r.Relation,
Object: r.Object,
Namespace: r.Namespace.Name,
Subject: sub,
}, err
}, nil
}

func (p *Persister) insertRelationTuple(ctx context.Context, rel *relationtuple.InternalRelationTuple) error {
n, err := p.namespaces.GetNamespace(ctx, rel.Namespace)
if err != nil {
return err
}

// TODO sharding
shardID := "default"

p.l.WithFields(rel.ToLoggerFields()).Trace("creating in database")

return sqlcon.HandleError(
p.connection(context.WithValue(ctx, namespaceContextKey, n)).Create(&relationTuple{
ID: shardID,
Object: rel.Object,
Relation: rel.Relation,
Subject: rel.Subject.String(),
CommitTime: time.Now(),
}),
)
}

func (p *Persister) GetRelationTuples(ctx context.Context, query *relationtuple.RelationQuery, options ...x.PaginationOptionSetter) ([]*relationtuple.InternalRelationTuple, string, error) {
Expand All @@ -83,15 +100,12 @@ func (p *Persister) GetRelationTuples(ctx context.Context, query *relationtuple.
}

var wheres []whereStmts

if query.Relation != "" {
wheres = append(wheres, whereStmts{stmt: "relation = ?", arg: query.Relation})
}

if query.Object != "" {
wheres = append(wheres, whereStmts{stmt: "object = ?", arg: query.Object})
}

if query.Subject != nil {
wheres = append(wheres, whereStmts{stmt: "subject = ?", arg: query.Subject.String()})
}
Expand Down

0 comments on commit fe507bb

Please sign in to comment.