| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using Microsoft.EntityFrameworkCore.Migrations;
- #nullable disable
- namespace Infrastructure.Migrations.AppDb
- {
- /// <inheritdoc />
- public partial class AddCrewIDToCrewWidgetConfig : Migration
- {
- /// <inheritdoc />
- protected override void Up(MigrationBuilder migrationBuilder)
- {
- migrationBuilder.AddColumn<int>(
- 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);
- }
- /// <inheritdoc />
- 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");
- }
- }
- }
|