LANCommander/LANCommander.Server.Data.MySQL/Migrations/20260513000000_AddGameExternalIds.cs
2026-05-14 00:30:45 -05:00

98 lines
4 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LANCommander.Server.Data.MySQL.Migrations
{
/// <inheritdoc />
public partial class AddGameExternalIds : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "GameExternalIds",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
GameId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
Provider = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false),
ExternalId = table.Column<string>(type: "varchar(256)", maxLength: 256, nullable: false),
CreatedOn = table.Column<DateTime>(type: "datetime(6)", nullable: false),
CreatedById = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
UpdatedOn = table.Column<DateTime>(type: "datetime(6)", nullable: false),
UpdatedById = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci")
},
constraints: table =>
{
table.PrimaryKey("PK_GameExternalIds", x => x.Id);
table.ForeignKey(
name: "FK_GameExternalIds_Games_GameId",
column: x => x.GameId,
principalTable: "Games",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_GameExternalIds_Users_CreatedById",
column: x => x.CreatedById,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_GameExternalIds_Users_UpdatedById",
column: x => x.UpdatedById,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateIndex(
name: "IX_GameExternalIds_CreatedById",
table: "GameExternalIds",
column: "CreatedById");
migrationBuilder.CreateIndex(
name: "IX_GameExternalIds_GameId",
table: "GameExternalIds",
column: "GameId");
migrationBuilder.CreateIndex(
name: "IX_GameExternalIds_UpdatedById",
table: "GameExternalIds",
column: "UpdatedById");
// Migrate existing IGDBId data
migrationBuilder.Sql(@"
INSERT INTO GameExternalIds (Id, GameId, Provider, ExternalId, CreatedOn, UpdatedOn)
SELECT UUID(), Id, 'IGDB', CAST(IGDBId AS CHAR), CreatedOn, UpdatedOn
FROM Games
WHERE IGDBId IS NOT NULL
");
migrationBuilder.DropColumn(
name: "IGDBId",
table: "Games");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<long>(
name: "IGDBId",
table: "Games",
type: "bigint",
nullable: true);
// Migrate data back
migrationBuilder.Sql(@"
UPDATE Games g
INNER JOIN GameExternalIds e ON e.GameId = g.Id AND e.Provider = 'IGDB'
SET g.IGDBId = CAST(e.ExternalId AS SIGNED)
");
migrationBuilder.DropTable(
name: "GameExternalIds");
}
}
}