first commit
This commit is contained in:
19
pkg/utils/db_utils.go
Normal file
19
pkg/utils/db_utils.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// IsDuplicateKeyError checks if the error is a PostgreSQL duplicate key violation
|
||||
func IsDuplicateKeyError(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
// 23505 is the PostgreSQL error code for unique_violation
|
||||
return pgErr.Code == "23505"
|
||||
}
|
||||
// Fallback for other drivers or if error wrapping is different
|
||||
return strings.Contains(err.Error(), "duplicate key value violates unique constraint")
|
||||
}
|
||||
Reference in New Issue
Block a user