fix unapplyMigration: simplify output to not crash when there's no new active migration
-use super::list_migrations::{convert_migration_to_list_migration_steps_output, ListMigrationStepsOutput};
use crate::commands::command::{CommandResult, MigrationCommand};
use crate::migration_engine::MigrationEngine;
-use migration_connector::*;
pub struct UnapplyMigrationCommand {
input: UnapplyMigrationInput,
@@ -20,20 +18,8 @@ impl MigrationCommand for UnapplyMigrationCommand {
let connector = engine.connector();
let result = match connector.migration_persistence().last() {
None => UnapplyMigrationOutput {
- rolled_back: ListMigrationStepsOutput {
- id: "foo".to_string(),
- datamodel_steps: Vec::new(),
- database_steps: serde_json::Value::Array(Vec::new()),
- status: MigrationStatus::Pending,
- datamodel: "".to_string(),
- },
- active: ListMigrationStepsOutput {
- id: "bar".to_string(),
- datamodel_steps: Vec::new(),
- database_steps: serde_json::Value::Array(Vec::new()),
- status: MigrationStatus::Pending,
- datamodel: "".to_string(),
- },
+ rolled_back: "not-applicable".to_string(),
+ active: None,
errors: vec!["There is no last migration that can be rolled back.".to_string()],
},
Some(migration_to_rollback) => {
@@ -43,14 +29,11 @@ impl MigrationCommand for UnapplyMigrationCommand {
.migration_applier()
.unapply(&migration_to_rollback, &database_migration);
- let new_active_migration = match connector.migration_persistence().last() {
- Some(m) => m,
- None => Migration::new("no-migration".to_string()),
- };
+ let new_active_migration = connector.migration_persistence().last().map(|m|m.name);
UnapplyMigrationOutput {
- rolled_back: convert_migration_to_list_migration_steps_output(&engine, migration_to_rollback),
- active: convert_migration_to_list_migration_steps_output(&engine, new_active_migration),
+ rolled_back: migration_to_rollback.name,
+ active: new_active_migration,
errors: Vec::new(),
}
}
@@ -68,7 +51,7 @@ pub struct UnapplyMigrationInput {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UnapplyMigrationOutput {
- pub rolled_back: ListMigrationStepsOutput,
- pub active: ListMigrationStepsOutput,
+ pub rolled_back: String,
+ pub active: Option<String>,
pub errors: Vec<String>,
}
