2022-08-26 09:28:38 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-07-19 21:40:57 +00:00
|
|
|
"context"
|
2022-08-26 09:28:38 +00:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2023-03-21 22:44:01 +00:00
|
|
|
func IsSerializedError(err error) bool {
|
|
|
|
var pqErr *pq.Error
|
|
|
|
if errors.As(err, &pqErr) {
|
|
|
|
return pqErr.Code.Name() == "serialization_failure"
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-26 09:28:38 +00:00
|
|
|
// IsUniqueViolation checks if the error is due to a unique violation.
|
|
|
|
// If one or more specific unique 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 unique violation.
|
|
|
|
func IsUniqueViolation(err error, uniqueConstraints ...UniqueConstraint) bool {
|
|
|
|
var pqErr *pq.Error
|
|
|
|
if errors.As(err, &pqErr) {
|
|
|
|
if pqErr.Code.Name() == "unique_violation" {
|
|
|
|
if len(uniqueConstraints) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, uc := range uniqueConstraints {
|
|
|
|
if pqErr.Constraint == string(uc) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2023-02-22 22:07:26 +00:00
|
|
|
|
2023-09-13 12:28:46 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-22 22:07:26 +00:00
|
|
|
// IsQueryCanceledError checks if the error is due to a query being canceled.
|
|
|
|
func IsQueryCanceledError(err error) bool {
|
|
|
|
var pqErr *pq.Error
|
|
|
|
if errors.As(err, &pqErr) {
|
2023-07-19 21:40:57 +00:00
|
|
|
return pqErr.Code == "57014" // query_canceled
|
|
|
|
} else if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return true
|
2023-02-22 22:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2023-03-23 19:09:13 +00:00
|
|
|
|
2023-07-28 15:57:23 +00:00
|
|
|
func IsWorkspaceAgentLogsLimitError(err error) bool {
|
2023-03-23 19:09:13 +00:00
|
|
|
var pqErr *pq.Error
|
|
|
|
if errors.As(err, &pqErr) {
|
2023-07-28 15:57:23 +00:00
|
|
|
return pqErr.Constraint == "max_logs_length" && pqErr.Table == "workspace_agents"
|
2023-03-23 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|