Skip to content

Commit

Permalink
Deferred transaction rollbacks (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui authored Apr 26, 2024
1 parent 178bb9d commit 34a0165
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
6 changes: 1 addition & 5 deletions services/location_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (service Service) CreateLocation(ctx context.Context, user *gmodel.User, in
return gmodel.Location{}, fmt.Errorf("could not start transaction")
}
service.TX = tx
defer service.TX.Rollback()

// must create address first
address, err := service.CreateAddress(ctx, user, *input.Address)
if err != nil {
tx.Rollback()
return gmodel.Location{}, err
}

Expand All @@ -51,27 +51,23 @@ func (service Service) CreateLocation(ctx context.Context, user *gmodel.User, in

var location gmodel.Location
if err := qb.QueryContext(ctx, tx, &location); err != nil {
tx.Rollback()
return gmodel.Location{}, fmt.Errorf("could not create location")
}
location.Address = &address

// add location instances
location.LocationInstances, err = service.BulkCreateLocationInstances(ctx, location.ID, input.Instances)
if err != nil {
tx.Rollback()
return gmodel.Location{}, err
}

// add location schedules
location.LocationSchedule, err = service.BulkCreateLocationSchedule(ctx, location.ID, input.Schedule)
if err != nil {
tx.Rollback()
return gmodel.Location{}, err
}

if err := tx.Commit(); err != nil {
tx.Rollback()
return gmodel.Location{}, fmt.Errorf("could not commit location via transaction")
}
return location, nil
Expand Down
13 changes: 3 additions & 10 deletions services/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (service Service) CreateInternalUser(ctx context.Context, input gmodel.Crea
if tx_err != nil {
return gmodel.User{}, model.EmailVerification{}, tx_err
}
defer tx.Rollback()

qb := table.User.
INSERT(
Expand All @@ -88,18 +89,15 @@ func (service Service) CreateInternalUser(ctx context.Context, input gmodel.Crea
}).
RETURNING(table.User.AllColumns)
if err := qb.QueryContext(ctx, tx, &user); err != nil {
tx.Rollback()
return gmodel.User{}, model.EmailVerification{}, fmt.Errorf("user entry could not be created. %s", err.Error())
}
service.TX = tx
if email_verification, err = service.CreateEmailVerification(ctx, user); err != nil {
tx.Rollback()
return gmodel.User{}, model.EmailVerification{}, err
}

// Commit changes from transaction
if err := tx.Commit(); err != nil {
tx.Rollback()
return gmodel.User{}, model.EmailVerification{}, err
}
return user, email_verification, nil
Expand Down Expand Up @@ -267,18 +265,17 @@ func (service Service) ResendEmailVerification(ctx context.Context, user gmodel.
if err != nil {
return model.EmailVerification{}, err
}
defer service.TX.Rollback()

_, err = table.EmailVerification.DELETE().
WHERE(table.EmailVerification.UserID.EQ(postgres.Int(user.ID))).
ExecContext(ctx, service.TX)
if err != nil {
service.TX.Rollback()
return model.EmailVerification{}, fmt.Errorf("user email verification entry deletion failed")
}

email_verification, err = service.CreateEmailVerification(ctx, user)
if err != nil {
service.TX.Rollback()
return model.EmailVerification{}, err
}
if err := service.TX.Commit(); err != nil {
Expand All @@ -303,18 +300,16 @@ func (service Service) VerifyUserEmail(ctx context.Context, verification_code st
var err error
service.TX, err = service.DB.BeginTx(ctx, nil)
if err != nil {
service.TX.Rollback()
return gmodel.User{}, err
}
defer service.TX.Rollback()

email_verification, err := service.FindEmailVerificationByCode(ctx, verification_code)
if err != nil {
service.TX.Rollback()
return gmodel.User{}, err
}

if time.Until(email_verification.CreatedAt).Abs() > time.Hour {
service.TX.Rollback()
// Delete verification entry since it's expired
del_query := table.EmailVerification.
DELETE().
Expand All @@ -330,7 +325,6 @@ func (service Service) VerifyUserEmail(ctx context.Context, verification_code st
SET(postgres.Bool(true), postgres.DateT(time.Now())).
WHERE(table.User.ID.EQ(postgres.Int(email_verification.UserID)))
if _, err := update.ExecContext(ctx, service.TX); err != nil {
service.TX.Rollback()
return gmodel.User{}, fmt.Errorf("could not update user email verification status to verified")
}

Expand All @@ -342,7 +336,6 @@ func (service Service) VerifyUserEmail(ctx context.Context, verification_code st
table.EmailVerification.Code.EQ(postgres.String(verification_code)),
))
if _, err := delete.ExecContext(ctx, service.TX); err != nil {
service.TX.Rollback()
return gmodel.User{}, fmt.Errorf("could not delete email verification entry")
}

Expand Down

0 comments on commit 34a0165

Please sign in to comment.