(
command: Option<MigrateSubcommands>,
migration_dir: &str,
database_schema: Option<String>,
database_url: Option<String>,
verbose: bool,
)
| 14 | |
| 15 | #[cfg(feature = "cli")] |
| 16 | pub fn run_migrate_command( |
| 17 | command: Option<MigrateSubcommands>, |
| 18 | migration_dir: &str, |
| 19 | database_schema: Option<String>, |
| 20 | database_url: Option<String>, |
| 21 | verbose: bool, |
| 22 | ) -> Result<(), Box<dyn Error>> { |
| 23 | match command { |
| 24 | Some(MigrateSubcommands::Init) => run_migrate_init(migration_dir)?, |
| 25 | Some(MigrateSubcommands::Generate { |
| 26 | migration_name, |
| 27 | universal_time: _, |
| 28 | local_time, |
| 29 | }) => run_migrate_generate(migration_dir, &migration_name, !local_time)?, |
| 30 | _ => { |
| 31 | let (subcommand, migration_dir, steps, verbose) = match command { |
| 32 | Some(MigrateSubcommands::Fresh) => ("fresh", migration_dir, None, verbose), |
| 33 | Some(MigrateSubcommands::Refresh) => ("refresh", migration_dir, None, verbose), |
| 34 | Some(MigrateSubcommands::Reset) => ("reset", migration_dir, None, verbose), |
| 35 | Some(MigrateSubcommands::Status) => ("status", migration_dir, None, verbose), |
| 36 | Some(MigrateSubcommands::Up { num }) => ("up", migration_dir, num, verbose), |
| 37 | Some(MigrateSubcommands::Down { num }) => { |
| 38 | ("down", migration_dir, Some(num), verbose) |
| 39 | } |
| 40 | _ => ("up", migration_dir, None, verbose), |
| 41 | }; |
| 42 | |
| 43 | // Construct the `--manifest-path` |
| 44 | let manifest_path = if migration_dir.ends_with('/') { |
| 45 | format!("{migration_dir}Cargo.toml") |
| 46 | } else { |
| 47 | format!("{migration_dir}/Cargo.toml") |
| 48 | }; |
| 49 | // Construct the arguments that will be supplied to `cargo` command |
| 50 | let mut args = vec!["run", "--manifest-path", &manifest_path, "--", subcommand]; |
| 51 | let mut envs = vec![]; |
| 52 | |
| 53 | let mut num: String = "".to_string(); |
| 54 | if let Some(steps) = steps { |
| 55 | num = steps.to_string(); |
| 56 | } |
| 57 | if !num.is_empty() { |
| 58 | args.extend(["-n", &num]) |
| 59 | } |
| 60 | if let Some(database_url) = &database_url { |
| 61 | envs.push(("DATABASE_URL", database_url)); |
| 62 | } |
| 63 | if let Some(database_schema) = &database_schema { |
| 64 | envs.push(("DATABASE_SCHEMA", database_schema)); |
| 65 | } |
| 66 | if verbose { |
| 67 | args.push("-v"); |
| 68 | } |
| 69 | // Run migrator CLI on user's behalf |
| 70 | println!("Running `cargo {}`", args.join(" ")); |
| 71 | let exit_status = Command::new("cargo").args(args).envs(envs).status()?; // Get the status code |
| 72 | if !exit_status.success() { |
| 73 | // Propagate the error if any |
no test coverage detected