Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-14590] Modify Notification database table #5361

Merged
merged 10 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Core/NotificationCenter/Entities/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Notification : ITableObject<Guid>
[MaxLength(256)]
public string? Title { get; set; }
public string? Body { get; set; }
public Guid? TaskId { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

โ›๏ธ I like these to be in order too.

public DateTime CreationDate { get; set; }
public DateTime RevisionDate { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public void Configure(EntityTypeBuilder<Notification> builder)
.HasIndex(n => n.UserId)
.IsClustered(false);

builder
.HasIndex(n => n.TaskId)
.IsClustered(false);

builder.ToTable(nameof(Notification));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
๏ปฟusing AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.Vault.Models;

namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;

public class Notification : Core.NotificationCenter.Entities.Notification
{
public virtual User User { get; set; }
public virtual Organization Organization { get; set; }
public virtual SecurityTask Task { get; set; }

Check warning on line 12 in src/Infrastructure.EntityFramework/NotificationCenter/Models/Notification.cs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L12 was not covered by tests
}

public class NotificationMapperProfile : Profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CREATE PROCEDURE [dbo].[Notification_Create]
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -23,7 +24,8 @@ BEGIN
[Title],
[Body],
[CreationDate],
[RevisionDate]
[RevisionDate],
[TaskId]
)
VALUES (
@Id,
Expand All @@ -35,6 +37,7 @@ BEGIN
@Title,
@Body,
@CreationDate,
@RevisionDate
@RevisionDate,
@TaskId
)
END
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CREATE PROCEDURE [dbo].[Notification_Update]
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -22,6 +23,7 @@ BEGIN
[Title] = @Title,
[Body] = @Body,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[TaskId] = @TaskId
WHERE [Id] = @Id
END
7 changes: 6 additions & 1 deletion src/Sql/NotificationCenter/dbo/Tables/Notification.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ CREATE TABLE [dbo].[Notification]
[Body] NVARCHAR (MAX) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
[TaskId] UNIQUEIDENTIFIER NULL,
CONSTRAINT [PK_Notification] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Notification_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
CONSTRAINT [FK_Notification_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
CONSTRAINT [FK_Notification_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]),
CONSTRAINT [FK_Notification_SecurityTask] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[SecurityTask] ([Id])
);


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

GO
CREATE NONCLUSTERED INDEX [IX_Notification_TaskId]
ON [dbo].[Notification] ([TaskId] ASC) WHERE TaskId IS NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
-- Add optional TaskId column to Notification table
IF COL_LENGTH('[dbo].[Notification]', 'TaskId') IS NULL
BEGIN
ALTER TABLE [dbo].[Notification]
ADD [TaskId] UNIQUEIDENTIFIER NULL

ALTER TABLE [dbo].[Notification]
ADD CONSTRAINT [FK_Notification_SecurityTask] FOREIGN KEY ([TaskId]) REFERENCES [dbo].[SecurityTask] ([Id])
END
GO

IF NOT EXISTS (SELECT *
FROM sys.indexes
WHERE name = 'IX_Notification_TaskId')
BEGIN
CREATE NONCLUSTERED INDEX [IX_Notification_TaskId]
ON [dbo].[Notification] ([TaskId] ASC) WHERE TaskId IS NOT NULL;
END
GO

-- Alter Notification_Create and Notification_Update stored procedures to include TaskId
CREATE OR ALTER PROCEDURE [dbo].[Notification_Create]
@Id UNIQUEIDENTIFIER OUTPUT,
@Priority TINYINT,
@Global BIT,
@ClientType TINYINT,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON

INSERT INTO [dbo].[Notification] (
[Id],
[Priority],
[Global],
[ClientType],
[UserId],
[OrganizationId],
[Title],
[Body],
[CreationDate],
[RevisionDate],
[TaskId]
)
VALUES (
@Id,
@Priority,
@Global,
@ClientType,
@UserId,
@OrganizationId,
@Title,
@Body,
@CreationDate,
@RevisionDate,
@TaskId
)
END
GO

CREATE OR ALTER PROCEDURE [dbo].[Notification_Update]
@Id UNIQUEIDENTIFIER,
@Priority TINYINT,
@Global BIT,
@ClientType TINYINT,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON

UPDATE [dbo].[Notification]
SET [Priority] = @Priority,
[Global] = @Global,
[ClientType] = @ClientType,
[UserId] = @UserId,
[OrganizationId] = @OrganizationId,
[Title] = @Title,
[Body] = @Body,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate,
[TaskId] = @TaskId
WHERE [Id] = @Id
END
GO

-- Recreate NotificationView
CREATE OR ALTER VIEW [dbo].[NotificationView]
AS
SELECT
*
FROM
[dbo].[Notification]
GO


Loading
Loading