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) {
|
||||
Write-Host "Please provide a migration name." -ForegroundColor Red
|
||||
}
|
||||
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
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$MigrationName
|
||||
)
|
||||
|
||||
dotnet build EllieBot.csproj
|
||||
dotnet ef migrations add $migrationName --context SqliteContext --project EllieBot.csproj --no-build
|
||||
|
||||
# dotnet ef migrations add "${migrationName}_p1" --context PostgresqlContext --project EllieBot.csproj --no-build
|
||||
# dotnet ef migrations add $migrationName --context PostgresqlContext --project EllieBot.csproj --no-build
|
||||
|
||||
dotnet build EllieBot.csproj
|
||||
|
||||
# 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)
|
||||
|
||||
$sqliteMigName = ($sqliteMigNames[1] | Select-Object -ExpandProperty id).Split(' ')[0].Trim()
|
||||
$sqliteTempMigName = ($sqliteMigNames[0] | Select-Object -ExpandProperty id).Split(' ')[0].Trim()
|
||||
|
||||
|
||||
# $pgMigrationFullName = (dotnet ef migrations list --no-build --no-connect --project EllieBot.csproj -c PostgresqlContext | Select-Object -Last 1)
|
||||
# $pgMigName1 = $sqliteMigNames[0].Split(' ')[0].Trim()
|
||||
# $pgMigName2 = $sqliteMigNames[1].Split(' ')[0].Trim()
|
||||
|
||||
dotnet ef migrations script $firstMigration $migrationName --project EllieBot.csproj --context SqliteContext --output Migrations/Sqlite/$sqliteMigName.sql --no-build
|
||||
# dotnet ef migrations script $firstMigration $migrationName --project EllieBot.csproj --context PostgresqlContext --output Migrations/PostgreSql/$pgMigName2.sql --no-build
|
||||
|
||||
# delete the old first migration .cs and .Designer.cs
|
||||
Remove-Item "Migrations/Sqlite/$firstMigration.cs"
|
||||
Remove-Item "Migrations/Sqlite/$firstMigration.Designer.cs"
|
||||
Remove-Item "Migrations/Sqlite/${sqliteTempMigName}.cs"
|
||||
Remove-Item "Migrations/Sqlite/${sqliteTempMigName}.Designer.cs"
|
||||
|
||||
|
||||
# Remove-Item "Migrations/PostgreSql/$firstPgMigration.cs"
|
||||
# Remove-Item "Migrations/PostgreSql/$firstPgMigration.Designer.cs"
|
||||
# Remove-Item "Migrations/PostgreSql/${pgMigName1}.cs"
|
||||
# Remove-Item "Migrations/PostgreSql/${pgMigName1}.Designer.cs"
|
||||
}
|
||||
Write-Output "Creating new migration..."
|
||||
|
||||
# Step 1: Create initial migrations
|
||||
dotnet build
|
||||
|
||||
# Get previous migration IDs
|
||||
$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
|
||||
|
||||
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
|
||||
|
||||
dotnet build
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Error "Error: Failed to create migrations"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Step 2: Generate SQL scripts
|
||||
Write-Output "Generating diff SQL scripts..."
|
||||
|
||||
$newMigrationIdSqlite = (dotnet ef migrations list --context SqliteContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||
$newMigrationIdPostgresql = (dotnet ef migrations list --context PostgresqlContext --no-build --no-connect | Select-Object -Last 2 | Select-Object -First 1) -split ' ' | Select-Object -First 1
|
||||
|
||||
dotnet ef migrations script ($firstMigrationIdSqlite -replace '^.*_', '') $MigrationName --context SqliteContext -o "Migrations/Sqlite/$newMigrationIdSqlite.sql" --no-build
|
||||
dotnet ef migrations script ($firstMigrationIdPostgresql -replace '^.*_', '') $MigrationName --context PostgresqlContext -o "Migrations/Postgresql/$newMigrationIdPostgresql.sql" --no-build
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Error "Error: Failed to generate SQL script"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 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