47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace DramaLing.Api.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class AddDifficultyLevelNumeric : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
// 1. 新增數字欄位 (預設值為 0)
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "difficulty_level_numeric",
|
|
table: "flashcards",
|
|
type: "INTEGER",
|
|
nullable: false,
|
|
defaultValue: 0);
|
|
|
|
// 2. 資料遷移:將現有字串值轉換為數字
|
|
migrationBuilder.Sql(@"
|
|
UPDATE flashcards
|
|
SET difficulty_level_numeric =
|
|
CASE difficulty_level
|
|
WHEN 'A1' THEN 1
|
|
WHEN 'A2' THEN 2
|
|
WHEN 'B1' THEN 3
|
|
WHEN 'B2' THEN 4
|
|
WHEN 'C1' THEN 5
|
|
WHEN 'C2' THEN 6
|
|
ELSE 0
|
|
END
|
|
WHERE difficulty_level IS NOT NULL;
|
|
");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropColumn(
|
|
name: "difficulty_level_numeric",
|
|
table: "flashcards");
|
|
}
|
|
}
|
|
}
|