Skip to content

Commit

Permalink
fix: implement withouttx
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Jun 14, 2024
1 parent 68ad2d4 commit f59148c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 3 deletions.
5 changes: 5 additions & 0 deletions core/relation/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
)

type Repository interface {
Transactor
Get(ctx context.Context, id string) (RelationV2, error)
Create(ctx context.Context, relation RelationV2) (RelationV2, error)
List(ctx context.Context) ([]RelationV2, error)
Expand All @@ -23,6 +24,10 @@ type Repository interface {
GetByFields(ctx context.Context, rel RelationV2) (RelationV2, error)
}

type Transactor interface {
WithoutTx(ctx context.Context) context.Context
}

type AuthzRepository interface {
Add(ctx context.Context, rel Relation) error
Check(ctx context.Context, rel Relation, act action.Action) (bool, error)
Expand Down
4 changes: 3 additions & 1 deletion core/relation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func (s Service) Create(ctx context.Context, rel RelationV2) (RelationV2, error)
return RelationV2{}, fmt.Errorf("%w: %s", ErrCreatingRelationInAuthzEngine, err.Error())
}

ctx = s.repository.WithoutTx(ctx)

go func() {
ctx := context.TODO()
ctx = context.WithoutCancel(ctx)
relationLogData := createdRelation.ToLogData()
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyRelationCreate, actor, relationLogData); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions core/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
)

type Repository interface {
Transactor
GetByID(ctx context.Context, id string) (Resource, error)
GetByURN(ctx context.Context, urn string) (Resource, error)
Upsert(ctx context.Context, resource Resource) (Resource, error)
Expand All @@ -24,6 +25,10 @@ type Repository interface {
GetByNamespace(ctx context.Context, name string, ns string) (Resource, error)
}

type Transactor interface {
WithoutTx(ctx context.Context) context.Context
}

type ConfigRepository interface {
GetAll(ctx context.Context) ([]YAML, error)
}
Expand Down
4 changes: 3 additions & 1 deletion core/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ func (s Service) Create(ctx context.Context, res Resource) (Resource, error) {
return Resource{}, err
}

ctx = s.repository.WithoutTx(ctx)

go func() {
ctx := context.TODO()
ctx = context.WithoutCancel(ctx)
resourceLogData := newResource.ToLogData()
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyResourceCreate, actor, resourceLogData); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions core/servicedata/mocks/servicedata_repository.go

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

4 changes: 3 additions & 1 deletion core/servicedata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) {
return Key{}, err
}

ctx = s.repository.WithoutTx(ctx)

go func() {
ctx := context.TODO()
ctx = context.WithoutCancel(ctx)
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyServiceDataKeyCreate, actor, key.ToKeyLogData()); err != nil {
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
Expand Down
1 change: 1 addition & 0 deletions core/servicedata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestService_CreateKey(t *testing.T) {
activityService := &mocks.ActivityService{}
repository.On("WithTransaction", mock.Anything).Return(context.TODO())
repository.On("Commit", mock.Anything).Return(nil)
repository.On("WithoutTx", mock.Anything).Return(context.TODO())
userService.EXPECT().FetchCurrentUser(mock.Anything).
Return(user.User{
ID: testUserID,
Expand Down
1 change: 1 addition & 0 deletions core/servicedata/servicedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Transactor interface {
WithTransaction(ctx context.Context) context.Context
Rollback(ctx context.Context, err error) error
Commit(ctx context.Context) error
WithoutTx(ctx context.Context) context.Context
}

type Key struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/store/postgres/relation_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,7 @@ func (r RelationRepository) GetByFields(ctx context.Context, rel relation.Relati

return fetchedRelation.transformToRelationV2(), nil
}

func (r RelationRepository) WithoutTx(ctx context.Context) context.Context {
return r.dbc.WithoutTx(ctx)
}
4 changes: 4 additions & 0 deletions internal/store/postgres/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,7 @@ func (r ResourceRepository) GetByNamespace(ctx context.Context, name string, ns

return fetchedResource.transformToResource(), nil
}

func (r ResourceRepository) WithoutTx(ctx context.Context) context.Context {
return r.dbc.WithoutTx(ctx)
}
4 changes: 4 additions & 0 deletions internal/store/postgres/servicedata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,7 @@ func (r ServiceDataRepository) Rollback(ctx context.Context, err error) error {
func (r ServiceDataRepository) Commit(ctx context.Context) error {
return r.dbc.Commit(ctx)
}

func (r ServiceDataRepository) WithoutTx(ctx context.Context) context.Context {
return r.dbc.WithoutTx(ctx)
}
4 changes: 4 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (c Client) Rollback(ctx context.Context) error {
return errors.New("no transaction")
}

func (c Client) WithoutTx(ctx context.Context) context.Context {
return context.WithValue(ctx, transactionContextKey, nil)
}

func extractTransaction(ctx context.Context) *sqlx.Tx {
if tx, ok := ctx.Value(transactionContextKey).(*sqlx.Tx); !ok {
return nil
Expand Down

0 comments on commit f59148c

Please sign in to comment.