use super::schema::SchemaManager;
use super::BoxFuture;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MigrationTransaction {
Enabled,
Disabled,
}
pub trait MigrationTrait: Send + Sync {
fn revision(&self) -> &'static str;
fn name(&self) -> &'static str;
fn up<'a>(&'a self, schema: &'a mut SchemaManager<'_>) -> BoxFuture<'a, crate::Result<()>>;
fn down<'a>(&'a self, schema: &'a mut SchemaManager<'_>) -> BoxFuture<'a, crate::Result<()>>;
fn transaction(&self) -> MigrationTransaction {
MigrationTransaction::Enabled
}
}
pub fn boxed<M: MigrationTrait + 'static>(migration: M) -> Box<dyn MigrationTrait> {
Box::new(migration)
}
pub struct MigrationSet {
migrations: Vec<Box<dyn MigrationTrait>>,
}
impl MigrationSet {
pub fn new(migrations: Vec<Box<dyn MigrationTrait>>) -> Self {
Self { migrations }
}
pub fn len(&self) -> usize {
self.migrations.len()
}
pub fn is_empty(&self) -> bool {
self.migrations.is_empty()
}
pub(crate) fn sorted(&self) -> Vec<&dyn MigrationTrait> {
let mut sorted: Vec<&dyn MigrationTrait> =
self.migrations.iter().map(Box::as_ref).collect();
sorted.sort_by(|a, b| a.revision().cmp(b.revision()));
sorted
}
pub(crate) fn find(&self, revision: &str) -> Option<&dyn MigrationTrait> {
self.migrations
.iter()
.map(Box::as_ref)
.find(|migration| migration.revision() == revision)
}
}