Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions Reset-EfNswag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ Example usage:
2. Run: ./Reset-EfNswag.ps1
or: ./Reset-EfNswag.ps1 -Products $customProducts
For each product
3. Script will delete EF migration
4. Script will recreate new migration with current model for that 1 product
5. Script will update database with new migration for that 1 product
6. Script will run NSwag client code generation script for that 1 product
3. Script will delete EF migrations (default)
4. Script will recreate InitialCreate migration (default)
5. Script will update database with migrations (default)
6. Script will run NSwag client code generation script for that 1 product

Recommended usage:
- Default full reset+migrate flow: ./Reset-EfNswag.ps1
- Push migrations only (keep existing migration files): ./Reset-EfNswag.ps1 -SkipResetMigrations
- Clear tables once per database before push (default behavior): ./Reset-EfNswag.ps1 -DropTables
- Drop database once per database before push: ./Reset-EfNswag.ps1 -DropDatabase
=====================================================================
#>

Expand All @@ -18,6 +24,7 @@ param (
[array]$Products = @(
@{ Name = "AgentFramework"; Root = ".\src"; Database = "AgentFramework"; ApiProject = "Presentation.Api" }
),
[switch]$SkipResetMigrations,
[switch]$DropDatabase,
[switch]$DropTables,
[string]$dropTablesPath = ".\data\Admin\Drop Tables.sql"
Expand Down Expand Up @@ -62,6 +69,7 @@ if ($DropDatabase -and $DropTables) {
# Preserve existing behavior for this script: drop tables unless explicitly overridden.
$useDropTables = $DropTables
$useDropDatabase = $DropDatabase
$useResetMigrations = -not $SkipResetMigrations
if (-not $DropDatabase -and -not $DropTables) {
$useDropTables = $true
}
Expand All @@ -73,13 +81,20 @@ try {
Write-Host "[DIAG] Running: " + $cmd -ForegroundColor Yellow
try {
Invoke-Expression $cmd
if ($LASTEXITCODE -ne 0) {
throw "Command exited with code $LASTEXITCODE"
}
}
catch {
Write-Error "[ERROR] Command failed: $cmd"
Write-Error $_
throw
}
}

$databasesDroppedByTable = @{}
$databasesDroppedByDrop = @{}

# Ensure dotnet-ef tool is available (idempotent)
$srcPath = Join-Path $PSScriptRoot 'src'
$toolsManifest = Join-Path $srcPath 'dotnet-tools.json'
Expand Down Expand Up @@ -128,40 +143,49 @@ try {
Invoke-DiagnosticCommand "dotnet restore $webApiProj"
Invoke-DiagnosticCommand "dotnet build $webApiProj --no-restore"

Write-Host "Removing migration files"
Remove-Item $infraPath -ErrorAction SilentlyContinue
Write-Host "Creating new InitialCreate migration without dropping database..." -ForegroundColor Cyan
if ($useResetMigrations) {
Write-Host "[STEP] Removing migration files for $name..." -ForegroundColor Magenta
Remove-Item $infraPath -ErrorAction SilentlyContinue
Write-Host "[STEP] Creating new InitialCreate migration for $context..." -ForegroundColor Cyan
}
else {
Write-Host "[STEP] SkipResetMigrations enabled. Using existing migrations for $name." -ForegroundColor DarkCyan
}
# Build absolute paths from $PSScriptRoot (immune to CWD changes; avoids [IO.Path]::GetFullPath which
# resolves against [Environment]::CurrentDirectory, not $PWD, causing wrong paths after Push-Location).
$infraProjAbs = Join-Path $PSScriptRoot ($infraProj -replace '^\.[\\/]', '')
$webApiProjAbs = Join-Path $PSScriptRoot ($webApiProj -replace '^\.[\\/]', '')

if ($useDropTables) {
if ($useDropTables -and -not $databasesDroppedByTable.ContainsKey($database)) {
if (Test-Path $dropTablesPath) {
if (Test-DatabaseExists -DatabaseName $database) {
Write-Host "[STEP] Dropping all tables for $name via $dropTablesPath..." -ForegroundColor Magenta
Write-Host "[STEP] Dropping all tables for shared database '$database' via $dropTablesPath..." -ForegroundColor Magenta
$dropResult = sqlcmd -S "(localdb)\MSSQLLocalDB" -d $database -b -i $dropTablesPath 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "[ERROR] Drop tables script failed for ${name}: $dropResult" -ForegroundColor Red
throw "[FAIL-FAST] Table drop failed for $name. Stopping script."
Write-Host "[ERROR] Drop tables script failed for database '${database}': $dropResult" -ForegroundColor Red
throw "[FAIL-FAST] Table drop failed for database '$database'. Stopping script."
}
Write-Host "[SUCCESS] All tables dropped for $name." -ForegroundColor Green
Write-Host "[SUCCESS] All tables dropped for shared database '$database'." -ForegroundColor Green
}
else {
Write-Host "[WARN] Database '$database' does not exist. Skipping drop tables step for $name." -ForegroundColor Yellow
Write-Host "[WARN] Database '$database' does not exist. Skipping drop tables step." -ForegroundColor Yellow
}
$databasesDroppedByTable[$database] = $true
}
else {
throw "Drop script not found at $dropTablesPath"
}
}
Push-Location $srcPath
try {
if ($useDropDatabase) {
Write-Host "[STEP] Dropping database for $name..." -ForegroundColor Magenta
if ($useDropDatabase -and -not $databasesDroppedByDrop.ContainsKey($database)) {
Write-Host "[STEP] Dropping shared database '$database' before applying contexts..." -ForegroundColor Magenta
Invoke-DiagnosticCommand "dotnet ef database drop --project `"$infraProjAbs`" --startup-project `"$webApiProjAbs`" --context $context --connection '$connection' --force --verbose"
$databasesDroppedByDrop[$database] = $true
}
if ($useResetMigrations) {
Invoke-DiagnosticCommand "dotnet ef migrations add InitialCreate-$context --project `"$infraProjAbs`" --startup-project `"$webApiProjAbs`" --context $context --verbose"
}
Invoke-DiagnosticCommand "dotnet ef migrations add InitialCreate-$context --project `"$infraProjAbs`" --startup-project `"$webApiProjAbs`" --context $context --verbose"
Invoke-DiagnosticCommand "dotnet ef database update --project `"$infraProjAbs`" --startup-project `"$webApiProjAbs`" --context $context --connection '$connection' --verbose"
} finally {
Pop-Location
Expand All @@ -180,8 +204,6 @@ try {
Write-Host "NSwag script not found for " + $name + " - " + $nswagScript -ForegroundColor Red
}

Pop-Location
Push-Location
}
}
finally {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Chat")
.HasAnnotation("ProductVersion", "10.0.9")
.HasAnnotation("ProductVersion", "10.0.10")
.HasAnnotation("Proxies:ChangeTracking", false)
.HasAnnotation("Proxies:CheckEquality", false)
.HasAnnotation("Proxies:LazyLoading", true)
Expand Down
Loading