updated migrate.ps1
This commit is contained in:
parent
ba01b9ae6a
commit
3e96405954
1 changed files with 54 additions and 48 deletions
|
@ -1,49 +1,55 @@
|
||||||
if ($args.Length -eq 0) {
|
param(
|
||||||
Write-Host "Please provide a migration name." -ForegroundColor Red
|
[Parameter(Mandatory=$true)]
|
||||||
}
|
[string]$MigrationName
|
||||||
else {
|
)
|
||||||
$migrationName = $args[0]
|
|
||||||
|
|
||||||
# find the first .cs item in the folder and get the name without extension
|
|
||||||
$firstMigration = (Get-ChildItem -Path "Migrations/Sqlite" -Filter *.cs | Select-Object -First 1).BaseName
|
|
||||||
$firstPgMigration = (Get-ChildItem -Path "Migrations/PostgreSql" -Filter *.cs | Select-Object -First 1).BaseName
|
|
||||||
|
|
||||||
dotnet build EllieBot.csproj
|
|
||||||
|
|
||||||
dotnet ef migrations add "${migrationName}_p1" --context SqliteContext --project EllieBot.csproj --no-build
|
|
||||||
dotnet ef migrations add "${migrationName}_p1" --context PostgresqlContext --project EllieBot.csproj --no-build
|
|
||||||
|
|
||||||
dotnet build EllieBot.csproj
|
Write-Output "Creating new migration..."
|
||||||
dotnet ef migrations add $migrationName --context SqliteContext --project EllieBot.csproj --no-build
|
|
||||||
|
# Step 1: Create initial migrations
|
||||||
# dotnet ef migrations add "${migrationName}_p1" --context PostgresqlContext --project EllieBot.csproj --no-build
|
dotnet build
|
||||||
# dotnet ef migrations add $migrationName --context PostgresqlContext --project EllieBot.csproj --no-build
|
|
||||||
|
# Get previous migration IDs
|
||||||
dotnet build EllieBot.csproj
|
$firstMigrationIdSqlite = (dotnet ef migrations list --context SqliteContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||||
|
$firstMigrationIdPostgresql = (dotnet ef migrations list --context PostgresqlContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||||
# list migrations as json, get their id and name
|
|
||||||
$sqliteMigNames = (dotnet ef migrations list --no-build --no-connect --project EllieBot.csproj -c SqliteContext --json | ConvertFrom-Json | Select-Object -Last 2)
|
dotnet ef migrations add $MigrationName --context SqliteContext --output-dir "Migrations/Sqlite" --no-build
|
||||||
|
dotnet ef migrations add $MigrationName --context PostgresqlContext --output-dir "Migrations/PostgreSql" --no-build
|
||||||
$sqliteMigName = ($sqliteMigNames[1] | Select-Object -ExpandProperty id).Split(' ')[0].Trim()
|
|
||||||
$sqliteTempMigName = ($sqliteMigNames[0] | Select-Object -ExpandProperty id).Split(' ')[0].Trim()
|
dotnet build
|
||||||
|
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
# $pgMigrationFullName = (dotnet ef migrations list --no-build --no-connect --project EllieBot.csproj -c PostgresqlContext | Select-Object -Last 1)
|
Write-Error "Error: Failed to create migrations"
|
||||||
# $pgMigName1 = $sqliteMigNames[0].Split(' ')[0].Trim()
|
exit 1
|
||||||
# $pgMigName2 = $sqliteMigNames[1].Split(' ')[0].Trim()
|
}
|
||||||
|
|
||||||
dotnet ef migrations script $firstMigration $migrationName --project EllieBot.csproj --context SqliteContext --output Migrations/Sqlite/$sqliteMigName.sql --no-build
|
# Step 2: Generate SQL scripts
|
||||||
# dotnet ef migrations script $firstMigration $migrationName --project EllieBot.csproj --context PostgresqlContext --output Migrations/PostgreSql/$pgMigName2.sql --no-build
|
Write-Output "Generating diff SQL scripts..."
|
||||||
|
|
||||||
# delete the old first migration .cs and .Designer.cs
|
$newMigrationIdSqlite = (dotnet ef migrations list --context SqliteContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||||
Remove-Item "Migrations/Sqlite/$firstMigration.cs"
|
$newMigrationIdPostgresql = (dotnet ef migrations list --context PostgresqlContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||||
Remove-Item "Migrations/Sqlite/$firstMigration.Designer.cs"
|
|
||||||
Remove-Item "Migrations/Sqlite/${sqliteTempMigName}.cs"
|
dotnet ef migrations script ($firstMigrationIdSqlite -replace '^.*_', '') $MigrationName --context SqliteContext -o "Migrations/Sqlite/$newMigrationIdSqlite.sql" --no-build
|
||||||
Remove-Item "Migrations/Sqlite/${sqliteTempMigName}.Designer.cs"
|
dotnet ef migrations script ($firstMigrationIdPostgresql -replace '^.*_', '') $MigrationName --context PostgresqlContext -o "Migrations/Postgresql/$newMigrationIdPostgresql.sql" --no-build
|
||||||
|
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
# Remove-Item "Migrations/PostgreSql/$firstPgMigration.cs"
|
Write-Error "Error: Failed to generate SQL script"
|
||||||
# Remove-Item "Migrations/PostgreSql/$firstPgMigration.Designer.cs"
|
exit 1
|
||||||
# Remove-Item "Migrations/PostgreSql/${pgMigName1}.cs"
|
}
|
||||||
# Remove-Item "Migrations/PostgreSql/${pgMigName1}.Designer.cs"
|
|
||||||
}
|
# Step 3: Cleanup migration files
|
||||||
|
Write-Output "Cleaning up all migration files..."
|
||||||
|
|
||||||
|
Get-ChildItem "Migrations/Sqlite" -File | Where-Object { $_.Name -like '*_*.cs' } | ForEach-Object {
|
||||||
|
Write-Output "Deleting: $($_.Name)"
|
||||||
|
Remove-Item $_.FullName -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
Get-ChildItem "Migrations/Postgresql" -File | Where-Object { $_.Name -like '*_*.cs' } | ForEach-Object {
|
||||||
|
Write-Output "Deleting: $($_.Name)"
|
||||||
|
Remove-Item $_.FullName -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
# Step 4: Create new initial migrations
|
||||||
|
Write-Output "Creating new initial migration..."
|
||||||
|
dotnet ef migrations add $MigrationName --context SqliteContext --output-dir "Migrations/Sqlite" --no-build
|
||||||
|
dotnet ef migrations add $MigrationName --context PostgresqlContext --output-dir "Migrations/PostgreSql" --no-build
|
Loading…
Add table
Reference in a new issue