Skip to content

Commit bde11da

Browse files
authored
[PM-14590] Modify Notification database table (#5361)
* Added notification type enum Added option type to entity * created migration files * made sprocs backward compatible * made sprocs backward compatible * Fixed linting * Altered table to require an optional taskId * formatted code * Added foreignkey * Formatted code * fixed order
1 parent e4d862f commit bde11da

16 files changed

+9330
-6
lines changed

src/Core/NotificationCenter/Entities/Notification.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Notification : ITableObject<Guid>
2020
public string? Body { get; set; }
2121
public DateTime CreationDate { get; set; }
2222
public DateTime RevisionDate { get; set; }
23+
public Guid? TaskId { get; set; }
2324

2425
public void SetNewId()
2526
{

src/Infrastructure.EntityFramework/NotificationCenter/Configurations/NotificationEntityTypeConfiguration.cs

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public void Configure(EntityTypeBuilder<Notification> builder)
3030
.HasIndex(n => n.UserId)
3131
.IsClustered(false);
3232

33+
builder
34+
.HasIndex(n => n.TaskId)
35+
.IsClustered(false);
36+
3337
builder.ToTable(nameof(Notification));
3438
}
3539
}

src/Infrastructure.EntityFramework/NotificationCenter/Models/Notification.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using AutoMapper;
22
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
33
using Bit.Infrastructure.EntityFramework.Models;
4+
using Bit.Infrastructure.EntityFramework.Vault.Models;
45

56
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
67

78
public class Notification : Core.NotificationCenter.Entities.Notification
89
{
910
public virtual User User { get; set; }
1011
public virtual Organization Organization { get; set; }
12+
public virtual SecurityTask Task { get; set; }
1113
}
1214

1315
public class NotificationMapperProfile : Profile

src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_Create.sql

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CREATE PROCEDURE [dbo].[Notification_Create]
88
@Title NVARCHAR(256),
99
@Body NVARCHAR(MAX),
1010
@CreationDate DATETIME2(7),
11-
@RevisionDate DATETIME2(7)
11+
@RevisionDate DATETIME2(7),
12+
@TaskId UNIQUEIDENTIFIER = NULL
1213
AS
1314
BEGIN
1415
SET NOCOUNT ON
@@ -23,7 +24,8 @@ BEGIN
2324
[Title],
2425
[Body],
2526
[CreationDate],
26-
[RevisionDate]
27+
[RevisionDate],
28+
[TaskId]
2729
)
2830
VALUES (
2931
@Id,
@@ -35,6 +37,7 @@ BEGIN
3537
@Title,
3638
@Body,
3739
@CreationDate,
38-
@RevisionDate
40+
@RevisionDate,
41+
@TaskId
3942
)
4043
END

src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_Update.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CREATE PROCEDURE [dbo].[Notification_Update]
88
@Title NVARCHAR(256),
99
@Body NVARCHAR(MAX),
1010
@CreationDate DATETIME2(7),
11-
@RevisionDate DATETIME2(7)
11+
@RevisionDate DATETIME2(7),
12+
@TaskId UNIQUEIDENTIFIER = NULL
1213
AS
1314
BEGIN
1415
SET NOCOUNT ON
@@ -22,6 +23,7 @@ BEGIN
2223
[Title] = @Title,
2324
[Body] = @Body,
2425
[CreationDate] = @CreationDate,
25-
[RevisionDate] = @RevisionDate
26+
[RevisionDate] = @RevisionDate,
27+
[TaskId] = @TaskId
2628
WHERE [Id] = @Id
2729
END

src/Sql/NotificationCenter/dbo/Tables/Notification.sql

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ CREATE TABLE [dbo].[Notification]
1010
[Body] NVARCHAR (MAX) NULL,
1111
[CreationDate] DATETIME2 (7) NOT NULL,
1212
[RevisionDate] DATETIME2 (7) NOT NULL,
13+
[TaskId] UNIQUEIDENTIFIER NULL,
1314
CONSTRAINT [PK_Notification] PRIMARY KEY CLUSTERED ([Id] ASC),
1415
CONSTRAINT [FK_Notification_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
15-
CONSTRAINT [FK_Notification_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
16+
CONSTRAINT [FK_Notification_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]),
17+
CONSTRAINT [FK_Notification_SecurityTask] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[SecurityTask] ([Id])
1618
);
1719

1820

@@ -30,3 +32,6 @@ GO
3032
CREATE NONCLUSTERED INDEX [IX_Notification_OrganizationId]
3133
ON [dbo].[Notification]([OrganizationId] ASC) WHERE OrganizationId IS NOT NULL;
3234

35+
GO
36+
CREATE NONCLUSTERED INDEX [IX_Notification_TaskId]
37+
ON [dbo].[Notification] ([TaskId] ASC) WHERE TaskId IS NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
-- Add optional TaskId column to Notification table
2+
IF COL_LENGTH('[dbo].[Notification]', 'TaskId') IS NULL
3+
BEGIN
4+
ALTER TABLE [dbo].[Notification]
5+
ADD [TaskId] UNIQUEIDENTIFIER NULL
6+
7+
ALTER TABLE [dbo].[Notification]
8+
ADD CONSTRAINT [FK_Notification_SecurityTask] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[SecurityTask] ([Id])
9+
END
10+
GO
11+
12+
IF NOT EXISTS (SELECT *
13+
FROM sys.indexes
14+
WHERE name = 'IX_Notification_TaskId')
15+
BEGIN
16+
CREATE NONCLUSTERED INDEX [IX_Notification_TaskId]
17+
ON [dbo].[Notification] ([TaskId] ASC) WHERE TaskId IS NOT NULL;
18+
END
19+
GO
20+
21+
-- Alter Notification_Create and Notification_Update stored procedures to include TaskId
22+
CREATE OR ALTER PROCEDURE [dbo].[Notification_Create]
23+
@Id UNIQUEIDENTIFIER OUTPUT,
24+
@Priority TINYINT,
25+
@Global BIT,
26+
@ClientType TINYINT,
27+
@UserId UNIQUEIDENTIFIER,
28+
@OrganizationId UNIQUEIDENTIFIER,
29+
@Title NVARCHAR(256),
30+
@Body NVARCHAR(MAX),
31+
@CreationDate DATETIME2(7),
32+
@RevisionDate DATETIME2(7),
33+
@TaskId UNIQUEIDENTIFIER = NULL
34+
AS
35+
BEGIN
36+
SET NOCOUNT ON
37+
38+
INSERT INTO [dbo].[Notification] (
39+
[Id],
40+
[Priority],
41+
[Global],
42+
[ClientType],
43+
[UserId],
44+
[OrganizationId],
45+
[Title],
46+
[Body],
47+
[CreationDate],
48+
[RevisionDate],
49+
[TaskId]
50+
)
51+
VALUES (
52+
@Id,
53+
@Priority,
54+
@Global,
55+
@ClientType,
56+
@UserId,
57+
@OrganizationId,
58+
@Title,
59+
@Body,
60+
@CreationDate,
61+
@RevisionDate,
62+
@TaskId
63+
)
64+
END
65+
GO
66+
67+
CREATE OR ALTER PROCEDURE [dbo].[Notification_Update]
68+
@Id UNIQUEIDENTIFIER,
69+
@Priority TINYINT,
70+
@Global BIT,
71+
@ClientType TINYINT,
72+
@UserId UNIQUEIDENTIFIER,
73+
@OrganizationId UNIQUEIDENTIFIER,
74+
@Title NVARCHAR(256),
75+
@Body NVARCHAR(MAX),
76+
@CreationDate DATETIME2(7),
77+
@RevisionDate DATETIME2(7),
78+
@TaskId UNIQUEIDENTIFIER = NULL
79+
AS
80+
BEGIN
81+
SET NOCOUNT ON
82+
83+
UPDATE [dbo].[Notification]
84+
SET [Priority] = @Priority,
85+
[Global] = @Global,
86+
[ClientType] = @ClientType,
87+
[UserId] = @UserId,
88+
[OrganizationId] = @OrganizationId,
89+
[Title] = @Title,
90+
[Body] = @Body,
91+
[CreationDate] = @CreationDate,
92+
[RevisionDate] = @RevisionDate,
93+
[TaskId] = @TaskId
94+
WHERE [Id] = @Id
95+
END
96+
GO
97+
98+
-- Recreate NotificationView
99+
CREATE OR ALTER VIEW [dbo].[NotificationView]
100+
AS
101+
SELECT
102+
*
103+
FROM
104+
[dbo].[Notification]
105+
GO
106+
107+

0 commit comments

Comments
 (0)