all repos

onasty @ e4abd99

a one-time notes service
2 files changed, 4 insertions(+), 7 deletions(-)
fix(noterepo): check if the .Create violates the unique constraint (#146)

* fix(noterepo): check if the .Create violates the unique constraint

* refactor(psqlutl): require the constraint name for IsDuplicateErr()
Author: Smirnov Oleksandr ss2316544@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2025-06-25 14:01:47 +0300
Parent: babda7d
M internal/store/psql/noterepo/noterepo.go

@@ -85,7 +85,7 @@ return err

} _, err = s.db.Exec(ctx, query, args...) - if psqlutil.IsDuplicateErr(err) { + if psqlutil.IsDuplicateErr(err, "notes_slug_key") { return models.ErrNoteSlugIsAlreadyInUse }
M internal/store/psqlutil/psqlutil.go

@@ -31,14 +31,11 @@ return nil

} // IsDuplicateErr function that checks if the error is a duplicate key violation. -func IsDuplicateErr(err error, constraintName ...string) bool { +func IsDuplicateErr(err error, constraintName string) bool { var pgErr *pgconn.PgError if errors.As(err, &pgErr) { - if len(constraintName) == 0 || len(constraintName) == 1 { - return pgErr.Code == "23505" && // unique_violation - pgErr.ConstraintName == constraintName[0] - } - return pgErr.Code == "23505" // unique_violation + return pgErr.Code == "23505" && // unique_violation + pgErr.ConstraintName == constraintName } return false }