use tracing::{error, info, instrument};
use crate::error::WalletError;
use crate::WalletResult;
#[cfg(feature = "sqlite")]
pub fn sqlite_migrator() -> sqlx::migrate::Migrator {
sqlx::migrate!("migrations/sqlite")
}
#[cfg(feature = "mysql")]
pub fn mysql_migrator() -> sqlx::migrate::Migrator {
sqlx::migrate!("migrations/mysql")
}
#[cfg(feature = "postgres")]
pub fn postgres_migrator() -> sqlx::migrate::Migrator {
sqlx::migrate!("migrations/postgres")
}
#[cfg(feature = "sqlite")]
#[instrument(skip(pool))]
pub async fn run_sqlite_migrations(pool: &sqlx::SqlitePool) -> WalletResult<()> {
info!("Running SQLite migrations");
let migrator = sqlite_migrator();
migrator.run(pool).await.map_err(|e| {
error!(error = %e, "SQLite migration failed");
WalletError::Internal(format!("SQLite migration failed: {e}"))
})?;
info!("SQLite migrations completed successfully");
Ok(())
}
#[cfg(feature = "mysql")]
#[instrument(skip(pool))]
pub async fn run_mysql_migrations(pool: &sqlx::MySqlPool) -> WalletResult<()> {
info!("Running MySQL migrations");
let migrator = mysql_migrator();
migrator.run(pool).await.map_err(|e| {
error!(error = %e, "MySQL migration failed");
WalletError::Internal(format!("MySQL migration failed: {e}"))
})?;
info!("MySQL migrations completed successfully");
Ok(())
}
#[cfg(feature = "postgres")]
#[instrument(skip(pool))]
pub async fn run_postgres_migrations(pool: &sqlx::PgPool) -> WalletResult<()> {
info!("Running PostgreSQL migrations");
let migrator = postgres_migrator();
migrator.run(pool).await.map_err(|e| {
error!(error = %e, "PostgreSQL migration failed");
WalletError::Internal(format!("PostgreSQL migration failed: {e}"))
})?;
info!("PostgreSQL migrations completed successfully");
Ok(())
}