From 6c5c7a502cae69f63c89bbf0c7039ea0eba07187 Mon Sep 17 00:00:00 2001 From: Denny Biasiolli Date: Mon, 19 Feb 2024 13:01:09 +0100 Subject: [PATCH] cli: writing the custom migration dir to the config file --- diesel_cli/src/main.rs | 14 +++++++++++--- diesel_cli/tests/setup.rs | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index b21e99ef7014..77df4aba7a59 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -96,8 +96,8 @@ where #[tracing::instrument] fn run_setup_command(matches: &ArgMatches) -> Result<(), crate::errors::Error> { - create_config_file(matches)?; let migrations_dir = create_migrations_dir(matches)?; + create_config_file(matches, &migrations_dir)?; database::setup_database(matches, &migrations_dir)?; Ok(()) @@ -136,12 +136,20 @@ fn create_migrations_dir(matches: &ArgMatches) -> Result Result<(), crate::errors::Error> { +fn create_config_file( + matches: &ArgMatches, + migrations_dir: &Path, +) -> Result<(), crate::errors::Error> { use std::io::Write; let path = Config::file_path(matches); if !path.exists() { + let source_content = include_str!("default_files/diesel.toml").to_string(); + let modified_content = source_content.replace( + "dir = \"migrations\"", + &format!("dir = \"{}\"", migrations_dir.display()), + ); let mut file = fs::File::create(path)?; - file.write_all(include_bytes!("default_files/diesel.toml"))?; + file.write_all(modified_content.as_bytes())?; } Ok(()) diff --git a/diesel_cli/tests/setup.rs b/diesel_cli/tests/setup.rs index d0bab07b5b17..f5d3adbab6be 100644 --- a/diesel_cli/tests/setup.rs +++ b/diesel_cli/tests/setup.rs @@ -170,6 +170,22 @@ fn setup_works_with_migration_dir_by_arg() { assert!(p.has_file("foo")); } +#[test] +fn setup_writes_migration_dir_by_arg_to_config_file() { + let p = project("setup_writes_migration_dir_by_arg_to_config_file").build(); + + // make sure the project builder doesn't create it for us + assert!(!p.has_file("migrations")); + assert!(!p.has_file("foo")); + + let result = p.command("setup").arg("--migration-dir=foo").run(); + + assert!(result.is_success(), "Result was unsuccessful {:?}", result); + assert!(!p.has_file("migrations")); + assert!(p.has_file("foo")); + assert!(p.file_contents("diesel.toml").contains("dir = \"foo\"")); +} + #[test] fn setup_works_with_migration_dir_by_env() { let p = project("setup_works_with_migration_dir_by_env").build();