Update IsUniqueConstraint to check error codes instead of message text (#7385)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f968c56890
Коммит
66a4d01125
@@ -222,7 +222,7 @@ func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *mo
|
||||
}
|
||||
|
||||
if err := transaction.Insert(channel); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Name", "channels_name_teamid_key"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) {
|
||||
dupChannel := model.Channel{}
|
||||
s.GetMaster().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name = :Name", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name})
|
||||
if dupChannel.DeleteAt > 0 {
|
||||
@@ -257,7 +257,7 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel {
|
||||
}
|
||||
|
||||
if count, err := s.GetMaster().Update(channel); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Name", "channels_name_teamid_key"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) {
|
||||
dupChannel := model.Channel{}
|
||||
s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name= :Name AND DeleteAt > 0", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name})
|
||||
if dupChannel.DeleteAt > 0 {
|
||||
@@ -888,7 +888,7 @@ func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *mode
|
||||
}
|
||||
|
||||
if err := transaction.Insert(member); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"ChannelId", "channelmembers_pkey"}) {
|
||||
if IsUniqueConstraintError(err, []string{"ChannelId", "channelmembers_pkey"}) {
|
||||
result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.exists.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.save.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusInternalServerError)
|
||||
|
||||
@@ -154,7 +154,7 @@ func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *mo
|
||||
result := StoreResult{}
|
||||
|
||||
if err := transaction.Insert(preference); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"UserId", "preferences_pkey"}) {
|
||||
if IsUniqueConstraintError(err, []string{"UserId", "preferences_pkey"}) {
|
||||
result.Err = model.NewLocAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.exists.app_error", nil,
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error())
|
||||
} else {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func (s *SqlSupplier) ReactionSave(ctx context.Context, reaction *model.Reaction
|
||||
transaction.Rollback()
|
||||
|
||||
// We don't consider duplicated save calls as an error
|
||||
if !IsUniqueConstraintError(err.Error(), []string{"reactions_pkey", "PRIMARY"}) {
|
||||
if !IsUniqueConstraintError(err, []string{"reactions_pkey", "PRIMARY"}) {
|
||||
result.Err = model.NewLocAppError("SqlPreferenceStore.Save", "store.sql_reaction.save.save.app_error", nil, err.Error())
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -79,7 +79,7 @@ func (s SqlTeamStore) Save(team *model.Team) StoreChannel {
|
||||
}
|
||||
|
||||
if err := s.GetMaster().Insert(team); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Name", "teams_name_key"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Name", "teams_name_key"}) {
|
||||
result.Err = model.NewAppError("SqlTeamStore.Save", "store.sql_team.save.domain_exists.app_error", nil, "id="+team.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.app_error", nil, "id="+team.Id+", "+err.Error())
|
||||
@@ -514,7 +514,7 @@ func (s SqlTeamStore) SaveMember(member *model.TeamMember) StoreChannel {
|
||||
}
|
||||
|
||||
if err := s.GetMaster().Insert(member); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"TeamId", "teammembers_pkey", "PRIMARY"}) {
|
||||
if IsUniqueConstraintError(err, []string{"TeamId", "teammembers_pkey", "PRIMARY"}) {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", TEAM_MEMBER_EXISTS_ERROR, nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", "store.sql_team.save_member.save.app_error", nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error())
|
||||
|
||||
@@ -107,9 +107,9 @@ func (us SqlUserStore) Save(user *model.User) StoreChannel {
|
||||
}
|
||||
|
||||
if err := us.GetMaster().Insert(user); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique"}) {
|
||||
result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.email_exists.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else if IsUniqueConstraintError(err.Error(), []string{"Username", "users_username_key", "idx_users_username_unique"}) {
|
||||
} else if IsUniqueConstraintError(err, []string{"Username", "users_username_key", "idx_users_username_unique"}) {
|
||||
result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.username_exists.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlUserStore.Save", "store.sql_user.save.app_error", nil, "user_id="+user.Id+", "+err.Error())
|
||||
@@ -182,9 +182,9 @@ func (us SqlUserStore) Update(user *model.User, trustedUpdateData bool) StoreCha
|
||||
}
|
||||
|
||||
if count, err := us.GetMaster().Update(user); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique"}) {
|
||||
result.Err = model.NewAppError("SqlUserStore.Update", "store.sql_user.update.email_taken.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else if IsUniqueConstraintError(err.Error(), []string{"Username", "users_username_key", "idx_users_username_unique"}) {
|
||||
} else if IsUniqueConstraintError(err, []string{"Username", "users_username_key", "idx_users_username_unique"}) {
|
||||
result.Err = model.NewAppError("SqlUserStore.Update", "store.sql_user.update.username_taken.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlUserStore.Update", "store.sql_user.update.updating.app_error", nil, "user_id="+user.Id+", "+err.Error())
|
||||
@@ -321,7 +321,7 @@ func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *s
|
||||
query += " WHERE Id = :UserId"
|
||||
|
||||
if _, err := us.GetMaster().Exec(query, map[string]interface{}{"LastPasswordUpdate": updateAt, "UpdateAt": updateAt, "UserId": userId, "AuthService": service, "AuthData": authData, "Email": email}); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) {
|
||||
if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) {
|
||||
result.Err = model.NewLocAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.email_exists.app_error", map[string]interface{}{"Service": service, "Email": email}, "user_id="+userId+", "+err.Error())
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.app_error", nil, "id="+userId+", "+err.Error())
|
||||
|
||||
Ссылка в новой задаче
Block a user