Skip to content

Commit

Permalink
feat: replace hydra's transaction impl with ory/popx/transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
grantzvolsky authored and aeneasr committed May 13, 2022
1 parent ebe86c2 commit a2b928e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 215 deletions.
3 changes: 2 additions & 1 deletion hsm/manager_hsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/ory/hydra/jwk"
"github.com/ory/hydra/x/contextx"

"github.com/ory/hydra/driver"
"github.com/ory/hydra/driver/config"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestDefaultKeyManager_HsmEnabled(t *testing.T) {
reg.WithLogger(l)
reg.WithConfig(c)
reg.WithHsmContext(mockHsmContext)
err := reg.Init(context.Background())
err := reg.Init(context.Background(), false, true, &contextx.TestContextualizer{})
assert.NoError(t, err)
assert.IsType(t, &jwk.ManagerStrategy{}, reg.KeyManager())
assert.IsType(t, &sql.Persister{}, reg.SoftwareKeyManager())
Expand Down
30 changes: 14 additions & 16 deletions persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
"reflect"

"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/x/randx"
"github.com/gofrs/uuid"

"github.com/gobuffalo/x/randx"
"github.com/pkg/errors"

"github.com/ory/fosite"
"github.com/ory/fosite/storage"
"github.com/ory/hydra/driver/config"
"github.com/ory/hydra/jwk"
"github.com/ory/hydra/persistence"
"github.com/ory/hydra/persistence/sql/transaction"
"github.com/ory/hydra/x"
"github.com/ory/hydra/x/contextx"
"github.com/ory/x/errorsx"
Expand Down Expand Up @@ -54,8 +53,8 @@ type (
)

func (p *Persister) BeginTX(ctx context.Context) (context.Context, error) {
_, ok := ctx.Value(transaction.TransactionContextKey).(*pop.Connection)
if ok {
fallback := &pop.Connection{TX: &pop.Tx{}}
if popx.GetConnection(ctx, fallback).TX != fallback.TX {
return ctx, errorsx.WithStack(ErrTransactionOpen)
}

Expand All @@ -69,25 +68,27 @@ func (p *Persister) BeginTX(ctx context.Context) (context.Context, error) {
ID: randx.String(30),
Dialect: p.conn.Dialect,
}
return context.WithValue(ctx, transaction.TransactionContextKey, c), err
return popx.WithTransaction(ctx, c), err
}

func (p *Persister) Commit(ctx context.Context) error {
c, ok := ctx.Value(transaction.TransactionContextKey).(*pop.Connection)
if !ok || c.TX == nil {
fallback := &pop.Connection{TX: &pop.Tx{}}
tx := popx.GetConnection(ctx, fallback)
if tx.TX == fallback.TX || tx.TX == nil {
return errorsx.WithStack(ErrNoTransactionOpen)
}

return errorsx.WithStack(c.TX.Commit())
return errorsx.WithStack(tx.TX.Commit())
}

func (p *Persister) Rollback(ctx context.Context) error {
c, ok := ctx.Value(transaction.TransactionContextKey).(*pop.Connection)
if !ok || c.TX == nil {
fallback := &pop.Connection{TX: &pop.Tx{}}
tx := popx.GetConnection(ctx, fallback)
if tx.TX == fallback.TX || tx.TX == nil {
return errorsx.WithStack(ErrNoTransactionOpen)
}

return errorsx.WithStack(c.TX.Rollback())
return errorsx.WithStack(tx.TX.Rollback())
}

func NewPersister(ctx context.Context, c *pop.Connection, r Dependencies, config *config.Provider, l *logrusx.Logger) (*Persister, error) {
Expand Down Expand Up @@ -142,10 +143,7 @@ func (p *Persister) QueryWithNetwork(ctx context.Context) *pop.Query {
}

func (p *Persister) Connection(ctx context.Context) *pop.Connection {
if c, ok := ctx.Value(transaction.TransactionContextKey).(*pop.Connection); ok {
return c.WithContext(ctx)
}
return p.conn.WithContext(ctx)
return popx.GetConnection(ctx, p.conn)
}

func (p *Persister) mustSetNetwork(nid uuid.UUID, v interface{}) interface{} {
Expand All @@ -163,5 +161,5 @@ func (p *Persister) mustSetNetwork(nid uuid.UUID, v interface{}) interface{} {
}

func (p *Persister) transaction(ctx context.Context, f func(ctx context.Context, c *pop.Connection) error) error {
return transaction.Transaction(ctx, p.conn, f)
return popx.Transaction(ctx, p.conn, f)
}
70 changes: 0 additions & 70 deletions persistence/sql/transaction/transaction.go

This file was deleted.

128 changes: 0 additions & 128 deletions persistence/sql/transaction/transaction_test.go

This file was deleted.

0 comments on commit a2b928e

Please sign in to comment.