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

fix: setup now sets migrations dir as relative path when possible #4439

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,13 @@ fn create_config_file(
let path = Config::file_path(matches);
if !path.exists() {
let source_content = include_str!("default_files/diesel.toml").to_string();
let migrations_dir_relative =
convert_absolute_path_to_relative(migrations_dir, &find_project_root()?);
// convert the path to a valid toml string (escaping backslashes on windows)
let migrations_dir_toml_string = migrations_dir.display().to_string().replace('\\', "\\\\");
let migrations_dir_toml_string = migrations_dir_relative
.display()
.to_string()
.replace('\\', "\\\\");
let modified_content = source_content.replace(
"dir = \"migrations\"",
&format!("dir = \"{}\"", migrations_dir_toml_string),
Expand Down
26 changes: 26 additions & 0 deletions diesel_cli/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,29 @@ fn setup_respects_migrations_dir_from_diesel_toml() {
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(p.has_file("custom_migrations"));
}

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

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

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(p
.file_contents("diesel.toml")
.contains("dir = \"migrations\""));
}

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

let migrations_dir_arg = p.directory_path().join("foo").display().to_string();
let result = p
.command("setup")
.arg(format!("--migration-dir={}", migrations_dir_arg))
.run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(p.file_contents("diesel.toml").contains("dir = \"foo\""));
}
4 changes: 4 additions & 0 deletions diesel_cli/tests/support/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ impl Project {
pub fn skip_drop_db(&mut self) {
self.skip_drop_db = true;
}

pub fn directory_path(&self) -> &Path {
self.directory.path()
}
}

#[cfg(not(feature = "sqlite"))]
Expand Down
Loading