use super::HistoryFile;
use crate::{Config, theme::dialoguer_theme};
use anyhow::Result;
use clap::Parser;
use console::style;
use dialoguer::Select;
use std::fs;
use toasty::Db;
#[derive(Parser, Debug)]
pub struct DropCommand {
#[arg(short, long)]
name: Option<String>,
#[arg(short, long)]
latest: bool,
}
impl DropCommand {
pub(crate) fn run(self, _db: &Db, config: &Config) -> Result<()> {
let history_path = config.migration.get_history_file_path();
let mut history = HistoryFile::load_or_default(&history_path)?;
if history.migrations().is_empty() {
eprintln!("{}", style("No migrations found in history").red().bold());
anyhow::bail!("No migrations found in history");
}
let migration_index = if self.latest {
history.migrations().len() - 1
} else if let Some(name) = &self.name {
history
.migrations()
.iter()
.position(|m| m.name == *name)
.ok_or_else(|| anyhow::anyhow!("Migration '{}' not found", name))?
} else {
println!();
println!(" {}", style("Drop Migration").cyan().bold().underlined());
println!();
let migration_display: Vec<String> = history
.migrations()
.iter()
.map(|m| format!(" {}", m.name))
.collect();
Select::with_theme(&dialoguer_theme())
.with_prompt(" Select migration to drop")
.items(&migration_display)
.default(migration_display.len() - 1)
.interact()?
};
println!();
let migration = &history.migrations()[migration_index];
let migration_name = migration.name.clone();
let snapshot_name = migration.snapshot_name.clone();
let migration_path = config.migration.get_migrations_dir().join(&migration_name);
if migration_path.exists() {
fs::remove_file(&migration_path)?;
println!(
" {} {}",
style("✓").green().bold(),
style(format!("Deleted migration: {}", migration_name)).dim()
);
} else {
println!(
" {} {}",
style("⚠").yellow().bold(),
style(format!("Migration file not found: {}", migration_name))
.yellow()
.dim()
);
}
let snapshot_path = config.migration.get_snapshots_dir().join(&snapshot_name);
if snapshot_path.exists() {
fs::remove_file(&snapshot_path)?;
println!(
" {} {}",
style("✓").green().bold(),
style(format!("Deleted snapshot: {}", snapshot_name)).dim()
);
} else {
println!(
" {} {}",
style("⚠").yellow().bold(),
style(format!("Snapshot file not found: {}", snapshot_name))
.yellow()
.dim()
);
}
history.remove_migration(migration_index);
history.save(&history_path)?;
println!(
" {} {}",
style("✓").green().bold(),
style("Updated migration history").dim()
);
println!();
println!(
" {} {}",
style("").magenta(),
style(format!(
"Migration '{}' successfully dropped",
migration_name
))
.green()
.bold()
);
println!();
Ok(())
}
}