Checking production environments for drift
Published 15 September 2025
It is a good idea to run drift analysis against your production environments to make sure that they are in the expected state and reduce the risk of deployment issues occurring.
It is particularly important to run drift analysis as part of an automated deployment pipeline, ensuring that the database is in the expected state immediately before deployment, though this check can be run at any time.
How Flyway drift detection works
Flyway uses database snapshots in order to perform drift analysis. There are alternative approaches to making this work, all of which rely on the Flyway check command, using the drift flag:
Option 1: Capture a snapshot of the database after every deployment and embed it for reuse (suitable for state-based or migrations-based deployments)
In this approach, every time a deployment is run, a snapshot is taken immediately after the deployment to capture the state of the database, and is embedded in the snapshot history table in that database.
This snapshot can then be compared directly against your database ahead of a new deployment (or on a scheduled or ad hoc basis).
The benefits of this solution are:
- it works for both state-based and migrations-based deployments
- it only captures drift since the last deployment
- it is easy to configure as no build environment or external storage is needed
- is is faster than using a build environment
- the snapshot being in the database means that there is less room for configuration error (e.g. it would be hard to accidentally compare against the wrong snapshot)
See this tutorial for exact steps.
Option 2: Capture a snapshot of the database after every deployment and persist it for reuse (suitable for state-based or migrations-based deployments)
This is the same as option 1, except that instead of embedding the snapshot in the target database, the snapshot is saved to a file on disk.
This approach requires more configuration effort as the snapshot needs to be saved somewhere future pipelines can access (e.g., an artifact repository, a network share) so that it can then be used whenever you perform subsequent drift checks.
This solution is only worth picking over option 1 if you don't want to store the snapshot in your database.
The main drawback with this approach is the overhead of persisting the snapshot between deployment runs, and the approach here will vary depending upon your infrastructure.
See this tutorial for exact steps.
Option 3: Use a build environment to capture migrations at different stages of deployment (suitable for migrations-based deployments only)
In this approach, Flyway performs the following steps (via a single check command invocation):
- Take a snapshot of the target environment
- Clean the build environment
- Query the target environment for the list of applied migrations
- Apply these migrations to the build environment
- Take a snapshot of the build environment
- Compare the two snapshots
- Report on drift
This approach is simple enough to configure if you have access to a build environment in your pipeline (which is required if you want to generate a deployment report for a migrations-based deployment).
There are some drawbacks in that this approach only works for migrations, it captures all drift relative to migrations rather than just drift since the last deployment, and it is likely to be a bit less performant than option 1 as there are more steps under the hood.
This solution is probably only worth picking over option 1 and 2 if you don't want to store the snapshot in your database or worry about external storage of the snapshot.
Configuring which objects are included in drift reporting using a filter
By default all objects which are different will be reported on, whether modified or existing only in the target.
Drift analysis uses the same database comparison configurations as are used for generating the schema model or for generating migrations.
It is possible to override the filtering logic for drift analysis by configuring a filter file parameter on the check command.
Resolving instances of drift
The first step to resolve drift is to make a decision about how to handle the drift. Is the drift valid and therefore needs to be reflected in version control and your other environments for a consistent development experience that is reflective of changes on other environments (usually Production)? Or, is the drift invalid and should be reverted to bring the environment back in line with what's in version control? A team may make quite different choices depending on the nature of the drift and the use case for the target environment. Below are some options for how to handle drift.
| Option | For state-based deployments | For migrations-based deployments |
|---|---|---|
Option 1: Incorporate the drift into your previous environments | Update your development database to match the drifted changes. Save this to the schema model. Commit the updated schema model to version control and rerun the deployment. Your environments are now consistent, which will help identify issues early. | Add a new versioned migration script that captures the drift and commit it to version control. Write the script idempotently or mark it as already applied on the target database (by using skipExecutingMigrations) before proceeding with the deployment. This script will bring all the other environments inline, but skip the environment that already has these changes. Your environments are now consistent, which will help identify issues early. |
You could use Flyway with custom filtering to generate a script that captures the changes for the drifted objects. (We are working on a way for Flyway to generate this script for you as part of the drift check. Get in touch if your interested in discussing this.) | ||
Option 2: Remove the drift from the database | State-based deployments make the target match the schema model, so by default it will revert all drift. So to remove drift, just proceed with the deployment as normal. | To bring the deployment target back in line with the previously deployed version in version control, undo the change that was made directly in the target environment. You can use Flyway to generate a script to remove the drift. We are working on a way for Flyway to generate this drift revert script for you as part of the drift check. Get in touch if your interested in discussing this. You can then proceed with flyway deployments. |
Option 3: Take a new snapshot to use with drift check | If the drift is ok, you can take a new snapshot of the target database and use this for your drift check. Be careful - Production is now different from your earlier environments, so this is not recommended. Future deployments will NOT notify you about this drift since it will be reflected in new snapshots going forward, so be sure to incorporate the drift as soon as possible using Option 1 to make sure your environments are consistent. | If the drift is ok this time, you can take a new snapshot of the target database and use this for your drift check. Be careful - Production is different from your earlier environments, so this is not recommended. If you are using the build environment for the drift check, this drift will continue to be a problem until it's fixed in the migration scripts. You can then proceed with flyway deployments. |
Option 4: Ignore the drift | It is NOT possible to ignore drift when deploying from the schema model. | Drift may ONLY cause a problem if the drifted objects are being modified in the pending migration scripts or if the pending changes are dependent on the drifted objects. If not, the drifted objects are not changed and will be ignored, so it is possible to just go ahead with the deployment. |
If the drifted objects exist only in the target database, it might be better practice to explicitly filter out the drifted objects (see above) and rerun the deployment. | ||