From eb34cc49c6ac497df1b29769bba403d27862be61 Mon Sep 17 00:00:00 2001 From: Chad Scharf <3904944+cscharf@users.noreply.github.com> Date: Thu, 2 Apr 2020 13:45:53 -0400 Subject: [PATCH] Fixed date time precision assignment for DeletedDate and RevisionDate (performance + match/data quality) --- .../dbo/Stored Procedures/Cipher_Restore.sql | 4 +- .../Stored Procedures/Cipher_SoftDelete.sql | 6 +- .../2020-04-02_00_CipherSoftDelete.sql | 136 ++++++++++++++++++ 3 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 util/Migrator/DbScripts/2020-04-02_00_CipherSoftDelete.sql diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Restore.sql b/src/Sql/dbo/Stored Procedures/Cipher_Restore.sql index fe127e262a..9480e82f42 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Restore.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Restore.sql @@ -23,11 +23,13 @@ BEGIN [Edit] = 1 AND [Id] IN (SELECT * FROM @Ids) + DECLARE @UtcNow DATETIME2(7); + SET @UtcNow = GETUTCDATE(); UPDATE [dbo].[Cipher] SET [DeletedDate] = NULL, - [RevisionDate] = GETUTCDATE() + [RevisionDate] = @UtcNow WHERE [Id] IN (SELECT [Id] FROM #Temp) diff --git a/src/Sql/dbo/Stored Procedures/Cipher_SoftDelete.sql b/src/Sql/dbo/Stored Procedures/Cipher_SoftDelete.sql index e329a1aaa3..126d354420 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_SoftDelete.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_SoftDelete.sql @@ -24,11 +24,13 @@ BEGIN AND [Id] IN (SELECT * FROM @Ids) -- Delete ciphers + DECLARE @UtcNow DATETIME2(7); + SET @UtcNow = GETUTCDATE(); UPDATE [dbo].[Cipher] SET - [DeletedDate] = SYSUTCDATETIME(), - [RevisionDate] = GETUTCDATE() + [DeletedDate] = @UtcNow, + [RevisionDate] = @UtcNow WHERE [Id] IN (SELECT [Id] FROM #Temp) diff --git a/util/Migrator/DbScripts/2020-04-02_00_CipherSoftDelete.sql b/util/Migrator/DbScripts/2020-04-02_00_CipherSoftDelete.sql new file mode 100644 index 0000000000..01ec1fb341 --- /dev/null +++ b/util/Migrator/DbScripts/2020-04-02_00_CipherSoftDelete.sql @@ -0,0 +1,136 @@ +/** + * Fix bulk Restore/Soft Delete sprocs for DateTime assignment + */ +IF OBJECT_ID('[dbo].[Cipher_Restore]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Cipher_Restore]; +END +GO +CREATE PROCEDURE [dbo].[Cipher_Restore] + @Ids AS [dbo].[GuidIdArray] READONLY, + @UserId AS UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + CREATE TABLE #Temp + ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [UserId] UNIQUEIDENTIFIER NULL, + [OrganizationId] UNIQUEIDENTIFIER NULL + ) + + INSERT INTO #Temp + SELECT + [Id], + [UserId], + [OrganizationId] + FROM + [dbo].[UserCipherDetails](@UserId) + WHERE + [Edit] = 1 + AND [Id] IN (SELECT * FROM @Ids) + + DECLARE @UtcNow DATETIME2(7); + SET @UtcNow = GETUTCDATE(); + UPDATE + [dbo].[Cipher] + SET + [DeletedDate] = NULL, + [RevisionDate] = @UtcNow + WHERE + [Id] IN (SELECT [Id] FROM #Temp) + + -- Bump orgs + DECLARE @OrgId UNIQUEIDENTIFIER + DECLARE [OrgCursor] CURSOR FORWARD_ONLY FOR + SELECT + [OrganizationId] + FROM + #Temp + WHERE + [OrganizationId] IS NOT NULL + GROUP BY + [OrganizationId] + OPEN [OrgCursor] + FETCH NEXT FROM [OrgCursor] INTO @OrgId + WHILE @@FETCH_STATUS = 0 BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId + FETCH NEXT FROM [OrgCursor] INTO @OrgId + END + CLOSE [OrgCursor] + DEALLOCATE [OrgCursor] + + -- Bump user + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + + DROP TABLE #Temp +END +GO + +IF OBJECT_ID('[dbo].[Cipher_SoftDelete]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Cipher_SoftDelete]; +END +GO +CREATE PROCEDURE [dbo].[Cipher_SoftDelete] + @Ids AS [dbo].[GuidIdArray] READONLY, + @UserId AS UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + CREATE TABLE #Temp + ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [UserId] UNIQUEIDENTIFIER NULL, + [OrganizationId] UNIQUEIDENTIFIER NULL + ) + + INSERT INTO #Temp + SELECT + [Id], + [UserId], + [OrganizationId] + FROM + [dbo].[UserCipherDetails](@UserId) + WHERE + [Edit] = 1 + AND [Id] IN (SELECT * FROM @Ids) + + -- Delete ciphers + DECLARE @UtcNow DATETIME2(7); + SET @UtcNow = GETUTCDATE(); + UPDATE + [dbo].[Cipher] + SET + [DeletedDate] = @UtcNow, + [RevisionDate] = @UtcNow + WHERE + [Id] IN (SELECT [Id] FROM #Temp) + + -- Cleanup orgs + DECLARE @OrgId UNIQUEIDENTIFIER + DECLARE [OrgCursor] CURSOR FORWARD_ONLY FOR + SELECT + [OrganizationId] + FROM + #Temp + WHERE + [OrganizationId] IS NOT NULL + GROUP BY + [OrganizationId] + OPEN [OrgCursor] + FETCH NEXT FROM [OrgCursor] INTO @OrgId + WHILE @@FETCH_STATUS = 0 BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId + FETCH NEXT FROM [OrgCursor] INTO @OrgId + END + CLOSE [OrgCursor] + DEALLOCATE [OrgCursor] + + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + + DROP TABLE #Temp +END +GO \ No newline at end of file