Implementing Deployments that Include Database Tasks
This lesson compares database deployment approaches, and explains zero-downtime schema changes.
Learning outcomes
After completing this lesson, you will be able to:
- Compare state-based and migration-based approaches.
- Apply the expand and contract pattern.
- Identify controls that protect data.
- Generate idempotent migration scripts.
Why databases are different
A code deployment can be undone by redeploying the old version. A database change often cannot, since data may already have been written in a new shape, and a dropped column cannot be recovered without a backup. Database changes need to be versioned, tested and automated like code, but with more care about order and compatibility.
State-based and migration-based approaches
State-based deployment
With a state-based approach, the repository declares what the schema should look like. For SQL Server and Azure SQL, a SQL database project builds a DACPAC, and SqlPackage compares it with the target and generates the changes. This is simple to use, but the generated script may not preserve data in complex changes, such as a column rename, which needs a refactor log.
- task: SqlAzureDacpacDeployment@1
inputs:
azureSubscription: sc-azure-prod
AuthenticationType: servicePrincipal
ServerName: sql-contoso.database.windows.net
DatabaseName: orders
deployType: DacpacTask
DeploymentAction: Publish
DacpacFile: $(Pipeline.Workspace)/db/Orders.dacpacMigration-based deployment
With a migration-based approach, the repository holds an ordered series of change scripts, and a tool records which have been applied. Entity Framework Core migrations, Flyway and Liquibase are popular examples. Each change is explicit, so a team controls exactly what runs, which suits data transformations.
# Entity Framework Core: generate an idempotent script in the build
dotnet ef migrations script --idempotent --output migrate.sql
# Or create a self-contained executable for the release
dotnet ef migrations bundle --output efbundle
./efbundle --connection "$CONNECTION_STRING"An --idempotent script checks what is already applied, so it can run safely on a database at any version. The following comparison recaps the choice.
State-based or migration-based?
Zero-downtime changes: expand and contract
During a rolling release, old and new application versions run together, and they share one database, so the schema must work with both. The expand and contract pattern, also called parallel change, achieves this by splitting a breaking change into safe steps. The following sequence renames a column from Fullname to DisplayName.
-
Expand
Add the new DisplayName column. Old code ignores it.
-
Write to both
Deploy application code that writes to both columns and reads the old one.
-
Migrate data
Copy existing values to the new column in batches, avoiding long locks.
-
Read the new column
Deploy code that reads DisplayName and still writes to both.
-
Stop writing the old column
Deploy code that uses only the new column.
-
Contract
In a later release, drop the old column.
Each step is safe by itself, and each can be rolled back. The price is that one change takes several releases. Destructive changes, such as dropping a column, belong only in the last step, after old code has gone.
The following diagram shows the steps of expand and contract.

Renaming a column with no downtime
Protecting data in a pipeline
- Run database stages first, with the application depending on them, unless a change needs code to go first.
- Back up, or rely on point-in-time restore, before a risky change. Azure SQL Database provides automated backups with point-in-time restore.
- Review the generated script. A pipeline can produce the script in one stage, and require an approval before applying it.
- Use a managed identity or federated identity for database access, instead of storing a SQL password in a variable.
- Test migrations against a recent copy of production-sized data, because a change that runs in seconds on test data may lock a large table for hours.
- Separate duties: the identity that changes the schema does not need to be the one that the application uses.
Database releases that go wrong
Renaming a column
An application runs on 20 instances, updated by a rolling deployment. A team needs to rename a column used on every request, with no downtime. What approach should you recommend?
Use expand and contract. Add the new column, deploy code that writes to both, backfill the data, switch reads to the new column, and remove the old column in a later release. A direct rename would break the old instances while the rolling update is running. A maintenance window conflicts with the no-downtime requirement, and a DACPAC deployment on its own does nothing to keep old and new versions compatible.
In expand and contract, when is the old column removed?
Show answer
In a later release, after no running code uses it. This is the only destructive step.
Exam Tips
Remember for the exam
- State-based uses a desired schema, such as a DACPAC.
- Migration-based uses ordered scripts, such as EF Core migrations.
- Expand and contract keeps old and new versions working together.
- Drop old structures only in a later release.
- An idempotent migration script can run on a database at any version.