From 6e87d9c21f1a3d0192c63971b0c0e1a387bc4e1a Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 24 Apr 2017 23:28:31 -0400 Subject: [PATCH] AccountRevDate bump via sprocs, no more triggers --- src/Sql/Sql.sqlproj | 3 + .../CipherDetails_Create.sql | 9 +++ .../CipherDetails_Update.sql | 9 +++ .../dbo/Stored Procedures/Cipher_Create.sql | 9 +++ .../Stored Procedures/Cipher_DeleteById.sql | 20 ++++++ .../dbo/Stored Procedures/Cipher_Update.sql | 9 +++ .../Cipher_UpdatePartial.sql | 5 ++ .../Cipher_UpdateWithSubvaults.sql | 9 +++ .../dbo/Stored Procedures/Folder_Create.sql | 2 + .../Stored Procedures/Folder_DeleteById.sql | 4 ++ .../dbo/Stored Procedures/Folder_Update.sql | 2 + .../SubvaultCipher_Create.sql | 6 ++ .../SubvaultCipher_Delete.sql | 6 ++ .../SubvaultCipher_UpdateSubvaults.sql | 5 ++ .../SubvaultCipher_UpdateSubvaultsAdmin.sql | 5 ++ .../Stored Procedures/SubvaultUser_Create.sql | 5 ++ .../SubvaultUser_DeleteById.sql | 7 +++ .../Stored Procedures/SubvaultUser_Update.sql | 5 ++ .../User_BumpAccountRevisionDate.sql | 13 ++++ ...umpAccountRevisionDateByOrganizationId.sql | 18 ++++++ ...ccountRevisionDateByOrganizationUserId.sql | 18 ++++++ src/Sql/dbo/Tables/Cipher.sql | 61 +------------------ 22 files changed, 170 insertions(+), 60 deletions(-) create mode 100644 src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDate.sql create mode 100644 src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationId.sql create mode 100644 src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationUserId.sql diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 92902d541d..01b7373489 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -176,5 +176,8 @@ + + + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_Create.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_Create.sql index 4cb75d8d48..03704c0404 100644 --- a/src/Sql/dbo/Stored Procedures/CipherDetails_Create.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_Create.sql @@ -41,4 +41,13 @@ BEGIN @CreationDate, @RevisionDate ) + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_Update.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_Update.sql index 642ebaf0b4..73f510b688 100644 --- a/src/Sql/dbo/Stored Procedures/CipherDetails_Update.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_Update.sql @@ -46,4 +46,13 @@ BEGIN [RevisionDate] = @RevisionDate WHERE [Id] = @Id + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Create.sql b/src/Sql/dbo/Stored Procedures/Cipher_Create.sql index eebd99b861..3dbf4a87e8 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Create.sql @@ -36,4 +36,13 @@ BEGIN @CreationDate, @RevisionDate ) + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql index 52074d74ab..672368e39d 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql @@ -4,9 +4,29 @@ AS BEGIN SET NOCOUNT ON + DECLARE @UserId UNIQUEIDENTIFIER + DECLARE @OrganizationId UNIQUEIDENTIFIER + + SELECT TOP 1 + @UserId = [UserId], + @OrganizationId = [OrganizationId] + FROM + [dbo].[Cipher] + WHERE + [Id] = @Id + DELETE FROM [dbo].[Cipher] WHERE [Id] = @Id + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Update.sql b/src/Sql/dbo/Stored Procedures/Cipher_Update.sql index 7378753397..bbc39fb41d 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Update.sql @@ -25,4 +25,13 @@ BEGIN [RevisionDate] = @RevisionDate WHERE [Id] = @Id + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_UpdatePartial.sql b/src/Sql/dbo/Stored Procedures/Cipher_UpdatePartial.sql index 64e73b82e8..5e59e08179 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_UpdatePartial.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_UpdatePartial.sql @@ -33,4 +33,9 @@ BEGIN END WHERE [Id] = @Id + + IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_UpdateWithSubvaults.sql b/src/Sql/dbo/Stored Procedures/Cipher_UpdateWithSubvaults.sql index d5278b0d24..019f8c9a43 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_UpdateWithSubvaults.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_UpdateWithSubvaults.sql @@ -53,4 +53,13 @@ BEGIN @SubvaultIds WHERE [Id] IN (SELECT [Id] FROM [AvailableSubvaultsCTE]) + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END + ELSE IF @UserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Folder_Create.sql b/src/Sql/dbo/Stored Procedures/Folder_Create.sql index 89b84f0d11..7b38c7283c 100644 --- a/src/Sql/dbo/Stored Procedures/Folder_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Folder_Create.sql @@ -24,4 +24,6 @@ BEGIN @CreationDate, @RevisionDate ) + + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Folder_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Folder_DeleteById.sql index fcc5a7c529..a5b597b13c 100644 --- a/src/Sql/dbo/Stored Procedures/Folder_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Folder_DeleteById.sql @@ -4,9 +4,13 @@ AS BEGIN SET NOCOUNT ON + DECLARE @UserId UNIQUEIDENTIFIER = (SELECT TOP 1 [UserId] FROM [dbo].[Folder] WHERE [Id] = @Id) + DELETE FROM [dbo].[Folder] WHERE [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Folder_Update.sql b/src/Sql/dbo/Stored Procedures/Folder_Update.sql index 2c6cd1d58b..d3aefc21d5 100644 --- a/src/Sql/dbo/Stored Procedures/Folder_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Folder_Update.sql @@ -17,4 +17,6 @@ BEGIN [RevisionDate] = @RevisionDate WHERE [Id] = @Id + + EXEC [dbo].[User_BumpAccountRevisionDate] @UserId END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultCipher_Create.sql b/src/Sql/dbo/Stored Procedures/SubvaultCipher_Create.sql index b9d41c9418..bf285acfe2 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultCipher_Create.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultCipher_Create.sql @@ -15,4 +15,10 @@ BEGIN @SubvaultId, @CipherId ) + + DECLARE @OrganizationId UNIQUEIDENTIFIER = (SELECT TOP 1 [OrganizationId] FROM [dbo].[Cipher] WHERE [Id] = @CipherId) + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultCipher_Delete.sql b/src/Sql/dbo/Stored Procedures/SubvaultCipher_Delete.sql index bf68b30d8a..4e1a3f958a 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultCipher_Delete.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultCipher_Delete.sql @@ -11,4 +11,10 @@ BEGIN WHERE [SubvaultId] = @SubvaultId AND [CipherId] = @CipherId + + DECLARE @OrganizationId UNIQUEIDENTIFIER = (SELECT TOP 1 [OrganizationId] FROM [dbo].[Cipher] WHERE [Id] = @CipherId) + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaults.sql b/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaults.sql index 7d19891af8..3e8487c542 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaults.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaults.sql @@ -51,4 +51,9 @@ BEGIN AND [Target].[SubvaultId] IN (SELECT [Id] FROM [AvailableSubvaultsCTE]) THEN DELETE ; + + IF @OrgId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaultsAdmin.sql b/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaultsAdmin.sql index 57454fc35e..dde179ad56 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaultsAdmin.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultCipher_UpdateSubvaultsAdmin.sql @@ -32,4 +32,9 @@ BEGIN AND [Target].[CipherId] = @CipherId THEN DELETE ; + + IF @OrganizationId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultUser_Create.sql b/src/Sql/dbo/Stored Procedures/SubvaultUser_Create.sql index 84039d4a9c..516188e5e1 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultUser_Create.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultUser_Create.sql @@ -27,4 +27,9 @@ BEGIN @CreationDate, @RevisionDate ) + + IF @OrganizationUserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @OrganizationUserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultUser_DeleteById.sql b/src/Sql/dbo/Stored Procedures/SubvaultUser_DeleteById.sql index b5f9a41135..e990920ec6 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultUser_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultUser_DeleteById.sql @@ -4,9 +4,16 @@ AS BEGIN SET NOCOUNT ON + DECLARE @OrganizationUserId UNIQUEIDENTIFIER = (SELECT TOP 1 [OrganizationUserId] FROM [dbo].[SubvaultUser] WHERE [Id] = @Id) + DELETE FROM [dbo].[SubvaultUser] WHERE [Id] = @Id + + IF @OrganizationUserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @OrganizationUserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/SubvaultUser_Update.sql b/src/Sql/dbo/Stored Procedures/SubvaultUser_Update.sql index 09e65a4db8..813949581f 100644 --- a/src/Sql/dbo/Stored Procedures/SubvaultUser_Update.sql +++ b/src/Sql/dbo/Stored Procedures/SubvaultUser_Update.sql @@ -19,4 +19,9 @@ BEGIN [RevisionDate] = @RevisionDate WHERE [Id] = @Id + + IF @OrganizationUserId IS NOT NULL + BEGIN + EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @OrganizationUserId + END END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDate.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDate.sql new file mode 100644 index 0000000000..fe10429880 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDate.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[User_BumpAccountRevisionDate] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [User] + SET + [AccountRevisionDate] = GETUTCDATE() + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationId.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationId.sql new file mode 100644 index 0000000000..d2a7ca7672 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationId.sql @@ -0,0 +1,18 @@ +CREATE PROCEDURE [dbo].[User_BumpAccountRevisionDateByOrganizationId] + @OrganizationId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + U + SET + U.[AccountRevisionDate] = GETUTCDATE() + FROM + [dbo].[User] U + INNER JOIN + [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] + WHERE + OU.[OrganizationId] = @OrganizationId + AND OU.[Status] = 2 -- Confirmed +END diff --git a/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationUserId.sql b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationUserId.sql new file mode 100644 index 0000000000..4e793439b1 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/User_BumpAccountRevisionDateByOrganizationUserId.sql @@ -0,0 +1,18 @@ +CREATE PROCEDURE [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] + @OrganizationUserId UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + UPDATE + U + SET + U.[AccountRevisionDate] = GETUTCDATE() + FROM + [dbo].[User] U + INNER JOIN + [dbo].[OrganizationUser] OU ON OU.[UserId] = U.[Id] + WHERE + OU.[Id] = @OrganizationUserId + AND OU.[Status] = 2 -- Confirmed +END diff --git a/src/Sql/dbo/Tables/Cipher.sql b/src/Sql/dbo/Tables/Cipher.sql index 8994eeae62..0a75c3a4e8 100644 --- a/src/Sql/dbo/Tables/Cipher.sql +++ b/src/Sql/dbo/Tables/Cipher.sql @@ -21,63 +21,4 @@ CREATE NONCLUSTERED INDEX [IX_Cipher_UserId_Type] GO CREATE NONCLUSTERED INDEX [IX_Cipher_OrganizationId_Type] ON [dbo].[Cipher]([OrganizationId] ASC, [Type] ASC) - WHERE [OrganizationId] IS NOT NULL; - - -GO -CREATE TRIGGER [dbo].[Cipher_Inserted] -ON [dbo].[Cipher] AFTER INSERT -AS -BEGIN - DECLARE @Count INT = (SELECT COUNT(1) FROM INSERTED) - IF @Count = 0 RETURN - - SET NOCOUNT ON - - DECLARE @UserId UNIQUEIDENTIFIER = (SELECT TOP 1 [UserId] FROM INSERTED) - - UPDATE - [User] - SET - [AccountRevisionDate] = GETUTCDATE() - WHERE - [Id] = @UserId -END -GO -CREATE TRIGGER [dbo].[Cipher_Updated] -ON [dbo].[Cipher] AFTER UPDATE -AS -BEGIN - DECLARE @Count INT = (SELECT COUNT(1) FROM INSERTED) - IF @Count = 0 RETURN - - SET NOCOUNT ON - - DECLARE @UserId UNIQUEIDENTIFIER = (SELECT TOP 1 [UserId] FROM INSERTED) - - UPDATE - [User] - SET - [AccountRevisionDate] = GETUTCDATE() - WHERE - [Id] = @UserId -END -GO -CREATE TRIGGER [dbo].[Cipher_Deleted] -ON [dbo].[Cipher] AFTER DELETE -AS -BEGIN - DECLARE @Count INT = (SELECT COUNT(1) FROM DELETED) - IF @Count = 0 RETURN - - SET NOCOUNT ON - - DECLARE @UserId UNIQUEIDENTIFIER = (SELECT TOP 1 [UserId] FROM DELETED) - - UPDATE - [User] - SET - [AccountRevisionDate] = GETUTCDATE() - WHERE - [Id] = @UserId -END \ No newline at end of file + WHERE [OrganizationId] IS NOT NULL; \ No newline at end of file