Skip to content

Commit

Permalink
cli: writing the custom migration dir to the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dennybiasiolli committed Feb 19, 2024
1 parent 6cd9b7e commit 6c5c7a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -136,12 +136,20 @@ fn create_migrations_dir(matches: &ArgMatches) -> Result<PathBuf, crate::errors:
Ok(dir)
}

fn create_config_file(matches: &ArgMatches) -> 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(())
Expand Down
16 changes: 16 additions & 0 deletions diesel_cli/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6c5c7a5

Please sign in to comment.