Skip to main content

Detect Migrations Drift in CI

How to detect schema drift in CI and ensure the migration directory is up to date with the desired schema state - ORM, HCL, SQL, or any other schema definition?

Answer

One option is to run atlas migrate diff --env=<ENV-NAME>, and then check that no files were generated by this command: status=$(git status --porcelain).

If an engineer forgot to run atlas migrate diff the schema state will not match the state of your migration directory and new migration file will be created. If the two states match, no new migration file will be generated.

Another option is to use schema diff (with a custom --format) to detect changes and fail if there are any:

atlas schema diff \
--from "file://migrations" \
--to env://orm \
--env local \
--format '{{ if .Changes }}fail{{ end }}' | grep '.' && exit 1

(Alternatively, convert it to: if [ -n "$output" ]; then .. fi).

See more details, see schema diff documentation.