Skip to content

Detecting drift

Drift is when the live database no longer matches NSchema’s latest snapshot in the state store. This happens when people make manual schema changes outside of NSchema, and it can cause data loss if not handled carefully. The nschema drift command helps detect it.

drift compares the latest backend state snapshot against the live database and outputs the difference. The diff answers “what has been changed outside of NSchema?”:

  • an out-of-band addition appears as an add;
  • a manual drop appears as a remove.
Terminal window
nschema drift

For a scheduled monitoring job, add --detailed-exitcode so the command’s exit code becomes 0 for no drift, 2 for drift (errors stay 1):

Terminal window
nschema drift --detailed-exitcode

Once you’ve reviewed the drift, you can choose to either adopt the drift using refresh, then updating your .sql files to match, or you can revert the drift using apply to bring the database back in line with your declared desired state.