summaryrefslogtreecommitdiffstats
path: root/store/sql_supplier.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-09-06 14:00:56 -0400
committerGitHub <noreply@github.com>2017-09-06 14:00:56 -0400
commit66a4d0112587608533aa744578d2db18bd88db56 (patch)
treecf1b5410162e156d5336a5c1666e7a66a405d083 /store/sql_supplier.go
parentf968c56890bd84295672ee0d46cc846cac2dbd47 (diff)
downloadchat-66a4d0112587608533aa744578d2db18bd88db56.tar.gz
chat-66a4d0112587608533aa744578d2db18bd88db56.tar.bz2
chat-66a4d0112587608533aa744578d2db18bd88db56.zip
Update IsUniqueConstraint to check error codes instead of message text (#7385)
Diffstat (limited to 'store/sql_supplier.go')
-rw-r--r--store/sql_supplier.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/store/sql_supplier.go b/store/sql_supplier.go
index 53153d911..2a91dbddf 100644
--- a/store/sql_supplier.go
+++ b/store/sql_supplier.go
@@ -16,6 +16,8 @@ import (
"time"
l4g "github.com/alecthomas/log4go"
+ "github.com/go-sql-driver/mysql"
+ "github.com/lib/pq"
"github.com/mattermost/gorp"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
@@ -661,11 +663,19 @@ func (ss *SqlSupplier) RemoveIndexIfExists(indexName string, tableName string) b
return true
}
-func IsUniqueConstraintError(err string, indexName []string) bool {
- unique := strings.Contains(err, "unique constraint") || strings.Contains(err, "Duplicate entry")
+func IsUniqueConstraintError(err error, indexName []string) bool {
+ unique := false
+ if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
+ unique = true
+ }
+
+ if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1062 {
+ unique = true
+ }
+
field := false
for _, contain := range indexName {
- if strings.Contains(err, contain) {
+ if strings.Contains(err.Error(), contain) {
field = true
break
}