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

@@ -233,15 +233,11 @@ WHERE %s AND id = @id
args := pgx.StrictNamedArgs{"id": c.ID}
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 access_review_campaign: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

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
}

View File

@@ -212,9 +212,8 @@ RETURNING rank;
err := conn.QueryRow(ctx, q, args).Scan(&c.Rank)
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 == "compliance_frameworks_trust_center_id_framework_id_key" {
return ErrResourceAlreadyExists
}
}

View File

@@ -294,15 +294,11 @@ WHERE %s AND id = @id
args := pgx.StrictNamedArgs{"id": c.ID}
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 connector: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -305,9 +305,8 @@ INSERT INTO document_version_approval_decisions (
_, 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 == "document_version_approval_decisions_quorum_id_approver_id_key" {
return ErrResourceAlreadyExists
}
}

View File

@@ -290,9 +290,8 @@ INSERT INTO document_version_approval_quorums (
_, err := conn.Exec(ctx, query, 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 == "document_one_pending_quorum_idx" {
return ErrResourceAlreadyExists
}
}

View File

@@ -214,7 +214,7 @@ VALUES (
}
if _, err := conn.Exec(ctx, q, args); err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "mailing_list_subscribers_mailing_list_id_email_key" {
return ErrResourceAlreadyExists
}
@@ -278,15 +278,11 @@ WHERE
args := pgx.StrictNamedArgs{"mailing_list_subscriber_id": cns.ID}
maps.Copy(args, scope.SQLArguments())
tag, err := conn.Exec(ctx, q, args)
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete mailing list subscriber: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -158,15 +158,11 @@ WHERE
}
maps.Copy(args, scope.SQLArguments())
tag, err := conn.Exec(ctx, q, args)
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete mailing list update: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -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
}

View File

@@ -1219,7 +1219,7 @@ VALUES (
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "idx_profiles_identity_id_organization_id" {
return ErrResourceAlreadyExists
}
@@ -1409,14 +1409,10 @@ WHERE
args := pgx.StrictNamedArgs{"profile_id": profileID}
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 profile: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -367,7 +367,7 @@ INSERT INTO iam_oauth2_consents (
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "iam_oauth2_consents_pkey" {
return ErrResourceAlreadyExists
}

View File

@@ -191,7 +191,7 @@ VALUES (@id, @tenant_id, @organization_id, @name, @description, @created_at, @up
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessments_pkey" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk assessment: %w", err)

View File

@@ -242,7 +242,7 @@ INSERT INTO risk_assessment_nodes (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_nodes_unique_name" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk assessment node: %w", err)

View File

@@ -248,7 +248,7 @@ INSERT INTO risk_assessment_processes (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_processes_unique_name" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk assessment process: %w", err)

View File

@@ -351,7 +351,7 @@ INSERT INTO risk_assessment_scenarios (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_scenarios_pkey" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk scenario: %w", err)

View File

@@ -60,7 +60,7 @@ INSERT INTO risk_assessment_scenario_risks (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_scenario_risks_pkey" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk scenario risk: %w", err)

View File

@@ -60,7 +60,7 @@ INSERT INTO risk_assessment_scenario_threats (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_scenario_threats_pkey" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk scenario threat: %w", err)

View File

@@ -196,7 +196,7 @@ INSERT INTO risk_assessment_scopes (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_scopes_pkey" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk assessment scope: %w", err)

View File

@@ -248,7 +248,7 @@ INSERT INTO risk_assessment_threats (
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "23505" && pgErr.ConstraintName == "risk_assessment_threats_unique_name" {
return ErrResourceAlreadyExists
}
return fmt.Errorf("cannot insert risk threat: %w", err)

View File

@@ -436,14 +436,10 @@ WHERE
args := pgx.StrictNamedArgs{"id": s.ID}
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 scim_bridge: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -227,9 +227,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 == "statements_of_applicability_document_id_key" {
return ErrResourceAlreadyExists
}
}
@@ -268,9 +267,8 @@ WHERE
result, 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 == "statements_of_applicability_document_id_key" {
return ErrResourceAlreadyExists
}
}
@@ -303,15 +301,11 @@ WHERE
}
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 statement_of_applicability: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -402,14 +402,10 @@ WHERE %s
}
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 webhook subscription: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}