Skip to content

Commit

Permalink
Fixing passing --database-url as global cli arg
Browse files Browse the repository at this point in the history
--database-url is passed as global cli argument, this means it may be
  not part of the current ArgMatches. To solve this we use the global
  ArgMatches and look recursivly in all current subcommands for database-url
  • Loading branch information
weiznich committed Sep 25, 2017
1 parent b9f5aa0 commit 514f7f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
24 changes: 15 additions & 9 deletions diesel_cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,21 @@ macro_rules! call_with_conn {
};
}

pub fn reset_database(args: &ArgMatches, migrations_dir: &Path) -> DatabaseResult<()> {
try!(drop_database(&database_url(args)));
setup_database(args, migrations_dir)
pub fn reset_database(db_url: String, migrations_dir: &Path) -> DatabaseResult<()> {
try!(drop_database(&db_url));
setup_database(db_url, migrations_dir)
}

pub fn setup_database(args: &ArgMatches, migrations_dir: &Path) -> DatabaseResult<()> {
let database_url = database_url(args);
pub fn setup_database(database_url: String, migrations_dir: &Path) -> DatabaseResult<()> {

create_database_if_needed(&database_url)?;
create_default_migration_if_needed(&database_url, migrations_dir)?;
create_schema_table_and_run_migrations_if_needed(&database_url, migrations_dir)?;
Ok(())
}

pub fn drop_database_command(args: &ArgMatches) -> DatabaseResult<()> {
drop_database(&database_url(args))
pub fn drop_database_command(db_url: String) -> DatabaseResult<()> {
drop_database(&db_url)
}

/// Creates the database specified in the connection url. It returns an error
Expand Down Expand Up @@ -312,10 +311,17 @@ pub fn schema_table_exists(database_url: &str) -> DatabaseResult<bool> {
}.map_err(Into::into)
}

pub fn database_url(matches: &ArgMatches) -> String {
fn database_url_from_cli(matches: &ArgMatches) -> Option<String> {
matches
.value_of("DATABASE_URL")
.map(|s| s.into())
.map(Into::into)
.or_else(|| {
matches.subcommand().1.and_then(|s| database_url_from_cli(s))
})
}

pub fn database_url(matches: &ArgMatches) -> String {
database_url_from_cli(matches)
.or_else(|| env::var("DATABASE_URL").ok())
.unwrap_or_else(|| handle_error(DatabaseError::DatabaseUrlMissing))
}
Expand Down
32 changes: 13 additions & 19 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,41 @@ fn main() {
dotenv().ok();

let matches = cli::build_cli().get_matches();

let db_url = database::database_url(&matches);
match matches.subcommand() {
("migration", Some(matches)) => run_migration_command(matches),
("setup", Some(matches)) => run_setup_command(matches),
("database", Some(matches)) => run_database_command(matches),
("migration", Some(matches)) => run_migration_command(matches, db_url),
("setup", Some(matches)) => run_setup_command(matches, db_url),
("database", Some(matches)) => run_database_command(matches, db_url),
("bash-completion", Some(matches)) => generate_bash_completion_command(matches),
("print-schema", Some(matches)) => run_infer_schema(matches),
("print-schema", Some(matches)) => run_infer_schema(matches, db_url),
_ => unreachable!("The cli parser should prevent reaching here"),
}
}

fn run_migration_command(matches: &ArgMatches) {
fn run_migration_command(matches: &ArgMatches, database_url: String) {
match matches.subcommand() {
("run", Some(args)) => {
let database_url = database::database_url(matches);
let dir = migrations_dir(args);
call_with_conn!(
database_url,
migrations::run_pending_migrations_in_directory(&dir, &mut stdout())
).unwrap_or_else(handle_error);
}
("revert", Some(args)) => {
let database_url = database::database_url(matches);
let dir = migrations_dir(args);
call_with_conn!(
database_url,
migrations::revert_latest_migration_in_directory(&dir)
).unwrap_or_else(handle_error);
}
("redo", Some(args)) => {
let database_url = database::database_url(matches);
let dir = migrations_dir(args);
call_with_conn!(database_url, redo_latest_migration(&dir));
}
("list", Some(args)) => {
use std::cmp::Ordering;
use std::ffi::OsStr;

let database_url = database::database_url(matches);
let dir = migrations_dir(args);
let migrations =
call_with_conn!(database_url, migrations::mark_migrations_in_directory(&dir))
Expand Down Expand Up @@ -131,7 +127,6 @@ fn run_migration_command(matches: &ArgMatches) {
}
}
("pending", Some(_)) => {
let database_url = database::database_url(matches);
let result = call_with_conn!(database_url, migrations::any_pending_migrations);
println!("{:?}", result.unwrap());
}
Expand Down Expand Up @@ -186,10 +181,10 @@ fn migrations_dir(matches: &ArgMatches) -> PathBuf {
})
}

fn run_setup_command(matches: &ArgMatches) {
fn run_setup_command(matches: &ArgMatches, db_url: String) {
let migrations_dir = create_migrations_dir(matches).unwrap_or_else(handle_error);

database::setup_database(matches, &migrations_dir).unwrap_or_else(handle_error);
database::setup_database(db_url, &migrations_dir).unwrap_or_else(handle_error);
}

fn create_migrations_dir(matches: &ArgMatches) -> DatabaseResult<PathBuf> {
Expand All @@ -210,17 +205,17 @@ fn create_migrations_dir(matches: &ArgMatches) -> DatabaseResult<PathBuf> {
Ok(dir.to_owned())
}

fn run_database_command(matches: &ArgMatches) {
fn run_database_command(matches: &ArgMatches, db_url: String) {
match matches.subcommand() {
("setup", Some(args)) => {
let migrations_dir = migrations_dir(args);
database::setup_database(args, &migrations_dir).unwrap_or_else(handle_error)
database::setup_database(db_url, &migrations_dir).unwrap_or_else(handle_error)
}
("reset", Some(args)) => {
let migrations_dir = migrations_dir(args);
database::reset_database(args, &migrations_dir).unwrap_or_else(handle_error)
database::reset_database(db_url, &migrations_dir).unwrap_or_else(handle_error)
}
("drop", Some(args)) => database::drop_database_command(args).unwrap_or_else(handle_error),
("drop", _) => database::drop_database_command(db_url).unwrap_or_else(handle_error),
_ => unreachable!("The cli parser should prevent reaching here"),
};
}
Expand Down Expand Up @@ -315,11 +310,10 @@ fn convert_absolute_path_to_relative(target_path: &Path, mut current_path: &Path
result.join(target_path.strip_prefix(current_path).unwrap())
}

fn run_infer_schema(matches: &ArgMatches) {
fn run_infer_schema(matches: &ArgMatches, database_url: String) {
use diesel_infer_schema::TableName;
use print_schema::*;

let database_url = database::database_url(matches);
let schema_name = matches.value_of("schema");

let filter = matches
Expand Down

0 comments on commit 514f7f0

Please sign in to comment.