ravel-cli 0.1.0

CLI tool for the Ravel framework: project scaffolding, code generation, migrations, dev server
//! ravel migrate — run / rollback migrations.
//!
//! These commands delegate to the user project's `migrate` binary via
//! `cargo run --bin migrate -- <subcommand> [steps]`.
//!
//! The `migrate` binary is generated by `ravel new` at `src/bin/migrate.rs`
//! and uses the Migrator registered in `database/migrations/mod.rs`.

use anyhow::Result;
use std::process::Command;

fn run_migrate(args: &[&str]) -> Result<()> {
    let status = Command::new("cargo")
        .arg("run")
        .arg("--bin")
        .arg("migrate")
        .arg("--")
        .args(args)
        .status()?;

    if !status.success() {
        anyhow::bail!(
            "Migration command failed with exit code {:?}",
            status.code()
        );
    }

    Ok(())
}

pub async fn handle_migrate(steps: Option<u32>) -> Result<()> {
    let mut args = vec!["up"];
    let steps_str;
    if let Some(s) = steps {
        steps_str = s.to_string();
        args.push(&steps_str);
    }
    run_migrate(&args)
}

pub async fn handle_rollback(steps: Option<u32>) -> Result<()> {
    let mut args = vec!["down"];
    let steps_str;
    if let Some(s) = steps {
        steps_str = s.to_string();
        args.push(&steps_str);
    }
    run_migrate(&args)
}

pub async fn handle_refresh() -> Result<()> {
    run_migrate(&["refresh"])
}

pub async fn handle_fresh() -> Result<()> {
    run_migrate(&["fresh"])
}

pub async fn handle_status() -> Result<()> {
    run_migrate(&["status"])
}