Skip to main content

Handling drift between my schema and the atlas_schema_revisions table

Question

My revisions table lists the following versions:

mysql> SELECT * FROM  "atlas_schema_revisions";
+-------------------------+---------+-----+
| version | applied | ... |
+-------------------------+---------+-----+
| .atlas_cloud_identifier | 0 | |
| 20251007053111 | 1 | |
| 20251007051222 | 1 | |
| 20250618084333 | 1 | |
+-------------------------+---------+-----+

However, when I inspect the target database, the schema changes from 20250618084333 were never applied. How can I delete the latest row(s) so the revision history reflects the actual state?

How to manage schema migration for a single table in a large schema

Given a large database schema, how can I manage schema migration for a single table without affecting the rest of the schema?

Answer

Atlas supports the --include flag (or the env.include attribute) to scope operations to specific resources. If you need to manage the lifecycle of a single table (or a set of tables), use this flag to avoid changes to the rest of the schema.

For example, to manage the products table in a MySQL database:

atlas schema apply \
--url "mysql://root:pass@:3308/db" \
--to "file://schema.sql" \
--dev-url "docker://mysql/8/dev" \
--include "products"

If the connection URL is not bound to a schema, specify the schema name in the pattern:

atlas schema apply \
--url "mysql://root:pass@:3308/" \
--to "file://schema.sql" \
--dev-url "docker://mysql/8" \
--include "db.products"

This command will only apply changes related to the products table, leaving other tables untouched.

Wildcard Support

If the table exists in multiple schemas (tenants), you can use wildcards to include it across all schemas. For example:

atlas schema apply \
--url "mysql://root:pass@:3308/" \
--to "file://schema.sql" \
--dev-url "docker://mysql/8" \
--include "*.products"

Read more:

Encoding for Atlas' schema files

Question

What encoding is used by Atlas for schema files? How do you avoid encoding issues with PowerShell on Windows?

Answer

Atlas uses UTF-8 to encode schema files and migration files generated by Atlas. UTF-8 is the default encoding on nearly all system terminals, but on Windows, PowerShell uses UTF-16 by default. This can cause an encoding issue when Atlas loads the schema files generated by the inspect command.

How do I resolve out-of-order migration conflicts after a hotfix?

When working with multiple branches and applying hotfixes directly to production, out-of-order migration conflicts may occur if migration files are created with timestamps that don't reflect the actual merge order.

The Problem

Consider this scenario:

Initial state:

  • Master branch:
    • 001_initial.sql
  • Dev branch (PR pending):
    • 001_initial.sql
    • 002_add_posts.sql
    • 004_add_index.sql

After hotfix applied directly to production:

  • Master branch:
    • 001_initial.sql
    • 003_hotfix_add_email.sql ← hotfix added
  • Dev branch (unchanged):
    • 001_initial.sql
    • 002_add_posts.sql
    • 004_add_index.sql

out-of-order migrations

After merging master into dev - the problem:

  • 001_initial.sql - Applied to production
  • 002_add_posts.sql - Dev-only, not applied to production
  • 003_hotfix_add_email.sql - Applied to production
  • 004_add_index.sql - Dev-only, not applied to production

This creates a non-linear migration history where migration files 002 and 004 were created before and after the hotfix timestamp but haven't been applied to production.

Checksum Mismatch between Different Environments

Question

When using Atlas on my macOS, I can run atlas migrate apply without any issues, and it applies the migrations successfully. But when running in CI on linux, I get the following error: Error: checksum mismatch

Answer

This issue is likely due to differences in line endings between operating systems. macOS uses LF (Line Feed) for line endings, while Windows uses CRLF (Carriage Return + Line Feed). When you run Atlas commands in a Docker container, it may be using a different line ending format than what your migrations were created with.

What Are Changesets in Atlas?

In version control systems like Git, a changeset represents an atomic unit of change. It groups related file modifications under a single hash, allowing them to be reviewed, applied, or reverted together.

In database migrations, a changeset is a unit of schema or data changes made up of SQL statements stored in a migration file, typically executed together in a single transaction.

Can I store migration files in S3? (CLI and Terraform examples)

With Atlas, we advocate for treating migration directories as deployment artifacts resulting from a structured build process. The preferred approach is to push migration directories to the Atlas Schema Registry. In addition to its role as a migration directory storage, the Schema Registry provides a tight integration with the Atlas CLI and the Atlas Cloud UI, allowing you to deploy migrations, visualize schemas over time, review deployment logs and errors, and more.

However, some users prefer to store their migration directories in S3, typically due to internal policies or requirements.