diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_session_store.go | 18 | ||||
-rw-r--r-- | store/sql_session_store_test.go | 23 | ||||
-rw-r--r-- | store/sql_store.go | 42 | ||||
-rw-r--r-- | store/sql_team_store.go | 5 | ||||
-rw-r--r-- | store/sql_user_store.go | 19 | ||||
-rw-r--r-- | store/sql_user_store_test.go | 20 | ||||
-rw-r--r-- | store/store.go | 4 |
7 files changed, 107 insertions, 24 deletions
diff --git a/store/sql_session_store.go b/store/sql_session_store.go index c1d2c852b..22411389d 100644 --- a/store/sql_session_store.go +++ b/store/sql_session_store.go @@ -140,6 +140,24 @@ func (me SqlSessionStore) Remove(sessionIdOrToken string) StoreChannel { return storeChannel } +func (me SqlSessionStore) RemoveAllSessionsForTeam(teamId string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) + if err != nil { + result.Err = model.NewAppError("SqlSessionStore.RemoveAllSessionsForTeam", "We couldn't remove all the sessions for the team", "id="+teamId+", err="+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (me SqlSessionStore) CleanUpExpiredSessions(userId string) StoreChannel { storeChannel := make(StoreChannel) diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go index 4ae680556..3d8aafe25 100644 --- a/store/sql_session_store_test.go +++ b/store/sql_session_store_test.go @@ -80,6 +80,29 @@ func TestSessionRemove(t *testing.T) { } } +func TestSessionRemoveAll(t *testing.T) { + Setup() + + s1 := model.Session{} + s1.UserId = model.NewId() + s1.TeamId = model.NewId() + Must(store.Session().Save(&s1)) + + if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil { + t.Fatal(rs1.Err) + } else { + if rs1.Data.(*model.Session).Id != s1.Id { + t.Fatal("should match") + } + } + + Must(store.Session().RemoveAllSessionsForTeam(s1.TeamId)) + + if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil { + t.Fatal("should have been removed") + } +} + func TestSessionRemoveToken(t *testing.T) { Setup() diff --git a/store/sql_store.go b/store/sql_store.go index 98703841a..6dcf2e8cd 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -51,11 +51,18 @@ func NewSqlStore() Store { utils.Cfg.SqlSettings.DataSource, utils.Cfg.SqlSettings.MaxIdleConns, utils.Cfg.SqlSettings.MaxOpenConns, utils.Cfg.SqlSettings.Trace) - sqlStore.replicas = make([]*gorp.DbMap, len(utils.Cfg.SqlSettings.DataSourceReplicas)) - for i, replica := range utils.Cfg.SqlSettings.DataSourceReplicas { - sqlStore.replicas[i] = setupConnection(fmt.Sprintf("replica-%v", i), utils.Cfg.SqlSettings.DriverName, replica, + if len(utils.Cfg.SqlSettings.DataSourceReplicas) == 0 { + sqlStore.replicas = make([]*gorp.DbMap, 1) + sqlStore.replicas[0] = setupConnection(fmt.Sprintf("replica-%v", 0), utils.Cfg.SqlSettings.DriverName, utils.Cfg.SqlSettings.DataSource, utils.Cfg.SqlSettings.MaxIdleConns, utils.Cfg.SqlSettings.MaxOpenConns, utils.Cfg.SqlSettings.Trace) + } else { + sqlStore.replicas = make([]*gorp.DbMap, len(utils.Cfg.SqlSettings.DataSourceReplicas)) + for i, replica := range utils.Cfg.SqlSettings.DataSourceReplicas { + sqlStore.replicas[i] = setupConnection(fmt.Sprintf("replica-%v", i), utils.Cfg.SqlSettings.DriverName, replica, + utils.Cfg.SqlSettings.MaxIdleConns, utils.Cfg.SqlSettings.MaxOpenConns, + utils.Cfg.SqlSettings.Trace) + } } schemaVersion := sqlStore.GetCurrentSchemaVersion() @@ -308,26 +315,21 @@ func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, } } -// func (ss SqlStore) RemoveColumnIfExists(tableName string, columnName string) bool { +func (ss SqlStore) RemoveColumnIfExists(tableName string, columnName string) bool { -// // XXX TODO FIXME this should be removed after 0.6.0 -// if utils.Cfg.SqlSettings.DriverName == "postgres" { -// return false -// } - -// if !ss.DoesColumnExist(tableName, columnName) { -// return false -// } + if !ss.DoesColumnExist(tableName, columnName) { + return false + } -// _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " DROP COLUMN " + columnName) -// if err != nil { -// l4g.Critical("Failed to drop column %v", err) -// time.Sleep(time.Second) -// panic("Failed to drop column " + err.Error()) -// } + _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " DROP COLUMN " + columnName) + if err != nil { + l4g.Critical("Failed to drop column %v", err) + time.Sleep(time.Second) + panic("Failed to drop column " + err.Error()) + } -// return true -// } + return true +} // func (ss SqlStore) RenameColumnIfExists(tableName string, oldColumnName string, newColumnName string, colType string) bool { diff --git a/store/sql_team_store.go b/store/sql_team_store.go index d2148c2e3..109fe5401 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -28,6 +28,7 @@ func NewSqlTeamStore(sqlStore *SqlStore) TeamStore { } func (s SqlTeamStore) UpgradeSchemaIfNeeded() { + s.RemoveColumnIfExists("Teams", "AllowValet") } func (s SqlTeamStore) CreateIndexesIfNotExists() { @@ -195,7 +196,7 @@ func (s SqlTeamStore) GetTeamsForEmail(email string) StoreChannel { return storeChannel } -func (s SqlTeamStore) GetForExport() StoreChannel { +func (s SqlTeamStore) GetAll() StoreChannel { storeChannel := make(StoreChannel) go func() { @@ -203,7 +204,7 @@ func (s SqlTeamStore) GetForExport() StoreChannel { var data []*model.Team if _, err := s.GetReplica().Select(&data, "SELECT * FROM Teams"); err != nil { - result.Err = model.NewAppError("SqlTeamStore.GetForExport", "We could not get all teams", err.Error()) + result.Err = model.NewAppError("SqlTeamStore.GetAllTeams", "We could not get all teams", err.Error()) } result.Data = data diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 3fd1c82b5..0a723d965 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -481,3 +481,22 @@ func (us SqlUserStore) GetForExport(teamId string) StoreChannel { return storeChannel } + +func (us SqlUserStore) GetTotalUsersCount() StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + if count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users"); err != nil { + result.Err = model.NewAppError("SqlUserStore.GetTotalUsersCount", "We could not count the users", err.Error()) + } else { + result.Data = count + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index ddd7e5bb8..e2a454023 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -42,7 +42,7 @@ func TestUserStoreSave(t *testing.T) { t.Fatal("should be unique username") } - for i := 0; i < 150; i++ { + for i := 0; i < 50; i++ { u1.Id = "" u1.Email = model.NewId() u1.Username = model.NewId() @@ -206,6 +206,24 @@ func TestUserStoreGet(t *testing.T) { } } +func TestUserCountt(t *testing.T) { + Setup() + + u1 := model.User{} + u1.TeamId = model.NewId() + u1.Email = model.NewId() + Must(store.User().Save(&u1)) + + if result := <-store.User().GetTotalUsersCount(); result.Err != nil { + t.Fatal(result.Err) + } else { + count := result.Data.(int64) + if count <= 0 { + t.Fatal() + } + } +} + func TestUserStoreGetProfiles(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index c9d40cfa5..23580f452 100644 --- a/store/store.go +++ b/store/store.go @@ -47,7 +47,7 @@ type TeamStore interface { Get(id string) StoreChannel GetByName(name string) StoreChannel GetTeamsForEmail(domain string) StoreChannel - GetForExport() StoreChannel + GetAll() StoreChannel } type ChannelStore interface { @@ -103,6 +103,7 @@ type UserStore interface { GetEtagForProfiles(teamId string) StoreChannel UpdateFailedPasswordAttempts(userId string, attempts int) StoreChannel GetForExport(teamId string) StoreChannel + GetTotalUsersCount() StoreChannel } type SessionStore interface { @@ -110,6 +111,7 @@ type SessionStore interface { Get(sessionIdOrToken string) StoreChannel GetSessions(userId string) StoreChannel Remove(sessionIdOrToken string) StoreChannel + RemoveAllSessionsForTeam(teamId string) StoreChannel UpdateLastActivityAt(sessionId string, time int64) StoreChannel UpdateRoles(userId string, roles string) StoreChannel } |