20260427112434_AddCrewIDToCrewWidgetConfig.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Microsoft.EntityFrameworkCore.Migrations;
  2. #nullable disable
  3. namespace Infrastructure.Migrations.AppDb
  4. {
  5. /// <inheritdoc />
  6. public partial class AddCrewIDToCrewWidgetConfig : Migration
  7. {
  8. /// <inheritdoc />
  9. protected override void Up(MigrationBuilder migrationBuilder)
  10. {
  11. migrationBuilder.AddColumn<int>(
  12. name: "CrewID",
  13. table: "CrewWidgetConfig",
  14. type: "int",
  15. nullable: false,
  16. defaultValue: 0);
  17. // 기존 row 보정: CrewWidgetConfig.ChannelID의 첫 번째 활성 Crew(없으면 모든 Crew 중 첫 번째)로 매핑
  18. migrationBuilder.Sql(@"
  19. UPDATE w
  20. SET w.CrewID = ISNULL(
  21. (SELECT TOP 1 c.ID FROM Crew c WHERE c.ChannelID = w.ChannelID AND c.IsActive = 1 ORDER BY c.ID),
  22. (SELECT TOP 1 c.ID FROM Crew c WHERE c.ChannelID = w.ChannelID ORDER BY c.ID)
  23. )
  24. FROM CrewWidgetConfig w
  25. WHERE w.CrewID = 0;
  26. ");
  27. // 보정 후에도 CrewID=0인 row(Crew 자체가 없는 채널의 위젯 설정)는 삭제
  28. migrationBuilder.Sql(@"DELETE FROM CrewWidgetConfig WHERE CrewID = 0;");
  29. migrationBuilder.CreateIndex(
  30. name: "IX_CrewWidgetConfig_CrewID",
  31. table: "CrewWidgetConfig",
  32. column: "CrewID");
  33. migrationBuilder.AddForeignKey(
  34. name: "FK_CrewWidgetConfig_Crew_CrewID",
  35. table: "CrewWidgetConfig",
  36. column: "CrewID",
  37. principalTable: "Crew",
  38. principalColumn: "ID",
  39. onDelete: ReferentialAction.NoAction);
  40. }
  41. /// <inheritdoc />
  42. protected override void Down(MigrationBuilder migrationBuilder)
  43. {
  44. migrationBuilder.DropForeignKey(
  45. name: "FK_CrewWidgetConfig_Crew_CrewID",
  46. table: "CrewWidgetConfig");
  47. migrationBuilder.DropIndex(
  48. name: "IX_CrewWidgetConfig_CrewID",
  49. table: "CrewWidgetConfig");
  50. migrationBuilder.DropColumn(
  51. name: "CrewID",
  52. table: "CrewWidgetConfig");
  53. }
  54. }
  55. }