Apply coredata rules: idempotent deletes and constraint checks
Delete methods no longer check RowsAffected — deletes are idempotent. PgError handlers now check both error code and constraint name to avoid misattributing violations. Also migrated remaining errors.As patterns to errors.AsType. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -138,8 +138,7 @@ VALUES (
|
||||
|
||||
result, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "authz_memberships_user_id_organization_id_key" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
|
||||
@@ -339,15 +338,11 @@ WHERE
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
result, err := conn.Exec(ctx, query, args)
|
||||
_, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete membership: %w", err)
|
||||
}
|
||||
|
||||
if result.RowsAffected() == 0 {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user