Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete OrgUsers When Deleting An Org #964

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ BEGIN
WHERE
[OrganizationId] = @Id

DELETE CU
FROM
[dbo].[CollectionUser] CU
INNER JOIN
[dbo].[OrganizationUser] OU ON [CU].[OrganizationUserId] = [OU].[Id]
WHERE
[OU].[OrganizationId] = @Id

DELETE
FROM
[dbo].[OrganizationUser]
WHERE
[OrganizationId] = @Id

DELETE
FROM
[dbo].[Organization]
WHERE
[Id] = @Id

COMMIT TRANSACTION Organization_DeleteById
END
END
69 changes: 69 additions & 0 deletions util/Migrator/DbScripts/2020-10-08_00_DeleteOrgUserWithOrg
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
IF OBJECT_ID('[dbo].[Organization_DeleteById]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Organization_DeleteById]
END
GO

CREATE PROCEDURE [dbo].[Organization_DeleteById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @Id

DECLARE @BatchSize INT = 100
WHILE @BatchSize > 0
BEGIN
BEGIN TRANSACTION Organization_DeleteById_Ciphers

DELETE TOP(@BatchSize)
FROM
[dbo].[Cipher]
WHERE
[UserId] IS NULL
AND [OrganizationId] = @Id

SET @BatchSize = @@ROWCOUNT

COMMIT TRANSACTION Organization_DeleteById_Ciphers
END

BEGIN TRANSACTION Organization_DeleteById

DELETE
FROM
[dbo].[SsoUser]
WHERE
[OrganizationId] = @Id

DELETE
FROM
[dbo].[SsoConfig]
WHERE
[OrganizationId] = @Id

DELETE CU
FROM
[dbo].[CollectionUser] CU
INNER JOIN
[dbo].[OrganizationUser] OU ON [CU].[OrganizationUserId] = [OU].[Id]
WHERE
[OU].[OrganizationId] = @Id

DELETE
FROM
[dbo].[OrganizationUser]
WHERE
[OrganizationId] = @Id

DELETE
FROM
[dbo].[Organization]
WHERE
[Id] = @Id

COMMIT TRANSACTION Organization_DeleteById
END

GO