Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Execute Azure deployments using azd, Terraform, or Bicep with built-in error recovery.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/recipes/azd/scripts/apply-migrations.ps1
1# Apply EF Core database migrations to Azure SQL using Managed Identity authentication.2#3# USAGE: Copy this file to scripts/apply-migrations.ps1 in your project root and add4# a postprovision hook in azure.yaml:5#6# hooks:7# postprovision:8# posix:9# shell: sh10# run: ./scripts/apply-migrations.sh11# windows:12# shell: pwsh13# run: ./scripts/apply-migrations.ps114#15# ENVIRONMENT VARIABLES (sourced from azd env):16# SQL_SERVER - SQL server name (without .database.windows.net)17# SQL_DATABASE - Database name18#19# PREREQUISITES:20# - dotnet-ef tool (installed automatically if missing)21# - A valid managed identity or Entra-authenticated session22#23# CONFIGURATION:24# Set $AppProjectPath below to the path of your application project (.csproj directory)2526$ErrorActionPreference = 'Stop'2728$AppProjectPath = "src/api" # Adjust to your project directory2930# Load azd environment variables31azd env get-values | ForEach-Object {32$name, $value = $_.Split('=', 2)33Set-Item "env:$name" $value.Trim('"')34}3536# Install dotnet-ef if not already installed (no-op when already present)37$globalTools = dotnet tool list --global 2>$null38if ($LASTEXITCODE -ne 0) {39throw "Failed to list globally installed .NET tools."40}41if (-not ($globalTools | Select-String -Pattern '^\s*dotnet-ef\s')) {42dotnet tool install --global dotnet-ef43if ($LASTEXITCODE -ne 0) {44throw "Failed to install dotnet-ef."45}46}47$env:PATH += ";$env:USERPROFILE\.dotnet\tools"4849$ConnectionString = "Server=tcp:$($env:SQL_SERVER).database.windows.net,1433;Database=$($env:SQL_DATABASE);Authentication=Active Directory Default;Encrypt=True;"5051Write-Host "Applying EF Core migrations..."52Set-Location $AppProjectPath53dotnet ef database update --connection $ConnectionString54Write-Host "Migrations applied successfully."55