diff --git a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj index e12159110..bb503bb5e 100644 --- a/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj +++ b/WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj @@ -203,6 +203,11 @@ + + + + + @@ -549,7 +554,6 @@ - @@ -567,8 +571,6 @@ - - @@ -585,6 +587,12 @@ + + + + + + diff --git a/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/TD-6159-turn-on-cdc.sql b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/TD-6159-turn-on-cdc.sql new file mode 100644 index 000000000..4128b97a8 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Scripts/Post-Deploy/Scripts/TD-6159-turn-on-cdc.sql @@ -0,0 +1,28 @@ +-- Re-enable CDC +IF NOT EXISTS ( + SELECT 1 + FROM sys.change_data_capture_tables + WHERE source_object_id = OBJECT_ID(N'hub.User') +) +BEGIN + EXEC sys.sp_cdc_enable_table + @source_schema = N'hub', + @source_name = N'User', + @role_name = NULL, + @supports_net_changes = 1; +END +GO + +IF NOT EXISTS ( + SELECT 1 + FROM sys.change_data_capture_tables + WHERE source_object_id = OBJECT_ID(N'hub.UserProfile') +) +BEGIN + EXEC sys.sp_cdc_enable_table + @source_schema = N'hub', + @source_name = N'UserProfile', + @role_name = NULL, + @supports_net_changes = 1; +END +GO diff --git a/WebAPI/LearningHub.Nhs.Database/Scripts/Pre-Deploy/Scripts/TD-6159-turn-off-cdc.sql b/WebAPI/LearningHub.Nhs.Database/Scripts/Pre-Deploy/Scripts/TD-6159-turn-off-cdc.sql new file mode 100644 index 000000000..57ee663c5 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Scripts/Pre-Deploy/Scripts/TD-6159-turn-off-cdc.sql @@ -0,0 +1,26 @@ +-- Disable CDC +IF EXISTS ( + SELECT 1 + FROM sys.change_data_capture_tables + WHERE source_object_id = OBJECT_ID(N'hub.User') +) +BEGIN + EXEC sys.sp_cdc_disable_table + @source_schema = N'hub', + @source_name = N'User', + @capture_instance = N'hub_User'; +END +GO + +IF EXISTS ( + SELECT 1 + FROM sys.change_data_capture_tables + WHERE source_object_id = OBJECT_ID(N'hub.UserProfile') +) +BEGIN + EXEC sys.sp_cdc_disable_table + @source_schema = N'hub', + @source_name = N'UserProfile', + @capture_instance = N'hub_UserProfile'; +END +GO diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/EmploymentReferenceTypeTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/EmploymentReferenceTypeTBL.sql new file mode 100644 index 000000000..be4a54abb --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/EmploymentReferenceTypeTBL.sql @@ -0,0 +1,13 @@ +CREATE TABLE [elfh].[employmentReferenceTypeTBL]( + [EmploymentReferenceTypeId] [int] NOT NULL, + [Title] [nvarchar](50) NOT NULL, + [RefAccess] [int] NOT NULL, + CONSTRAINT [PK_EmploymentReferenceType] PRIMARY KEY CLUSTERED +( + [EmploymentReferenceTypeId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO + +ALTER TABLE [elfh].[employmentReferenceTypeTBL] ADD CONSTRAINT [DF_employmentReferenceTypeTBL_RefAccess] DEFAULT ((0)) FOR [RefAccess] +GO diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/ExternalSystemTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/ExternalSystemTBL.sql deleted file mode 100644 index 0a565aaa7..000000000 --- a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/ExternalSystemTBL.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE TABLE [elfh].[externalSystemTBL]( - [externalSystemId] [int] IDENTITY(1,1) NOT NULL, - [externalSystemName] [nvarchar](50) NOT NULL, - [url] [nvarchar](256) NULL, - [tsAndCs] [nvarchar](max) NULL, - [deleted] [bit] NOT NULL, - [amendUserId] [int] NOT NULL, - [amendUserDate] [datetimeoffset](7) NOT NULL, - [securityGuid] [uniqueidentifier] NULL, - [restrictToSSO] [bit] NOT NULL, - [defaultUserGroupId] [int] NULL, - [defaultStaffGroupId] [int] NULL, - [defaultJobRoleId] [int] NULL, - [defaultGradingId] [int] NULL, - [defaultSpecialtyId] [int] NULL, - [defaultLocationId] [int] NULL, - [reportUserId] [int] NULL, - [code] [nvarchar](50) NOT NULL, - CONSTRAINT [PK_externalSystem] PRIMARY KEY CLUSTERED -( - [externalSystemId] ASC -)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY] -) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] -GO - -ALTER TABLE [elfh].[externalSystemTBL] ADD CONSTRAINT [DF_externalSystemTBL_RestrictToSSO] DEFAULT ((0)) FOR [restrictToSSO] -GO - -ALTER TABLE [elfh].[externalSystemTBL] ADD DEFAULT ('') FOR [code] -GO - diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/MergeUserTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/MergeUserTBL.sql new file mode 100644 index 000000000..510244acb --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/MergeUserTBL.sql @@ -0,0 +1,12 @@ +CREATE TABLE [elfh].[mergeUserTBL]( + [mergeUserId] [int] IDENTITY(1,1) NOT NULL, + [fromUserId] [int] NOT NULL, + [intoUserId] [int] NOT NULL, + [amendUserId] [int] NOT NULL, + [createdDatetime] [datetimeoffset](7) NOT NULL, + CONSTRAINT [PK_mergeUserTBL] PRIMARY KEY CLUSTERED +( + [mergeUserId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO \ No newline at end of file diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserAdminLocationTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserAdminLocationTBL.sql new file mode 100644 index 000000000..4c88913e6 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserAdminLocationTBL.sql @@ -0,0 +1,42 @@ + +CREATE TABLE [elfh].[userAdminLocationTBL]( + [userId] [int] NOT NULL, + [adminLocationId] [int] NOT NULL, + [deleted] [bit] NOT NULL, + [amendUserId] [int] NOT NULL, + [amendDate] [datetimeoffset](7) NOT NULL, + [createdUserId] [int] NOT NULL, + [createdDate] [datetimeoffset](7) NOT NULL, +PRIMARY KEY CLUSTERED +( + [adminLocationId] ASC, + [userId] ASC, + [deleted] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] ADD DEFAULT ((0)) FOR [deleted] +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] ADD DEFAULT (sysdatetimeoffset()) FOR [amendDate] +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] ADD DEFAULT (sysdatetimeoffset()) FOR [createdDate] +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] WITH CHECK ADD CONSTRAINT [FK_userAdminLocationTBL_locationTBL] FOREIGN KEY([adminLocationId]) +REFERENCES [elfh].[locationTBL] ([locationId]) +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] CHECK CONSTRAINT [FK_userAdminLocationTBL_locationTBL] +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] WITH CHECK ADD CONSTRAINT [FK_userAdminLocationTBL_userTBL] FOREIGN KEY([userId]) +REFERENCES [hub].[user] ([Id]) +GO + +ALTER TABLE [elfh].[userAdminLocationTBL] CHECK CONSTRAINT [FK_userAdminLocationTBL_userTBL] +GO + + diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentReferenceTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentReferenceTBL.sql new file mode 100644 index 000000000..66bc74d7f --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentReferenceTBL.sql @@ -0,0 +1,31 @@ +CREATE TABLE [elfh].[userEmploymentReferenceTBL]( + [userEmploymentReferenceId] [int] IDENTITY(1,1) NOT NULL, + [employmentReferenceTypeId] [int] NOT NULL, + [userEmploymentId] [int] NOT NULL, + [referenceValue] [nvarchar](100) NOT NULL, + [deleted] [bit] NOT NULL, + [amendUserId] [int] NOT NULL, + [amendDate] [datetimeoffset](7) NOT NULL, + CONSTRAINT [PK_userEmploymentReferenceTBL] PRIMARY KEY CLUSTERED +( + [userEmploymentReferenceId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO + +ALTER TABLE [elfh].[userEmploymentReferenceTBL] ADD CONSTRAINT [DF_userEmploymentReferenceTBL_deleted] DEFAULT ((0)) FOR [deleted] +GO + +ALTER TABLE [elfh].[userEmploymentReferenceTBL] WITH CHECK ADD CONSTRAINT [FK_userEmploymentReferenceTBL_employmentReferenceTypeTBL] FOREIGN KEY([employmentReferenceTypeId]) +REFERENCES [elfh].[employmentReferenceTypeTBL] ([EmploymentReferenceTypeId]) +GO + +ALTER TABLE [elfh].[userEmploymentReferenceTBL] CHECK CONSTRAINT [FK_userEmploymentReferenceTBL_employmentReferenceTypeTBL] +GO + +ALTER TABLE [elfh].[userEmploymentReferenceTBL] WITH CHECK ADD CONSTRAINT [FK_userEmploymentReferenceTBL_userEmploymentTBL] FOREIGN KEY([userEmploymentId]) +REFERENCES [elfh].[userEmploymentTBL] ([userEmploymentId]) +GO + +ALTER TABLE [elfh].[userEmploymentReferenceTBL] CHECK CONSTRAINT [FK_userEmploymentReferenceTBL_userEmploymentTBL] +GO \ No newline at end of file diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentResponsibilityTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentResponsibilityTBL.sql new file mode 100644 index 000000000..546aead55 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserEmploymentResponsibilityTBL.sql @@ -0,0 +1,15 @@ +CREATE TABLE [elfh].[userEmploymentResponsibilityTBL]( + [userEmploymentResponsibilityId] [int] IDENTITY(1,1) NOT NULL, + [userEmploymentId] [int] NOT NULL, + [additionalResponsibilityId] [int] NOT NULL, + [deleted] [bit] NOT NULL, + [amendUserId] [int] NOT NULL, +PRIMARY KEY CLUSTERED +( + [userEmploymentResponsibilityId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO + +ALTER TABLE [elfh].[userEmploymentResponsibilityTBL] ADD DEFAULT ((0)) FOR [deleted] +GO \ No newline at end of file diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserReportingUserTBL.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserReportingUserTBL.sql new file mode 100644 index 000000000..a96df3713 --- /dev/null +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Elfh/UserReportingUserTBL.sql @@ -0,0 +1,31 @@ +CREATE TABLE [elfh].[userReportingUserTBL]( + [userReportingUserId] [int] IDENTITY(1,1) NOT NULL, + [userId] [int] NOT NULL, + [reportingUserId] [int] NOT NULL, + [reportable] [bit] NOT NULL, + [Deleted] [bit] NOT NULL, + [AmendUserID] [int] NOT NULL, + [AmendDate] [datetimeoffset](7) NOT NULL, + CONSTRAINT [PK_userReportingUser] PRIMARY KEY CLUSTERED +( + [userReportingUserId] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] +) ON [PRIMARY] +GO + +ALTER TABLE [elfh].[userReportingUserTBL] ADD DEFAULT ((1)) FOR [reportable] +GO + +ALTER TABLE [elfh].[userReportingUserTBL] WITH CHECK ADD CONSTRAINT [FK_userReportingUser_reportingUser] FOREIGN KEY([reportingUserId]) +REFERENCES [hub].[user] ([Id]) +GO + +ALTER TABLE [elfh].[userReportingUserTBL] CHECK CONSTRAINT [FK_userReportingUser_reportingUser] +GO + +ALTER TABLE [elfh].[userReportingUserTBL] WITH CHECK ADD CONSTRAINT [FK_userReportingUser_user] FOREIGN KEY([userId]) +REFERENCES [hub].[user] ([Id]) +GO + +ALTER TABLE [elfh].[userReportingUserTBL] CHECK CONSTRAINT [FK_userReportingUser_user] +GO \ No newline at end of file diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Hub/User.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Hub/User.sql index eb2d34132..3af869df7 100644 --- a/WebAPI/LearningHub.Nhs.Database/Tables/Hub/User.sql +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Hub/User.sql @@ -32,10 +32,6 @@ ), PERIOD FOR SYSTEM_TIME ([VersionStartTime], [VersionEndTime]) ) ON [PRIMARY] -WITH -( -SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [hub].[UserHistory] ) -) GO ALTER TABLE [hub].[User] ADD DEFAULT (getutcdate()) FOR [VersionStartTime] diff --git a/WebAPI/LearningHub.Nhs.Database/Tables/Hub/UserProfile.sql b/WebAPI/LearningHub.Nhs.Database/Tables/Hub/UserProfile.sql index aa7032ef9..61c95a2f5 100644 --- a/WebAPI/LearningHub.Nhs.Database/Tables/Hub/UserProfile.sql +++ b/WebAPI/LearningHub.Nhs.Database/Tables/Hub/UserProfile.sql @@ -20,10 +20,6 @@ )WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY], PERIOD FOR SYSTEM_TIME ([VersionStartTime], [VersionEndTime]) ) ON [PRIMARY] -WITH -( -SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [hub].[UserProfileHistory] ) -) GO ALTER TABLE hub.[UserProfile] ADD DEFAULT (getutcdate()) FOR [VersionStartTime]