-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationRunner.cs
More file actions
47 lines (42 loc) · 1.62 KB
/
MigrationRunner.cs
File metadata and controls
47 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
public static class MigrationRunner
{
// 履歴ファイル名(Application.StartupPath に作成)
private const string HistoryFileName = "migrations_applied.txt";
public static void RunMigrations(IEnumerable<IMigration> migrations)
{
if (migrations == null) return;
var historyPath = Path.Combine(Application.StartupPath, HistoryFileName);
var applied = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (File.Exists(historyPath))
{
foreach (var line in File.ReadAllLines(historyPath))
{
var t = line.Trim();
if (!string.IsNullOrEmpty(t)) applied.Add(t);
}
}
var pending = migrations.OrderBy(m => m.Id).Where(m => !applied.Contains(m.Id)).ToList();
if (pending.Count == 0) return;
foreach (var mig in pending)
{
try
{
mig.Up();
File.AppendAllText(historyPath, mig.Id + Environment.NewLine);
}
catch (Exception ex)
{
// 移行失敗時はユーザーに通知して処理を中断
MessageBox.Show($"マイグレーション '{mig.Id}' に失敗しました: {mig.Description}\n\n{ex.Message}",
"マイグレーションエラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 失敗したら以降のマイグレーションは行わない
break;
}
}
}
}