MM-26753 Create initial sidebar categories on demand if migration hasn't ran (#14981)

* MM-26753 Change CreateInitialSidebarCategories to only take a user ID

* MM-26753 Create initial sidebar categories on demand if migration hasn't ran

* Wait for sidebar categories to be loaded in case of replication lag
Этот коммит содержится в:
Harrison Healey
2020-07-08 12:07:42 -04:00
коммит произвёл GitHub
родитель 31200fc657
Коммит 302e59a0fe
10 изменённых файлов: 219 добавлений и 167 удалений

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

@@ -2603,8 +2603,8 @@ func (a *App) ClearChannelMembersCache(channelID string) {
}
}
func (a *App) createInitialSidebarCategories(user *model.User, team *model.Team) *model.AppError {
nErr := a.Srv().Store.Channel().CreateInitialSidebarCategories(user, team.Id)
func (a *App) createInitialSidebarCategories(userId, teamId string) *model.AppError {
nErr := a.Srv().Store.Channel().CreateInitialSidebarCategories(userId, teamId)
if nErr != nil {
return model.NewAppError("createInitialSidebarCategories", "app.channel.create_initial_sidebar_categories.internal_error", nil, nErr.Error(), http.StatusInternalServerError)
@@ -2614,7 +2614,45 @@ func (a *App) createInitialSidebarCategories(user *model.User, team *model.Team)
}
func (a *App) GetSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError) {
return a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
categories, err := a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
if len(categories.Categories) == 0 && err == nil {
// A user must always have categories, so migration must not have happened yet, and we should run it ourselves
nErr := a.createInitialSidebarCategories(userId, teamId)
if nErr != nil {
return nil, nErr
}
categories, err = a.waitForSidebarCategories(userId, teamId)
}
return categories, err
}
// waitForSidebarCategories is used to get a user's sidebar categories after they've been created since there may be
// replication lag if any database replicas exist. It will wait until results are available to return them.
func (a *App) waitForSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError) {
if len(a.Config().SqlSettings.DataSourceReplicas) == 0 {
// The categories should be available immediately on a single database
return a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
}
now := model.GetMillis()
for model.GetMillis()-now < 12000 {
time.Sleep(100 * time.Millisecond)
categories, err := a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
if err != nil || len(categories.Categories) > 0 {
// We've found something, so return
return categories, err
}
}
mlog.Error("waitForSidebarCategories giving up", mlog.String("user_id", userId), mlog.String("team_id", teamId))
return &model.OrderedSidebarCategories{}, nil
}
func (a *App) GetSidebarCategoryOrder(userId, teamId string) ([]string, *model.AppError) {