1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 00:22:50 -05:00

[AC-1682] Placed temp tables outside transactions

This commit is contained in:
Rui Tome
2024-01-11 18:01:11 +00:00
parent 3bf1b53536
commit c4ad7d72e8
2 changed files with 484 additions and 484 deletions

View File

@ -5,8 +5,6 @@ BEGIN
SET NOCOUNT ON
-- Step 1: AccessAll migration for Groups
BEGIN TRY
BEGIN TRANSACTION;
-- Create a temporary table to store the groups with AccessAll = 1
SELECT [Id] AS [GroupId], [OrganizationId]
INTO #TempGroup
@ -14,6 +12,14 @@ BEGIN
WHERE [AccessAll] = 1
AND [OrganizationId] = @OrganizationId;
-- Create a temporary table to store distinct OrganizationUserIds
SELECT DISTINCT GU.[OrganizationUserId]
INTO #TempOrganizationUsers
FROM [dbo].[GroupUser] GU
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
BEGIN TRY
BEGIN TRANSACTION;
-- Update existing rows in [dbo].[CollectionGroup]
UPDATE CG
SET
@ -39,12 +45,6 @@ BEGIN
FROM [dbo].[Group] G
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
-- Create a temporary table to store distinct OrganizationUserIds
SELECT DISTINCT GU.[OrganizationUserId]
INTO #TempOrganizationUsers
FROM [dbo].[GroupUser] GU
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
@ -66,10 +66,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempGroup;
DROP TABLE #TempOrganizationUsers;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -77,9 +73,11 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempGroup;
DROP TABLE #TempOrganizationUsers;
-- Step 2: AccessAll migration for users
BEGIN TRY
BEGIN TRANSACTION;
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser
@ -87,6 +85,8 @@ BEGIN
WHERE [AccessAll] = 1
AND [OrganizationId] = @OrganizationId;
BEGIN TRY
BEGIN TRANSACTION;
-- Update existing rows in [dbo].[CollectionUser]
UPDATE target
SET
@ -140,9 +140,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempOrgUser;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -150,9 +147,10 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempOrgUser;
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT ou.[Id] AS [OrganizationUserId]
INTO #TempStep3
@ -160,6 +158,8 @@ BEGIN
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
BEGIN TRY
BEGIN TRANSACTION;
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
UPDATE cu
SET cu.[ReadOnly] = 0,
@ -189,9 +189,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep3;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -199,9 +196,10 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep3;
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
INTO #TempStep4
@ -214,6 +212,8 @@ BEGIN
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
);
BEGIN TRY
BEGIN TRANSACTION;
-- Insert rows into [dbo].[CollectionUser] using the temporary table
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
@ -240,9 +240,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep4;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -250,15 +247,18 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep4;
-- Step 5: Set all Managers to Users
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT [Id] AS [OrganizationUserId]
INTO #TempStep5
FROM [dbo].[OrganizationUser]
WHERE [Type] = 3; -- Manager
BEGIN TRY
BEGIN TRANSACTION;
-- Update [dbo].[OrganizationUser] based on the temporary table
UPDATE ou
SET ou.[Type] = 2 -- User
@ -286,14 +286,14 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep5;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep5;
END
GO

View File

@ -5,8 +5,6 @@ BEGIN
SET NOCOUNT ON
-- Step 1: AccessAll migration for Groups
BEGIN TRY
BEGIN TRANSACTION;
-- Create a temporary table to store the groups with AccessAll = 1
SELECT [Id] AS [GroupId], [OrganizationId]
INTO #TempGroup
@ -14,6 +12,14 @@ BEGIN
WHERE [AccessAll] = 1
AND [OrganizationId] = @OrganizationId;
-- Create a temporary table to store distinct OrganizationUserIds
SELECT DISTINCT GU.[OrganizationUserId]
INTO #TempOrganizationUsers
FROM [dbo].[GroupUser] GU
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
BEGIN TRY
BEGIN TRANSACTION;
-- Update existing rows in [dbo].[CollectionGroup]
UPDATE CG
SET
@ -39,12 +45,6 @@ BEGIN
FROM [dbo].[Group] G
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
-- Create a temporary table to store distinct OrganizationUserIds
SELECT DISTINCT GU.[OrganizationUserId]
INTO #TempOrganizationUsers
FROM [dbo].[GroupUser] GU
JOIN #TempGroup TG ON GU.[GroupId] = TG.[GroupId];
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
DECLARE @Step1OrganizationUserId UNIQUEIDENTIFIER
@ -66,10 +66,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempGroup;
DROP TABLE #TempOrganizationUsers;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -77,9 +73,11 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempGroup;
DROP TABLE #TempOrganizationUsers;
-- Step 2: AccessAll migration for users
BEGIN TRY
BEGIN TRANSACTION;
-- Create a temporary table to store the OrganizationUsers with AccessAll = 1
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser
@ -87,6 +85,8 @@ BEGIN
WHERE [AccessAll] = 1
AND [OrganizationId] = @OrganizationId;
BEGIN TRY
BEGIN TRANSACTION;
-- Update existing rows in [dbo].[CollectionUser]
UPDATE target
SET
@ -140,9 +140,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempOrgUser;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -150,9 +147,10 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempOrgUser;
-- Step 3: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT ou.[Id] AS [OrganizationUserId]
INTO #TempStep3
@ -160,6 +158,8 @@ BEGIN
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
BEGIN TRY
BEGIN TRANSACTION;
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
UPDATE cu
SET cu.[ReadOnly] = 0,
@ -189,9 +189,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep3;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -199,9 +196,10 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep3;
-- Step 4: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
INTO #TempStep4
@ -214,6 +212,8 @@ BEGIN
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
);
BEGIN TRY
BEGIN TRANSACTION;
-- Insert rows into [dbo].[CollectionUser] using the temporary table
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
@ -240,9 +240,6 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep4;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
@ -250,15 +247,18 @@ BEGIN
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep4;
-- Step 5: Set all Managers to Users
BEGIN TRY
BEGIN TRANSACTION;
-- Store the results in a temporary table
SELECT [Id] AS [OrganizationUserId]
INTO #TempStep5
FROM [dbo].[OrganizationUser]
WHERE [Type] = 3; -- Manager
BEGIN TRY
BEGIN TRANSACTION;
-- Update [dbo].[OrganizationUser] based on the temporary table
UPDATE ou
SET ou.[Type] = 2 -- User
@ -286,14 +286,14 @@ BEGIN
CLOSE UniqueOrgUserIdCursor
DEALLOCATE UniqueOrgUserIdCursor;
-- Drop the temporary table
DROP TABLE #TempStep5;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH;
-- Drop the temporary table
DROP TABLE #TempStep5;
END
GO