Automatically infer migration type (#2664)

This commit is contained in:
Max Vorobev
2023-08-01 21:16:43 +03:00
committed by GitHub
parent 487b89a4b6
commit d0fbe7feff
4 changed files with 24 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ pub enum MigrateError {
#[error("migration {0} is newer than the latest applied migration {1}")]
VersionTooNew(i64, i64),
#[deprecated = "migration types are now inferred"]
#[error("cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations")]
InvalidMixReversibleAndSimple,

View File

@@ -1,3 +1,5 @@
use super::Migrator;
/// Migration Type represents the type of migration
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum MigrationType {
@@ -71,4 +73,17 @@ impl MigrationType {
MigrationType::ReversibleDown => "-- Add down migration script here\n",
}
}
pub fn infer(migrator: &Migrator, reversible: bool) -> MigrationType {
match migrator.iter().next() {
Some(first_migration) => first_migration.migration_type,
None => {
if reversible {
MigrationType::ReversibleUp
} else {
MigrationType::Simple
}
}
}
}
}