100 lines
3.3 KiB
C#
100 lines
3.3 KiB
C#
using System;
|
|||
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||
|
|
|
||
|
|
#nullable disable
|
||
|
|
|
||
|
|
namespace mws.backend.dotnet.infrastructure.Migrations
|
||
|
|
{
|
||
|
|
/// <inheritdoc />
|
||
|
|
public partial class AddProjectAuditFields : Migration
|
||
|
|
{
|
||
|
|
/// <inheritdoc />
|
||
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||
|
|
{
|
||
|
|
migrationBuilder.AddColumn<Guid>(
|
||
|
|
name: "CreatedBy",
|
||
|
|
table: "projects",
|
||
|
|
type: "uuid",
|
||
|
|
nullable: false,
|
||
|
|
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
|
||
|
|
|
||
|
|
migrationBuilder.AddColumn<Guid>(
|
||
|
|
name: "UpdatedBy",
|
||
|
|
table: "projects",
|
||
|
|
type: "uuid",
|
||
|
|
nullable: true);
|
||
|
|
|
||
|
|
migrationBuilder.CreateIndex(
|
||
|
|
name: "IX_projects_CreatedBy",
|
||
|
|
table: "projects",
|
||
|
|
column: "CreatedBy");
|
||
|
|
|
||
|
|
migrationBuilder.CreateIndex(
|
||
|
|
name: "IX_projects_UpdatedBy",
|
||
|
|
table: "projects",
|
||
|
|
column: "UpdatedBy");
|
||
|
|
|
||
|
|
migrationBuilder.Sql(
|
||
|
|
"""
|
||
|
|
UPDATE projects
|
||
|
|
SET "CreatedBy" = COALESCE(
|
||
|
|
(SELECT pm."UserId" FROM project_members pm WHERE pm."ProjectId" = projects."Id" AND pm."Role" = 'Owner' LIMIT 1),
|
||
|
|
(SELECT pm."UserId" FROM project_members pm WHERE pm."ProjectId" = projects."Id" LIMIT 1),
|
||
|
|
(SELECT "Id" FROM users ORDER BY "CreatedAt" LIMIT 1)
|
||
|
|
)
|
||
|
|
WHERE "CreatedBy" = '00000000-0000-0000-0000-000000000000';
|
||
|
|
""");
|
||
|
|
|
||
|
|
migrationBuilder.AlterColumn<Guid>(
|
||
|
|
name: "CreatedBy",
|
||
|
|
table: "projects",
|
||
|
|
type: "uuid",
|
||
|
|
nullable: false);
|
||
|
|
|
||
|
|
migrationBuilder.AddForeignKey(
|
||
|
|
name: "FK_projects_users_CreatedBy",
|
||
|
|
table: "projects",
|
||
|
|
column: "CreatedBy",
|
||
|
|
principalTable: "users",
|
||
|
|
principalColumn: "Id",
|
||
|
|
onDelete: ReferentialAction.Restrict);
|
||
|
|
|
||
|
|
migrationBuilder.AddForeignKey(
|
||
|
|
name: "FK_projects_users_UpdatedBy",
|
||
|
|
table: "projects",
|
||
|
|
column: "UpdatedBy",
|
||
|
|
principalTable: "users",
|
||
|
|
principalColumn: "Id",
|
||
|
|
onDelete: ReferentialAction.Restrict);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <inheritdoc />
|
||
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||
|
|
{
|
||
|
|
migrationBuilder.DropForeignKey(
|
||
|
|
name: "FK_projects_users_CreatedBy",
|
||
|
|
table: "projects");
|
||
|
|
|
||
|
|
migrationBuilder.DropForeignKey(
|
||
|
|
name: "FK_projects_users_UpdatedBy",
|
||
|
|
table: "projects");
|
||
|
|
|
||
|
|
migrationBuilder.DropIndex(
|
||
|
|
name: "IX_projects_CreatedBy",
|
||
|
|
table: "projects");
|
||
|
|
|
||
|
|
migrationBuilder.DropIndex(
|
||
|
|
name: "IX_projects_UpdatedBy",
|
||
|
|
table: "projects");
|
||
|
|
|
||
|
|
migrationBuilder.DropColumn(
|
||
|
|
name: "CreatedBy",
|
||
|
|
table: "projects");
|
||
|
|
|
||
|
|
migrationBuilder.DropColumn(
|
||
|
|
name: "UpdatedBy",
|
||
|
|
table: "projects");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|