Skip to content

Commit

Permalink
Added foreignkey
Browse files Browse the repository at this point in the history
  • Loading branch information
gbubemismith committed Feb 7, 2025
1 parent f1d8071 commit 052b2cf
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 109 deletions.
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; }
}

public class NotificationMapperProfile : Profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ CREATE PROCEDURE [dbo].[Notification_Create]
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@TaskId UNIQUEIDENTIFIER = NULL,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -23,9 +23,9 @@ BEGIN
[OrganizationId],
[Title],
[Body],
[TaskId],
[CreationDate],
[RevisionDate]
[RevisionDate],
[TaskId]
)
VALUES (
@Id,
Expand All @@ -36,8 +36,8 @@ BEGIN
@OrganizationId,
@Title,
@Body,
@TaskId,
@CreationDate,
@RevisionDate
@RevisionDate,
@TaskId
)
END
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ CREATE PROCEDURE [dbo].[Notification_Update]
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@TaskId UNIQUEIDENTIFIER = NULL,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -22,8 +22,8 @@ BEGIN
[OrganizationId] = @OrganizationId,
[Title] = @Title,
[Body] = @Body,
[TaskId] = @TaskId,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[TaskId] = @TaskId
WHERE [Id] = @Id
END
8 changes: 6 additions & 2 deletions src/Sql/NotificationCenter/dbo/Tables/Notification.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ CREATE TABLE [dbo].[Notification]
[OrganizationId] UNIQUEIDENTIFIER NULL,
[Title] NVARCHAR (256) NULL,
[Body] NVARCHAR (MAX) NULL,
[TaskId] UNIQUEIDENTIFIER 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 @@ -31,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
@@ -1,12 +1,24 @@
-- Add optional Type column to Notification table
-- 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

-- Alter Notification_Create and Notification_Update stored procedures to include Type
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,
Expand All @@ -16,9 +28,9 @@ CREATE OR ALTER PROCEDURE [dbo].[Notification_Create]
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@TaskId UNIQUEIDENTIFIER = NULL,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -32,9 +44,9 @@ BEGIN
[OrganizationId],
[Title],
[Body],
[TaskId],
[CreationDate],
[RevisionDate]
[RevisionDate],
[TaskId]
)
VALUES (
@Id,
Expand All @@ -45,9 +57,9 @@ BEGIN
@OrganizationId,
@Title,
@Body,
@TaskId,
@CreationDate,
@RevisionDate
@RevisionDate,
@TaskId
)
END
GO
Expand All @@ -61,9 +73,9 @@ CREATE OR ALTER PROCEDURE [dbo].[Notification_Update]
@OrganizationId UNIQUEIDENTIFIER,
@Title NVARCHAR(256),
@Body NVARCHAR(MAX),
@TaskId UNIQUEIDENTIFIER = NULL,
@CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7)
@RevisionDate DATETIME2(7),
@TaskId UNIQUEIDENTIFIER = NULL
AS
BEGIN
SET NOCOUNT ON
Expand All @@ -76,9 +88,9 @@ BEGIN
[OrganizationId] = @OrganizationId,
[Title] = @Title,
[Body] = @Body,
[TaskId] = @TaskId,
[CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate
[RevisionDate] = @RevisionDate,
[TaskId] = @TaskId
WHERE [Id] = @Id
END
GO
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;

Check warning on line 1 in util/MySqlMigrations/Migrations/20250207204741_AddOptionalNotificationTaskId.cs

View workflow job for this annotation

GitHub Actions / Lint

Using directive is unnecessary.
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Bit.MySqlMigrations.Migrations

Check warning on line 6 in util/MySqlMigrations/Migrations/20250207204741_AddOptionalNotificationTaskId.cs

View workflow job for this annotation

GitHub Actions / Lint

Convert to file-scoped namespace
{
/// <inheritdoc />
public partial class AddOptionalNotifificationTaskId : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "TaskId",
table: "Notification",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");

migrationBuilder.CreateIndex(
name: "IX_Notification_TaskId",
table: "Notification",
column: "TaskId");

migrationBuilder.AddForeignKey(
name: "FK_Notification_SecurityTask_TaskId",
table: "Notification",
column: "TaskId",
principalTable: "SecurityTask",
principalColumn: "Id");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Notification_SecurityTask_TaskId",
table: "Notification");

migrationBuilder.DropIndex(
name: "IX_Notification_TaskId",
table: "Notification");

migrationBuilder.DropColumn(
name: "TaskId",
table: "Notification");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasIndex("OrganizationId")
.HasAnnotation("SqlServer:Clustered", false);

b.HasIndex("TaskId")
.HasAnnotation("SqlServer:Clustered", false);

b.HasIndex("UserId")
.HasAnnotation("SqlServer:Clustered", false);

Expand Down Expand Up @@ -2631,12 +2634,18 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.WithMany()
.HasForeignKey("OrganizationId");

b.HasOne("Bit.Infrastructure.EntityFramework.Vault.Models.SecurityTask", "Task")
.WithMany()
.HasForeignKey("TaskId");

b.HasOne("Bit.Infrastructure.EntityFramework.Models.User", "User")
.WithMany()
.HasForeignKey("UserId");

b.Navigation("Organization");

b.Navigation("Task");

b.Navigation("User");
});

Expand Down

This file was deleted.

Loading

0 comments on commit 052b2cf

Please sign in to comment.