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:
Émile Ré
2026-05-20 09:44:00 +04:00
parent 9156d6a16a
commit 46c6068559
22 changed files with 34 additions and 81 deletions

View File

@@ -250,9 +250,8 @@ VALUES (
_, err := conn.Exec(ctx, q, args)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
if pgErr.Code == "23505" && pgErr.ConstraintName == "states_of_applicability_contr_state_of_applicability_id_con_key" {
return ErrResourceAlreadyExists
}
}
@@ -382,15 +381,11 @@ WHERE
args := pgx.StrictNamedArgs{"id": applicabilityStatementID}
maps.Copy(args, scope.SQLArguments())
result, err := conn.Exec(ctx, q, args)
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete applicability statement: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}