Migrate Groups store to sync by default (GetGroups and Counts) (#10998)

Этот коммит содержится в:
Jesús Espino
2019-05-30 20:39:43 +02:00
коммит произвёл GitHub
родитель b3118ce605
Коммит 76e00b406e
5 изменённых файлов: 185 добавлений и 187 удалений

Просмотреть файл

@@ -150,41 +150,33 @@ func (a *App) ChannelMembersToRemove() ([]*model.ChannelMember, *model.AppError)
} }
func (a *App) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) { func (a *App) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) {
result := <-a.Srv.Store.Group().GetGroupsByChannel(channelId, opts) groups, err := a.Srv.Store.Group().GetGroupsByChannel(channelId, opts)
if result.Err != nil { if err != nil {
return nil, 0, result.Err return nil, 0, err
} }
groups := result.Data.([]*model.Group)
result = <-a.Srv.Store.Group().CountGroupsByChannel(channelId, opts) count, err := a.Srv.Store.Group().CountGroupsByChannel(channelId, opts)
if result.Err != nil { if err != nil {
return nil, 0, result.Err return nil, 0, err
} }
count := result.Data.(int64)
return groups, int(count), nil return groups, int(count), nil
} }
func (a *App) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) { func (a *App) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) {
result := <-a.Srv.Store.Group().GetGroupsByTeam(teamId, opts) groups, err := a.Srv.Store.Group().GetGroupsByTeam(teamId, opts)
if result.Err != nil { if err != nil {
return nil, 0, result.Err return nil, 0, err
} }
groups := result.Data.([]*model.Group)
result = <-a.Srv.Store.Group().CountGroupsByTeam(teamId, opts) count, err := a.Srv.Store.Group().CountGroupsByTeam(teamId, opts)
if result.Err != nil { if err != nil {
return nil, 0, result.Err return nil, 0, err
} }
count := result.Data.(int64)
return groups, int(count), nil return groups, int(count), nil
} }
func (a *App) GetGroups(page, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) { func (a *App) GetGroups(page, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
result := <-a.Srv.Store.Group().GetGroups(page, perPage, opts) return a.Srv.Store.Group().GetGroups(page, perPage, opts)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.Group), nil
} }

Просмотреть файл

@@ -836,32 +836,23 @@ func (s *SqlGroupStore) TeamMembersToRemove() ([]*model.TeamMember, *model.AppEr
return teamMembers, nil return teamMembers, nil
} }
func (s *SqlGroupStore) CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) store.StoreChannel { func (s *SqlGroupStore) CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) (int64, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
countQuery := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeChannel, selectCountGroups, channelId, opts) countQuery := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeChannel, selectCountGroups, channelId, opts)
countQueryString, args, err := countQuery.ToSql() countQueryString, args, err := countQuery.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.CountGroupsByChannel", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError) return int64(0), model.NewAppError("SqlGroupStore.CountGroupsByChannel", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
count, err := s.GetReplica().SelectInt(countQueryString, args...) count, err := s.GetReplica().SelectInt(countQueryString, args...)
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.CountGroupsByChannel", "store.select_error", nil, err.Error(), http.StatusInternalServerError) return int64(0), model.NewAppError("SqlGroupStore.CountGroupsByChannel", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
result.Data = count return count, nil
return
})
} }
func (s *SqlGroupStore) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) store.StoreChannel { func (s *SqlGroupStore) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
query := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeChannel, selectGroups, channelId, opts) query := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeChannel, selectGroups, channelId, opts)
if opts.PageOpts != nil { if opts.PageOpts != nil {
@@ -871,22 +862,17 @@ func (s *SqlGroupStore) GetGroupsByChannel(channelId string, opts model.GroupSea
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroupsByChannel", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroupsByChannel", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
var groups []*model.Group var groups []*model.Group
_, err = s.GetReplica().Select(&groups, queryString, args...) _, err = s.GetReplica().Select(&groups, queryString, args...)
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroupsByChannel", "store.select_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroupsByChannel", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
result.Data = groups return groups, nil
return
})
} }
// ChannelMembersToRemove returns all channel members that should be removed based on group constraints. // ChannelMembersToRemove returns all channel members that should be removed based on group constraints.
@@ -985,32 +971,23 @@ func (s *SqlGroupStore) groupsBySyncableBaseQuery(st model.GroupSyncableType, t
return query return query
} }
func (s *SqlGroupStore) CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) store.StoreChannel { func (s *SqlGroupStore) CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) (int64, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
countQuery := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeTeam, selectCountGroups, teamId, opts) countQuery := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeTeam, selectCountGroups, teamId, opts)
countQueryString, args, err := countQuery.ToSql() countQueryString, args, err := countQuery.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.CountGroupsByTeam", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError) return int64(0), model.NewAppError("SqlGroupStore.CountGroupsByTeam", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
count, err := s.GetReplica().SelectInt(countQueryString, args...) count, err := s.GetReplica().SelectInt(countQueryString, args...)
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.CountGroupsByTeam", "store.select_error", nil, err.Error(), http.StatusInternalServerError) return int64(0), model.NewAppError("SqlGroupStore.CountGroupsByTeam", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
result.Data = count return count, nil
return
})
} }
func (s *SqlGroupStore) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) store.StoreChannel { func (s *SqlGroupStore) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
query := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeTeam, selectGroups, teamId, opts) query := s.groupsBySyncableBaseQuery(model.GroupSyncableTypeTeam, selectGroups, teamId, opts)
if opts.PageOpts != nil { if opts.PageOpts != nil {
@@ -1020,26 +997,20 @@ func (s *SqlGroupStore) GetGroupsByTeam(teamId string, opts model.GroupSearchOpt
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroupsByTeam", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroupsByTeam", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
var groups []*model.Group var groups []*model.Group
_, err = s.GetReplica().Select(&groups, queryString, args...) _, err = s.GetReplica().Select(&groups, queryString, args...)
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroupsByTeam", "store.select_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroupsByTeam", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
result.Data = groups return groups, nil
return
})
} }
func (s *SqlGroupStore) GetGroups(page, perPage int, opts model.GroupSearchOpts) store.StoreChannel { func (s *SqlGroupStore) GetGroups(page, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
return store.Do(func(result *store.StoreResult) {
var groups []*model.Group var groups []*model.Group
groupsQuery := s.getQueryBuilder().Select("g.*").From("UserGroups g").Limit(uint64(perPage)).Offset(uint64(page * perPage)).OrderBy("g.DisplayName") groupsQuery := s.getQueryBuilder().Select("g.*").From("UserGroups g").Limit(uint64(perPage)).Offset(uint64(page * perPage)).OrderBy("g.DisplayName")
@@ -1097,16 +1068,12 @@ func (s *SqlGroupStore) GetGroups(page, perPage int, opts model.GroupSearchOpts)
queryString, args, err := groupsQuery.ToSql() queryString, args, err := groupsQuery.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroups", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroups", "store.sql_group.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
if _, err = s.GetReplica().Select(&groups, queryString, args...); err != nil { if _, err = s.GetReplica().Select(&groups, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlGroupStore.GetGroups", "store.select_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlGroupStore.GetGroups", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
result.Data = groups return groups, nil
return
})
} }

Просмотреть файл

@@ -594,13 +594,13 @@ type GroupStore interface {
TeamMembersToRemove() ([]*model.TeamMember, *model.AppError) TeamMembersToRemove() ([]*model.TeamMember, *model.AppError)
ChannelMembersToRemove() ([]*model.ChannelMember, *model.AppError) ChannelMembersToRemove() ([]*model.ChannelMember, *model.AppError)
GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) StoreChannel GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError)
CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) StoreChannel CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) (int64, *model.AppError)
GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) StoreChannel GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError)
CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) StoreChannel CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) (int64, *model.AppError)
GetGroups(page, perPage int, opts model.GroupSearchOpts) StoreChannel GetGroups(page, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError)
} }
type LinkMetadataStore interface { type LinkMetadataStore interface {

Просмотреть файл

@@ -1729,12 +1729,12 @@ func testGetGroupsByChannel(t *testing.T, ss store.Store) {
} }
tc.Opts.PageOpts.Page = tc.Page tc.Opts.PageOpts.Page = tc.Page
tc.Opts.PageOpts.PerPage = tc.PerPage tc.Opts.PageOpts.PerPage = tc.PerPage
res := <-ss.Group().GetGroupsByChannel(tc.ChannelId, tc.Opts) groups, err := ss.Group().GetGroupsByChannel(tc.ChannelId, tc.Opts)
require.Nil(t, res.Err) require.Nil(t, err)
require.ElementsMatch(t, tc.Result, res.Data.([]*model.Group)) require.ElementsMatch(t, tc.Result, groups)
if tc.TotalCount != nil { if tc.TotalCount != nil {
res = <-ss.Group().CountGroupsByChannel(tc.ChannelId, tc.Opts) var count int64
count := res.Data.(int64) count, err = ss.Group().CountGroupsByChannel(tc.ChannelId, tc.Opts)
require.Equal(t, *tc.TotalCount, count) require.Equal(t, *tc.TotalCount, count)
} }
}) })
@@ -1931,13 +1931,12 @@ func testGetGroupsByTeam(t *testing.T, ss store.Store) {
} }
tc.Opts.PageOpts.Page = tc.Page tc.Opts.PageOpts.Page = tc.Page
tc.Opts.PageOpts.PerPage = tc.PerPage tc.Opts.PageOpts.PerPage = tc.PerPage
res := <-ss.Group().GetGroupsByTeam(tc.TeamId, tc.Opts) groups, err := ss.Group().GetGroupsByTeam(tc.TeamId, tc.Opts)
require.Nil(t, res.Err) require.Nil(t, err)
groups := res.Data.([]*model.Group)
require.ElementsMatch(t, tc.Result, groups) require.ElementsMatch(t, tc.Result, groups)
if tc.TotalCount != nil { if tc.TotalCount != nil {
res = <-ss.Group().CountGroupsByTeam(tc.TeamId, tc.Opts) var count int64
count := res.Data.(int64) count, err = ss.Group().CountGroupsByTeam(tc.TeamId, tc.Opts)
require.Equal(t, *tc.TotalCount, count) require.Equal(t, *tc.TotalCount, count)
} }
}) })
@@ -2209,9 +2208,8 @@ func testGetGroups(t *testing.T, ss store.Store) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
res := <-ss.Group().GetGroups(tc.Page, tc.PerPage, tc.Opts) groups, err := ss.Group().GetGroups(tc.Page, tc.PerPage, tc.Opts)
require.Nil(t, res.Err) require.Nil(t, err)
groups := res.Data.([]*model.Group)
require.True(t, tc.Resultf(groups)) require.True(t, tc.Resultf(groups))
}) })
} }

Просмотреть файл

@@ -64,35 +64,49 @@ func (_m *GroupStore) ChannelMembersToRemove() ([]*model.ChannelMember, *model.A
} }
// CountGroupsByChannel provides a mock function with given fields: channelId, opts // CountGroupsByChannel provides a mock function with given fields: channelId, opts
func (_m *GroupStore) CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) store.StoreChannel { func (_m *GroupStore) CountGroupsByChannel(channelId string, opts model.GroupSearchOpts) (int64, *model.AppError) {
ret := _m.Called(channelId, opts) ret := _m.Called(channelId, opts)
var r0 store.StoreChannel var r0 int64
if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) int64); ok {
r0 = rf(channelId, opts) r0 = rf(channelId, opts)
} else { } else {
if ret.Get(0) != nil { r0 = ret.Get(0).(int64)
r0 = ret.Get(0).(store.StoreChannel) }
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, model.GroupSearchOpts) *model.AppError); ok {
r1 = rf(channelId, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
} }
} }
return r0 return r0, r1
} }
// CountGroupsByTeam provides a mock function with given fields: teamId, opts // CountGroupsByTeam provides a mock function with given fields: teamId, opts
func (_m *GroupStore) CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) store.StoreChannel { func (_m *GroupStore) CountGroupsByTeam(teamId string, opts model.GroupSearchOpts) (int64, *model.AppError) {
ret := _m.Called(teamId, opts) ret := _m.Called(teamId, opts)
var r0 store.StoreChannel var r0 int64
if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) int64); ok {
r0 = rf(teamId, opts) r0 = rf(teamId, opts)
} else { } else {
if ret.Get(0) != nil { r0 = ret.Get(0).(int64)
r0 = ret.Get(0).(store.StoreChannel) }
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, model.GroupSearchOpts) *model.AppError); ok {
r1 = rf(teamId, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
} }
} }
return r0 return r0, r1
} }
// Create provides a mock function with given fields: group // Create provides a mock function with given fields: group
@@ -272,51 +286,78 @@ func (_m *GroupStore) GetGroupSyncable(groupID string, syncableID string, syncab
} }
// GetGroups provides a mock function with given fields: page, perPage, opts // GetGroups provides a mock function with given fields: page, perPage, opts
func (_m *GroupStore) GetGroups(page int, perPage int, opts model.GroupSearchOpts) store.StoreChannel { func (_m *GroupStore) GetGroups(page int, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
ret := _m.Called(page, perPage, opts) ret := _m.Called(page, perPage, opts)
var r0 store.StoreChannel var r0 []*model.Group
if rf, ok := ret.Get(0).(func(int, int, model.GroupSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(int, int, model.GroupSearchOpts) []*model.Group); ok {
r0 = rf(page, perPage, opts) r0 = rf(page, perPage, opts)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.Group)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int, int, model.GroupSearchOpts) *model.AppError); ok {
r1 = rf(page, perPage, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetGroupsByChannel provides a mock function with given fields: channelId, opts // GetGroupsByChannel provides a mock function with given fields: channelId, opts
func (_m *GroupStore) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) store.StoreChannel { func (_m *GroupStore) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
ret := _m.Called(channelId, opts) ret := _m.Called(channelId, opts)
var r0 store.StoreChannel var r0 []*model.Group
if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) []*model.Group); ok {
r0 = rf(channelId, opts) r0 = rf(channelId, opts)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.Group)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, model.GroupSearchOpts) *model.AppError); ok {
r1 = rf(channelId, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetGroupsByTeam provides a mock function with given fields: teamId, opts // GetGroupsByTeam provides a mock function with given fields: teamId, opts
func (_m *GroupStore) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) store.StoreChannel { func (_m *GroupStore) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
ret := _m.Called(teamId, opts) ret := _m.Called(teamId, opts)
var r0 store.StoreChannel var r0 []*model.Group
if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, model.GroupSearchOpts) []*model.Group); ok {
r0 = rf(teamId, opts) r0 = rf(teamId, opts)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).([]*model.Group)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, model.GroupSearchOpts) *model.AppError); ok {
r1 = rf(teamId, opts)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetMemberCount provides a mock function with given fields: groupID // GetMemberCount provides a mock function with given fields: groupID