using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastructure.Persistence.Migrations
{
///
public partial class a7 : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
// 1. PopupPosition 테이블 생성
migrationBuilder.CreateTable(
name: "PopupPosition",
columns: table => new
{
ID = table.Column(type: "int", nullable: false, comment: "PK")
.Annotation("SqlServer:Identity", "1, 1"),
Code = table.Column(type: "nvarchar(30)", maxLength: 30, nullable: false, comment: "위치 구분"),
Subject = table.Column(type: "nvarchar(255)", maxLength: 255, nullable: false, comment: "위치 명"),
IsActive = table.Column(type: "bit", nullable: false, comment: "사용 여부"),
UpdatedAt = table.Column(type: "datetime2", nullable: true, comment: "수정 일시"),
CreatedAt = table.Column(type: "datetime2", nullable: false, comment: "등록 일시")
},
constraints: table =>
{
table.PrimaryKey("PK_PopupPosition", x => x.ID);
},
comment: "팝업 위치");
migrationBuilder.CreateIndex(
name: "IX_PopupPosition_Code",
table: "PopupPosition",
column: "Code",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_PopupPosition_Code_IsActive",
table: "PopupPosition",
columns: new[] { "Code", "IsActive" });
migrationBuilder.CreateIndex(
name: "IX_PopupPosition_IsActive",
table: "PopupPosition",
column: "IsActive");
// 2. 기본 위치 생성
migrationBuilder.Sql(
"INSERT INTO PopupPosition (Code, Subject, IsActive, CreatedAt) VALUES ('default', N'기본', 1, GETUTCDATE())");
// 3. Popup 테이블에 PositionID 컬럼 추가 (기본값 = 기본 위치 ID)
migrationBuilder.Sql(
"ALTER TABLE Popup ADD PositionID int NOT NULL DEFAULT 0");
// 4. 기존 팝업 데이터를 기본 위치로 매핑
migrationBuilder.Sql(
"UPDATE Popup SET PositionID = (SELECT TOP 1 ID FROM PopupPosition WHERE Code = 'default')");
// 5. DEFAULT 제약 조건 제거
migrationBuilder.Sql(@"
DECLARE @name NVARCHAR(256)
SELECT @name = dc.name
FROM sys.default_constraints dc
JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
WHERE c.name = 'PositionID' AND dc.parent_object_id = OBJECT_ID('Popup')
IF @name IS NOT NULL EXEC('ALTER TABLE Popup DROP CONSTRAINT ' + @name)");
// 6. 인덱스 및 FK 추가
migrationBuilder.CreateIndex(
name: "IX_Popup_PositionID",
table: "Popup",
column: "PositionID");
migrationBuilder.AddForeignKey(
name: "FK_Popup_PopupPosition_PositionID",
table: "Popup",
column: "PositionID",
principalTable: "PopupPosition",
principalColumn: "ID",
onDelete: ReferentialAction.Cascade);
}
///
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Popup_PopupPosition_PositionID",
table: "Popup");
migrationBuilder.DropTable(
name: "PopupPosition");
migrationBuilder.DropIndex(
name: "IX_Popup_PositionID",
table: "Popup");
migrationBuilder.DropColumn(
name: "PositionID",
table: "Popup");
}
}
}