20260215093503_a7.cs 4.1 KB

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