Migration Analyzers
The database is often the most critical component in software architectures. Being a stateful component, it cannot be easily rebuilt, scaled-out or fixed by a restart. Outages that involve damage to data or simply unavailability of the database are notoriously hard to manage and recover from, often taking long hours of careful work by a team's most senior engineers.
As most outages happen directly as a result of a change to a system, Atlas provides users with means to verify the
safety of planned changes before they happen. The sqlcheck
package provides interfaces for analyzing the contents of SQL files to generate insights on the safety of many kinds of
changes to database schemas. With this package developers may define an Analyzer that can be used to diagnose the impact
of SQL statements on the target database.
Using these interfaces, Atlas provides different Analyzer implementations that are useful for determining the
safety of migration scripts.
Analyzers
Below are the Analyzer implementations currently supported by Atlas.
Non-Linear Changes
Non-additive changes, often referred to as non-linear changes, are changes to the migration directory that are not added in a sequential order. This is a bit like the linear history in version control systems, where migration files are commits and the migration directory is the repository. Let's explain with three examples why ensuring the linearity of the migration directory is important:
- When a developer introduces a new migration file without having the latest state of the directory, there is a risk of generating an incorrect file that might conflict with the actual schema and cause a failure during deployment.
- When a developer merges a feature branch to the main branch with a new migration file that is not positioned at the end of the directory, there is a risk that Atlas will skip this migration file, as there might be some database that already contains a higher version of the migration directory. This scenario can cause unexpected and surprising behavior during deployments.
- Having non-linear history can make it challenging to roll back (or revert changes) to a specific version of the migration directory, as the state of the database might be different than the state of the migration directory. Applying the migration directory in consistent order promises deterministic behavior.
Luckily, Atlas detects non-linear and non-additive changes made to a migration directory. To enable this behavior in your project, integrate Atlas into your GitHub Actions or GitLab CI pipelines, and Atlas will automatically detect and report non-linear changes during the CI run.
By default, non-linear changes are reported but not cause migration linting to fail. Users can change this by
configuring the non_linear changes detector in the atlas.hcl file:
lint {
non_linear {
error = true
}
}
Relax Non-Linear Errors on Edits
Users who configured the non_linear change detector to fail migration linting (error = true) can relax or ignore
these errors when editing existing migration files by setting the on_edit option to WARN or IGNORE.
WARN: file edits that would trigger a non-linear error are downgraded to warnings.IGNORE: file edits are ignored and do not produce any diagnostic.
lint {
non_linear {
error = true
on_edit = WARN // IGNORE | ERROR
}
}
This is useful when developers need to update the latest migration files right after they land on the main branch and want to keep CI pipelines unblocked while non-linear checks still apply to older files.
Destructive Changes
Destructive changes are changes to a database schema that result in loss of data. For instance, consider a statement such as:
ALTER TABLE `users` DROP COLUMN `email_address`;
This statement is considered destructive because whatever data is stored in the email_address column
will be deleted from disk, with no way to recover it. There are definitely situations where this type
of change is desired, but they are relatively rare. Using the destructive (GoDoc)
Analyzer, teams can detect this type of change and design workflows that prevent it from happening accidentally.
Running migration linting locally on in CI fails with exit code 1 in case destructive changes are detected. However,
users can disable this by configuring the destructive analyzer in the atlas.hcl
file:
lint {
destructive {
error = false
}
}
Enforce Destructive Change Checks Atlas Pro
In some teams, destructive changes are considered high-risk and should never be skipped, regardless of whether a developer
adds an -- atlas:nolint directive. In these cases, the force option can be set on the destructive analyzer. This guarantees
the check always runs and reports diagnostics, even if explicitly excluded.
lint {
destructive {
force = true
}
}