mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat(coderd/database): generate foreign key constraints and add database.IsForeignKeyViolation (#9657)
* feat(coderd/database): generate foreign key constraints, add database.IsForeignKeyViolation * address PR comments
This commit is contained in:
@ -37,6 +37,28 @@ func IsUniqueViolation(err error, uniqueConstraints ...UniqueConstraint) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsForeignKeyViolation checks if the error is due to a foreign key violation.
|
||||
// If one or more specific foreign key constraints are given as arguments,
|
||||
// the error must be caused by one of them. If no constraints are given,
|
||||
// this function returns true for any foreign key violation.
|
||||
func IsForeignKeyViolation(err error, foreignKeyConstraints ...ForeignKeyConstraint) bool {
|
||||
var pqErr *pq.Error
|
||||
if errors.As(err, &pqErr) {
|
||||
if pqErr.Code.Name() == "foreign_key_violation" {
|
||||
if len(foreignKeyConstraints) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, fc := range foreignKeyConstraints {
|
||||
if pqErr.Constraint == string(fc) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsQueryCanceledError checks if the error is due to a query being canceled.
|
||||
func IsQueryCanceledError(err error) bool {
|
||||
var pqErr *pq.Error
|
||||
|
Reference in New Issue
Block a user