MM-25507: Remove old migration state marker before running fresh migration (#14768)

While resetting permissions, we were not removing old migration state residue
for the EMOJIS_PERMISSIONS_MIGRATION_KEY and GUEST_ROLES_CREATION_MIGRATION_KEY.
Therefore, during their migration, they got skipped because the code checks
if a migration key is already present or not.

To fix this, we remove the migration key just as we do for ADVANCED_PERMISSIONS_MIGRATION_KEY.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Agniva De Sarker
2020-06-25 13:36:47 +05:30
committed by GitHub
parent 23e306bc3a
commit 1648b11e82
3 changed files with 36 additions and 2 deletions

View File

@@ -134,8 +134,11 @@ func (a *App) DoEmojisPermissionsMigration() {
return
}
systemAdminRole.Permissions = append(systemAdminRole.Permissions, model.PERMISSION_CREATE_EMOJIS.Id, model.PERMISSION_DELETE_EMOJIS.Id)
systemAdminRole.Permissions = append(systemAdminRole.Permissions, model.PERMISSION_DELETE_OTHERS_EMOJIS.Id)
systemAdminRole.Permissions = append(systemAdminRole.Permissions,
model.PERMISSION_CREATE_EMOJIS.Id,
model.PERMISSION_DELETE_EMOJIS.Id,
model.PERMISSION_DELETE_OTHERS_EMOJIS.Id,
)
if _, err := a.Srv().Store.Role().Save(systemAdminRole); err != nil {
mlog.Critical("Failed to migrate emojis creation permissions from mattermost config.", mlog.Err(err))
return

View File

@@ -58,6 +58,16 @@ func (a *App) ResetPermissionsSystem() *model.AppError {
return err
}
// Remove the "System" table entry that marks the emoji permissions migration as done.
if _, err := a.Srv().Store.System().PermanentDeleteByName(EMOJIS_PERMISSIONS_MIGRATION_KEY); err != nil {
return err
}
// Remove the "System" table entry that marks the guest roles permissions migration as done.
if _, err := a.Srv().Store.System().PermanentDeleteByName(GUEST_ROLES_CREATION_MIGRATION_KEY); err != nil {
return err
}
// Now that the permissions system has been reset, re-run the migration to reinitialise it.
a.DoAppMigrations()

View File

@@ -10,6 +10,8 @@ import (
"testing"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testWriter struct {
@@ -253,6 +255,25 @@ func TestImportPermissions_schemeDeletedOnRoleFailure(t *testing.T) {
}
func TestEmojiMigration(t *testing.T) {
th := Setup(t)
defer th.TearDown()
role, err := th.App.GetRoleByName(model.SYSTEM_ADMIN_ROLE_ID)
require.Nil(t, err)
assert.Contains(t, role.Permissions, model.PERMISSION_CREATE_EMOJIS.Id)
assert.Contains(t, role.Permissions, model.PERMISSION_DELETE_EMOJIS.Id)
assert.Contains(t, role.Permissions, model.PERMISSION_DELETE_OTHERS_EMOJIS.Id)
th.App.ResetPermissionsSystem()
role, err = th.App.GetRoleByName(model.SYSTEM_ADMIN_ROLE_ID)
require.Nil(t, err)
assert.Contains(t, role.Permissions, model.PERMISSION_CREATE_EMOJIS.Id)
assert.Contains(t, role.Permissions, model.PERMISSION_DELETE_EMOJIS.Id)
assert.Contains(t, role.Permissions, model.PERMISSION_DELETE_OTHERS_EMOJIS.Id)
}
func withMigrationMarkedComplete(th *TestHelper, f func()) {
// Mark the migration as done.
th.App.Srv().Store.System().PermanentDeleteByName(model.MIGRATION_KEY_ADVANCED_PERMISSIONS_PHASE_2)