From 4e4644e17d5fbe4b0f57df03ae2d005b6491ad80 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 6 Jan 2020 14:26:48 -0500 Subject: [PATCH] stub out organization policy db schema --- src/Sql/Sql.sqlproj | 7 + .../dbo/Stored Procedures/Policy_Create.sql | 35 ++++ .../Stored Procedures/Policy_DeleteById.sql | 18 ++ .../dbo/Stored Procedures/Policy_ReadById.sql | 13 ++ .../Policy_ReadByOrganizationId.sql | 13 ++ .../dbo/Stored Procedures/Policy_Update.sql | 26 +++ src/Sql/dbo/Tables/Policy.sql | 17 ++ src/Sql/dbo/Views/PolicyView.sql | 6 + .../DbScripts/2020-01-06_00_PolicySetup.sql | 177 ++++++++++++++++++ 9 files changed, 312 insertions(+) create mode 100644 src/Sql/dbo/Stored Procedures/Policy_Create.sql create mode 100644 src/Sql/dbo/Stored Procedures/Policy_DeleteById.sql create mode 100644 src/Sql/dbo/Stored Procedures/Policy_ReadById.sql create mode 100644 src/Sql/dbo/Stored Procedures/Policy_ReadByOrganizationId.sql create mode 100644 src/Sql/dbo/Stored Procedures/Policy_Update.sql create mode 100644 src/Sql/dbo/Tables/Policy.sql create mode 100644 src/Sql/dbo/Views/PolicyView.sql create mode 100644 util/Migrator/DbScripts/2020-01-06_00_PolicySetup.sql diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 479509991..bdaba04a5 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -252,5 +252,12 @@ + + + + + + + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Policy_Create.sql b/src/Sql/dbo/Stored Procedures/Policy_Create.sql new file mode 100644 index 000000000..c2aea915f --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Policy_Create.sql @@ -0,0 +1,35 @@ +CREATE PROCEDURE [dbo].[Policy_Create] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @Type TINYINT, + @Data NVARCHAR(MAX), + @Enabled BIT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[Policy] + ( + [Id], + [OrganizationId], + [Type], + [Data], + [Enabled], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationId, + @Type, + @Data, + @Enabled, + @CreationDate, + @RevisionDate + ) + + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Policy_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Policy_DeleteById.sql new file mode 100644 index 000000000..537cdfd57 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Policy_DeleteById.sql @@ -0,0 +1,18 @@ +CREATE PROCEDURE [dbo].[Policy_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DECLARE @OrganizationId UNIQUEIDENTIFIER = (SELECT TOP 1 [OrganizationId] FROM [dbo].[Policy] WHERE [Id] = @Id) + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + + DELETE + FROM + [dbo].[Policy] + WHERE + [Id] = @Id +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Policy_ReadById.sql b/src/Sql/dbo/Stored Procedures/Policy_ReadById.sql new file mode 100644 index 000000000..38e15266d --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Policy_ReadById.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Policy_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[PolicyView] + WHERE + [Id] = @Id +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Policy_ReadByOrganizationId.sql b/src/Sql/dbo/Stored Procedures/Policy_ReadByOrganizationId.sql new file mode 100644 index 000000000..c01b22e98 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Policy_ReadByOrganizationId.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Policy_ReadByOrganizationId] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[PolicyView] + WHERE + [OrganizationId] = @OrganizationId +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Policy_Update.sql b/src/Sql/dbo/Stored Procedures/Policy_Update.sql new file mode 100644 index 000000000..c87a427a5 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Policy_Update.sql @@ -0,0 +1,26 @@ +CREATE PROCEDURE [dbo].[Policy_Update] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @Type TINYINT, + @Data NVARCHAR(MAX), + @Enabled BIT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[Policy] + SET + [OrganizationId] = @OrganizationId, + [Type] = @Type, + [Data] = @Data, + [Enabled] = @Enabled, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId +END \ No newline at end of file diff --git a/src/Sql/dbo/Tables/Policy.sql b/src/Sql/dbo/Tables/Policy.sql new file mode 100644 index 000000000..ba7cf44f5 --- /dev/null +++ b/src/Sql/dbo/Tables/Policy.sql @@ -0,0 +1,17 @@ +CREATE TABLE [dbo].[Policy] ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [OrganizationId] UNIQUEIDENTIFIER NOT NULL, + [Type] TINYINT NOT NULL, + [Data] NVARCHAR (MAX) NOT NULL, + [Enabled] BIT NOT NULL, + [CreationDate] DATETIME2 (7) NOT NULL, + [RevisionDate] DATETIME2 (7) NOT NULL, + CONSTRAINT [PK_Policy] PRIMARY KEY CLUSTERED ([Id] ASC), + CONSTRAINT [FK_Policy_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE +); + + +GO +CREATE NONCLUSTERED INDEX [IX_Policy_OrganizationId_Enabled] + ON [dbo].[Policy]([OrganizationId] ASC, [Enabled] ASC); + diff --git a/src/Sql/dbo/Views/PolicyView.sql b/src/Sql/dbo/Views/PolicyView.sql new file mode 100644 index 000000000..1b6b68834 --- /dev/null +++ b/src/Sql/dbo/Views/PolicyView.sql @@ -0,0 +1,6 @@ +CREATE VIEW [dbo].[PolicyView] +AS +SELECT + * +FROM + [dbo].[Policy] \ No newline at end of file diff --git a/util/Migrator/DbScripts/2020-01-06_00_PolicySetup.sql b/util/Migrator/DbScripts/2020-01-06_00_PolicySetup.sql new file mode 100644 index 000000000..f7cfcd495 --- /dev/null +++ b/util/Migrator/DbScripts/2020-01-06_00_PolicySetup.sql @@ -0,0 +1,177 @@ +IF OBJECT_ID('[dbo].[Policy]') IS NULL +BEGIN + CREATE TABLE [dbo].[Policy] ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [OrganizationId] UNIQUEIDENTIFIER NOT NULL, + [Type] TINYINT NOT NULL, + [Data] NVARCHAR (MAX) NOT NULL, + [Enabled] BIT NOT NULL, + [CreationDate] DATETIME2 (7) NOT NULL, + [RevisionDate] DATETIME2 (7) NOT NULL, + CONSTRAINT [PK_Policy] PRIMARY KEY CLUSTERED ([Id] ASC), + CONSTRAINT [FK_Policy_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE + ); + + CREATE NONCLUSTERED INDEX [IX_Policy_OrganizationId_Enabled] + ON [dbo].[Policy]([OrganizationId] ASC, [Enabled] ASC); +END +GO + +IF OBJECT_ID('[dbo].[Policy_Create]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Policy_Create] +END +GO + +CREATE PROCEDURE [dbo].[Policy_Create] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @Type TINYINT, + @Data NVARCHAR(MAX), + @Enabled BIT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + INSERT INTO [dbo].[Policy] + ( + [Id], + [OrganizationId], + [Type], + [Data], + [Enabled], + [CreationDate], + [RevisionDate] + ) + VALUES + ( + @Id, + @OrganizationId, + @Type, + @Data, + @Enabled, + @CreationDate, + @RevisionDate + ) + + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId +END +GO + +IF OBJECT_ID('[dbo].[Policy_DeleteById]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Policy_DeleteById] +END +GO + +CREATE PROCEDURE [dbo].[Policy_DeleteById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + DECLARE @OrganizationId UNIQUEIDENTIFIER = (SELECT TOP 1 [OrganizationId] FROM [dbo].[Policy] WHERE [Id] = @Id) + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + + DELETE + FROM + [dbo].[Policy] + WHERE + [Id] = @Id +END +GO + +IF OBJECT_ID('[dbo].[Policy_ReadById]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Policy_ReadById] +END +GO + +CREATE PROCEDURE [dbo].[Policy_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[PolicyView] + WHERE + [Id] = @Id +END +GO + +IF OBJECT_ID('[dbo].[Policy_ReadByOrganizationId]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Policy_ReadByOrganizationId] +END +GO + +CREATE PROCEDURE [dbo].[Policy_ReadByOrganizationId] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[PolicyView] + WHERE + [OrganizationId] = @OrganizationId +END +GO + +IF OBJECT_ID('[dbo].[Policy_Update]') IS NOT NULL +BEGIN + DROP PROCEDURE [dbo].[Policy_Update] +END +GO + +CREATE PROCEDURE [dbo].[Policy_Update] + @Id UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, + @Type TINYINT, + @Data NVARCHAR(MAX), + @Enabled BIT, + @CreationDate DATETIME2(7), + @RevisionDate DATETIME2(7) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[Policy] + SET + [OrganizationId] = @OrganizationId, + [Type] = @Type, + [Data] = @Data, + [Enabled] = @Enabled, + [CreationDate] = @CreationDate, + [RevisionDate] = @RevisionDate + WHERE + [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId +END +GO + +IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'PolicyView') +BEGIN + DROP VIEW [dbo].[PolicyView] +END +GO + +CREATE VIEW [dbo].[PolicyView] +AS +SELECT + * +FROM + [dbo].[Policy] +GO