Lesson 4 of 4 SQL Database Projects

Detecting and Resolving Schema Drift

This lesson explains how schema drift happens, the tools that detect it and how to respond to it.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the causes and effects of schema drift.
  • Distinguish between schema compare, DeployReport and DriftReport.
  • Describe how to keep or remove a drifted change.

How drift happens

Drift appears when someone changes a database directly: an emergency index added in production, a hotfix to a procedure, a column added by a script, or permissions granted by hand. The next deployment from the project either removes the change (if it differs from the project) or fails, and environments slowly diverge, so tests no longer represent production.

Detection methods

Several methods detect drift, compared in the following table.

Schema compare
ComparesProject or dacpac with a database (either direction)
OutputInteractive list of differences, can update either side
RequiresVS Code, Visual Studio or SqlPackage
Choose when Investigating and pulling changes into the project
DeployReport
ComparesDacpac with a target database
OutputXML list of changes a publish would make
RequiresNothing extra
Choose when Pipeline gate before every deployment
DriftReport
ComparesA registered database with its registration snapshot
OutputXML list of changes since registration
RequiresPublishing with RegisterDataTierApplication=True
Choose when Knowing what changed since the last registered deployment
sqlpackage /Action:DeployReport /SourceFile:bin/Release/Inventory.dacpac \
  /TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
  /AccessToken:$TOKEN /OutputPath:deploy-report.xml

sqlpackage /Action:DriftReport \
  /TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
  /AccessToken:$TOKEN /OutputPath:drift-report.xml
Sort into groups

Which drift tool?

Exam skills 2.3.7

Choose the drift detection method for each need.

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

Responding to drift

When drift is found, the team decides whether each change should stay. Changes that should stay, such as a useful emergency index, are added to the project (for example with schema compare from the database to the project) and go through a pull request. Changes that should not stay are removed by the next deployment. A pipeline can run a DeployReport before deployment and fail, or require approval, when it contains changes outside the reviewed release, and restricting direct DDL permissions in production prevents most drift in the first place.

Scenario questions

Responding to drift

Exam skills 2.3.7

Choose how to respond to each kind of drift.

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

Why some drift is invisible in a DeployReport

A DeployReport lists only what a publish with the same options would change, so its publish properties decide which kinds of drift appear. With the default DropObjectsNotInSource=False, an object that exists only in the target, such as a procedure created by hand, is not listed, because the publish would leave it alone. Indexes and constraints behave differently, because their own properties default to dropping objects that are not in the project, which is why the emergency index in the worked example appears as a DROP INDEX. Permissions and role memberships granted by hand are also left alone by default. The following table summarizes these defaults.

Publish propertyDefaultDrift that the report shows by default
DropObjectsNotInSourceFalseNone for objects that exist only in the target
DropIndexesNotInSourceTrueIndexes that exist only in the target, as DROP INDEX
DropConstraintsNotInSourceTrueConstraints that exist only in the target
DropPermissionsNotInSourceFalseNone for permissions granted by hand

Changes to objects that exist in both the project and the database, such as an altered procedure body or a widened column, always appear, because the publish would change them back. To see every difference, including extra objects and permissions, a team can run a schema compare or generate a report-only DeployReport with DropObjectsNotInSource=True, which is never used to publish. DriftReport is independent of these properties, because it compares the database with its own registration snapshot rather than with the project.

Sort into groups

Does the default DeployReport show it?

Exam skills 2.3.7

Decide whether a DeployReport with default properties lists each drifted change.

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

An emergency index

During an incident, a DBA added a nonclustered index directly in production. The project does not contain it, and the next release's DeployReport shows a DROP INDEX for it. The index should be kept. What should the team do?

Add the index definition to the SQL project, for example by running a schema compare from the production database to the project and applying only that difference, commit it through a pull request, and rebuild. The DeployReport then no longer drops it. Setting DropObjectsNotInSource to False does not help because indexes are governed by DropIndexesNotInSource, and ignoring the report would let the release drop the index.

Check yourself

What must happen before SqlPackage DriftReport can be used on a database?

Show answer

The database must have been published with RegisterDataTierApplication=True so a registration snapshot exists.

Exam Tips

Remember for the exam

  • Preview what a deployment would change: DeployReport; drift since registration: DriftReport.
  • DriftReport needs a database published with RegisterDataTierApplication=True.
  • Keep a wanted out-of-band change by adding it to the project.
  • Schema compare can update the project from a database.