using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Infrastructure.Migrations.AppDb { /// public partial class AddCrewIDToCrewWidgetConfig : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "CrewID", table: "CrewWidgetConfig", type: "int", nullable: false, defaultValue: 0); // 기존 row 보정: CrewWidgetConfig.ChannelID의 첫 번째 활성 Crew(없으면 모든 Crew 중 첫 번째)로 매핑 migrationBuilder.Sql(@" UPDATE w SET w.CrewID = ISNULL( (SELECT TOP 1 c.ID FROM Crew c WHERE c.ChannelID = w.ChannelID AND c.IsActive = 1 ORDER BY c.ID), (SELECT TOP 1 c.ID FROM Crew c WHERE c.ChannelID = w.ChannelID ORDER BY c.ID) ) FROM CrewWidgetConfig w WHERE w.CrewID = 0; "); // 보정 후에도 CrewID=0인 row(Crew 자체가 없는 채널의 위젯 설정)는 삭제 migrationBuilder.Sql(@"DELETE FROM CrewWidgetConfig WHERE CrewID = 0;"); migrationBuilder.CreateIndex( name: "IX_CrewWidgetConfig_CrewID", table: "CrewWidgetConfig", column: "CrewID"); migrationBuilder.AddForeignKey( name: "FK_CrewWidgetConfig_Crew_CrewID", table: "CrewWidgetConfig", column: "CrewID", principalTable: "Crew", principalColumn: "ID", onDelete: ReferentialAction.NoAction); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_CrewWidgetConfig_Crew_CrewID", table: "CrewWidgetConfig"); migrationBuilder.DropIndex( name: "IX_CrewWidgetConfig_CrewID", table: "CrewWidgetConfig"); migrationBuilder.DropColumn( name: "CrewID", table: "CrewWidgetConfig"); } } }