MM-27007 Remove automatic sidebar migration (#15087)
* MM-27007 Add migration of favorited channels to CreateInitialSidebarCategories * MM-27007 Rewrite migrateFavortitesToSidebarT to use ROW_NUMBER() when available * MM-27007 Remove automatic sidebar migration * Remove old i18n strings * Fix typo * Address feedback Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ed34468996
Коммит
504f45b6ec
@@ -436,46 +436,6 @@ func (s SqlChannelStore) createIndexesIfNotExists() {
|
||||
s.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId")
|
||||
}
|
||||
|
||||
// MigrateSidebarCategories creates 3 initial categories for all existing user/team pairs
|
||||
// **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
|
||||
func (s SqlChannelStore) MigrateSidebarCategories(fromTeamId, fromUserId string) (map[string]interface{}, error) {
|
||||
var userTeamMap []struct {
|
||||
UserId string
|
||||
TeamId string
|
||||
}
|
||||
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer finalizeTransaction(transaction)
|
||||
|
||||
if _, err := transaction.Select(&userTeamMap, "SELECT TeamId, UserId FROM TeamMembers LEFT JOIN Users ON Users.Id=UserId WHERE (TeamId, UserId) > (:FromTeamId, :FromUserId) ORDER BY TeamId, UserId LIMIT 100", map[string]interface{}{"FromTeamId": fromTeamId, "FromUserId": fromUserId}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(userTeamMap) == 0 {
|
||||
// No more team members in query result means that the migration has finished.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, u := range userTeamMap {
|
||||
if err := s.createInitialSidebarCategoriesT(transaction, u.UserId, u.TeamId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := transaction.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["TeamId"] = userTeamMap[len(userTeamMap)-1].TeamId
|
||||
data["UserId"] = userTeamMap[len(userTeamMap)-1].UserId
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) CreateInitialSidebarCategories(userId, teamId string) error {
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
@@ -510,20 +470,22 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
|
||||
return errors.Wrap(err, "createInitialSidebarCategoriesT: failed to select existing categories")
|
||||
}
|
||||
|
||||
hasCategoryOfType := func(categoryType model.SidebarCategoryType) bool {
|
||||
for _, existingType := range existingTypes {
|
||||
if categoryType == existingType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
hasCategoryOfType := make(map[model.SidebarCategoryType]bool, len(existingTypes))
|
||||
for _, existingType := range existingTypes {
|
||||
hasCategoryOfType[existingType] = true
|
||||
}
|
||||
|
||||
if !hasCategoryOfType(model.SidebarCategoryFavorites) {
|
||||
if !hasCategoryOfType[model.SidebarCategoryFavorites] {
|
||||
favoritesCategoryId := model.NewId()
|
||||
|
||||
// Create the SidebarChannels first since there's more opportunity for something to fail here
|
||||
if err := s.migrateFavoritesToSidebarT(transaction, userId, teamId, favoritesCategoryId); err != nil {
|
||||
return errors.Wrap(err, "createInitialSidebarCategoriesT: failed to migrate favorites to sidebar")
|
||||
}
|
||||
|
||||
if err := transaction.Insert(&model.SidebarCategory{
|
||||
DisplayName: "Favorites", // This will be retranslated by the client into the user's locale
|
||||
Id: model.NewId(),
|
||||
Id: favoritesCategoryId,
|
||||
UserId: userId,
|
||||
TeamId: teamId,
|
||||
Sorting: model.SidebarCategorySortDefault,
|
||||
@@ -534,7 +496,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
|
||||
}
|
||||
}
|
||||
|
||||
if !hasCategoryOfType(model.SidebarCategoryChannels) {
|
||||
if !hasCategoryOfType[model.SidebarCategoryChannels] {
|
||||
if err := transaction.Insert(&model.SidebarCategory{
|
||||
DisplayName: "Channels", // This will be retranslateed by the client into the user's locale
|
||||
Id: model.NewId(),
|
||||
@@ -548,7 +510,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
|
||||
}
|
||||
}
|
||||
|
||||
if !hasCategoryOfType(model.SidebarCategoryDirectMessages) {
|
||||
if !hasCategoryOfType[model.SidebarCategoryDirectMessages] {
|
||||
if err := transaction.Insert(&model.SidebarCategory{
|
||||
DisplayName: "Direct Messages", // This will be retranslateed by the client into the user's locale
|
||||
Id: model.NewId(),
|
||||
@@ -595,6 +557,45 @@ func (s SqlChannelStore) migrateMembershipToSidebar(transaction *gorp.Transactio
|
||||
return memberships, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) migrateFavoritesToSidebarT(transaction *gorp.Transaction, userId, teamId, favoritesCategoryId string) error {
|
||||
favoritesQuery, favoritesParams, _ := s.getQueryBuilder().
|
||||
Select("Preferences.Name").
|
||||
From("Preferences").
|
||||
Join("Channels on Preferences.Name = Channels.Id").
|
||||
Join("ChannelMembers on Preferences.Name = ChannelMembers.ChannelId and Preferences.UserId = ChannelMembers.UserId").
|
||||
Where(sq.Eq{
|
||||
"Preferences.UserId": userId,
|
||||
"Preferences.Category": model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
|
||||
"Preferences.Value": "true",
|
||||
}).
|
||||
Where(sq.Or{
|
||||
sq.Eq{"Channels.TeamId": teamId},
|
||||
sq.Eq{"Channels.TeamId": ""},
|
||||
}).
|
||||
OrderBy(
|
||||
"Channels.DisplayName",
|
||||
"Channels.Name ASC",
|
||||
).ToSql()
|
||||
|
||||
var favoriteChannelIds []string
|
||||
if _, err := transaction.Select(&favoriteChannelIds, favoritesQuery, favoritesParams...); err != nil {
|
||||
return errors.Wrap(err, "migrateFavoritesToSidebarT: unable to get favorite channel IDs")
|
||||
}
|
||||
|
||||
for i, channelId := range favoriteChannelIds {
|
||||
if err := transaction.Insert(&model.SidebarChannel{
|
||||
ChannelId: channelId,
|
||||
CategoryId: favoritesCategoryId,
|
||||
UserId: userId,
|
||||
SortOrder: int64(i * model.MinimalSidebarSortDistance),
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "migrateFavoritesToSidebarT: unable to insert SidebarChannel")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MigrateFavoritesToSidebarChannels populates the SidebarChannels table by analyzing existing user preferences for favorites
|
||||
// **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
|
||||
func (s SqlChannelStore) MigrateFavoritesToSidebarChannels(lastUserId string, runningOrder int64) (map[string]interface{}, error) {
|
||||
|
||||
Ссылка в новой задаче
Block a user