Created
November 22, 2023 08:50
-
-
Save xbotter/e67d11627058b2d61ea4da5923e3bcc5 to your computer and use it in GitHub Desktop.
Generate dotnet ef migration scripts for all migrations that don't have a script yet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# build the latest code | |
dotnet build | |
# get all migration ids and store them in an array | |
$migrations = dotnet ef migrations list --no-build --no-connect --json | ConvertFrom-Json | |
# iterate through the array | |
for ($i = 0; $i -lt $migrations.Length; $i++) { | |
# get the current migration id | |
$current = $migrations[$i].id | |
# get the previous migration id, if it doesn't exist, set it to 0 | |
$previous = if ($i -gt 0) { $migrations[$i-1].id } else { 0 } | |
# if the migration file doesn't exist | |
if (-not (Test-Path "sql\$current.sql")) { | |
Write-Host "Generating migration script for $current" | |
# generate the SQL script for the current migration and save it as <migrationId.sql> | |
dotnet ef migrations script --no-build $previous $current -o "sql\$current.sql" -i | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment