Skip to content

Commit

Permalink
chore: fix account requery
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap authored and ezr-ondrej committed Oct 25, 2023
1 parent f26e2b5 commit 539560c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions internal/dao/pgx/account_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package pgx
import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/RHEnVision/provisioning-backend/internal/dao"
"github.com/RHEnVision/provisioning-backend/internal/db"
"github.com/RHEnVision/provisioning-backend/internal/models"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -73,17 +75,17 @@ func (x *accountDao) GetOrCreateByIdentity(ctx context.Context, orgId string, ac
LIMIT 1;`

err := pgxscan.Get(ctx, db.Pool, result, query, orgId, account)
if err != nil {
return nil, fmt.Errorf("pgx error: %w", err)
}

// Step 4: requery in case transaction isolation (simultaneous transactions)
if result.ID == 0 && result.OrgID == "" {
zerolog.Ctx(ctx).Warn().Bool("requery", true).Msgf("Organization id %s requery", orgId)
if errors.Is(err, pgx.ErrNoRows) {
// Step 4: requery in case transaction isolation. This will happen in case of simultaneous transactions when
// the other transaction inserts the record after transaction is opened, therefore the conflict will trigger
// and no account will be actually retrieved from the database. The driver returns no rows error in this case.
zerolog.Ctx(ctx).Warn().Bool("requery", true).Msgf("Organization id %s account requery", orgId)
err = pgxscan.Get(ctx, db.Pool, result, query, orgId, account)
if err != nil {
return nil, fmt.Errorf("pgx requery error: %w", err)
}
} else if err != nil {
return nil, fmt.Errorf("pgx error: %w", err)
}

return result, nil
Expand Down

0 comments on commit 539560c

Please sign in to comment.