feat(saas): add down migrations for all incremental schema changes (AUD3-DB-01)
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled

- 16 down SQL files in migrations/down/ for each incremental migration
- db::run_down_migrations() executes rollback files in reverse order
- migrate_down CLI task: task=migrate_down timestamp=20260402
- Initial schema and seed data excluded (would be destructive)
This commit is contained in:
iven
2026-04-05 01:35:33 +08:00
parent 3b0ab1a7b7
commit 745c2fd754
18 changed files with 123 additions and 2 deletions

View File

@@ -3,7 +3,7 @@
//! 提供可手动执行的运维命令:
//! - seed_admin — 创建管理员账号
//! - cleanup_devices — 清理不活跃设备
//! - migrate_schema — 手动触发 schema 迁移
//! - migrate_down — 回滚指定迁移timestamp=20260402 回滚所有 >= 该前缀的迁移
use std::collections::HashMap;
use sqlx::PgPool;
@@ -27,6 +27,7 @@ pub fn builtin_tasks() -> Vec<Box<dyn Task>> {
vec![
Box::new(SeedAdminTask),
Box::new(CleanupDevicesTask),
Box::new(MigrateDownTask),
]
}
@@ -86,3 +87,18 @@ impl Task for CleanupDevicesTask {
Ok(())
}
}
/// 回滚数据库迁移
struct MigrateDownTask;
#[async_trait::async_trait]
impl Task for MigrateDownTask {
fn name(&self) -> &str { "migrate_down" }
fn description(&self) -> &str { "回滚指定时间戳之后的迁移timestamp=20260402 回滚 >= 该前缀的所有增量迁移)" }
async fn run(&self, db: &PgPool, args: &HashMap<String, String>) -> SaasResult<()> {
let timestamp = args.get("timestamp")
.ok_or_else(|| crate::error::SaasError::InvalidInput("Missing 'timestamp' argument (e.g. 20260402)".into()))?;
crate::db::run_down_migrations(db, timestamp).await
}
}