diff --git a/sqlx-core/src/migrate/migration.rs b/sqlx-core/src/migrate/migration.rs index 79721d244d..a434ac9684 100644 --- a/sqlx-core/src/migrate/migration.rs +++ b/sqlx-core/src/migrate/migration.rs @@ -1,5 +1,6 @@ use sha2::{Digest, Sha384}; use std::borrow::Cow; +use std::cmp::Ordering; use crate::sql_str::SqlStr; @@ -15,6 +16,28 @@ pub struct Migration { pub no_tx: bool, } +impl PartialEq for Migration { + fn eq(&self, other: &Self) -> bool { + self.version == other.version && self.migration_type == other.migration_type + } +} + +impl Eq for Migration {} + +impl PartialOrd for Migration { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Migration { + fn cmp(&self, other: &Self) -> Ordering { + self.version + .cmp(&other.version) + .then_with(|| self.migration_type.cmp(&other.migration_type)) + } +} + impl Migration { pub fn new( version: i64, diff --git a/sqlx-core/src/migrate/migration_type.rs b/sqlx-core/src/migrate/migration_type.rs index 350ddb3f27..107cdd246c 100644 --- a/sqlx-core/src/migrate/migration_type.rs +++ b/sqlx-core/src/migrate/migration_type.rs @@ -1,7 +1,7 @@ use super::Migrator; /// Migration Type represents the type of migration -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] pub enum MigrationType { /// Simple migration are single file migrations with no up / down queries Simple, diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 53295c92d0..7aa284a60c 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -87,8 +87,7 @@ impl Migrator { /// let m = Migrator::with_migrations(migrations); /// ``` pub fn with_migrations(mut migrations: Vec) -> Self { - // Ensure that we are sorted by version in ascending order. - migrations.sort_by_key(|m| m.version); + migrations.sort(); Self { migrations: Cow::Owned(migrations), ..Self::DEFAULT diff --git a/sqlx-core/src/migrate/source.rs b/sqlx-core/src/migrate/source.rs index 4648e53f1e..10fc7c7b8e 100644 --- a/sqlx-core/src/migrate/source.rs +++ b/sqlx-core/src/migrate/source.rs @@ -248,8 +248,7 @@ pub fn resolve_blocking_with_config( )); } - // Ensure that we are sorted by version in ascending order. - migrations.sort_by_key(|(m, _)| m.version); + migrations.sort(); Ok(migrations) }