Skip to content

Commit 4b32656

Browse files
authored
Merge pull request #745 from TechnologyEnhancedLearning/Develop/Fixes/TD-5036-Update-Database-Schema-and-Include-Trigger
TD-5036: Update Database Schema and Include Trigger
2 parents 75cc7ee + b73136b commit 4b32656

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@
534534
<Build Include="Tables\Resource\ScormResourceReferenceEvent.sql" />
535535
<Build Include="Tables\Resource\ScormResourceReferenceEventType.sql" />
536536
<None Include="Scripts\Post-Deploy\Scripts\UpdateFileTypes.sql" />
537-
<Build Include="Triggers\external\AddDigitalLearningSolutionsExternalSystem.sql" />
537+
<None Include="Scripts\Post-Deploy\Scripts\AddDigitalLearningSolutionsExternalSystem.sql" />
538538
</ItemGroup>
539539
<ItemGroup>
540540
<None Include="Scripts\Pre-Deploy\Scripts\Card5766_AuthorTableChanges.PreDeployment.sql" />

WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Script.PostDeployment.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ END
6969

7070
UPDATE [resources].[ResourceVersion] SET CertificateEnabled = 0 WHERE VersionStatusId <> 1 AND CertificateEnabled IS NULL
7171

72+
-- do not delete the script AddDigitalLearningSolutionsExternalSystem
73+
:r .\Scripts\AddDigitalLearningSolutionsExternalSystem.sql
7274
:r .\Scripts\Bookmark.sql
7375
:r .\Scripts\PopulateEventTypes.sql
7476
:r .\Scripts\InternalSystem.sql
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
-------------------------------------------------------------------------------
3+
-- Author Colin Beeby
4+
-- Created 21-03-2024
5+
-- Purpose Ensure that when a user is associated with external system DigitalLearningSolutionsSso, they are also associated with DigitalLearningSolutions
6+
--
7+
-- Modification History
8+
--
9+
-- 21-03-2024 ColB Initial Version
10+
-- 15-11-2024 SA Modified the script to execute check if the trigger exists in the table schema
11+
-------------------------------------------------------------------------------
12+
13+
-- Check if the trigger exists in the [external] schema and the [ExternalSystemUser] table
14+
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'InsertTrigger_AddDigitalLearningSolutionsExternalSystem' AND parent_id = OBJECT_ID('[external].[ExternalSystemUser]'))
15+
BEGIN
16+
-- Create the trigger if it doesn't exist
17+
EXEC('
18+
CREATE TRIGGER [external].InsertTrigger_AddDigitalLearningSolutionsExternalSystem
19+
ON [external].[ExternalSystemUser]
20+
FOR INSERT
21+
AS
22+
BEGIN
23+
DECLARE @DigitalLearningSolutionsId INT;
24+
DECLARE @DigitalLearningSolutionsSsoId INT;
25+
26+
-- Get the DigitalLearningSolutions ExternalSystemId
27+
SELECT @DigitalLearningSolutionsId = id
28+
FROM [external].[ExternalSystem]
29+
WHERE code = ''DigitalLearningSolutions'';
30+
31+
-- Get the DigitalLearningSolutionsSso ExternalSystemId
32+
SELECT @DigitalLearningSolutionsSsoId = id
33+
FROM [external].[ExternalSystem]
34+
WHERE code = ''DigitalLearningSolutionsSso'';
35+
36+
-- Check if the inserted row has ExternalSystemId matching DigitalLearningSolutionsSsoId
37+
IF EXISTS (SELECT 1 FROM inserted WHERE ExternalSystemId = @DigitalLearningSolutionsSsoId)
38+
BEGIN
39+
-- Insert into ExternalSystemUser table
40+
INSERT INTO [external].[ExternalSystemUser]
41+
(UserId, ExternalSystemId, Deleted, CreateUserId, CreateDate, AmendUserId, AmendDate)
42+
SELECT UserId, @DigitalLearningSolutionsId, Deleted, CreateUserId, CreateDate, AmendUserId, AmendDate
43+
FROM inserted;
44+
END
45+
END;
46+
');
47+
END;

0 commit comments

Comments
 (0)