From b73136ba92efee087672f4956cd637e33c85c468 Mon Sep 17 00:00:00 2001 From: Swapnamol Abraham Date: Fri, 15 Nov 2024 14:39:48 +0000 Subject: [PATCH] TD-5036: Update Database Schema and Include Trigger --- .../LearningHub.Nhs.Database.sqlproj | 2 +- .../Post-Deploy/Script.PostDeployment.sql | 2 + ...DigitalLearningSolutionsExternalSystem.sql | 47 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/AddDigitalLearningSolutionsExternalSystem.sql diff --git a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj index e3ceaba79..054baf7a8 100644 --- a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj +++ b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj @@ -534,7 +534,7 @@ - + diff --git a/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Script.PostDeployment.sql b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Script.PostDeployment.sql index 2d0cae38b..34ba8ad39 100644 --- a/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Script.PostDeployment.sql +++ b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Script.PostDeployment.sql @@ -69,6 +69,8 @@ END UPDATE [resources].[ResourceVersion] SET CertificateEnabled = 0 WHERE VersionStatusId <> 1 AND CertificateEnabled IS NULL +-- do not delete the script AddDigitalLearningSolutionsExternalSystem +:r .\Scripts\AddDigitalLearningSolutionsExternalSystem.sql :r .\Scripts\Bookmark.sql :r .\Scripts\PopulateEventTypes.sql :r .\Scripts\InternalSystem.sql diff --git a/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/AddDigitalLearningSolutionsExternalSystem.sql b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/AddDigitalLearningSolutionsExternalSystem.sql new file mode 100644 index 000000000..4ca9670d4 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/AddDigitalLearningSolutionsExternalSystem.sql @@ -0,0 +1,47 @@ + +------------------------------------------------------------------------------- +-- Author Colin Beeby +-- Created 21-03-2024 +-- Purpose Ensure that when a user is associated with external system DigitalLearningSolutionsSso, they are also associated with DigitalLearningSolutions +-- +-- Modification History +-- +-- 21-03-2024 ColB Initial Version +-- 15-11-2024 SA Modified the script to execute check if the trigger exists in the table schema +------------------------------------------------------------------------------- + +-- Check if the trigger exists in the [external] schema and the [ExternalSystemUser] table +IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = 'InsertTrigger_AddDigitalLearningSolutionsExternalSystem' AND parent_id = OBJECT_ID('[external].[ExternalSystemUser]')) +BEGIN + -- Create the trigger if it doesn't exist + EXEC(' + CREATE TRIGGER [external].InsertTrigger_AddDigitalLearningSolutionsExternalSystem + ON [external].[ExternalSystemUser] + FOR INSERT + AS + BEGIN + DECLARE @DigitalLearningSolutionsId INT; + DECLARE @DigitalLearningSolutionsSsoId INT; + + -- Get the DigitalLearningSolutions ExternalSystemId + SELECT @DigitalLearningSolutionsId = id + FROM [external].[ExternalSystem] + WHERE code = ''DigitalLearningSolutions''; + + -- Get the DigitalLearningSolutionsSso ExternalSystemId + SELECT @DigitalLearningSolutionsSsoId = id + FROM [external].[ExternalSystem] + WHERE code = ''DigitalLearningSolutionsSso''; + + -- Check if the inserted row has ExternalSystemId matching DigitalLearningSolutionsSsoId + IF EXISTS (SELECT 1 FROM inserted WHERE ExternalSystemId = @DigitalLearningSolutionsSsoId) + BEGIN + -- Insert into ExternalSystemUser table + INSERT INTO [external].[ExternalSystemUser] + (UserId, ExternalSystemId, Deleted, CreateUserId, CreateDate, AmendUserId, AmendDate) + SELECT UserId, @DigitalLearningSolutionsId, Deleted, CreateUserId, CreateDate, AmendUserId, AmendDate + FROM inserted; + END + END; + '); +END;