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.
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.xmlWhich drift tool?
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.
Responding to drift
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 property | Default | Drift that the report shows by default |
|---|---|---|
| DropObjectsNotInSource | False | None for objects that exist only in the target |
| DropIndexesNotInSource | True | Indexes that exist only in the target, as DROP INDEX |
| DropConstraintsNotInSource | True | Constraints that exist only in the target |
| DropPermissionsNotInSource | False | None 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.
Does the default DeployReport show it?
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.
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.