Skip to content

Commit 633f28b

Browse files
authored
Merge pull request #1561 from TechnologyEnhancedLearning/Develop/Features/TD-6148-Create-a-PoC-mechanism-which-will-allow-us-to-synchronise-user-tables-between-ELFH-and-User-Profile-DBs
TD-6148: Hotfix changes and production release changes
2 parents 09f926d + 38ee2a9 commit 633f28b

File tree

9 files changed

+111
-104
lines changed

9 files changed

+111
-104
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,6 @@
679679
<Build Include="Stored Procedures\Adf\AdfMergeUserEmploymentResponsibility.sql" />
680680
<Build Include="Stored Procedures\Adf\AdfMergeUserGroupTypeInputValidation.sql" />
681681
<Build Include="Stored Procedures\Adf\AdfMergeUserHistory.sql" />
682-
<Build Include="Stored Procedures\Adf\AdfMergeUserHistoryAttribute.sql" />
683682
<Build Include="Stored Procedures\Adf\AdfMergeUserHistoryType.sql" />
684683
<Build Include="Stored Procedures\Adf\AdfMergeUserPasswordValidationToke.sql" />
685684
<Build Include="Stored Procedures\Adf\AdfMergeUserProfileData.sql" />
@@ -690,6 +689,7 @@
690689
<None Include="Scripts\Post-Deploy\Scripts\TD-6148-ADFtableData.sql" />
691690
<Build Include="Tables\Adf\ADFSyncMetadata.sql" />
692691
<Build Include="Stored Procedures\Adf\proc_UpdateLastSyncTimeAdf.sql" />
692+
<Build Include="Stored Procedures\Adf\AdfMergeUserGroupReporter.sql" />
693693
<Build Include="Tables\Databricks\ReportHistory.sql" />
694694
<Build Include="Tables\Elfh\UserGroupReporterTBL.sql" />
695695
<None Include="Scripts\Post-Deploy\Scripts\TD-6625-AddtionalTable-CDC.sql" />

WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/TD-6148-ADFtableData.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ VALUES
4242
('ELFHtoLH', 'JobRoleTbl', '1900-01-01'),
4343
('ELFHtoLH', 'userTermsAndConditionsTBL', '1900-01-01'),
4444
('ELFHtoLH', 'medicalCouncilTBL', '1900-01-01'),
45-
('ELFHtoLH', 'staffGroupTBL', '1900-01-01')
45+
('ELFHtoLH', 'staffGroupTBL', '1900-01-01'),
46+
('ELFHtoLH', 'usergroupreportertbl', '1900-01-01')

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeUserAdminLocation.sql

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,56 @@
77
--
88
-- 04-11-2025 Sarathlal Initial Revision
99
-------------------------------------------------------------------------------
10-
CREATE PROCEDURE [AdfMergeUserAdminLocation]
10+
CREATE PROCEDURE [dbo].[AdfMergeUserAdminLocation]
1111
@UserAdminLocationList dbo.UserAdminLocationType READONLY
1212
AS
1313
BEGIN
1414
SET NOCOUNT ON;
1515

16-
MERGE [elfh].[userAdminLocationTBL] AS target
17-
USING @UserAdminLocationList AS source
18-
ON target.[userId] = source.[userId]
19-
AND target.[adminLocationId] = source.[adminLocationId] -- composite key match
16+
-- Deduplicate source first
17+
;WITH DedupedSource AS (
18+
SELECT *,
19+
ROW_NUMBER() OVER (
20+
PARTITION BY userId, adminLocationId, deleted
21+
ORDER BY amendDate DESC, createdDate DESC
22+
) AS rn
23+
FROM @UserAdminLocationList
24+
),
25+
CleanSource AS (
26+
SELECT * FROM DedupedSource WHERE rn = 1
27+
)
28+
29+
MERGE elfh.userAdminLocationTBL AS target
30+
USING CleanSource AS source
31+
ON target.userId = source.userId
32+
AND target.adminLocationId = source.adminLocationId
33+
AND target.deleted = source.deleted -- IMPORTANT!
2034

2135
WHEN MATCHED THEN
2236
UPDATE SET
23-
target.[deleted] = source.[deleted],
24-
target.[amendUserId] = source.[amendUserId],
25-
target.[amendDate] = source.[amendDate],
26-
target.[createdUserId] = source.[createdUserId],
27-
target.[createdDate] = source.[createdDate]
37+
target.amendUserId = source.amendUserId,
38+
target.amendDate = source.amendDate,
39+
target.createdUserId = source.createdUserId,
40+
target.createdDate = source.createdDate
2841

2942
WHEN NOT MATCHED BY TARGET THEN
3043
INSERT (
31-
[userId],
32-
[adminLocationId],
33-
[deleted],
34-
[amendUserId],
35-
[amendDate],
36-
[createdUserId],
37-
[createdDate]
44+
userId,
45+
adminLocationId,
46+
deleted,
47+
amendUserId,
48+
amendDate,
49+
createdUserId,
50+
createdDate
3851
)
3952
VALUES (
40-
source.[userId],
41-
source.[adminLocationId],
42-
source.[deleted],
43-
source.[amendUserId],
44-
source.[amendDate],
45-
source.[createdUserId],
46-
source.[createdDate]
53+
source.userId,
54+
source.adminLocationId,
55+
source.deleted,
56+
source.amendUserId,
57+
source.amendDate,
58+
source.createdUserId,
59+
source.createdDate
4760
);
48-
4961
END
5062
GO

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeUserEmploymentReference.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
--
88
-- 04-11-2025 Sarathlal Initial Revision
99
-------------------------------------------------------------------------------
10-
CREATE PROCEDURE [AdfMergeUserEmploymentReference]
10+
CREATE PROCEDURE [dbo].[AdfMergeUserEmploymentReference]
1111
@UserEmploymentReferenceList dbo.UserEmploymentReferenceType READONLY
1212
AS
1313
BEGIN
1414
SET NOCOUNT ON;
1515

1616
SET IDENTITY_INSERT [elfh].[userEmploymentReferenceTBL] ON;
17+
ALTER TABLE [elfh].[userEmploymentReferenceTBL] NOCHECK CONSTRAINT ALL;
1718
MERGE [elfh].[userEmploymentReferenceTBL] AS target
1819
USING @UserEmploymentReferenceList AS source
1920
ON target.[userEmploymentReferenceId] = source.[userEmploymentReferenceId]
@@ -47,5 +48,6 @@ BEGIN
4748
source.[amendDate]
4849
);
4950
SET IDENTITY_INSERT [elfh].[userEmploymentReferenceTBL] OFF;
51+
ALTER TABLE [elfh].[userEmploymentReferenceTBL] NOCHECK CONSTRAINT ALL;
5052
END
5153
GO
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-------------------------------------------------------------------------------
2+
-- Author Sarathlal
3+
-- Created 25-11-2025
4+
-- Purpose ELFH-LH Data sync
5+
--
6+
-- Modification History
7+
--
8+
-- 25-11-2025 Sarathlal Initial Revision
9+
-------------------------------------------------------------------------------
10+
CREATE PROCEDURE [dbo].[AdfMergeUserGroupReporter]
11+
@UserGroupReporterList [dbo].[UserGroupReporterType] READONLY
12+
AS
13+
BEGIN
14+
SET NOCOUNT ON;
15+
SET IDENTITY_INSERT [elfh].[userGroupReporterTBL] ON;
16+
ALTER TABLE elfh.userGroupReporterTBL NOCHECK CONSTRAINT FK_userGroupReporterTBL_userGroupTBL;
17+
MERGE [elfh].[userGroupReporterTBL] AS target
18+
USING @UserGroupReporterList AS source
19+
ON target.[userGroupReporterId] = source.[userGroupReporterId]
20+
21+
WHEN MATCHED THEN
22+
UPDATE SET
23+
target.[userId] = source.[userId],
24+
target.[userGroupId] = source.[userGroupId],
25+
target.[deleted] = source.[deleted],
26+
target.[amendUserId] = source.[amendUserId],
27+
target.[amendDate] = source.[amendDate]
28+
29+
WHEN NOT MATCHED BY TARGET THEN
30+
INSERT (
31+
[userGroupReporterId],
32+
[userId],
33+
[userGroupId],
34+
[deleted],
35+
[amendUserId],
36+
[amendDate]
37+
)
38+
VALUES (
39+
source.[userGroupReporterId],
40+
source.[userId],
41+
source.[userGroupId],
42+
source.[deleted],
43+
source.[amendUserId],
44+
source.[amendDate]
45+
);
46+
SET IDENTITY_INSERT [elfh].[userGroupReporterTBL] OFF;
47+
ALTER TABLE elfh.userGroupReporterTBL CHECK CONSTRAINT FK_userGroupReporterTBL_userGroupTBL;
48+
END
49+
GO
50+

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeUserHistoryAttribute.sql

Lines changed: 0 additions & 62 deletions
This file was deleted.

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeUserPasswordValidationToke.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ BEGIN
1515

1616
-- Enable identity insert if userPasswordValidationTokenId is an IDENTITY column
1717
SET IDENTITY_INSERT [elfh].[userPasswordValidationTokenTBL] ON;
18+
ALTER TABLE [elfh].[userPasswordValidationTokenTBL] NOCHECK CONSTRAINT ALL;
19+
ALTER TABLE [hub].[User] NOCHECK CONSTRAINT ALL;
1820
MERGE [elfh].[userPasswordValidationTokenTBL] AS target
1921
USING @userPasswordValidationTokenList AS source
2022
ON target.userPasswordValidationTokenId = source.userPasswordValidationTokenId
@@ -56,5 +58,7 @@ BEGIN
5658

5759
-- Disable identity insert
5860
SET IDENTITY_INSERT [elfh].[userPasswordValidationTokenTBL] OFF;
61+
ALTER TABLE [hub].[User] CHECK CONSTRAINT ALL;
62+
ALTER TABLE [elfh].[userPasswordValidationTokenTBL] CHECK CONSTRAINT ALL;
5963
END
6064
GO

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeattribute.sql

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ CREATE TYPE dbo.UserReportingUserType AS TABLE
3232
[AmendDate] DATETIMEOFFSET
3333
);
3434
GO
35-
CREATE TYPE UserHistoryAttributeType AS TABLE
36-
(
37-
[userHistoryAttributeId] INT,
38-
[userHistoryId] INT,
39-
[attributeId] INT,
40-
[intValue] INT NULL,
41-
[textValue] NVARCHAR(1000) NULL,
42-
[booleanValue] BIT NULL,
43-
[dateValue] DATETIMEOFFSET NULL,
44-
[deleted] BIT,
45-
[amendUserId] INT,
46-
[amendDate] DATETIMEOFFSET
47-
);
48-
GO
4935
CREATE TYPE UserEmploymentResponsibilityType AS TABLE
5036
(
5137
[userEmploymentResponsibilityId] INT,
@@ -584,6 +570,16 @@ CREATE TYPE dbo.MedicalCouncil AS TABLE
584570
amendDate datetimeoffset(7)
585571
);
586572
GO
573+
CREATE TYPE [dbo].[UserGroupReporterType] AS TABLE
574+
(
575+
[userGroupReporterId] INT,
576+
[userId] INT,
577+
[userGroupId] INT,
578+
[deleted] BIT,
579+
[amendUserId] INT,
580+
[amendDate] DATETIMEOFFSET(7)
581+
);
582+
GO
587583
CREATE PROCEDURE [dbo].[AdfMergeattribute]
588584
@attributeList dbo.Attribute READONLY -- Table-valued parameter
589585
AS

WebAPI/LearningHub.Nhs.Database/Stored Procedures/Adf/AdfMergeuserEmployment.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ BEGIN
1414
SET NOCOUNT ON;
1515

1616
SET IDENTITY_INSERT [elfh].[userEmploymentTBL] ON;
17+
ALTER TABLE [elfh].[userEmploymentTBL] NOCHECK CONSTRAINT ALL;
18+
ALTER TABLE [hub].[User] NOCHECK CONSTRAINT ALL;
1719
MERGE [elfh].[userEmploymentTBL] AS target
1820
USING @userEmploymentList AS source
1921
ON target.userEmploymentId = source.userEmploymentId
@@ -73,5 +75,7 @@ BEGIN
7375

7476
-- Disable identity insert
7577
SET IDENTITY_INSERT [elfh].[userEmploymentTBL] OFF;
78+
ALTER TABLE [elfh].[userEmploymentTBL] NOCHECK CONSTRAINT ALL;
79+
ALTER TABLE [hub].[User] NOCHECK CONSTRAINT ALL;
7680
END
7781
GO

0 commit comments

Comments
 (0)