Chore: delete team related entries for an org after the org gets deleted (#76706)

* delete team related entries for an org after the org gets deleted

* fix tests

* one more test fix
This commit is contained in:
Ieva 2023-10-18 10:40:26 +01:00 committed by GitHub
parent 42f5e42b54
commit 1fc375855c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 2 deletions

View File

@ -22,4 +22,5 @@ type Service interface {
RemoveOrgUser(context.Context, *RemoveOrgUserCommand) error
GetOrgUsers(context.Context, *GetOrgUsersQuery) ([]*OrgUserDTO, error)
SearchOrgUsers(context.Context, *SearchOrgUsersQuery) (*SearchOrgUsersQueryResult, error)
RegisterDelete(query string)
}

View File

@ -241,3 +241,7 @@ func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) {
limits.Set(userTag, cfg.Quota.User.Org)
return limits, nil
}
func (s *Service) RegisterDelete(query string) {
s.store.RegisterDelete(query)
}

View File

@ -154,3 +154,6 @@ func (f *FakeOrgStore) RemoveOrgUser(ctx context.Context, cmd *org.RemoveOrgUser
func (f *FakeOrgStore) Count(ctx context.Context, _ *quota.ScopeParameters) (*quota.Map, error) {
return nil, nil
}
func (f *FakeOrgStore) RegisterDelete(query string) {
}

View File

@ -46,6 +46,7 @@ type store interface {
RemoveOrgUser(context.Context, *org.RemoveOrgUserCommand) error
Count(context.Context, *quota.ScopeParameters) (*quota.Map, error)
RegisterDelete(query string)
}
type sqlStore struct {
@ -54,6 +55,7 @@ type sqlStore struct {
//TODO: moved to service
log log.Logger
cfg *setting.Cfg
deletes []string
}
func (ss *sqlStore) Get(ctx context.Context, orgID int64) (*org.Org, error) {
@ -239,8 +241,16 @@ func (ss *sqlStore) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) error
"DELETE FROM alert WHERE org_id = ?",
"DELETE FROM annotation WHERE org_id = ?",
"DELETE FROM kv_store WHERE org_id = ?",
"DELETE FROM team WHERE org_id = ?",
"DELETE FROM team_member WHERE org_id = ?",
"DELETE FROM team_role WHERE org_id = ?",
"DELETE FROM user_role WHERE org_id = ?",
"DELETE FROM builtin_role WHERE org_id = ?",
}
// Add registered deletes
deletes = append(deletes, ss.deletes...)
for _, sql := range deletes {
_, err := sess.Exec(sql, cmd.ID)
if err != nil {
@ -822,3 +832,8 @@ func removeUserOrg(sess *db.Session, userID int64) error {
_, err := sess.ID(userID).MustCols("org_id").Update(&user)
return err
}
// RegisterDelete registers a delete query to be executed when an org is deleted, used to delete enterprise data.
func (ss *sqlStore) RegisterDelete(query string) {
ss.deletes = append(ss.deletes, query)
}

View File

@ -99,3 +99,6 @@ func (f *FakeOrgService) RemoveOrgUser(ctx context.Context, cmd *org.RemoveOrgUs
func (f *FakeOrgService) SearchOrgUsers(ctx context.Context, query *org.SearchOrgUsersQuery) (*org.SearchOrgUsersQueryResult, error) {
return f.ExpectedSearchOrgUsersResult, f.ExpectedError
}
func (f *FakeOrgService) RegisterDelete(query string) {
}