From 09df3f64d30d8e64fd9c6a6d62245f53ed7bad62 Mon Sep 17 00:00:00 2001 From: Matt Portune Date: Tue, 23 Jun 2020 23:54:27 -0400 Subject: [PATCH 1/3] Updates to SSO config DB setup --- src/Sql/dbo/Tables/Organization.sql | 4 ++++ src/Sql/dbo/Tables/SsoConfig.sql | 5 ++++- src/Sql/dbo/Views/SsoConfigView.sql | 6 ++++-- .../DbScripts/2020-06-23_00_OrgIdentifier.sql | 18 ++++++++++++++++++ ..._OrgSso.sql => 2020-06-23_01_SsoConfig.sql} | 16 ++++++++++------ 5 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql rename util/Migrator/DbScripts/{2020-06-16_00_OrgSso.sql => 2020-06-23_01_SsoConfig.sql} (73%) diff --git a/src/Sql/dbo/Tables/Organization.sql b/src/Sql/dbo/Tables/Organization.sql index 226335de70..9840c2fdd0 100644 --- a/src/Sql/dbo/Tables/Organization.sql +++ b/src/Sql/dbo/Tables/Organization.sql @@ -1,5 +1,6 @@ CREATE TABLE [dbo].[Organization] ( [Id] UNIQUEIDENTIFIER NOT NULL, + [Identifier] NVARCHAR (50) NULL, [Name] NVARCHAR (50) NOT NULL, [BusinessName] NVARCHAR (50) NULL, [BusinessAddress1] NVARCHAR (50) NULL, @@ -42,3 +43,6 @@ CREATE NONCLUSTERED INDEX [IX_Organization_Enabled] ON [dbo].[Organization]([Id] ASC, [Enabled] ASC) INCLUDE ([UseTotp]); +GO +CREATE NONCLUSTERED INDEX [IX_Organization_Identifier] + ON [dbo].[Organization]([Identifier] ASC) diff --git a/src/Sql/dbo/Tables/SsoConfig.sql b/src/Sql/dbo/Tables/SsoConfig.sql index 2c0ecfeb7d..e425300eb9 100644 --- a/src/Sql/dbo/Tables/SsoConfig.sql +++ b/src/Sql/dbo/Tables/SsoConfig.sql @@ -1,8 +1,11 @@ CREATE TABLE [dbo].[SsoConfig] ( - [OrganizationId] UNIQUEIDENTIFIER NULL, + [Id] BIGINT IDENTITY (1, 1) NOT NULL, + [Enabled] BIT NOT NULL, + [OrganizationId] UNIQUEIDENTIFIER NOT NULL, [Identifier] NVARCHAR (50) NULL, [Data] NVARCHAR (MAX) NULL, [CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL, + CONSTRAINT [PK_SsoConfig] PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_SsoConfig_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ); diff --git a/src/Sql/dbo/Views/SsoConfigView.sql b/src/Sql/dbo/Views/SsoConfigView.sql index 7bcfe15b4b..03f43f2fa8 100644 --- a/src/Sql/dbo/Views/SsoConfigView.sql +++ b/src/Sql/dbo/Views/SsoConfigView.sql @@ -1,6 +1,8 @@ CREATE VIEW [dbo].[SsoConfigView] AS SELECT - * + SSO.* FROM - [dbo].[SsoConfig] + [dbo].[SsoConfig] SSO +INNER JOIN + [dbo].[Organization] O ON O.[Identifier] = SSO.[Identifier] diff --git a/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql new file mode 100644 index 0000000000..ad7140fbda --- /dev/null +++ b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql @@ -0,0 +1,18 @@ +IF COL_LENGTH('[dbo].[Organization]', 'Identifier') IS NULL +BEGIN + ALTER TABLE + [dbo].[Organization] + ADD + [Identifier] NVARCHAR (50) NULL +END +GO + +IF NOT EXISTS ( + SELECT * FROM sys.indexes WHERE [Name]='IX_Organization_Identifier' + AND object_id = OBJECT_ID('[dbo].[Organization]') +) +BEGIN + CREATE NONCLUSTERED INDEX [IX_Organization_Identifier] + ON [dbo].[Organization]([Identifier] ASC) +END +GO diff --git a/util/Migrator/DbScripts/2020-06-16_00_OrgSso.sql b/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql similarity index 73% rename from util/Migrator/DbScripts/2020-06-16_00_OrgSso.sql rename to util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql index a3ba8442da..33ee3f1047 100644 --- a/util/Migrator/DbScripts/2020-06-16_00_OrgSso.sql +++ b/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql @@ -1,11 +1,14 @@ IF OBJECT_ID('[dbo].[SsoConfig]') IS NULL BEGIN CREATE TABLE [dbo].[SsoConfig] ( - [OrganizationId] UNIQUEIDENTIFIER NULL, + [Id] BIGINT IDENTITY (1, 1) NOT NULL, + [Enabled] BIT NOT NULL, + [OrganizationId] UNIQUEIDENTIFIER NOT NULL, [Identifier] NVARCHAR (50) NULL, [Data] NVARCHAR (MAX) NULL, [CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL, + CONSTRAINT [PK_SsoConfig] PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_SsoConfig_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ); END @@ -20,10 +23,11 @@ GO CREATE VIEW [dbo].[SsoConfigView] AS SELECT - * + SSO.* FROM - [dbo].[SsoConfig] -GO + [dbo].[SsoConfig] SSO +INNER JOIN + [dbo].[Organization] O ON O.[Identifier] = SSO.[Identifier] IF OBJECT_ID('[dbo].[SsoConfig_ReadByIdentifier]') IS NOT NULL BEGIN @@ -42,7 +46,7 @@ BEGIN FROM [dbo].[SsoConfigView] WHERE - [Identifier] = @Identifier + [Identifier] = @Identifier END GO @@ -63,6 +67,6 @@ BEGIN FROM [dbo].[SsoConfigView] WHERE - [OrganizationId] = @OrganizationId + [OrganizationId] = @OrganizationId END GO From 05891f212264aaff762d35d2b5c71fbe581fded2 Mon Sep 17 00:00:00 2001 From: Matt Portune Date: Wed, 24 Jun 2020 12:24:36 -0400 Subject: [PATCH 2/3] Requested updates --- .../dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql | 9 ++++++--- src/Sql/dbo/Tables/Organization.sql | 4 +++- src/Sql/dbo/Views/SsoConfigView.sql | 6 ++---- util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql | 3 ++- util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql | 6 ++---- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql b/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql index bcbd01f7ea..61a6db05fc 100644 --- a/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql +++ b/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql @@ -5,9 +5,12 @@ BEGIN SET NOCOUNT ON SELECT TOP 1 - * + SSO.*, + O.[Identifier] FROM - [dbo].[SsoConfigView] + [dbo].[SsoConfigView] SSO + INNER JOIN + [dbo].[Organization] O ON O.[Id] = SSO.[OrganizationId] WHERE - [Identifier] = @Identifier + O.[Identifier] = @Identifier END diff --git a/src/Sql/dbo/Tables/Organization.sql b/src/Sql/dbo/Tables/Organization.sql index 9840c2fdd0..d013eeccb1 100644 --- a/src/Sql/dbo/Tables/Organization.sql +++ b/src/Sql/dbo/Tables/Organization.sql @@ -44,5 +44,7 @@ CREATE NONCLUSTERED INDEX [IX_Organization_Enabled] INCLUDE ([UseTotp]); GO -CREATE NONCLUSTERED INDEX [IX_Organization_Identifier] +CREATE UNIQUE NONCLUSTERED INDEX [IX_Organization_Identifier] ON [dbo].[Organization]([Identifier] ASC) + WHERE [Identifier] IS NOT NULL; + diff --git a/src/Sql/dbo/Views/SsoConfigView.sql b/src/Sql/dbo/Views/SsoConfigView.sql index 03f43f2fa8..7bcfe15b4b 100644 --- a/src/Sql/dbo/Views/SsoConfigView.sql +++ b/src/Sql/dbo/Views/SsoConfigView.sql @@ -1,8 +1,6 @@ CREATE VIEW [dbo].[SsoConfigView] AS SELECT - SSO.* + * FROM - [dbo].[SsoConfig] SSO -INNER JOIN - [dbo].[Organization] O ON O.[Identifier] = SSO.[Identifier] + [dbo].[SsoConfig] diff --git a/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql index ad7140fbda..04ed6b7089 100644 --- a/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql +++ b/util/Migrator/DbScripts/2020-06-23_00_OrgIdentifier.sql @@ -12,7 +12,8 @@ IF NOT EXISTS ( AND object_id = OBJECT_ID('[dbo].[Organization]') ) BEGIN - CREATE NONCLUSTERED INDEX [IX_Organization_Identifier] + CREATE UNIQUE NONCLUSTERED INDEX [IX_Organization_Identifier] ON [dbo].[Organization]([Identifier] ASC) + WHERE [Identifier] IS NOT NULL; END GO diff --git a/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql b/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql index 33ee3f1047..9e3b9a48b8 100644 --- a/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql +++ b/util/Migrator/DbScripts/2020-06-23_01_SsoConfig.sql @@ -23,11 +23,9 @@ GO CREATE VIEW [dbo].[SsoConfigView] AS SELECT - SSO.* + * FROM - [dbo].[SsoConfig] SSO -INNER JOIN - [dbo].[Organization] O ON O.[Identifier] = SSO.[Identifier] + [dbo].[SsoConfig] IF OBJECT_ID('[dbo].[SsoConfig_ReadByIdentifier]') IS NOT NULL BEGIN From 0f008435ba47d7b2a14d43021c7bf6f09362df65 Mon Sep 17 00:00:00 2001 From: Matt Portune Date: Wed, 24 Jun 2020 13:59:00 -0400 Subject: [PATCH 3/3] sproc tweak --- .../dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql b/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql index 61a6db05fc..470c28e0de 100644 --- a/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql +++ b/src/Sql/dbo/Stored Procedures/SsoConfig_ReadByIdentifier.sql @@ -5,12 +5,10 @@ BEGIN SET NOCOUNT ON SELECT TOP 1 - SSO.*, - O.[Identifier] + SSO.* FROM [dbo].[SsoConfigView] SSO INNER JOIN [dbo].[Organization] O ON O.[Id] = SSO.[OrganizationId] - WHERE - O.[Identifier] = @Identifier + AND O.[Identifier] = @Identifier END