From 0d8609a094d0446ce18a2406228a0745df491523 Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Wed, 10 Jan 2024 15:29:06 +0000 Subject: [PATCH] =?UTF-8?q?[AC-1682]=C2=A0Added=20script=20to=20migrate=20?= =?UTF-8?q?all=20organization=20data=20for=20flexible=20collections?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00_AllOrgsEnableCollectionEnhancements.sql | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 util/Migrator/DbScripts_transition/2024-01-10_00_AllOrgsEnableCollectionEnhancements.sql diff --git a/util/Migrator/DbScripts_transition/2024-01-10_00_AllOrgsEnableCollectionEnhancements.sql b/util/Migrator/DbScripts_transition/2024-01-10_00_AllOrgsEnableCollectionEnhancements.sql new file mode 100644 index 0000000000..fe37747760 --- /dev/null +++ b/util/Migrator/DbScripts_transition/2024-01-10_00_AllOrgsEnableCollectionEnhancements.sql @@ -0,0 +1,44 @@ +-- This script will enable collection enhancements for all organizations. + +-- Step 1: Insert into a temporary table with an additional column for batch processing, update 50 k at a time +SELECT [Id] AS [OrganizationId], CAST(ROW_NUMBER() OVER(ORDER BY [Id]) / 50000 AS INT) AS Batch +INTO #TempOrg +FROM [dbo].[Organization]; +-- WHERE [FlexibleCollections] = 0; + +-- Step 2: Get the maximum batch number +DECLARE @MaxBatch INT = (SELECT MAX(Batch) FROM #TempOrg); +DECLARE @CurrentBatch INT = 0; + +-- Step 3: Process each batch +WHILE @CurrentBatch <= @MaxBatch +BEGIN + -- Execute the stored procedure for each OrganizationId in the current batch + DECLARE @OrganizationId UNIQUEIDENTIFIER; + + DECLARE OrgCursor CURSOR FOR + SELECT [OrganizationId] + FROM #TempOrg + WHERE [Batch] = @CurrentBatch; + + OPEN OrgCursor; + + FETCH NEXT FROM OrgCursor INTO @OrganizationId; + + WHILE @@FETCH_STATUS = 0 + BEGIN + -- Execute the stored procedure for the current OrganizationId + EXEC [dbo].[Organization_EnableCollectionEnhancements] @OrganizationId; + + FETCH NEXT FROM OrgCursor INTO @OrganizationId; + END; + + CLOSE OrgCursor; + DEALLOCATE OrgCursor; + + -- Move to the next batch + SET @CurrentBatch = @CurrentBatch + 1; +END; + +-- Step 4: Drop the temporary table +DROP TABLE #TempOrg;