Skip to content

Commit 4106aee

Browse files
committed
Ensure CLI still works with no config file
We can handle an empty config file just fine, but I forgot to handle cases where there's *no* config file.
1 parent b91db48 commit 4106aee

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

diesel_cli/src/config.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use toml;
99
use super::{find_project_root, handle_error};
1010
use print_schema;
1111

12-
#[derive(Deserialize)]
12+
#[derive(Deserialize, Default)]
1313
#[serde(deny_unknown_fields)]
1414
pub struct Config {
1515
#[serde(default)]
@@ -31,9 +31,14 @@ impl Config {
3131

3232
pub fn read(matches: &ArgMatches) -> Result<Self, Box<Error>> {
3333
let path = Self::file_path(matches);
34-
let mut bytes = Vec::new();
35-
fs::File::open(path)?.read_to_end(&mut bytes)?;
36-
toml::from_slice(&bytes).map_err(Into::into)
34+
35+
if path.exists() {
36+
let mut bytes = Vec::new();
37+
fs::File::open(path)?.read_to_end(&mut bytes)?;
38+
toml::from_slice(&bytes).map_err(Into::into)
39+
} else {
40+
Ok(Self::default())
41+
}
3742
}
3843
}
3944

diesel_cli/tests/migration_run.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,31 @@ file = "src/my_schema.rs"
346346
);
347347
assert!(p.has_file("src/my_schema.rs"));
348348
}
349+
350+
#[test]
351+
fn migrations_can_be_run_with_no_config_file() {
352+
let p = project("migration_run_no_config_file")
353+
.folder("migrations")
354+
.build();
355+
let db = database(&p.database_url());
356+
357+
p.command("database").arg("setup").run();
358+
359+
p.create_migration(
360+
"12345_create_users_table",
361+
"CREATE TABLE users ( id INTEGER )",
362+
"DROP TABLE users",
363+
);
364+
365+
assert!(!db.table_exists("users"));
366+
367+
let result = p.command("migration").arg("run").run();
368+
369+
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
370+
assert!(
371+
result.stdout().contains("Running migration 12345"),
372+
"Unexpected stdout {}",
373+
result.stdout()
374+
);
375+
assert!(db.table_exists("users"));
376+
}

0 commit comments

Comments
 (0)