Lesson 3 of 4 SQL Database Projects

Deploying Changes with SqlPackage

This lesson explains the SqlPackage actions, publish properties and profiles, and safe deployment practices.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between the SqlPackage actions.
  • Explain the effect of BlockOnPossibleDataLoss and DropObjectsNotInSource.
  • Describe practices that make database deployments safe.

SqlPackage actions

SqlPackage is a cross-platform command-line tool (installable as a .NET tool) whose actions are summarized in the following table.

ActionPurpose
PublishUpdate a database to match a dacpac
ScriptGenerate the T-SQL that Publish would run, for review
DeployReportGenerate an XML report of the changes Publish would make
DriftReportReport changes made to a registered database since it was registered
ExtractCreate a dacpac from an existing database's schema
Export / ImportCreate or restore a bacpac containing schema and data
TOKEN=$(az account get-access-token --resource https://database.windows.net/ --query accessToken -o tsv)

sqlpackage /Action:Script /SourceFile:bin/Debug/Inventory.dacpac \
  /TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
  /AccessToken:$TOKEN /OutputPath:deploy.sql

sqlpackage /Action:Publish /SourceFile:bin/Debug/Inventory.dacpac \
  /TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
  /AccessToken:$TOKEN /Profile:Inventory.Production.publish.xml

The following diagram shows the SqlPackage actions around a dacpac.

SqlPackage actions in a deployment. A SQL project builds a dacpac, the desired schema. DeployReport and Script show what Publish would change, for review. Publish updates the target database to match the dacpac. Extract creates a dacpac from an existing database, DriftReport shows changes made to a registered database since registration, and Export and Import move schema and data as a bacpac.
Review the Script or DeployReport before Publish.
Match the pairs

SqlPackage actions

Exam skills 2.3.8

Match each need with the SqlPackage action.

Log in to try this activity Interactive activities are free with an account.

Publish properties and profiles

Publish behaviour is controlled by properties, set with /p:Name=Value or stored in a publish profile (.publish.xml) per environment. Important ones are listed in the following table.

PropertyDefaultEffect
BlockOnPossibleDataLossTrueStops the deployment if a change could lose data, such as dropping or narrowing a column with data
DropObjectsNotInSourceFalseDrops objects in the target that are not in the dacpac
GenerateSmartDefaultsFalseSupplies defaults when adding NOT NULL columns to tables with data
BackupDatabaseBeforeChangesFalseBacks up the database before deploying (SQL Server)
BlockWhenDriftDetectedTrueBlocks deployment to a registered database whose schema drifted
RegisterDataTierApplicationFalseRegisters the schema so DriftReport can detect later changes
Scenario questions

Publish properties

Exam skills 2.3.8

Choose the publish property for each requirement.

Log in to try this activity Interactive activities are free with an account.

Safe deployment practices

A pipeline builds the dacpac once and deploys the same artifact to each environment in turn, so production receives exactly what was tested. Before production, a Script or DeployReport is generated and reviewed for unexpected drops or table rebuilds. Changes that the declarative model cannot express safely, such as splitting a column and moving its data, are handled in pre- and post-deployment scripts, or in several releases (add new column, migrate data, remove old column). Renames are recorded in the project's refactor log so SqlPackage renames objects instead of dropping and re-creating them. Vector indexes are not deployed with a dacpac when the table has fewer than 100 rows with vectors, so they may need a post-deployment step.

Select all that apply

Safe deployments

Exam skills 2.3.8

Identify the safe deployment practices for SQL projects.

Log in to try this activity Interactive activities are free with an account.
Worked example

A deployment that would drop data

A developer removes an obsolete column from a table in the project. The production deployment fails with a message that the column contains data. The data has been archived and the column must be removed. What should the team do?

Review the DeployReport, confirm the archive, and deploy that release with /p:BlockOnPossibleDataLoss=False (in the production profile for that deployment only). The default True value is a safety net that stops accidental data loss, so it should be overridden deliberately, not removed permanently. DropObjectsNotInSource controls dropping whole objects that are not in the project and does not affect columns removed from a table definition.

Check yourself

What is the default value of BlockOnPossibleDataLoss?

Show answer

True, so deployments that could lose data stop unless it is deliberately overridden.

Exam Tips

Remember for the exam

  • Deploy a dacpac: SqlPackage Publish; preview: Script or DeployReport.
  • BlockOnPossibleDataLoss defaults to True; override deliberately for intended drops.
  • DropObjectsNotInSource defaults to False.
  • Build once, deploy the same dacpac to every environment.