use async_trait::async_trait;
use crate::connection::ConnectError;
use std::fmt::Debug;
#[cfg(any(feature = "mysql", feature = "postgres"))]
pub use {migrator::Migrator, schema::Schema};
#[cfg(any(feature = "mysql", feature = "postgres"))]
mod migrator;
#[cfg(any(feature = "mysql", feature = "postgres"))]
pub mod schema;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to connect to database.")]
Connection(#[from] ConnectError),
#[error("{0}")]
Database(String),
#[error("Could not locate the {0} migration.")]
NotFound(String),
#[error("Failed to receive column in schema.")]
SendColumn,
#[error("Failed to obtain connection")]
Lock,
#[error("Failed to deserialize migration data.")]
Decode(#[from] rbs::Error),
}
#[macro_export]
macro_rules! migrate {
($($migration:ty),*) => {
async move {
let mut migrator = $crate::migrations::Migrator::new().await?;
$(
migrator.register(stringify!($migration).to_string(), Box::new(<$migration>::default()));
)*
migrator.run().await
}
};
}
#[async_trait]
pub trait Migration: Sync + Send {
async fn up(&self) -> Result<(), Error>;
async fn down(&self) -> Result<(), Error>;
}