From 8510a753a8fcd61c8c5430ece9e08890404518c7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 2 Sep 2020 10:48:15 -0400 Subject: [PATCH] delete sso config when deleting org (#904) * delete ssouser on org and user delete * delete sso config when deleting org --- .../Organization_DeleteById.sql | 10 ++++ .../2020-09-02_00_DeleteSsoConfig.sql | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 util/Migrator/DbScripts/2020-09-02_00_DeleteSsoConfig.sql diff --git a/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql index 3fdb9948f7..ddbbafefff 100644 --- a/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Organization_DeleteById.sql @@ -23,15 +23,25 @@ BEGIN 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 FROM [dbo].[Organization] WHERE [Id] = @Id + + COMMIT TRANSACTION Organization_DeleteById END \ No newline at end of file diff --git a/util/Migrator/DbScripts/2020-09-02_00_DeleteSsoConfig.sql b/util/Migrator/DbScripts/2020-09-02_00_DeleteSsoConfig.sql new file mode 100644 index 0000000000..7175b6e600 --- /dev/null +++ b/util/Migrator/DbScripts/2020-09-02_00_DeleteSsoConfig.sql @@ -0,0 +1,54 @@ +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 + FROM + [dbo].[Organization] + WHERE + [Id] = @Id + + COMMIT TRANSACTION Organization_DeleteById +END +GO