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.sh
1#!/bin/bash2# Apply EF Core database migrations to Azure SQL using Managed Identity authentication.3#4# USAGE: Copy this file to scripts/apply-migrations.sh in your project root and add5# a postprovision hook in azure.yaml:6#7# hooks:8# postprovision:9# posix:10# shell: sh11# run: ./scripts/apply-migrations.sh12# windows:13# shell: pwsh14# run: ./scripts/apply-migrations.ps115#16# ENVIRONMENT VARIABLES (sourced from azd env):17# SQL_SERVER - SQL server name (without .database.windows.net)18# SQL_DATABASE - Database name19#20# PREREQUISITES:21# - dotnet-ef tool (installed automatically if missing)22# - A valid managed identity or Entra-authenticated session23#24# CONFIGURATION:25# Set APP_PROJECT_PATH below to the path of your application project (.csproj directory)2627set -e2829APP_PROJECT_PATH="src/api" # Adjust to your project directory3031# Safely load azd environment variables without eval32while IFS= read -r line; do33[ -n "$line" ] || continue34key=${line%%=*}35value=${line#*=}36case "$value" in37\"*\") value=${value#\"}; value=${value%\"} ;;38\'*\') value=${value#\'}; value=${value%\'} ;;39esac40export "$key=$value"41done < <(azd env get-values)4243# Install dotnet-ef only when it is not already installed (no-op when already present)44if ! dotnet tool list --global 2>/dev/null | grep -q '^\s*dotnet-ef\s'; then45dotnet tool install --global dotnet-ef46fi47export PATH="$PATH:$HOME/.dotnet/tools"4849CONNECTION_STRING="Server=tcp:${SQL_SERVER}.database.windows.net,1433;Database=${SQL_DATABASE};Authentication=Active Directory Default;Encrypt=True;"5051echo "Applying EF Core migrations..."52cd "$APP_PROJECT_PATH"53dotnet ef database update --connection "$CONNECTION_STRING"54echo "Migrations applied successfully."55