From c37016e19e4b1bb030cda2c91c423a1c63093cb5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 7 Mar 2018 00:27:06 -0500 Subject: [PATCH] use completion table on login uris update --- .../DbScripts/2018-02-28_00_LoginUris.sql | 72 +++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/util/Setup/DbScripts/2018-02-28_00_LoginUris.sql b/util/Setup/DbScripts/2018-02-28_00_LoginUris.sql index 12e0b08c9..356457528 100644 --- a/util/Setup/DbScripts/2018-02-28_00_LoginUris.sql +++ b/util/Setup/DbScripts/2018-02-28_00_LoginUris.sql @@ -19,17 +19,41 @@ } */ -SET NOCOUNT ON +IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CompletedIds') +BEGIN + CREATE TABLE [CompletedIds] ([Id] UNIQUEIDENTIFIER PRIMARY KEY) +END +GO DECLARE @UriPath VARCHAR(50) = '$.Uri' DECLARE @UrisPath VARCHAR(50) = '$.Uris' DECLARE @BatchSize INT = 1000 +DECLARE @LastId UNIQUEIDENTIFIER = NULL -- Step 1: Add new Uris property with data from Uri WHILE @BatchSize > 0 BEGIN - UPDATE TOP (@BatchSize) - [Cipher] + SELECT TOP 1 + @LastId = [Id] + FROM + [CompletedIds] + ORDER BY + [Id] DESC + + ;WITH [CTE] AS ( + SELECT TOP (@BatchSize) + * + FROM + [Cipher] + WHERE + (@LastId IS NULL OR [Id] > @LastId) + AND JSON_VALUE([Data], @UriPath) IS NOT NULL + AND JSON_QUERY([Data], @UrisPath) IS NULL + ORDER BY + [Id] ASC + ) + UPDATE + [CTE] SET [Data] = JSON_MODIFY( [Data], @@ -39,27 +63,51 @@ BEGIN '$' ) ) - WHERE - JSON_VALUE([Data], @UriPath) IS NOT NULL - AND JSON_QUERY([Data], @UrisPath) IS NULL + OUTPUT INSERTED.[Id] INTO [CompletedIds] SET @BatchSize = @@ROWCOUNT RAISERROR('Updated %d ciphers with Uris', 0, 1, @BatchSize) WITH NOWAIT END --- Step 2: Remove old Uri Property +-- Reset +TRUNCATE TABLE [CompletedIds] SET @BatchSize = 1000 +-- Step 2: Remove old Uri Property WHILE @BatchSize > 0 BEGIN - UPDATE TOP (@BatchSize) - [Cipher] + SELECT TOP 1 + @LastId = [Id] + FROM + [CompletedIds] + ORDER BY + [Id] DESC + + ;WITH [CTE] AS ( + SELECT TOP (@BatchSize) + * + FROM + [Cipher] + WHERE + (@LastId IS NULL OR [Id] > @LastId) + AND JSON_VALUE([Data], @UriPath) IS NOT NULL + AND JSON_QUERY([Data], @UrisPath) IS NOT NULL + ORDER BY + [Id] ASC + ) + UPDATE + [CTE] SET [Data] = JSON_MODIFY([Data], @UriPath, NULL) - WHERE - JSON_VALUE([Data], @UriPath) IS NOT NULL - AND JSON_QUERY([Data], @UrisPath) IS NOT NULL + OUTPUT INSERTED.[Id] INTO [CompletedIds] SET @BatchSize = @@ROWCOUNT RAISERROR('Updated %d ciphers with Uri removal', 0, 1, @BatchSize) WITH NOWAIT END +GO + +IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CompletedIds') +BEGIN + DROP TABLE [CompletedIds] +END +GO