AvaloniaStack/scripts/add-migration.ps1
luoqian 1ab9a90831 feat: 重构数据库迁移架构,支持多数据库提供程序 (SQLite/SQLServer/PostgreSQL/MySQL)
- 将迁移文件按数据库类型分目录存放 (Migrations/SQLite, MySQL, PostgreSQL, SqlServer)
- 新增各数据库提供程序的 DesignTimeDbContextFactory,支持 --provider 参数切换
- 新增 ProviderAppDataContexts,定义各数据库对应的 AppDataContext 子类
- DatabaseExtensions 增加 AddProviderAppDataContext 方法,按配置自动注册对应 DbContext
- 修正 MySQL 提供程序调用方式 (UseMySql -> UseMySQL)
- UserEntity 模型增加新字段
- 更新 add-migration.ps1
2026-05-20 16:40:57 +08:00

93 lines
3.4 KiB
PowerShell

param(
[string]$Name,
[ValidateSet("SQLite", "SqlServer", "PostgreSQL", "MySQL", "All")]
[string]$Provider = "All",
[string]$Project = "Avalonia-EFCore/Avalonia-EFCore.csproj",
[string]$StartupProject = "Avalonia-API/Avalonia-API.csproj",
[string]$OutputDir = "Migrations"
)
$ErrorActionPreference = "Stop"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
Set-Location $repoRoot
if ([string]::IsNullOrWhiteSpace($Name)) {
$Name = "AutoMigration_{0}" -f (Get-Date -Format "yyyyMMddHHmmss")
}
Write-Host "Restoring local dotnet tools..."
dotnet tool restore
if ($LASTEXITCODE -ne 0) {
throw "dotnet tool restore failed."
}
function Get-ContextName([string]$providerName) {
switch ($providerName) {
"SQLite" { return "SqliteAppDataContext" }
"SqlServer" { return "SqlServerAppDataContext" }
"PostgreSQL" { return "PostgreSqlAppDataContext" }
"MySQL" { return "MySqlAppDataContext" }
default { throw "Unsupported provider '$providerName'." }
}
}
function Add-ProviderMigration([string]$providerName) {
$context = Get-ContextName $providerName
$providerOutputDir = Join-Path $OutputDir $providerName
Write-Host "Generating migration '$Name' for $providerName..."
dotnet tool run dotnet-ef migrations add $Name `
--project $Project `
--startup-project $StartupProject `
--context $context `
--output-dir $providerOutputDir
if ($LASTEXITCODE -ne 0) {
throw "dotnet ef migrations add failed for $providerName."
}
$migrationDir = Join-Path (Split-Path $Project -Parent) $providerOutputDir
$migrationFile = Get-ChildItem $migrationDir -Filter "*_$Name.cs" |
Where-Object { $_.Name -notlike "*.Designer.cs" } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if ($null -eq $migrationFile) {
throw "Migration file was not found for '$Name' ($providerName)."
}
$content = Get-Content $migrationFile.FullName -Raw
$upMatch = [regex]::Match($content, "protected override void Up\(MigrationBuilder migrationBuilder\)\s*\{(?<body>.*?)\n\s*\}", "Singleline")
$downMatch = [regex]::Match($content, "protected override void Down\(MigrationBuilder migrationBuilder\)\s*\{(?<body>.*?)\n\s*\}", "Singleline")
$upBody = if ($upMatch.Success) { $upMatch.Groups["body"].Value.Trim() } else { "" }
$downBody = if ($downMatch.Success) { $downMatch.Groups["body"].Value.Trim() } else { "" }
if ([string]::IsNullOrWhiteSpace($upBody) -and [string]::IsNullOrWhiteSpace($downBody)) {
Write-Host "No model changes were detected for $providerName. Removing empty migration '$Name'..."
dotnet tool run dotnet-ef migrations remove --force `
--project $Project `
--startup-project $StartupProject `
--context $context
if ($LASTEXITCODE -ne 0) {
throw "dotnet ef migrations remove failed for $providerName."
}
return
}
Write-Host "Migration generated for ${providerName}:"
Write-Host " $($migrationFile.FullName)"
}
$providers = if ($Provider -eq "All") {
@("SQLite", "SqlServer", "PostgreSQL", "MySQL")
} else {
@($Provider)
}
foreach ($providerName in $providers) {
Add-ProviderMigration $providerName
}
Write-Host "Review the migration files, then start the app. Startup will apply the migration set matching DatabaseConfiguration.Provider."