forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
45 lines (40 loc) · 1.12 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package gormsupport
import "github.com/lib/pq"
const (
errCheckViolation = "23514"
errUniqueViolation = "23505"
errForeignKeyViolation = "23503"
)
// IsCheckViolation returns true if the error is a violation of the given check
func IsCheckViolation(err error, constraintName string) bool {
if err == nil {
return false
}
pqError, ok := err.(*pq.Error)
if !ok {
return false
}
return pqError.Code == errCheckViolation && pqError.Constraint == constraintName
}
// IsUniqueViolation returns true if the error is a violation of the given unique index
func IsUniqueViolation(err error, indexName string) bool {
if err == nil {
return false
}
pqError, ok := err.(*pq.Error)
if !ok {
return false
}
return pqError.Code == errUniqueViolation && pqError.Constraint == indexName
}
// IsForeignKeyViolation returns true if the error is a violation of the given foreign key index
func IsForeignKeyViolation(err error, indexName string) bool {
if err == nil {
return false
}
pqError, ok := err.(*pq.Error)
if !ok {
return false
}
return pqError.Code == errForeignKeyViolation && pqError.Constraint == indexName
}