From a4729cdcc8fc55f953b89c354b7994f578c5c111 Mon Sep 17 00:00:00 2001 From: Stuart Hinson Date: Mon, 17 Aug 2020 20:46:18 -0700 Subject: [PATCH] Add source param to command line --- sqlx-cli/src/lib.rs | 6 +++--- sqlx-cli/src/migrate.rs | 21 ++++++++++++++------- sqlx-cli/src/opt.rs | 4 ++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sqlx-cli/src/lib.rs b/sqlx-cli/src/lib.rs index 3da90e03..12b9753f 100644 --- a/sqlx-cli/src/lib.rs +++ b/sqlx-cli/src/lib.rs @@ -31,9 +31,9 @@ hint: This command only works in the manifest directory of a Cargo package."# match opt.command { Command::Migrate(migrate) => match migrate.command { - MigrateCommand::Add { description } => migrate::add(&description)?, - MigrateCommand::Run => migrate::run(&database_url).await?, - MigrateCommand::Info => migrate::info(&database_url).await?, + MigrateCommand::Add { description } => migrate::add(&migrate.source, &description)?, + MigrateCommand::Run => migrate::run(&migrate.source, &database_url).await?, + MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?, }, Command::Database(database) => match database.command { diff --git a/sqlx-cli/src/migrate.rs b/sqlx-cli/src/migrate.rs index 8fe7d656..589b4db5 100644 --- a/sqlx-cli/src/migrate.rs +++ b/sqlx-cli/src/migrate.rs @@ -8,11 +8,12 @@ use std::path::Path; const MIGRATION_FOLDER: &'static str = "migrations"; -pub fn add(description: &str) -> anyhow::Result<()> { +pub fn add(source_path: &Option, description: &str) -> anyhow::Result<()> { use chrono::prelude::*; use std::path::PathBuf; + let migration_folder = migration_path(source_path); - fs::create_dir_all(MIGRATION_FOLDER).context("Unable to create migrations directory")?; + fs::create_dir_all(migration_folder).context("Unable to create migrations directory")?; let dt = Utc::now(); let mut file_name = dt.format("%Y%m%d%H%M%S").to_string(); @@ -21,7 +22,7 @@ pub fn add(description: &str) -> anyhow::Result<()> { file_name.push_str(".sql"); let mut path = PathBuf::new(); - path.push(MIGRATION_FOLDER); + path.push(migration_folder); path.push(&file_name); println!("Creating {}", style(path.display()).cyan()); @@ -33,8 +34,9 @@ pub fn add(description: &str) -> anyhow::Result<()> { Ok(()) } -pub async fn info(uri: &str) -> anyhow::Result<()> { - let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?; +pub async fn info(source_path: &Option, uri: &str) -> anyhow::Result<()> { + let migration_folder = migration_path(source_path); + let migrator = Migrator::new(Path::new(migration_folder)).await?; let mut conn = AnyConnection::connect(uri).await?; conn.ensure_migrations_table().await?; @@ -57,8 +59,9 @@ pub async fn info(uri: &str) -> anyhow::Result<()> { Ok(()) } -pub async fn run(uri: &str) -> anyhow::Result<()> { - let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?; +pub async fn run(source_path: &Option, uri: &str) -> anyhow::Result<()> { + let migration_folder = migration_path(source_path); + let migrator = Migrator::new(Path::new(migration_folder)).await?; let mut conn = AnyConnection::connect(uri).await?; conn.ensure_migrations_table().await?; @@ -87,3 +90,7 @@ pub async fn run(uri: &str) -> anyhow::Result<()> { Ok(()) } + +fn migration_path<'a>(source_path: &'a Option) -> &'a str { + source_path.as_deref().unwrap_or(MIGRATION_FOLDER) +} diff --git a/sqlx-cli/src/opt.rs b/sqlx-cli/src/opt.rs index a0f5afac..9efb4bbe 100644 --- a/sqlx-cli/src/opt.rs +++ b/sqlx-cli/src/opt.rs @@ -71,6 +71,10 @@ pub enum DatabaseCommand { /// Group of commands for creating and running migrations. #[derive(Clap, Debug)] pub struct MigrateOpt { + /// Path to folder containing migrations. Defaults to 'migrations' + #[clap(long)] + pub source: Option, + #[clap(subcommand)] pub command: MigrateCommand, }