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

      
        86
        86
         

      
        87
        87
         	_, err = s.db.Exec(ctx, query, args...)

      
        88
        
        -	if psqlutil.IsDuplicateErr(err) {

      
        
        88
        +	if psqlutil.IsDuplicateErr(err, "notes_slug_key") {

      
        89
        89
         		return models.ErrNoteSlugIsAlreadyInUse

      
        90
        90
         	}

      
        91
        91
         

      
M internal/store/psqlutil/psqlutil.go
···
        31
        31
         }

      
        32
        32
         

      
        33
        33
         // IsDuplicateErr function that checks if the error is a duplicate key violation.

      
        34
        
        -func IsDuplicateErr(err error, constraintName ...string) bool {

      
        
        34
        +func IsDuplicateErr(err error, constraintName string) bool {

      
        35
        35
         	var pgErr *pgconn.PgError

      
        36
        36
         	if errors.As(err, &pgErr) {

      
        37
        
        -		if len(constraintName) == 0 || len(constraintName) == 1 {

      
        38
        
        -			return pgErr.Code == "23505" && // unique_violation

      
        39
        
        -				pgErr.ConstraintName == constraintName[0]

      
        40
        
        -		}

      
        41
        
        -		return pgErr.Code == "23505" // unique_violation

      
        
        37
        +		return pgErr.Code == "23505" && // unique_violation

      
        
        38
        +			pgErr.ConstraintName == constraintName

      
        42
        39
         	}

      
        43
        40
         	return false

      
        44
        41
         }