Files
vtuber-awards/Backend/Migrations/20260623131228_AddAwardResultCategoryLock.cs
AzuTear 1a74c03621
CI - Build & Verify / Build, Typecheck & Hygiene (push) Successful in 57s
CI - Build & Verify / Deploy to award.noveria.net (push) Failing after 56s
Restore production migration history
2026-06-29 17:57:47 +02:00

94 lines
2.9 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Backend.Migrations
{
/// <inheritdoc />
public partial class AddAwardResultCategoryLock : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Results_SeasonId",
table: "Results");
migrationBuilder.AddColumn<int>(
name: "CategoryId",
table: "Results",
type: "integer",
nullable: true);
migrationBuilder.Sql(
"""
UPDATE "Results" AS r
SET "CategoryId" = c."Id"
FROM "Categories" AS c
WHERE r."SeasonId" = c."SeasonId"
AND lower(trim(r."CategoryName")) = lower(trim(c."Name"));
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM "Results" WHERE "CategoryId" IS NULL) THEN
RAISE EXCEPTION 'Could not backfill CategoryId for one or more rows in Results.';
END IF;
END $$;
""");
migrationBuilder.AlterColumn<int>(
name: "CategoryId",
table: "Results",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "integer",
oldNullable: true);
migrationBuilder.CreateIndex(
name: "IX_Results_CategoryId",
table: "Results",
column: "CategoryId");
migrationBuilder.CreateIndex(
name: "IX_Results_SeasonId_CategoryId",
table: "Results",
columns: new[] { "SeasonId", "CategoryId" },
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_Results_Categories_CategoryId",
table: "Results",
column: "CategoryId",
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Results_Categories_CategoryId",
table: "Results");
migrationBuilder.DropIndex(
name: "IX_Results_CategoryId",
table: "Results");
migrationBuilder.DropIndex(
name: "IX_Results_SeasonId_CategoryId",
table: "Results");
migrationBuilder.DropColumn(
name: "CategoryId",
table: "Results");
migrationBuilder.CreateIndex(
name: "IX_Results_SeasonId",
table: "Results",
column: "SeasonId");
}
}
}