Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HMS-2757): use transactions for reservations #707

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions internal/dao/pgx/reservation_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,31 @@ func getReservationDao(ctx context.Context) dao.ReservationDao {
}

func (x *reservationDao) CreateNoop(ctx context.Context, reservation *models.NoopReservation) error {
reservation.Provider = models.ProviderTypeNoop
if err := x.createGenericReservation(ctx, &reservation.Reservation); err != nil {
return fmt.Errorf("failed to create reservation record: %w", err)
txErr := dao.WithTransaction(ctx, func(tx pgx.Tx) error {
reservation.Provider = models.ProviderTypeNoop
if err := x.createGenericReservation(ctx, tx, &reservation.Reservation); err != nil {
return fmt.Errorf("failed to create reservation record: %w", err)
}

return nil
})

if txErr != nil {
return fmt.Errorf("pgx tx error: %w", txErr)
}
return nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make things simple and because pgx.Tx and pgx.Pool are incompatible types, I am putting this in tx block although it does not need to be. I think I could have used pgx.Querier or some interface, not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah Querier is not from the pgx package, I would rather not use it. These are two different types.

}

func (x *reservationDao) CreateAWS(ctx context.Context, reservation *models.AWSReservation) error {
txErr := dao.WithTransaction(ctx, func(tx pgx.Tx) error {
reservation.Provider = models.ProviderTypeAWS
if err := x.createGenericReservation(ctx, &reservation.Reservation); err != nil {
if err := x.createGenericReservation(ctx, tx, &reservation.Reservation); err != nil {
return err
}

awsQuery := `INSERT INTO aws_reservation_details (reservation_id, pubkey_id, source_id, image_id, detail)
VALUES ($1, $2, $3, $4, $5)`
tag, err := db.Pool.Exec(ctx, awsQuery,
tag, err := tx.Exec(ctx, awsQuery,
reservation.ID,
reservation.PubkeyID,
reservation.SourceID,
Expand All @@ -68,13 +76,13 @@ func (x *reservationDao) CreateAWS(ctx context.Context, reservation *models.AWSR
func (x *reservationDao) CreateAzure(ctx context.Context, reservation *models.AzureReservation) error {
txErr := dao.WithTransaction(ctx, func(tx pgx.Tx) error {
reservation.Provider = models.ProviderTypeAzure
if err := x.createGenericReservation(ctx, &reservation.Reservation); err != nil {
if err := x.createGenericReservation(ctx, tx, &reservation.Reservation); err != nil {
return err
}

azureQuery := `INSERT INTO azure_reservation_details (reservation_id, pubkey_id, source_id, image_id, detail)
VALUES ($1, $2, $3, $4, $5)`
tag, err := db.Pool.Exec(ctx, azureQuery,
tag, err := tx.Exec(ctx, azureQuery,
reservation.ID,
reservation.PubkeyID,
reservation.SourceID,
Expand All @@ -99,13 +107,13 @@ func (x *reservationDao) CreateAzure(ctx context.Context, reservation *models.Az
func (x *reservationDao) CreateGCP(ctx context.Context, reservation *models.GCPReservation) error {
txErr := dao.WithTransaction(ctx, func(tx pgx.Tx) error {
reservation.Provider = models.ProviderTypeGCP
if err := x.createGenericReservation(ctx, &reservation.Reservation); err != nil {
if err := x.createGenericReservation(ctx, tx, &reservation.Reservation); err != nil {
return err
}

gcpQuery := `INSERT INTO gcp_reservation_details (reservation_id, pubkey_id, source_id, image_id, detail)
VALUES ($1, $2, $3, $4, $5)`
tag, err := db.Pool.Exec(ctx, gcpQuery,
tag, err := tx.Exec(ctx, gcpQuery,
reservation.ID,
reservation.PubkeyID,
reservation.SourceID,
Expand All @@ -127,13 +135,13 @@ func (x *reservationDao) CreateGCP(ctx context.Context, reservation *models.GCPR
return nil
}

func (x *reservationDao) createGenericReservation(ctx context.Context, reservation *models.Reservation) error {
func (x *reservationDao) createGenericReservation(ctx context.Context, tx pgx.Tx, reservation *models.Reservation) error {
reservation.AccountID = identity.AccountId(ctx)
reservation.Status = "Created"

reservationQuery := `INSERT INTO reservations (provider, account_id, steps, step_titles, status)
VALUES ($1, $2, $3, $4, $5) RETURNING id, created_at`
err := db.Pool.QueryRow(ctx, reservationQuery,
err := tx.QueryRow(ctx, reservationQuery,
reservation.Provider,
reservation.AccountID,
reservation.Steps,
Expand Down