20260215093503_a7.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using Microsoft.EntityFrameworkCore.Migrations;
  3. #nullable disable
  4. namespace Infrastructure.Persistence.Migrations;
  5. /// <inheritdoc />
  6. public partial class a7 : Migration
  7. {
  8. /// <inheritdoc />
  9. protected override void Up(MigrationBuilder migrationBuilder)
  10. {
  11. // 1. PopupPosition 테이블 생성
  12. migrationBuilder.CreateTable(
  13. name: "PopupPosition",
  14. columns: table => new
  15. {
  16. ID = table.Column<int>(type: "int", nullable: false, comment: "PK")
  17. .Annotation("SqlServer:Identity", "1, 1"),
  18. Code = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false, comment: "위치 구분"),
  19. Subject = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false, comment: "위치 명"),
  20. IsActive = table.Column<bool>(type: "bit", nullable: false, comment: "사용 여부"),
  21. UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "수정 일시"),
  22. CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, comment: "등록 일시")
  23. },
  24. constraints: table =>
  25. {
  26. table.PrimaryKey("PK_PopupPosition", x => x.ID);
  27. },
  28. comment: "팝업 위치");
  29. migrationBuilder.CreateIndex(
  30. name: "IX_PopupPosition_Code",
  31. table: "PopupPosition",
  32. column: "Code",
  33. unique: true);
  34. migrationBuilder.CreateIndex(
  35. name: "IX_PopupPosition_Code_IsActive",
  36. table: "PopupPosition",
  37. columns: new[] { "Code", "IsActive" });
  38. migrationBuilder.CreateIndex(
  39. name: "IX_PopupPosition_IsActive",
  40. table: "PopupPosition",
  41. column: "IsActive");
  42. // 2. 기본 위치 생성
  43. migrationBuilder.Sql(
  44. "INSERT INTO PopupPosition (Code, Subject, IsActive, CreatedAt) VALUES ('default', N'기본', 1, GETUTCDATE())");
  45. // 3. Popup 테이블에 PositionID 컬럼 추가 (기본값 = 기본 위치 ID)
  46. migrationBuilder.Sql(
  47. "ALTER TABLE Popup ADD PositionID int NOT NULL DEFAULT 0");
  48. // 4. 기존 팝업 데이터를 기본 위치로 매핑
  49. migrationBuilder.Sql(
  50. "UPDATE Popup SET PositionID = (SELECT TOP 1 ID FROM PopupPosition WHERE Code = 'default')");
  51. // 5. DEFAULT 제약 조건 제거
  52. migrationBuilder.Sql(@"
  53. DECLARE @name NVARCHAR(256)
  54. SELECT @name = dc.name
  55. FROM sys.default_constraints dc
  56. JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
  57. WHERE c.name = 'PositionID' AND dc.parent_object_id = OBJECT_ID('Popup')
  58. IF @name IS NOT NULL EXEC('ALTER TABLE Popup DROP CONSTRAINT ' + @name)");
  59. // 6. 인덱스 및 FK 추가
  60. migrationBuilder.CreateIndex(
  61. name: "IX_Popup_PositionID",
  62. table: "Popup",
  63. column: "PositionID");
  64. migrationBuilder.AddForeignKey(
  65. name: "FK_Popup_PopupPosition_PositionID",
  66. table: "Popup",
  67. column: "PositionID",
  68. principalTable: "PopupPosition",
  69. principalColumn: "ID",
  70. onDelete: ReferentialAction.Cascade);
  71. }
  72. /// <inheritdoc />
  73. protected override void Down(MigrationBuilder migrationBuilder)
  74. {
  75. migrationBuilder.DropForeignKey(
  76. name: "FK_Popup_PopupPosition_PositionID",
  77. table: "Popup");
  78. migrationBuilder.DropTable(
  79. name: "PopupPosition");
  80. migrationBuilder.DropIndex(
  81. name: "IX_Popup_PositionID",
  82. table: "Popup");
  83. migrationBuilder.DropColumn(
  84. name: "PositionID",
  85. table: "Popup");
  86. }
  87. }