Skip to main content

Drift Detection

Schema Drift

Schema migrations on production databases should ideally be automated within CI/CD pipelines, keeping developers from needing root access. However, this setup isn't always the case, leading to Schema Drift, where database schemas drift from their intended state, causing inconsistencies and issues.

Schema drift is dangerous because it can lead to unexpected behavior in applications, data corruption, and even system outages. When the actual database schema deviates from the expected schema, queries may fail, data integrity constraints may be violated, and application logic may break. Additionally, schema migrations are planned based on the assumption the target database is in a specific state and may fail and cause unknown side effects. This can result in hard-to-debug issues and degraded system performance, ultimately affecting the reliability and stability of your application.

You can use Atlas Schema Monitoring to detect schema drift and ensure your database schema are always in sync with their intended state. Atlas schema monitoring automatically detects schema drift by comparing the actual database schema with the intended schema. If a drift is detected, Atlas will notify you and provide detailed information about the drift, including an ERD, HCL and SQL of the diff.

Prerequisites:

Creating desired database schema

First, we will need to deploy our migration directory to a target database. This will be our desired state.

To deploy, create the next atlas.hcl file:

env {
name = atlas.env
url = "docker://mysql/8/test"
migration {
dir = "atlas://<your-migration-directory>"
}
}

And run the following command:

atlas migrate apply --env local

Enabling Drift Detection

Atlas enables you to automatically monitor schema drifts by periodically checking if your deployed database schema align with the desired schema.

To enable periodic inspection, go the settings tab of the database you want to monitor and enable the Periodic Inspection toggle. This will instruct Atlas to periodically take snapshots of the database schema.

Screenshot Example

Now, you can enable drift detection by toggling the Drift Detection switch, you will need to choose which migration directory and database target to compare against as the desired state.

Screenshot Example

Once enabled, if there is a drift, Atlas will provide you with detailed information about the drift, including an ERD, HCL and SQL diff.

You can see the drift in the Changelog tab.

Screenshot Example

And you can also see the drift in the deployed database itself.

Screenshot Example

Notifications

You can instruct Atlas to notify you if there is a drift on the target database. Atlas supports various channels, such as Slack, Workplace or by a plain webhook.

Screenshot Example