Files
2026-11-29 19:35:13 +01:00

104 lines
3.1 KiB
Go

package repository
import (
"errors"
"strings"
"github.com/jackc/pgx/v5/pgconn"
"github.com/mattn/go-sqlite3"
"gorm.io/gorm"
)
// UniqueKey names a unique constraint for IsUniqueViolation, per engine.
// SQLite is the table.column text sqlite puts in its message, such as
// campaign_recipients.lure_code. Postgres is the constraint or index name,
// such as idx_campaign_recipients_lure_code. An empty name matches any
// unique constraint of that engine.
type UniqueKey struct {
SQLite string
Postgres string
}
// AnyUniqueKey matches every unique constraint
var AnyUniqueKey = UniqueKey{}
// sqlite extended result codes of a unique violation
const (
sqliteConstraintPrimaryKey = 1555
sqliteConstraintUnique = 2067
)
// postgres SQLSTATE codes
const (
pgUniqueViolation = "23505"
pgSerializationFailure = "40001"
pgDeadlockDetected = "40P01"
pgLockNotAvailable = "55P03"
)
// sqliteUniqueMessage starts the message sqlite gives for a unique violation
const sqliteUniqueMessage = "UNIQUE constraint failed"
// IsUniqueViolation reports whether err is a unique constraint violation of
// the key. It reads the sqlite error code and message, and the postgres
// SQLSTATE and constraint name. A gorm.ErrDuplicatedKey that carries no
// driver error matches only AnyUniqueKey, as its constraint is unknown.
func IsUniqueViolation(err error, key UniqueKey) bool {
if err == nil {
return false
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code != pgUniqueViolation {
return false
}
return key.Postgres == "" || pgErr.ConstraintName == key.Postgres
}
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
code := int(sqliteErr.ExtendedCode)
if code != sqliteConstraintUnique && code != sqliteConstraintPrimaryKey {
return false
}
return key.SQLite == "" || strings.Contains(sqliteErr.Error(), key.SQLite)
}
if errors.Is(err, gorm.ErrDuplicatedKey) {
return key == AnyUniqueKey
}
// an error that lost the driver type keeps the sqlite message
message := err.Error()
return strings.Contains(message, sqliteUniqueMessage) &&
(key.SQLite == "" || strings.Contains(message, key.SQLite))
}
// IsRetryable reports whether a write failed only because another writer
// held the data, so the same write is worth running again: sqlite busy or
// locked, postgres serialization failure, deadlock or lock not available. On
// postgres the transaction is rolled back and the whole transaction must run
// again, not one statement of it.
func IsRetryable(err error) bool {
if err == nil {
return false
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case pgSerializationFailure, pgDeadlockDetected, pgLockNotAvailable:
return true
}
return false
}
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Code == sqlite3.ErrBusy || sqliteErr.Code == sqlite3.ErrLocked
}
return false
}
// isLockNotAvailable reports whether a postgres statement gave up waiting
// for a lock, as after lock_timeout
func isLockNotAvailable(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == pgLockNotAvailable
}