Skip to main content

spawn_db/commands/
check.rs

1use crate::commands::{Command, Outcome, TelemetryDescribe, TelemetryInfo};
2use crate::config::Config;
3use crate::store::list_migration_fs_status;
4use anyhow::Result;
5use console::style;
6
7pub struct Check;
8
9impl TelemetryDescribe for Check {
10    fn telemetry(&self) -> TelemetryInfo {
11        TelemetryInfo::new("check")
12    }
13}
14
15impl Command for Check {
16    async fn execute(&self, config: &Config) -> Result<Outcome> {
17        // Validate the database reference if one was provided
18        if config.database.is_some() {
19            config.db_config()?;
20        }
21
22        let mut warnings: Vec<String> = Vec::new();
23
24        // Grab status from store
25        let fs_status = list_migration_fs_status(config.operator(), &config.pather(), None).await?;
26
27        for (name, status) in &fs_status {
28            if status.has_up_sql && !status.has_lock_toml {
29                warnings.push(format!("Migration {} is not pinned", style(name).yellow()));
30            }
31        }
32
33        if warnings.is_empty() {
34            println!("No issues found.");
35            Ok(Outcome::Success)
36        } else {
37            println!(
38                "\nFound {} warning{}:\n",
39                warnings.len(),
40                if warnings.len() == 1 { "" } else { "s" }
41            );
42            for (i, warning) in warnings.iter().enumerate() {
43                println!("  {}. {}", i + 1, warning);
44            }
45            println!();
46            Ok(Outcome::CheckFailed)
47        }
48    }
49}