use anyhow::{Result, bail};
use crate::{cargo, project};
#[derive(Debug, Clone, Copy)]
pub enum Action {
Run,
Status,
Rollback,
}
impl Action {
fn arg(self) -> &'static str {
match self {
Self::Run => "run",
Self::Status => "status",
Self::Rollback => "rollback",
}
}
}
pub fn run(action: Action) -> Result<()> {
let root = project::find_root()?;
if !root.join("src/bin/migrate.rs").is_file() {
bail!(
"src/bin/migrate.rs not found; this application was created without migration support"
);
}
let status = cargo::run(
&root,
&["run", "--quiet", "--bin", "migrate", "--", action.arg()],
&[],
)?;
if !status.success() {
bail!("`{}` did not complete successfully", command_name(action));
}
Ok(())
}
fn command_name(action: Action) -> &'static str {
match action {
Action::Run => "slave migrate",
Action::Status => "slave migrate:status",
Action::Rollback => "slave migrate:rollback",
}
}