Skip to content

Commit 7a79a5e

Browse files
committed
TD-6072 script
1 parent 7f2f7cc commit 7a79a5e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
<None Include="Scripts\Pre-Deploy\Scripts\TD-887_PreLoginLandingPageChanges.sql" />
203203
<None Include="Scripts\Pre-Deploy\Scripts\TD-2902 Add resource types to Content Server.sql" />
204204
<None Include="Scripts\Post-Deploy\Scripts\TD-6109_Enable_CDC.sql" />
205+
<None Include="Scripts\LH Database Scripts\Move CC resource to a custom catalogue.sql" />
205206
</ItemGroup>
206207
<ItemGroup>
207208
<RefactorLog Include="LearningHub.Nhs.Database.refactorlog" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
BEGIN TRY
3+
BEGIN TRANSACTION;
4+
5+
DECLARE @NewCatalogueId INT = 0; --this is the NodePathId in the resource reference table
6+
DECLARE @ResourceOwnerId INT = 0; -- this is CreateUserId of the resources to be moved.
7+
DECLARE @NewNodeId INT;
8+
DECLARE @NextDisplayOrder INT;
9+
10+
-- Get the NodeId for the new catalogue
11+
SELECT @NewNodeId = NodeId FROM [hierarchy].[NodePath] WHERE Id = @NewCatalogueId AND Deleted = 0;
12+
13+
-- Get the current max DisplayOrder for that Node
14+
SELECT @NextDisplayOrder = ISNULL(MAX(DisplayOrder), 0) FROM [hierarchy].[NodeResource] WHERE NodeId = @NewNodeId AND Deleted = 0;
15+
16+
DECLARE @UpdatedResources TABLE (ResourceId INT);
17+
18+
INSERT INTO @UpdatedResources (ResourceId)
19+
SELECT DISTINCT rr.ResourceId FROM [resources].[ResourceReference] rr
20+
INNER JOIN [resources].[Resource] r
21+
ON rr.ResourceId = r.Id
22+
WHERE r.CreateUserId = @ResourceOwnerId AND r.Deleted = 0 AND rr.CreateUserId = @ResourceOwnerId AND rr.Deleted = 0 AND rr.NodePathId = 1;
23+
24+
-- Update ResourceReference to point to new catalogue
25+
UPDATE rr
26+
SET rr.NodePathId = @NewCatalogueId
27+
FROM [resources].[ResourceReference] rr
28+
INNER JOIN @UpdatedResources ur
29+
ON rr.ResourceId = ur.ResourceId
30+
WHERE rr.CreateUserId = @ResourceOwnerId
31+
AND rr.Deleted = 0
32+
AND rr.NodePathId = 1;
33+
34+
-- Update NodeResource with new NodeId and incrementing DisplayOrder
35+
;WITH Ordered AS
36+
(
37+
SELECT
38+
nr.ResourceId,
39+
ROW_NUMBER() OVER (ORDER BY nr.ResourceId) AS RowNum
40+
FROM [hierarchy].[NodeResource] nr
41+
INNER JOIN @UpdatedResources ur
42+
ON nr.ResourceId = ur.ResourceId
43+
WHERE nr.NodeId = 1
44+
AND nr.CreateUserId = @ResourceOwnerId
45+
AND nr.Deleted = 0
46+
)
47+
UPDATE nr
48+
SET nr.NodeId = @NewNodeId,nr.DisplayOrder = @NextDisplayOrder + o.RowNum
49+
FROM [hierarchy].[NodeResource] nr
50+
INNER JOIN Ordered o ON nr.ResourceId = o.ResourceId;
51+
52+
53+
--update noderesourcelookup(Im not sure this is absolutely necessary)
54+
update nrl set nrl.NodeId=@NewNodeId FROM [hierarchy].[NodeResourceLookup] nrl
55+
INNER JOIN @UpdatedResources ur ON nrl.ResourceId = ur.ResourceId
56+
WHERE nrl.CreateUserId = @ResourceOwnerId AND nrl.Deleted = 0
57+
58+
-- Show updated resources
59+
SELECT DISTINCT rv.ResourceId, rv.Title FROM [resources].[ResourceVersion] rv
60+
INNER JOIN @UpdatedResources ur ON rv.ResourceId = ur.ResourceId;
61+
62+
COMMIT TRANSACTION;
63+
END TRY
64+
BEGIN CATCH
65+
ROLLBACK TRANSACTION;
66+
67+
SELECT
68+
ERROR_NUMBER() AS ErrorNumber,
69+
ERROR_MESSAGE() AS ErrorMessage;
70+
END CATCH;

0 commit comments

Comments
 (0)