use crate::error::{Result, TidewayError};
use sea_orm_migration::MigratorTrait;
pub async fn run_migrations<M: MigratorTrait>(db: &sea_orm::DatabaseConnection) -> Result<()> {
M::up(db, None)
.await
.map_err(|e| TidewayError::internal(format!("Migration failed: {}", e)))?;
tracing::info!("Database migrations completed successfully");
Ok(())
}
pub async fn migration_status<M: MigratorTrait>(db: &sea_orm::DatabaseConnection) -> Result<()> {
M::status(db)
.await
.map_err(|e| TidewayError::internal(format!("Failed to check migration status: {}", e)))
}
pub async fn rollback_migration<M: MigratorTrait>(
db: &sea_orm::DatabaseConnection,
steps: Option<u32>,
) -> Result<()> {
M::down(db, steps)
.await
.map_err(|e| TidewayError::internal(format!("Rollback failed: {}", e)))?;
tracing::info!("Rolled back {} migration(s)", steps.unwrap_or(1));
Ok(())
}
pub async fn reset_database<M: MigratorTrait>(db: &sea_orm::DatabaseConnection) -> Result<()> {
M::fresh(db)
.await
.map_err(|e| TidewayError::internal(format!("Database reset failed: {}", e)))?;
tracing::info!("Database reset completed");
Ok(())
}