toasty_cli/migration/
reset.rs

1use super::apply_migrations;
2use crate::Config;
3use crate::theme::dialoguer_theme;
4use anyhow::Result;
5use clap::Parser;
6use console::style;
7use dialoguer::Confirm;
8use toasty::Db;
9
10/// Drops all tables in the database, then optionally re-applies migrations.
11///
12/// Prompts for confirmation before proceeding. After the reset, all
13/// migrations from the history file are re-applied unless `--skip-migrations`
14/// is passed.
15#[derive(Parser, Debug)]
16pub struct ResetCommand {
17    /// Skip applying migrations after reset
18    #[arg(long)]
19    skip_migrations: bool,
20}
21
22impl ResetCommand {
23    pub(crate) async fn run(self, db: &Db, config: &Config) -> Result<()> {
24        println!();
25        println!("  {}", style("Reset Database").cyan().bold().underlined());
26        println!();
27        println!(
28            "  {}",
29            style(format!(
30                "Connected to {}",
31                crate::utility::redact_url_password(&db.driver().url())
32            ))
33            .dim()
34        );
35        println!();
36
37        let theme = {
38            let mut t = dialoguer_theme();
39            t.success_prefix = style(" ".to_string());
40            t.prompt_prefix = style(" ".to_string());
41            t.prompt_style = console::Style::new().red().bold();
42            t
43        };
44
45        let confirmed = Confirm::with_theme(&theme)
46            .with_prompt("This will drop all tables and data. Are you sure?")
47            .default(false)
48            .interact()?;
49
50        if !confirmed {
51            println!();
52            println!("  {}", style("Aborted.").dim());
53            println!();
54            return Ok(());
55        }
56
57        println!();
58        println!("  {} Resetting database...", style("→").cyan());
59
60        db.reset_db().await?;
61
62        println!(
63            "  {} {}",
64            style("✓").green().bold(),
65            style("Database reset successfully").dim()
66        );
67        println!();
68
69        if !self.skip_migrations {
70            apply_migrations(db, config).await?;
71        }
72
73        Ok(())
74    }
75}