Skip to content

Commit a98686c

Browse files
authored
handle rollbacks (#1586)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 6ecced1 commit a98686c

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

app/controlplane/pkg/data/casbackend.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,19 @@ func (r *CASBackendRepo) FindFallbackBackend(ctx context.Context, orgID uuid.UUI
8383

8484
// Create creates a new CAS backend in the given organization
8585
// If it's set as default, it will unset the previous default backend
86-
func (r *CASBackendRepo) Create(ctx context.Context, opts *biz.CASBackendCreateOpts) (*biz.CASBackend, error) {
86+
func (r *CASBackendRepo) Create(ctx context.Context, opts *biz.CASBackendCreateOpts) (b *biz.CASBackend, err error) {
8787
tx, err := r.data.DB.Tx(ctx)
8888
if err != nil {
8989
return nil, fmt.Errorf("failed to create transaction: %w", err)
9090
}
9191

92+
defer func() {
93+
// Unblock the row if there was an error
94+
if err != nil {
95+
_ = tx.Rollback()
96+
}
97+
}()
98+
9299
// 1 - unset default backend for all the other backends in the org
93100
if opts.Default {
94101
if err := tx.CASBackend.Update().
@@ -129,12 +136,19 @@ func (r *CASBackendRepo) Create(ctx context.Context, opts *biz.CASBackendCreateO
129136
return r.FindByID(ctx, backend.ID)
130137
}
131138

132-
func (r *CASBackendRepo) Update(ctx context.Context, opts *biz.CASBackendUpdateOpts) (*biz.CASBackend, error) {
139+
func (r *CASBackendRepo) Update(ctx context.Context, opts *biz.CASBackendUpdateOpts) (b *biz.CASBackend, err error) {
133140
tx, err := r.data.DB.Tx(ctx)
134141
if err != nil {
135142
return nil, fmt.Errorf("failed to create transaction: %w", err)
136143
}
137144

145+
defer func() {
146+
// Unblock the row if there was an error
147+
if err != nil {
148+
_ = tx.Rollback()
149+
}
150+
}()
151+
138152
// 1 - unset default backend for all the other backends in the org
139153
if opts.Default {
140154
if err := tx.CASBackend.Update().

app/controlplane/pkg/data/integration.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,19 @@ func (r *IntegrationRepo) FindByNameInOrg(ctx context.Context, orgID uuid.UUID,
112112
return entIntegrationToBiz(integration), nil
113113
}
114114

115-
func (r *IntegrationRepo) SoftDelete(ctx context.Context, id uuid.UUID) error {
115+
func (r *IntegrationRepo) SoftDelete(ctx context.Context, id uuid.UUID) (err error) {
116116
tx, err := r.data.DB.Tx(ctx)
117117
if err != nil {
118118
return err
119119
}
120120

121+
defer func() {
122+
// Unblock the row if there was an error
123+
if err != nil {
124+
_ = tx.Rollback()
125+
}
126+
}()
127+
121128
// soft-delete attachments associated with this workflow
122129
if err := tx.IntegrationAttachment.Update().Where(integrationattachment.HasIntegrationWith(integration.ID(id))).SetDeletedAt(time.Now()).Exec(ctx); err != nil {
123130
return err

app/controlplane/pkg/data/membership.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (r *MembershipRepo) FindByIDInOrg(ctx context.Context, orgID, membershipID
145145
return entMembershipToBiz(m), nil
146146
}
147147

148-
func (r *MembershipRepo) SetCurrent(ctx context.Context, membershipID uuid.UUID) (*biz.Membership, error) {
148+
func (r *MembershipRepo) SetCurrent(ctx context.Context, membershipID uuid.UUID) (mr *biz.Membership, err error) {
149149
// Load membership to find user
150150
m, err := r.loadMembership(ctx, membershipID)
151151
if err != nil {
@@ -158,6 +158,13 @@ func (r *MembershipRepo) SetCurrent(ctx context.Context, membershipID uuid.UUID)
158158
return nil, err
159159
}
160160

161+
defer func() {
162+
// Unblock the row if there was an error
163+
if err != nil {
164+
_ = tx.Rollback()
165+
}
166+
}()
167+
161168
// 1 - Set all the memberships to current=false
162169
if err = tx.Membership.Update().Where(membership.HasUserWith(user.ID(m.Edges.User.ID))).
163170
SetCurrent(false).Exec(ctx); err != nil {

app/controlplane/pkg/data/referrer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ func NewReferrerRepo(data *Data, wfRepo biz.WorkflowRepo, logger log.Logger) biz
4545

4646
type storedReferrerMap map[string]*ent.Referrer
4747

48-
func (r *ReferrerRepo) Save(ctx context.Context, referrers []*biz.Referrer, workflowID uuid.UUID) error {
48+
func (r *ReferrerRepo) Save(ctx context.Context, referrers []*biz.Referrer, workflowID uuid.UUID) (err error) {
4949
// Start transaction
5050
tx, err := r.data.DB.Tx(ctx)
5151
if err != nil {
5252
return fmt.Errorf("failed to create transaction: %w", err)
5353
}
5454

55+
defer func() {
56+
// Unblock the row if there was an error
57+
if err != nil {
58+
_ = tx.Rollback()
59+
}
60+
}()
61+
5562
// find the workflow
5663
wf, err := r.workflowRepo.FindByID(ctx, workflowID)
5764
if err != nil {

app/controlplane/pkg/data/workflow.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,19 @@ func (r *WorkflowRepo) FindByID(ctx context.Context, id uuid.UUID) (*biz.Workflo
314314
}
315315

316316
// Soft delete workflow, attachments and related projects (if applicable)
317-
func (r *WorkflowRepo) SoftDelete(ctx context.Context, id uuid.UUID) error {
317+
func (r *WorkflowRepo) SoftDelete(ctx context.Context, id uuid.UUID) (err error) {
318318
tx, err := r.data.DB.Tx(ctx)
319319
if err != nil {
320320
return err
321321
}
322322

323+
defer func() {
324+
// Unblock the row if there was an error
325+
if err != nil {
326+
_ = tx.Rollback()
327+
}
328+
}()
329+
323330
// soft-delete attachments associated with this workflow
324331
if err := tx.IntegrationAttachment.Update().Where(integrationattachment.HasWorkflowWith(workflow.ID(id))).SetDeletedAt(time.Now()).Exec(ctx); err != nil {
325332
return err

0 commit comments

Comments
 (0)