MM-10615: Reset teams/channels to default scheme on delete scheme. (#8811)

Этот коммит содержится в:
George Goldberg
2018-05-17 12:48:31 +01:00
коммит произвёл GitHub
родитель c2ab85e0a3
Коммит 319d61123a
4 изменённых файлов: 34 добавлений и 61 удалений

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

@@ -615,29 +615,6 @@ func TestDeleteScheme(t *testing.T) {
assert.Nil(t, res.Err)
team := res.Data.(*model.Team)
// Try and fail to delete the scheme.
_, r2 := th.SystemAdminClient.DeleteScheme(s1.Id)
CheckInternalErrorStatus(t, r2)
role1, roleRes1 = th.SystemAdminClient.GetRole(s1.DefaultTeamAdminRole)
CheckNoError(t, roleRes1)
role2, roleRes2 = th.SystemAdminClient.GetRole(s1.DefaultTeamUserRole)
CheckNoError(t, roleRes2)
role3, roleRes3 = th.SystemAdminClient.GetRole(s1.DefaultChannelAdminRole)
CheckNoError(t, roleRes3)
role4, roleRes4 = th.SystemAdminClient.GetRole(s1.DefaultChannelUserRole)
CheckNoError(t, roleRes4)
assert.Zero(t, role1.DeleteAt)
assert.Zero(t, role2.DeleteAt)
assert.Zero(t, role3.DeleteAt)
assert.Zero(t, role4.DeleteAt)
// Change the team using it to a different scheme.
emptyString := ""
team.SchemeId = &emptyString
res = <-th.App.Srv.Store.Team().Update(team)
// Delete the Scheme.
_, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
CheckNoError(t, r3)
@@ -656,6 +633,11 @@ func TestDeleteScheme(t *testing.T) {
assert.NotZero(t, role2.DeleteAt)
assert.NotZero(t, role3.DeleteAt)
assert.NotZero(t, role4.DeleteAt)
// Check the team now uses the default scheme
c2, resp := th.SystemAdminClient.GetTeam(team.Id, "")
CheckNoError(t, resp)
assert.Equal(t, "", *c2.SchemeId)
})
t.Run("ValidChannelScheme", func(t *testing.T) {
@@ -702,23 +684,6 @@ func TestDeleteScheme(t *testing.T) {
assert.Nil(t, res.Err)
channel := res.Data.(*model.Channel)
// Try and fail to delete the scheme.
_, r2 := th.SystemAdminClient.DeleteScheme(s1.Id)
CheckInternalErrorStatus(t, r2)
role3, roleRes3 = th.SystemAdminClient.GetRole(s1.DefaultChannelAdminRole)
CheckNoError(t, roleRes3)
role4, roleRes4 = th.SystemAdminClient.GetRole(s1.DefaultChannelUserRole)
CheckNoError(t, roleRes4)
assert.Zero(t, role3.DeleteAt)
assert.Zero(t, role4.DeleteAt)
// Change the team using it to a different scheme.
emptyString := ""
channel.SchemeId = &emptyString
res = <-th.App.Srv.Store.Channel().Update(channel)
// Delete the Scheme.
_, r3 := th.SystemAdminClient.DeleteScheme(s1.Id)
CheckNoError(t, r3)
@@ -731,6 +696,11 @@ func TestDeleteScheme(t *testing.T) {
assert.NotZero(t, role3.DeleteAt)
assert.NotZero(t, role4.DeleteAt)
// Check the channel now uses the default scheme
c2, resp := th.SystemAdminClient.GetChannelByName(channel.Name, channel.TeamId, "")
CheckNoError(t, resp)
assert.Equal(t, "", *c2.SchemeId)
})
t.Run("FailureCases", func(t *testing.T) {

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

@@ -6851,16 +6851,16 @@
"translation": "Unable to get the scheme"
},
{
"id": "store.sql_scheme.team_count.app_error",
"translation": "Unable to count the number of teams using this scheme"
"id": "store.sql_scheme.reset_teams.app_error",
"translation": "Unable to reset all teams using this scheme to the default scheme"
},
{
"id": "store.sql_scheme.delete.scheme_in_use.app_error",
"translation": "Unable to delete the scheme as it in use by 1 or more teams or channels"
},
{
"id": "store.sql_scheme.channel_count.app_error",
"translation": "Unable to count the number of channels using this scheme"
"id": "store.sql_scheme.reset_channels.app_error",
"translation": "Unable to reset all channels using this scheme to the default scheme"
},
{
"id": "store.sql_scheme.delete.role_update.app_error",

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

@@ -210,27 +210,20 @@ func (s *SqlSupplier) SchemeDelete(ctx context.Context, schemeId string, hints .
return result
}
// Check that the scheme isn't being used on any Teams or Channels.
// Update any teams or channels using this scheme to the default scheme.
if scheme.Scope == model.SCHEME_SCOPE_TEAM {
if c, err := s.GetReplica().SelectInt("SELECT COUNT(*) FROM Teams WHERE SchemeId = :SchemeId", map[string]interface{}{"SchemeId": schemeId}); err != nil {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.team_count.app_error", nil, "Id="+schemeId+", "+err.Error(), http.StatusInternalServerError)
if _, err := s.GetReplica().Exec("UPDATE Teams SET SchemeId = '' WHERE SchemeId = :SchemeId", map[string]interface{}{"SchemeId": schemeId}); err != nil {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.reset_teams.app_error", nil, "Id="+schemeId+", "+err.Error(), http.StatusInternalServerError)
return result
} else {
if c > 0 {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.delete.scheme_in_use.app_error", nil, "Id="+schemeId, http.StatusInternalServerError)
return result
}
}
} else if scheme.Scope == model.SCHEME_SCOPE_CHANNEL {
if c, err := s.GetReplica().SelectInt("SELECT COUNT(*) FROM Channels WHERE SchemeId = :SchemeId", map[string]interface{}{"SchemeId": schemeId}); err != nil {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.channel_count.app_error", nil, "Id="+schemeId+", "+err.Error(), http.StatusInternalServerError)
if _, err := s.GetReplica().Exec("UPDATE Channels SET SchemeId = '' WHERE SchemeId = :SchemeId", map[string]interface{}{"SchemeId": schemeId}); err != nil {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.reset_channels.app_error", nil, "Id="+schemeId+", "+err.Error(), http.StatusInternalServerError)
return result
} else {
if c > 0 {
result.Err = model.NewAppError("SqlSchemeStore.Delete", "store.sql_scheme.delete.scheme_in_use.app_error", nil, "Id="+schemeId, http.StatusInternalServerError)
return result
}
}
// Blow away the channel caches.
s.Channel().ClearCaches()
}
// Delete the roles belonging to the scheme.

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

@@ -336,7 +336,12 @@ func testSchemeStoreDelete(t *testing.T, ss store.Store) {
t4 = tres4.Data.(*model.Team)
sres4 := <-ss.Scheme().Delete(d4.Id)
assert.NotNil(t, sres4.Err)
assert.Nil(t, sres4.Err)
tres5 := <-ss.Team().Get(t4.Id)
assert.Nil(t, tres5.Err)
t5 := tres5.Data.(*model.Team)
assert.Equal(t, "", *t5.SchemeId)
// Try deleting a channel scheme that's in use.
s5 := &model.Scheme{
@@ -360,5 +365,10 @@ func testSchemeStoreDelete(t *testing.T, ss store.Store) {
c5 = cres5.Data.(*model.Channel)
sres5 := <-ss.Scheme().Delete(d5.Id)
assert.NotNil(t, sres5.Err)
assert.Nil(t, sres5.Err)
cres6 := <-ss.Channel().Get(c5.Id, true)
assert.Nil(t, cres6.Err)
c6 := cres6.Data.(*model.Channel)
assert.Equal(t, "", *c6.SchemeId)
}