diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 6 | ||||
-rw-r--r-- | store/sql_command_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_compliance_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_emoji_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_job_store_test.go | 7 | ||||
-rw-r--r-- | store/sql_license_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_oauth_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_preference_store.go | 2 | ||||
-rw-r--r-- | store/sql_session_store_test.go | 3 | ||||
-rw-r--r-- | store/sql_supplier.go | 16 | ||||
-rw-r--r-- | store/sql_supplier_reactions.go | 2 | ||||
-rw-r--r-- | store/sql_team_store.go | 4 | ||||
-rw-r--r-- | store/sql_upgrade.go | 8 | ||||
-rw-r--r-- | store/sql_user_store.go | 10 |
14 files changed, 49 insertions, 24 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index b18e60f81..a6261b25b 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -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) diff --git a/store/sql_command_store_test.go b/store/sql_command_store_test.go index cd7db4c1d..fbd0f005e 100644 --- a/store/sql_command_store_test.go +++ b/store/sql_command_store_test.go @@ -4,8 +4,9 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestCommandStoreSave(t *testing.T) { diff --git a/store/sql_compliance_store_test.go b/store/sql_compliance_store_test.go index 4f4b01012..b84e35b48 100644 --- a/store/sql_compliance_store_test.go +++ b/store/sql_compliance_store_test.go @@ -4,9 +4,10 @@ package store import ( - "github.com/mattermost/platform/model" "testing" "time" + + "github.com/mattermost/platform/model" ) func TestSqlComplianceStore(t *testing.T) { diff --git a/store/sql_emoji_store_test.go b/store/sql_emoji_store_test.go index 25e0f7916..1e0a63f1e 100644 --- a/store/sql_emoji_store_test.go +++ b/store/sql_emoji_store_test.go @@ -4,9 +4,10 @@ package store import ( - "github.com/mattermost/platform/model" "testing" "time" + + "github.com/mattermost/platform/model" ) func TestEmojiSaveDelete(t *testing.T) { diff --git a/store/sql_job_store_test.go b/store/sql_job_store_test.go index ac0fe3aed..ac71519ec 100644 --- a/store/sql_job_store_test.go +++ b/store/sql_job_store_test.go @@ -6,8 +6,9 @@ package store import ( "testing" - "github.com/mattermost/platform/model" "time" + + "github.com/mattermost/platform/model" ) func TestJobSaveGet(t *testing.T) { @@ -17,7 +18,7 @@ func TestJobSaveGet(t *testing.T) { Id: model.NewId(), Type: model.NewId(), Status: model.NewId(), - Data: map[string]string{ + Data: map[string]string{ "Processed": "0", "Total": "12345", "LastProcessed": "abcd", @@ -187,7 +188,7 @@ func TestJobGetAllByStatus(t *testing.T) { Type: jobType, CreateAt: 1000, Status: status, - Data: map[string]string{ + Data: map[string]string{ "test": "data", }, }, diff --git a/store/sql_license_store_test.go b/store/sql_license_store_test.go index 737a79a60..f899e4d36 100644 --- a/store/sql_license_store_test.go +++ b/store/sql_license_store_test.go @@ -4,8 +4,9 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestLicenseStoreSave(t *testing.T) { diff --git a/store/sql_oauth_store_test.go b/store/sql_oauth_store_test.go index 4dc09b830..afd75d6e5 100644 --- a/store/sql_oauth_store_test.go +++ b/store/sql_oauth_store_test.go @@ -4,8 +4,9 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestOAuthStoreSaveApp(t *testing.T) { diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index e008e05eb..c2b9e3da8 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -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 { diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go index 2867adadf..cbf3ece43 100644 --- a/store/sql_session_store_test.go +++ b/store/sql_session_store_test.go @@ -4,8 +4,9 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestSessionStoreSave(t *testing.T) { 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 } diff --git a/store/sql_supplier_reactions.go b/store/sql_supplier_reactions.go index 30ca6beed..52697204c 100644 --- a/store/sql_supplier_reactions.go +++ b/store/sql_supplier_reactions.go @@ -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 { diff --git a/store/sql_team_store.go b/store/sql_team_store.go index d08242bc4..747bc0568 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -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()) diff --git a/store/sql_upgrade.go b/store/sql_upgrade.go index 7ec831a17..259cb0ab3 100644 --- a/store/sql_upgrade.go +++ b/store/sql_upgrade.go @@ -15,6 +15,7 @@ import ( ) const ( + VERSION_4_3_0 = "4.3.0" VERSION_4_2_0 = "4.2.0" VERSION_4_1_0 = "4.1.0" VERSION_4_0_0 = "4.0.0" @@ -300,3 +301,10 @@ func UpgradeDatabaseToVersion42(sqlStore SqlStore) { saveSchemaVersion(sqlStore, VERSION_4_2_0) } } + +func UpgradeDatabaseToVersion43(sqlStore SqlStore) { + // TODO: Uncomment following condition when version 4.3.0 is released + //if shouldPerformUpgrade(sqlStore, VERSION_4_2_0, VERSION_4_3_0) { + // saveSchemaVersion(sqlStore, VERSION_4_3_0) + //} +} diff --git a/store/sql_user_store.go b/store/sql_user_store.go index f37a4765e..7796b7cc2 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -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()) |