Skip to content

Commit

Permalink
Fix create_database_if_needed output.
Browse files Browse the repository at this point in the history
We should only notify that we're creating a database when we actually
are. Sqlite has always been missing this, and it seems to have been
introduced to Postgres unintentionally.
  • Loading branch information
mcasper committed Feb 18, 2016
1 parent 9f0142c commit 5ff0f52
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
38 changes: 18 additions & 20 deletions diesel_cli/src/database.rs
Expand Up @@ -12,6 +12,8 @@ use database_error::DatabaseResult;
use std::error::Error;
use std::{env, fs};

use std::path::Path;

// FIXME: Remove the duplicates of this macro once expression level attributes
// are stable (I believe this is in 1.7)
#[cfg(all(feature = "sqlite", feature = "postgres"))]
Expand Down Expand Up @@ -78,13 +80,17 @@ fn create_database_if_needed(database_url: &String) -> DatabaseResult<()> {
match backend(database_url) {
"postgres" => {
if PgConnection::establish(database_url).is_err() {
let(database, postgres_url) = split_pg_connection_string(database_url);
try!(create_postgres_database(&postgres_url, &database));
let (database, postgres_url) = split_pg_connection_string(database_url);
println!("Creating database: {}", database);
let conn = try!(PgConnection::establish(&postgres_url));
try!(conn.execute(&format!("CREATE DATABASE {}", database)));
}
},
"sqlite" => {
println!("Creating database: {}", database_url);
try!(SqliteConnection::establish(database_url));
if !Path::new(database_url).exists() {
println!("Creating database: {}", database_url);
try!(SqliteConnection::establish(database_url));
}
},
_ => unreachable!("The backend function should ensure we never get here."),
}
Expand All @@ -94,32 +100,24 @@ fn create_database_if_needed(database_url: &String) -> DatabaseResult<()> {

#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
fn create_database_if_needed(database_url: &String) -> DatabaseResult<()> {
println!("Creating database: {}", database_url);
try!(SqliteConnection::establish(database_url));
if !Path::new(database_url).exists() {
println!("Creating database: {}", database_url);
try!(SqliteConnection::establish(database_url));
}
Ok(())
}

#[cfg(all(not(feature = "sqlite"), feature = "postgres"))]
fn create_database_if_needed(database_url: &String) -> DatabaseResult<()> {
println!("Creating database: {}", database_url);
if PgConnection::establish(database_url).is_err() {
let(database, postgres_url) = split_pg_connection_string(database_url);
try!(create_postgres_database(&postgres_url, &database));
let (database, postgres_url) = split_pg_connection_string(database_url);
println!("Creating database: {}", database);
let conn = try!(PgConnection::establish(&postgres_url));
try!(conn.execute(&format!("CREATE DATABASE {}", database)));
}
Ok(())
}

#[cfg(feature = "postgres")]
fn create_postgres_database(database_url: &String, database: &String)
-> DatabaseResult<()>
{
let conn = try!(PgConnection::establish(database_url));
println!("Creating database: {}", database);
try!(conn.execute(&format!("CREATE DATABASE {}", database)));
Ok(())
}


/// Creates the __diesel_schema_migrations table if it doesn't exist. If the
/// table didn't exist, it also runs any pending migrations. Returns a
/// `DatabaseError::ConnectionError` if it can't create the table, and exits
Expand Down
23 changes: 21 additions & 2 deletions diesel_cli/tests/setup.rs
Expand Up @@ -11,8 +11,6 @@ fn setup_creates_database() {
let result = p.command("setup").run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(result.stdout().contains("Creating database:"),
"Unexpected stdout {}", result.stdout());
assert!(db.exists());
}

Expand Down Expand Up @@ -60,3 +58,24 @@ fn setup_runs_migrations_if_no_schema_table() {
"Unexpected stdout {}", result.stdout());
assert!(db.table_exists("users"));
}

#[test]
fn setup_notifies_when_creating_a_database() {
let p = project("setup_notifies").build();

let result = p.command("setup").run();

assert!(result.stdout().contains("Creating database:"),
"Unexpected stdout {}", result.stdout());
}

#[test]
fn setup_doesnt_notify_when_not_creating_a_database() {
let p = project("setup_doesnt_notify").build();
let db = database(&p.database_url()).create();

let result = p.command("setup").run();

assert!(!result.stdout().contains("Creating database:"),
"Unexpected stdout {}", result.stdout());
}

0 comments on commit 5ff0f52

Please sign in to comment.