Skip to content

Commit 504f45b

Browse files
hmhealeymattermod
andauthored
MM-27007 Remove automatic sidebar migration (mattermost#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 <[email protected]>
1 parent ed34468 commit 504f45b

12 files changed

+279
-449
lines changed

i18n/en.json

-8
Original file line numberDiff line numberDiff line change
@@ -5238,14 +5238,6 @@
52385238
"id": "migrations.worker.run_migration.unknown_key",
52395239
"translation": "Unable to run migration job due to unknown migration key."
52405240
},
5241-
{
5242-
"id": "migrations.worker.run_sidebar_categories_phase_2_migration.internal_error",
5243-
"translation": "Migration failed due to database error."
5244-
},
5245-
{
5246-
"id": "migrations.worker.run_sidebar_categories_phase_2_migration.invalid_progress",
5247-
"translation": "Migration failed due to invalid progress data."
5248-
},
52495241
{
52505242
"id": "model.access.is_valid.access_token.app_error",
52515243
"translation": "Invalid access token."

migrations/migrations.go

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func init() {
3232
func MakeMigrationsList() []string {
3333
return []string{
3434
model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2,
35-
model.MIGRATION_KEY_SIDEBAR_CATEGORIES_PHASE_2,
3635
}
3736
}
3837

migrations/sidebar_categories_phase_2.go

-128
This file was deleted.

migrations/worker.go

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ func (worker *Worker) runMigration(key string, lastDone string) (bool, string, *
150150
var err *model.AppError
151151

152152
switch key {
153-
case model.MIGRATION_KEY_SIDEBAR_CATEGORIES_PHASE_2:
154-
done, progress, err = worker.runSidebarCategoriesPhase2Migration(lastDone)
155153
case model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2:
156154
done, progress, err = worker.runAdvancedPermissionsPhase2Migration(lastDone)
157155
default:

model/migration.go

-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ const (
1717
MIGRATION_KEY_ADD_MANAGE_GUESTS_PERMISSIONS = "add_manage_guests_permissions"
1818
MIGRATION_KEY_CHANNEL_MODERATIONS_PERMISSIONS = "channel_moderations_permissions"
1919
MIGRATION_KEY_ADD_USE_GROUP_MENTIONS_PERMISSION = "add_use_group_mentions_permission"
20-
21-
MIGRATION_KEY_SIDEBAR_CATEGORIES_PHASE_2 = "migration_sidebar_categories_phase_2"
2220
)

store/opentracing_layer.go

-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

store/sqlstore/channel_store.go

+53-52
Original file line numberDiff line numberDiff line change
@@ -436,46 +436,6 @@ func (s SqlChannelStore) createIndexesIfNotExists() {
436436
s.CreateIndexIfNotExists("idx_channels_scheme_id", "Channels", "SchemeId")
437437
}
438438

439-
// MigrateSidebarCategories creates 3 initial categories for all existing user/team pairs
440-
// **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
441-
func (s SqlChannelStore) MigrateSidebarCategories(fromTeamId, fromUserId string) (map[string]interface{}, error) {
442-
var userTeamMap []struct {
443-
UserId string
444-
TeamId string
445-
}
446-
447-
transaction, err := s.GetMaster().Begin()
448-
if err != nil {
449-
return nil, err
450-
}
451-
452-
defer finalizeTransaction(transaction)
453-
454-
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 {
455-
return nil, err
456-
}
457-
458-
if len(userTeamMap) == 0 {
459-
// No more team members in query result means that the migration has finished.
460-
return nil, nil
461-
}
462-
463-
for _, u := range userTeamMap {
464-
if err := s.createInitialSidebarCategoriesT(transaction, u.UserId, u.TeamId); err != nil {
465-
return nil, err
466-
}
467-
}
468-
if err := transaction.Commit(); err != nil {
469-
return nil, err
470-
}
471-
472-
data := make(map[string]interface{})
473-
data["TeamId"] = userTeamMap[len(userTeamMap)-1].TeamId
474-
data["UserId"] = userTeamMap[len(userTeamMap)-1].UserId
475-
476-
return data, nil
477-
}
478-
479439
func (s SqlChannelStore) CreateInitialSidebarCategories(userId, teamId string) error {
480440
transaction, err := s.GetMaster().Begin()
481441
if err != nil {
@@ -510,20 +470,22 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
510470
return errors.Wrap(err, "createInitialSidebarCategoriesT: failed to select existing categories")
511471
}
512472

513-
hasCategoryOfType := func(categoryType model.SidebarCategoryType) bool {
514-
for _, existingType := range existingTypes {
515-
if categoryType == existingType {
516-
return true
517-
}
518-
}
519-
520-
return false
473+
hasCategoryOfType := make(map[model.SidebarCategoryType]bool, len(existingTypes))
474+
for _, existingType := range existingTypes {
475+
hasCategoryOfType[existingType] = true
521476
}
522477

523-
if !hasCategoryOfType(model.SidebarCategoryFavorites) {
478+
if !hasCategoryOfType[model.SidebarCategoryFavorites] {
479+
favoritesCategoryId := model.NewId()
480+
481+
// Create the SidebarChannels first since there's more opportunity for something to fail here
482+
if err := s.migrateFavoritesToSidebarT(transaction, userId, teamId, favoritesCategoryId); err != nil {
483+
return errors.Wrap(err, "createInitialSidebarCategoriesT: failed to migrate favorites to sidebar")
484+
}
485+
524486
if err := transaction.Insert(&model.SidebarCategory{
525487
DisplayName: "Favorites", // This will be retranslated by the client into the user's locale
526-
Id: model.NewId(),
488+
Id: favoritesCategoryId,
527489
UserId: userId,
528490
TeamId: teamId,
529491
Sorting: model.SidebarCategorySortDefault,
@@ -534,7 +496,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
534496
}
535497
}
536498

537-
if !hasCategoryOfType(model.SidebarCategoryChannels) {
499+
if !hasCategoryOfType[model.SidebarCategoryChannels] {
538500
if err := transaction.Insert(&model.SidebarCategory{
539501
DisplayName: "Channels", // This will be retranslateed by the client into the user's locale
540502
Id: model.NewId(),
@@ -548,7 +510,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
548510
}
549511
}
550512

551-
if !hasCategoryOfType(model.SidebarCategoryDirectMessages) {
513+
if !hasCategoryOfType[model.SidebarCategoryDirectMessages] {
552514
if err := transaction.Insert(&model.SidebarCategory{
553515
DisplayName: "Direct Messages", // This will be retranslateed by the client into the user's locale
554516
Id: model.NewId(),
@@ -595,6 +557,45 @@ func (s SqlChannelStore) migrateMembershipToSidebar(transaction *gorp.Transactio
595557
return memberships, nil
596558
}
597559

560+
func (s SqlChannelStore) migrateFavoritesToSidebarT(transaction *gorp.Transaction, userId, teamId, favoritesCategoryId string) error {
561+
favoritesQuery, favoritesParams, _ := s.getQueryBuilder().
562+
Select("Preferences.Name").
563+
From("Preferences").
564+
Join("Channels on Preferences.Name = Channels.Id").
565+
Join("ChannelMembers on Preferences.Name = ChannelMembers.ChannelId and Preferences.UserId = ChannelMembers.UserId").
566+
Where(sq.Eq{
567+
"Preferences.UserId": userId,
568+
"Preferences.Category": model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
569+
"Preferences.Value": "true",
570+
}).
571+
Where(sq.Or{
572+
sq.Eq{"Channels.TeamId": teamId},
573+
sq.Eq{"Channels.TeamId": ""},
574+
}).
575+
OrderBy(
576+
"Channels.DisplayName",
577+
"Channels.Name ASC",
578+
).ToSql()
579+
580+
var favoriteChannelIds []string
581+
if _, err := transaction.Select(&favoriteChannelIds, favoritesQuery, favoritesParams...); err != nil {
582+
return errors.Wrap(err, "migrateFavoritesToSidebarT: unable to get favorite channel IDs")
583+
}
584+
585+
for i, channelId := range favoriteChannelIds {
586+
if err := transaction.Insert(&model.SidebarChannel{
587+
ChannelId: channelId,
588+
CategoryId: favoritesCategoryId,
589+
UserId: userId,
590+
SortOrder: int64(i * model.MinimalSidebarSortDistance),
591+
}); err != nil {
592+
return errors.Wrap(err, "migrateFavoritesToSidebarT: unable to insert SidebarChannel")
593+
}
594+
}
595+
596+
return nil
597+
}
598+
598599
// MigrateFavoritesToSidebarChannels populates the SidebarChannels table by analyzing existing user preferences for favorites
599600
// **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
600601
func (s SqlChannelStore) MigrateFavoritesToSidebarChannels(lastUserId string, runningOrder int64) (map[string]interface{}, error) {

store/store.go

-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ type ChannelStore interface {
216216
ResetAllChannelSchemes() *model.AppError
217217
ClearAllCustomRoleAssignments() *model.AppError
218218
MigratePublicChannels() error
219-
MigrateSidebarCategories(fromTeamId, fromUserId string) (map[string]interface{}, error)
220219
CreateInitialSidebarCategories(userId, teamId string) error
221-
MigrateFavoritesToSidebarChannels(lastUserId string, runningOrder int64) (map[string]interface{}, error)
222220
GetSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError)
223221
GetSidebarCategory(categoryId string) (*model.SidebarCategoryWithChannels, *model.AppError)
224222
GetSidebarCategoryOrder(userId, teamId string) ([]string, *model.AppError)

0 commit comments

Comments
 (0)