Shadow Database or Schema - Technical Details
Published 18 July 2023
Why is a shadow database/schema required?
When is the shadow reprovisioned or cleaned?
The shadow database/schema gets automatically updated from the migration scripts in your project when you visit/refresh the Generate migrations tab. This update involves potentially reprovisioning the database (i.e. resetting the shadow database to an empty or baseline state) prior to updating it with the migration scripts depending upon the current state of the shadow.
If no provisioners are used, then the reprovisioning is done by cleaning the database (i.e. dropping all objects). When provisioniners are used, the reprovisioning is done using the associated technology.
Reprovisioning will then require applying all the migration scripts from the project, which is sometimes unnecessary if some of those migration scripts have already been applied. Because of this, the shadow is typically only reprovisioned in the following situations:
- If the migration scripts have been edited in a way that changes their checksum from those recorded in the schema history table.
- If the version of the shadow database is greater than the migration scripts.
- If names or descriptions of the migration scripts on disk do not match what's recorded in the history table.
- If a prior migration attempt failed when applying to the shadow.
- If your project only contains repeatable migration scripts.
If the shadow database is not reprovisioned, then Pending migrations are simply applied on top of the existing state of the shadow.
Creating multiple shadow databases at once
We sometimes get asked how to create a shadow database for the 300 databases I currently have on my dev server. Here's a sample script for SQL Server that may give you ideas to help with this.
SELECT 'CREATE DATABASE ' + 'z' + name + '_Shadow' FROM master.sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb', 'Resource', 'distribution' , 'reportserver', 'reportservertempdb')
If there are other databases that are not being actively developed and you don't want a shadow for, then add these names to the NOT IN list.