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

cli setup improvements #3939

Merged
merged 3 commits into from
Feb 22, 2024
Merged
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
18 changes: 14 additions & 4 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,22 @@ 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();
// convert the path to a valid toml string (escaping backslashes on windows)
let migrations_dir_toml_string = migrations_dir.display().to_string().replace('\\', "\\\\");
let modified_content = source_content.replace(
"dir = \"migrations\"",
&format!("dir = \"{}\"", migrations_dir_toml_string),
);
let mut file = fs::File::create(path)?;
file.write_all(include_bytes!("default_files/diesel.toml"))?;
file.write_all(modified_content.as_bytes())?;
}

Ok(())
@@ -183,7 +193,7 @@ fn generate_completions_command(matches: &ArgMatches) {
/// Returns a `DatabaseError::ProjectRootNotFound` if no Cargo.toml is found.
fn create_migrations_directory(path: &Path) -> Result<PathBuf, crate::errors::Error> {
println!("Creating migrations directory at: {}", path.display());
fs::create_dir(path)?;
fs::create_dir_all(path)?;
fs::File::create(path.join(".keep"))?;
Ok(path.to_owned())
}
33 changes: 33 additions & 0 deletions diesel_cli/tests/database_setup.rs
Original file line number Diff line number Diff line change
@@ -152,6 +152,39 @@ fn database_setup_respects_migration_dir_by_arg() {
assert!(db.table_exists("users"));
}

#[test]
fn database_setup_respects_migration_nested_dir_by_arg() {
let p = project("database_setup_respects_migration_nested_dir_by_arg")
.folder("foo/bar")
.build();
let db = database(&p.database_url());

p.create_migration_in_directory(
"foo/bar",
"12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
Some("DROP TABLE users"),
None,
);

// sanity check
assert!(!db.exists());

let result = p
.command("database")
.arg("setup")
.arg("--migration-dir=foo/bar")
.run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(
result.stdout().contains("Running migration 12345"),
"Unexpected stdout {}",
result.stdout()
);
assert!(db.table_exists("users"));
}

#[test]
fn database_setup_respects_migration_dir_by_env() {
let p = project("database_setup_respects_migration_dir_by_env")
35 changes: 35 additions & 0 deletions diesel_cli/tests/setup.rs
Original file line number Diff line number Diff line change
@@ -170,6 +170,41 @@ 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]
#[cfg(windows)]
fn setup_writes_migration_dir_by_arg_to_config_file_win() {
let p = project("setup_writes_migration_dir_by_arg_to_config_file_win").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\\bar").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\\\\bar\""));
}

#[test]
fn setup_works_with_migration_dir_by_env() {
let p = project("setup_works_with_migration_dir_by_env").build();
2 changes: 1 addition & 1 deletion diesel_cli/tests/support/project_builder.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ impl ProjectBuilder {
File::create(tempdir.path().join("Cargo.toml")).unwrap();

for folder in self.folders {
fs::create_dir(tempdir.path().join(folder)).unwrap();
fs::create_dir_all(tempdir.path().join(folder)).unwrap();
}

for (file, contents) in self.files {