diff options
author | Corey Hulen <corey@hulen.com> | 2015-07-23 11:21:54 -0800 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2015-07-23 11:21:54 -0800 |
commit | b813234f4cc38e72bb94556d3a4acdcde5071468 (patch) | |
tree | f9a0eb8f9596c91531c6997617b4dd66e05dd317 /store/sql_channel_store.go | |
parent | 32e8dddd0cd8532e45f89290a154d5d7911c16dd (diff) | |
parent | 98751a4f65a656839dc3724e72e672b585cb18c9 (diff) | |
download | chat-b813234f4cc38e72bb94556d3a4acdcde5071468.tar.gz chat-b813234f4cc38e72bb94556d3a4acdcde5071468.tar.bz2 chat-b813234f4cc38e72bb94556d3a4acdcde5071468.zip |
Merge pull request #170 from mattermost/mm-1420
Fixes mm-1420 adding initial support for postgres
Diffstat (limited to 'store/sql_channel_store.go')
-rw-r--r-- | store/sql_channel_store.go | 110 |
1 files changed, 65 insertions, 45 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 6820b2326..8961d5d97 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -6,7 +6,6 @@ package store import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" - "strings" ) type SqlChannelStore struct { @@ -37,11 +36,14 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore { } func (s SqlChannelStore) UpgradeSchemaIfNeeded() { - s.CreateColumnIfNotExists("ChannelMembers", "LastUpdateAt", "NotifyLevel", "bigint(20)", "0") // Remove after 6/7/2015 prod push } func (s SqlChannelStore) CreateIndexesIfNotExists() { - s.CreateIndexIfNotExists("idx_team_id", "Channels", "TeamId") + s.CreateIndexIfNotExists("idx_channels_team_id", "Channels", "TeamId") + s.CreateIndexIfNotExists("idx_channels_name", "Channels", "Name") + + s.CreateIndexIfNotExists("idx_channelmembers_channel_id", "ChannelMembers", "ChannelId") + s.CreateIndexIfNotExists("idx_channelmembers_user_id", "ChannelMembers", "UserId") } func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel { @@ -65,7 +67,7 @@ func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel { return } - if count, err := s.GetMaster().SelectInt("SELECT COUNT(0) FROM Channels WHERE TeamId = ? AND DeleteAt = 0 AND (Type ='O' || Type ='P')", channel.TeamId); err != nil { + if count, err := s.GetMaster().SelectInt("SELECT COUNT(0) FROM Channels WHERE TeamId = :TeamId AND DeleteAt = 0 AND (Type = 'O' OR Type = 'P')", map[string]interface{}{"TeamId": channel.TeamId}); err != nil { result.Err = model.NewAppError("SqlChannelStore.Save", "Failed to get current channel count", "teamId="+channel.TeamId+", "+err.Error()) storeChannel <- result close(storeChannel) @@ -78,9 +80,9 @@ func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel { } if err := s.GetMaster().Insert(channel); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { + if IsUniqueConstraintError(err.Error(), "Name", "channels_name_teamid_key") { dupChannel := model.Channel{} - s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) + 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 { result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name was previously created", "id="+channel.Id+", "+err.Error()) } else { @@ -116,7 +118,7 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel { } if count, err := s.GetMaster().Update(channel); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { + if IsUniqueConstraintError(err.Error(), "Name", "channels_name_teamid_key") { dupChannel := model.Channel{} s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) if dupChannel.DeleteAt > 0 { @@ -167,7 +169,7 @@ func (s SqlChannelStore) Delete(channelId string, time int64) StoreChannel { go func() { result := StoreResult{} - _, err := s.GetMaster().Exec("Update Channels SET DeleteAt = ?, UpdateAt = ? WHERE Id = ?", time, time, channelId) + _, err := s.GetMaster().Exec("Update Channels SET DeleteAt = :Time, UpdateAt = :Time WHERE Id = :ChannelId", map[string]interface{}{"Time": time, "ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.Delete", "We couldn't delete the channel", "id="+channelId+", err="+err.Error()) } @@ -191,7 +193,7 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string) StoreChannel result := StoreResult{} var data []channelWithMember - _, err := s.GetReplica().Select(&data, "SELECT * FROM Channels, ChannelMembers WHERE Id = ChannelId AND TeamId = ? AND UserId = ? AND DeleteAt = 0 ORDER BY DisplayName", teamId, userId) + _, err := s.GetReplica().Select(&data, "SELECT * FROM Channels, ChannelMembers WHERE Id = ChannelId AND TeamId = :TeamId AND UserId = :UserId AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetChannels", "We couldn't get the channels", "teamId="+teamId+", userId="+userId+", err="+err.Error()) @@ -230,8 +232,8 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string) StoreChan FROM Channels WHERE - TeamId = ? - AND Type IN ("O") + TeamId = :TeamId1 + AND Type IN ('O') AND DeleteAt = 0 AND Id NOT IN (SELECT Channels.Id @@ -240,11 +242,11 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string) StoreChan ChannelMembers WHERE Id = ChannelId - AND TeamId = ? - AND UserId = ? + AND TeamId = :TeamId2 + AND UserId = :UserId AND DeleteAt = 0) ORDER BY DisplayName`, - teamId, teamId, userId) + map[string]interface{}{"TeamId1": teamId, "TeamId2": teamId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetMoreChannels", "We couldn't get the channels", "teamId="+teamId+", userId="+userId+", err="+err.Error()) @@ -267,7 +269,7 @@ func (s SqlChannelStore) GetByName(teamId string, name string) StoreChannel { channel := model.Channel{} - if err := s.GetReplica().SelectOne(&channel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt = 0", teamId, name); err != nil { + if err := s.GetReplica().SelectOne(&channel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name= :Name AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil { result.Err = model.NewAppError("SqlChannelStore.GetByName", "We couldn't find the existing channel", "teamId="+teamId+", "+"name="+name+", "+err.Error()) } else { result.Data = &channel @@ -293,7 +295,7 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel { } if err := s.GetMaster().Insert(member); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'ChannelId'") { + if IsUniqueConstraintError(err.Error(), "ChannelId", "channelmembers_pkey") { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "A channel member with that id already exists", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error()) } else { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "We couldn't save the channel member", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error()) @@ -316,7 +318,7 @@ func (s SqlChannelStore) GetMembers(channelId string) StoreChannel { result := StoreResult{} var members []model.ChannelMember - _, err := s.GetReplica().Select(&members, "SELECT * FROM ChannelMembers WHERE ChannelId = ?", channelId) + _, err := s.GetReplica().Select(&members, "SELECT * FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetMembers", "We couldn't get the channel members", "channel_id="+channelId+err.Error()) } else { @@ -337,7 +339,7 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) StoreChannel result := StoreResult{} var member model.ChannelMember - err := s.GetReplica().SelectOne(&member, "SELECT * FROM ChannelMembers WHERE ChannelId = ? AND UserId = ?", channelId, userId) + err := s.GetReplica().SelectOne(&member, "SELECT * FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetMember", "We couldn't get the channel member", "channel_id="+channelId+"user_id="+userId+","+err.Error()) } else { @@ -358,7 +360,7 @@ func (s SqlChannelStore) GetExtraMembers(channelId string, limit int) StoreChann result := StoreResult{} var members []model.ExtraMember - _, err := s.GetReplica().Select(&members, "SELECT Id, Nickname, Email, ChannelMembers.Roles, Username FROM ChannelMembers, Users WHERE ChannelMembers.UserId = Users.Id AND ChannelId = ? LIMIT ?", channelId, limit) + _, err := s.GetReplica().Select(&members, "SELECT Id, Nickname, Email, ChannelMembers.Roles, Username FROM ChannelMembers, Users WHERE ChannelMembers.UserId = Users.Id AND ChannelId = :ChannelId LIMIT :Limit", map[string]interface{}{"ChannelId": channelId, "Limit": limit}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetExtraMembers", "We couldn't get the extra info for channel members", "channel_id="+channelId+", "+err.Error()) } else { @@ -381,7 +383,7 @@ func (s SqlChannelStore) RemoveMember(channelId string, userId string) StoreChan go func() { result := StoreResult{} - _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = ? AND UserId = ?", channelId, userId) + _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "We couldn't remove the channel member", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) } @@ -407,11 +409,11 @@ func (s SqlChannelStore) CheckPermissionsTo(teamId string, channelId string, use ChannelMembers WHERE Channels.Id = ChannelMembers.ChannelId - AND Channels.TeamId = ? + AND Channels.TeamId = :TeamId AND Channels.DeleteAt = 0 - AND ChannelMembers.ChannelId = ? - AND ChannelMembers.UserId = ?`, - teamId, channelId, userId) + AND ChannelMembers.ChannelId = :ChannelId + AND ChannelMembers.UserId = :UserId`, + map[string]interface{}{"TeamId": teamId, "ChannelId": channelId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.CheckPermissionsTo", "We couldn't check the permissions", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) } else { @@ -439,11 +441,11 @@ func (s SqlChannelStore) CheckPermissionsToByName(teamId string, channelName str ChannelMembers WHERE Channels.Id = ChannelMembers.ChannelId - AND Channels.TeamId = ? - AND Channels.Name = ? + AND Channels.TeamId = :TeamId + AND Channels.Name = :Name AND Channels.DeleteAt = 0 - AND ChannelMembers.UserId = ?`, - teamId, channelName, userId) + AND ChannelMembers.UserId = :UserId`, + map[string]interface{}{"TeamId": teamId, "Name": channelName, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.CheckPermissionsToByName", "We couldn't check the permissions", "channel_id="+channelName+", user_id="+userId+", "+err.Error()) } else { @@ -469,10 +471,10 @@ func (s SqlChannelStore) CheckOpenChannelPermissions(teamId string, channelId st FROM Channels WHERE - Channels.Id = ? - AND Channels.TeamId = ? - AND Channels.Type = ?`, - channelId, teamId, model.CHANNEL_OPEN) + Channels.Id = :ChannelId + AND Channels.TeamId = :TeamId + AND Channels.Type = :ChannelType`, + map[string]interface{}{"ChannelId": channelId, "TeamId": teamId, "ChannelType": model.CHANNEL_OPEN}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.CheckOpenChannelPermissions", "We couldn't check the permissions", "channel_id="+channelId+", "+err.Error()) } else { @@ -492,8 +494,24 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto go func() { result := StoreResult{} - _, err := s.GetMaster().Exec( - `UPDATE + var query string + + if utils.Cfg.SqlSettings.DriverName == "postgres" { + query = `UPDATE + ChannelMembers + SET + MentionCount = 0, + MsgCount = Channels.TotalMsgCount, + LastViewedAt = Channels.LastPostAt, + LastUpdateAt = Channels.LastPostAt + FROM + Channels + WHERE + Channels.Id = ChannelMembers.ChannelId + AND UserId = :UserId + AND ChannelId = :ChannelId` + } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + query = `UPDATE ChannelMembers, Channels SET ChannelMembers.MentionCount = 0, @@ -502,9 +520,11 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto ChannelMembers.LastUpdateAt = Channels.LastPostAt WHERE Channels.Id = ChannelMembers.ChannelId - AND UserId = ? - AND ChannelId = ?`, - userId, channelId) + AND UserId = :UserId + AND ChannelId = :ChannelId` + } + + _, err := s.GetMaster().Exec(query, map[string]interface{}{"ChannelId": channelId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.UpdateLastViewedAt", "We couldn't update the last viewed at time", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) } @@ -528,9 +548,9 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) SET MentionCount = MentionCount + 1 WHERE - UserId = ? - AND ChannelId = ?`, - userId, channelId) + UserId = :UserId + AND ChannelId = :ChannelId`, + map[string]interface{}{"ChannelId": channelId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.IncrementMentionCount", "We couldn't increment the mention count", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) } @@ -554,12 +574,12 @@ func (s SqlChannelStore) UpdateNotifyLevel(channelId, userId, notifyLevel string `UPDATE ChannelMembers SET - NotifyLevel = ?, - LastUpdateAt = ? + NotifyLevel = :NotifyLevel, + LastUpdateAt = :LastUpdateAt WHERE - UserId = ? - AND ChannelId = ?`, - notifyLevel, updateAt, userId, channelId) + UserId = :UserId + AND ChannelId = :ChannelId`, + map[string]interface{}{"ChannelId": channelId, "UserId": userId, "NotifyLevel": notifyLevel, "LastUpdateAt": updateAt}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.UpdateNotifyLevel", "We couldn't update the notify level", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) } |