Skip to content

Ensure Deterministic Migration Order #4136

Open
aoengin wants to merge 4 commits intolaunchbadge:mainfrom
aoengin:oguz/deterministic-migration-order
Open

Ensure Deterministic Migration Order #4136
aoengin wants to merge 4 commits intolaunchbadge:mainfrom
aoengin:oguz/deterministic-migration-order

Conversation

@aoengin
Copy link
Copy Markdown

@aoengin aoengin commented Jan 4, 2026

What / why

When embedding migrations into the binary via migrate!, the iteration order of migration files can vary across filesystems/OSes. That makes the resulting embedded migration list (and thus the final binary) non-deterministic, which breaks reproducible builds.

This PR makes migration ordering deterministic by explicitly ordering reversible migration pairs so that up is always before down for the same migration version.

Changes

  • Add MigrationType::direction_order() to define a stable tie-break rule (up/simple first, down second).

  • Update Migrator::with_migrations() to sort migrations by:

    1. version ascending
    2. then direction_order() for version ties.
  • Apply the same deterministic sort when resolving migrations from a source (blocking resolver).

Impact

  • Not a breaking change: applied migrations and version ordering remain the same; only the deterministic ordering of reversible pairs is enforced.
  • Fixes non-determinism in builds that embed migrations.

@aoengin aoengin marked this pull request as draft January 4, 2026 11:05
@aoengin aoengin marked this pull request as ready for review January 4, 2026 12:00
@aoengin
Copy link
Copy Markdown
Author

aoengin commented Jan 30, 2026

@abonander

1 similar comment
@aoengin
Copy link
Copy Markdown
Author

aoengin commented Apr 30, 2026

@abonander

Comment thread sqlx-core/src/migrate/migrator.rs Outdated
Comment on lines +91 to +97
migrations.sort_by(|a, b| {
a.version.cmp(&b.version).then_with(|| {
a.migration_type
.direction_order()
.cmp(&b.migration_type.direction_order())
})
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just implement PartialOrd and Ord for Migration and then this can just be migrations.sort().

Comment thread sqlx-core/src/migrate/migration_type.rs Outdated
}

/// Ordering helper to sort ups before downs when versions tie.
pub fn direction_order(&self) -> u8 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add #[derive(PartialOrd, Ord)] for MigrationType. It'd be a degenerate case, but Simple and ReversibleUp comparing equal could still result in nondeterminism.

@aoengin
Copy link
Copy Markdown
Author

aoengin commented May 1, 2026

Migration equality and ordering are based on identity, not full content. A (version, migration_type) pair represents one logical migration slot.
Please let me know if there is any usage that contradicts this assumption.

@abonander

@aoengin aoengin requested a review from abonander May 1, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants