-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SM-1075] Fix bulk remove organization users with Secrets Manager (#4197
- Loading branch information
1 parent
c4f176a
commit 8a1b64a
Showing
2 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
util/Migrator/DbScripts/2024-06-20_00_FixOrganizationUserDeleteByIds.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_DeleteByIds] | ||
@Ids [dbo].[GuidIdArray] READONLY | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @Ids | ||
|
||
DECLARE @UserAndOrganizationIds [dbo].[TwoGuidIdArray] | ||
|
||
INSERT INTO @UserAndOrganizationIds | ||
(Id1, Id2) | ||
SELECT | ||
UserId, | ||
OrganizationId | ||
FROM | ||
[dbo].[OrganizationUser] OU | ||
INNER JOIN | ||
@Ids OUIds ON OUIds.Id = OU.Id | ||
WHERE | ||
UserId IS NOT NULL AND | ||
OrganizationId IS NOT NULL | ||
|
||
BEGIN | ||
EXEC [dbo].[SsoUser_DeleteMany] @UserAndOrganizationIds | ||
END | ||
|
||
DECLARE @BatchSize INT = 100 | ||
|
||
-- Delete CollectionUsers | ||
WHILE @BatchSize > 0 | ||
BEGIN | ||
BEGIN TRANSACTION CollectionUser_DeleteMany_CUs | ||
|
||
DELETE TOP(@BatchSize) CU | ||
FROM | ||
[dbo].[CollectionUser] CU | ||
INNER JOIN | ||
@Ids I ON I.Id = CU.OrganizationUserId | ||
|
||
SET @BatchSize = @@ROWCOUNT | ||
|
||
COMMIT TRANSACTION CollectionUser_DeleteMany_CUs | ||
END | ||
|
||
SET @BatchSize = 100; | ||
|
||
-- Delete GroupUsers | ||
WHILE @BatchSize > 0 | ||
BEGIN | ||
BEGIN TRANSACTION GroupUser_DeleteMany_GroupUsers | ||
|
||
DELETE TOP(@BatchSize) GU | ||
FROM | ||
[dbo].[GroupUser] GU | ||
INNER JOIN | ||
@Ids I ON I.Id = GU.OrganizationUserId | ||
|
||
SET @BatchSize = @@ROWCOUNT | ||
|
||
COMMIT TRANSACTION GroupUser_DeleteMany_GroupUsers | ||
END | ||
|
||
SET @BatchSize = 100; | ||
|
||
-- Delete User Access Policies | ||
WHILE @BatchSize > 0 | ||
BEGIN | ||
BEGIN TRANSACTION AccessPolicy_DeleteMany_Users | ||
|
||
DELETE TOP(@BatchSize) AP | ||
FROM | ||
[dbo].[AccessPolicy] AP | ||
INNER JOIN | ||
@Ids I ON I.Id = AP.OrganizationUserId | ||
|
||
SET @BatchSize = @@ROWCOUNT | ||
|
||
COMMIT TRANSACTION AccessPolicy_DeleteMany_Users | ||
END | ||
|
||
EXEC [dbo].[OrganizationSponsorship_OrganizationUsersDeleted] @Ids | ||
|
||
SET @BatchSize = 100; | ||
|
||
-- Delete OrganizationUsers | ||
WHILE @BatchSize > 0 | ||
BEGIN | ||
BEGIN TRANSACTION OrganizationUser_DeleteMany_OUs | ||
|
||
DELETE TOP(@BatchSize) OU | ||
FROM | ||
[dbo].[OrganizationUser] OU | ||
INNER JOIN | ||
@Ids I ON I.Id = OU.Id | ||
|
||
SET @BatchSize = @@ROWCOUNT | ||
|
||
COMMIT TRANSACTION OrganizationUser_DeleteMany_OUs | ||
END | ||
END | ||
GO |