Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing passing --database-url as global cli arg #1201

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn build_cli() -> App<'static, 'static> {
.subcommand(generate_bash_completion_subcommand)
.subcommand(infer_schema_subcommand)
.setting(AppSettings::SubcommandRequiredElseHelp)
.setting(AppSettings::PropagateGlobalValuesDown)
}

fn migration_dir_arg<'a, 'b>() -> Arg<'a, 'b> {
Expand Down
16 changes: 14 additions & 2 deletions diesel_cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,22 @@ pub fn schema_table_exists(database_url: &str) -> DatabaseResult<bool> {
}.map_err(Into::into)
}

pub fn database_url(matches: &ArgMatches) -> String {
// FIXME: remove the recursive call here as soon as
// https://github.com/kbknapp/clap-rs/issues/978 is fixed
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
68 changes: 68 additions & 0 deletions diesel_cli/tests/migration_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,71 @@ fn any_pending_migrations_after_running_and_creating() {

assert!(result.stdout().contains("true\n"));
}

#[test]
fn migration_run_runs_pending_migrations_custom_database_url_1() {
let p = project("migration_run_custom_db_url_1")
.folder("migrations")
.build();
let db_url = p.database_url();
let db = database(&db_url);

// Make sure the project is setup
p.command("setup").run();

p.create_migration(
"12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
"DROP TABLE users",
);

assert!(!db.table_exists("users"));

let result = p.command_without_database_url("migration")
.arg("run")
.arg("--database-url")
.arg(db_url)
.run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(
result.stdout().contains("Running migration 12345"),
"Unexpected stdout {}",
result.stdout()
);
assert!(db.table_exists("users"));
}

#[test]
fn migration_run_runs_pending_migrations_custom_database_url_2() {
let p = project("migration_run_custom_db_url_2")
.folder("migrations")
.build();
let db_url = p.database_url();
let db = database(&db_url);

// Make sure the project is setup
p.command("setup").run();

p.create_migration(
"12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
"DROP TABLE users",
);

assert!(!db.table_exists("users"));

let result = p.command_without_database_url("migration")
.arg("--database-url")
.arg(db_url)
.arg("run")
.run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(
result.stdout().contains("Running migration 12345"),
"Unexpected stdout {}",
result.stdout()
);
assert!(db.table_exists("users"));
}