Skip to main content

doido_model/
migrate.rs

1//! Migration rollback helpers (Rails `db:rollback STEP=n` / `db:migrate:redo`)
2//! over sea-orm's `MigratorTrait`.
3
4use crate::sea_orm::DatabaseConnection;
5use crate::sea_orm_migration::MigratorTrait;
6use doido_core::Result;
7
8/// Roll back the last `steps` migrations (Rails `db:rollback STEP=steps`).
9pub async fn rollback<M: MigratorTrait>(conn: &DatabaseConnection, steps: u32) -> Result<()> {
10    M::down(conn, Some(steps))
11        .await
12        .map_err(|e| doido_core::anyhow::anyhow!("rollback failed: {e}"))
13}
14
15/// Roll back then re-apply the last `steps` migrations (Rails `db:migrate:redo`).
16pub async fn redo<M: MigratorTrait>(conn: &DatabaseConnection, steps: u32) -> Result<()> {
17    M::down(conn, Some(steps))
18        .await
19        .map_err(|e| doido_core::anyhow::anyhow!("redo down failed: {e}"))?;
20    M::up(conn, Some(steps))
21        .await
22        .map_err(|e| doido_core::anyhow::anyhow!("redo up failed: {e}"))
23}