|
| 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