diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 106 | ||||
-rw-r--r-- | store/sql_channel_store_test.go | 32 | ||||
-rw-r--r-- | store/sql_post_store.go | 4 | ||||
-rw-r--r-- | store/sql_post_store_test.go | 9 | ||||
-rw-r--r-- | store/sql_store.go | 22 | ||||
-rw-r--r-- | store/store.go | 2 |
6 files changed, 105 insertions, 70 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 877246fc3..3bbe7e716 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -4,6 +4,7 @@ package store import ( + l4g "code.google.com/p/log4go" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -30,13 +31,55 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore { tablem.ColMap("ChannelId").SetMaxSize(26) tablem.ColMap("UserId").SetMaxSize(26) tablem.ColMap("Roles").SetMaxSize(64) - tablem.ColMap("NotifyLevel").SetMaxSize(20) + tablem.ColMap("NotifyProps").SetMaxSize(2000) } return s } func (s SqlChannelStore) UpgradeSchemaIfNeeded() { + if s.CreateColumnIfNotExists("ChannelMembers", "NotifyProps", "varchar(2000)", "varchar(2000)", "{}") { + // populate NotifyProps from existing NotifyLevel field + + // set default values + _, err := s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = CONCAT('{"desktop":"', CONCAT(NotifyLevel, '","mark_unread":"` + model.CHANNEL_MARK_UNREAD_ALL + `"}'))`) + if err != nil { + l4g.Error("Unable to set default values for ChannelMembers.NotifyProps") + l4g.Error(err.Error()) + } + + // assume channels with all notifications enabled are just using the default settings + _, err = s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = '{"desktop":"` + model.CHANNEL_NOTIFY_DEFAULT + `","mark_unread":"` + model.CHANNEL_MARK_UNREAD_ALL + `"}' + WHERE + NotifyLevel = '` + model.CHANNEL_NOTIFY_ALL + `'`) + if err != nil { + l4g.Error("Unable to set values for ChannelMembers.NotifyProps when members previously had notifyLevel=all") + l4g.Error(err.Error()) + } + + // set quiet mode channels to have no notifications and only mark the channel unread on mentions + _, err = s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = '{"desktop":"` + model.CHANNEL_NOTIFY_NONE + `","mark_unread":"` + model.CHANNEL_MARK_UNREAD_MENTION + `"}' + WHERE + NotifyLevel = 'quiet'`) + if err != nil { + l4g.Error("Unable to set values for ChannelMembers.NotifyProps when members previously had notifyLevel=quiet") + l4g.Error(err.Error()) + } + + s.RemoveColumnIfExists("ChannelMembers", "NotifyLevel") + } } func (s SqlChannelStore) CreateIndexesIfNotExists() { @@ -386,6 +429,34 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel { return storeChannel } +func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + member.PreUpdate() + + if result.Err = member.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + if _, err := s.GetMaster().Update(member); err != nil { + result.Err = model.NewAppError("SqlChannelStore.UpdateMember", "We encounted an error updating the channel member", + "channel_id="+member.ChannelId+", "+"user_id="+member.UserId+", "+err.Error()) + } else { + result.Data = member + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetMembers(channelId string) StoreChannel { storeChannel := make(StoreChannel) @@ -583,7 +654,7 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto var query string - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { query = `UPDATE ChannelMembers SET @@ -597,7 +668,7 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelId string, userId string) Sto Channels.Id = ChannelMembers.ChannelId AND UserId = :UserId AND ChannelId = :ChannelId` - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { query = `UPDATE ChannelMembers, Channels SET @@ -649,35 +720,6 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) return storeChannel } -func (s SqlChannelStore) UpdateNotifyLevel(channelId, userId, notifyLevel string) StoreChannel { - storeChannel := make(StoreChannel) - - go func() { - result := StoreResult{} - - updateAt := model.GetMillis() - - _, err := s.GetMaster().Exec( - `UPDATE - ChannelMembers - SET - NotifyLevel = :NotifyLevel, - LastUpdateAt = :LastUpdateAt - WHERE - 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()) - } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel -} - func (s SqlChannelStore) GetForExport(teamId string) StoreChannel { storeChannel := make(StoreChannel) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index dabe39904..b6d05684b 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -135,13 +135,13 @@ func TestChannelStoreDelete(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o2.Id m2.UserId = m1.UserId - m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) if r := <-store.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { @@ -222,13 +222,13 @@ func TestChannelMemberStore(t *testing.T) { o1 := model.ChannelMember{} o1.ChannelId = c1.Id o1.UserId = u1.Id - o1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + o1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&o1)) o2 := model.ChannelMember{} o2.ChannelId = c1.Id o2.UserId = u2.Id - o2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + o2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&o2)) c1t2 := (<-store.Channel().Get(c1.Id)).Data.(*model.Channel) @@ -291,7 +291,7 @@ func TestChannelStorePermissionsTo(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) count := (<-store.Channel().CheckPermissionsTo(o1.TeamId, o1.Id, m1.UserId)).Data.(int64) @@ -371,19 +371,19 @@ func TestChannelStoreGetChannels(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() - m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() - m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) cresult := <-store.Channel().GetChannels(o1.TeamId, m1.UserId) @@ -414,19 +414,19 @@ func TestChannelStoreGetMoreChannels(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() - m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() - m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) o3 := model.Channel{} @@ -482,19 +482,19 @@ func TestChannelStoreGetChannelCounts(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() - m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() - m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) cresult := <-store.Channel().GetChannelCounts(o1.TeamId, m1.UserId) @@ -523,7 +523,7 @@ func TestChannelStoreUpdateLastViewedAt(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().UpdateLastViewedAt(m1.ChannelId, m1.UserId)).Err @@ -551,7 +551,7 @@ func TestChannelStoreIncrementMentionCount(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 21e8e9d00..668f45fbb 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -418,7 +418,7 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht var posts []*model.Post - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { // Parse text for wildcards if wildcard, err := regexp.Compile("\\*($| )"); err == nil { @@ -451,7 +451,7 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error()) } - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { searchQuery := fmt.Sprintf(`SELECT * FROM diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index bc1cb2c2c..6a6364dc8 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -484,7 +484,7 @@ func TestPostStoreSearch(t *testing.T) { m1 := model.ChannelMember{} m1.ChannelId = c1.Id m1.UserId = userId - m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) c2 := &model.Channel{} @@ -530,13 +530,6 @@ func TestPostStoreSearch(t *testing.T) { t.Fatal("returned wrong serach result") } - // if utils.Cfg.SqlSettings.DriverName == "mysql" { - // r2 := (<-store.Post().Search(teamId, userId, "new york", false)).Data.(*model.PostList) - // if len(r2.Order) >= 1 && r2.Order[0] != o2.Id { - // t.Fatal("returned wrong serach result") - // } - // } - r3 := (<-store.Post().Search(teamId, userId, "new", false)).Data.(*model.PostList) if len(r3.Order) != 2 && r3.Order[0] != o1.Id { t.Fatal("returned wrong serach result") diff --git a/store/sql_store.go b/store/sql_store.go index 6dcf2e8cd..3ef9cfbc4 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -160,9 +160,9 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle if driver == "sqlite3" { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.SqliteDialect{}} - } else if driver == "mysql" { + } else if driver == model.DATABASE_DRIVER_MYSQL { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8MB4"}} - } else if driver == "postgres" { + } else if driver == model.DATABASE_DRIVER_POSTGRES { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.PostgresDialect{}} } else { l4g.Critical("Failed to create dialect specific driver") @@ -183,7 +183,7 @@ func (ss SqlStore) GetCurrentSchemaVersion() string { } func (ss SqlStore) DoesTableExist(tableName string) bool { - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { count, err := ss.GetMaster().SelectInt( `SELECT count(relname) FROM pg_class WHERE relname=$1`, strings.ToLower(tableName), @@ -197,7 +197,7 @@ func (ss SqlStore) DoesTableExist(tableName string) bool { return count > 0 - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { count, err := ss.GetMaster().SelectInt( `SELECT @@ -228,7 +228,7 @@ func (ss SqlStore) DoesTableExist(tableName string) bool { } func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool { - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { count, err := ss.GetMaster().SelectInt( `SELECT COUNT(0) FROM pg_attribute @@ -251,7 +251,7 @@ func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool { return count > 0 - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { count, err := ss.GetMaster().SelectInt( `SELECT @@ -288,7 +288,7 @@ func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, return false } - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType + " DEFAULT '" + defaultValue + "'") if err != nil { l4g.Critical("Failed to create column %v", err) @@ -298,7 +298,7 @@ func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, return true - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType + " DEFAULT '" + defaultValue + "'") if err != nil { l4g.Critical("Failed to create column %v", err) @@ -334,7 +334,7 @@ func (ss SqlStore) RemoveColumnIfExists(tableName string, columnName string) boo // func (ss SqlStore) RenameColumnIfExists(tableName string, oldColumnName string, newColumnName string, colType string) bool { // // XXX TODO FIXME this should be removed after 0.6.0 -// if utils.Cfg.SqlSettings.DriverName == "postgres" { +// if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { // return false // } @@ -363,7 +363,7 @@ func (ss SqlStore) CreateFullTextIndexIfNotExists(indexName string, tableName st func (ss SqlStore) createIndexIfNotExists(indexName string, tableName string, columnName string, fullText bool) { - if utils.Cfg.SqlSettings.DriverName == "postgres" { + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { _, err := ss.GetMaster().SelectStr("SELECT $1::regclass", indexName) // It should fail if the index does not exist if err == nil { @@ -383,7 +383,7 @@ func (ss SqlStore) createIndexIfNotExists(indexName string, tableName string, co time.Sleep(time.Second) panic("Failed to create index " + err.Error()) } - } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { count, err := ss.GetMaster().SelectInt("SELECT COUNT(0) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = ? AND index_name = ?", tableName, indexName) if err != nil { diff --git a/store/store.go b/store/store.go index 23580f452..887913bc6 100644 --- a/store/store.go +++ b/store/store.go @@ -62,6 +62,7 @@ type ChannelStore interface { GetForExport(teamId string) StoreChannel SaveMember(member *model.ChannelMember) StoreChannel + UpdateMember(member *model.ChannelMember) StoreChannel GetMembers(channelId string) StoreChannel GetMember(channelId string, userId string) StoreChannel RemoveMember(channelId string, userId string) StoreChannel @@ -71,7 +72,6 @@ type ChannelStore interface { CheckPermissionsToByName(teamId string, channelName string, userId string) StoreChannel UpdateLastViewedAt(channelId string, userId string) StoreChannel IncrementMentionCount(channelId string, userId string) StoreChannel - UpdateNotifyLevel(channelId string, userId string, notifyLevel string) StoreChannel } type PostStore interface { |