Repository: diesel-rs/diesel
File: diesel_cli/src/main.rs (Rust)

Code before:
    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)
            .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
        file.write_all(modified_content.as_bytes())
            .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
    }

    Ok(())
}

#[tracing::instrument]
fn run_database_command(matches: &ArgMatches) -> Result<(), crate::errors::Error> {
    match matches
        .subcommand()
        .expect("Clap should prevent reaching this without subcommand")
    {
        ("setup", args) => {
            let migrations_dir = self::migrations::migrations_dir(matches)?;
            database::setup_database(args, &migrations_dir, false)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("reset", args) => {
            let migrations_dir = self::migrations::migrations_dir(matches)?;
            database::reset_database(args, &migrations_dir)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("drop", args) => database::drop_database_command(args)?,
        _ => unreachable!("The cli parser should prevent reaching here"),
    };
    Ok(())
}

#[tracing::instrument]
fn generate_completions_command(matches: &ArgMatches) {
    let shell: &Shell = matches.get_one("SHELL").expect("Shell is set here?");
    let mut app = cli::build_cli();
    let name = app.get_name().to_string();
    generate(*shell, &mut app, name, &mut stdout());
}

/// Looks for a migrations directory in the current path and all parent paths,
/// and creates one in the same directory as the Cargo.toml if it can't find
/// one. It also sticks a .keep in the directory so git will pick it up.
/// Returns a `DatabaseError::ProjectRootNotFound` if no Cargo.toml is found.

Review comment (suggestion):
This changes the current default behavior. We should also use the `--no-default-migration` flag here instead.

Code after:
    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)
            .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
        file.write_all(modified_content.as_bytes())
            .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
    }

    Ok(())
}

#[tracing::instrument]
fn run_database_command(matches: &ArgMatches) -> Result<(), crate::errors::Error> {
    match matches
        .subcommand()
        .expect("Clap should prevent reaching this without subcommand")
    {
        ("setup", args) => {
            let migrations_dir = self::migrations::migrations_dir(matches)?;
            database::setup_database(args, &migrations_dir)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("reset", args) => {
            let migrations_dir = self::migrations::migrations_dir(matches)?;
            database::reset_database(args, &migrations_dir)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("drop", args) => database::drop_database_command(args)?,
        _ => unreachable!("The cli parser should prevent reaching here"),
    };
    Ok(())
}

#[tracing::instrument]
fn generate_completions_command(matches: &ArgMatches) {
    let shell: &Shell = matches.get_one("SHELL").expect("Shell is set here?");
    let mut app = cli::build_cli();
    let name = app.get_name().to_string();
    generate(*shell, &mut app, name, &mut stdout());
}

/// Looks for a migrations directory in the current path and all parent paths,
/// and creates one in the same directory as the Cargo.toml if it can't find
/// one. It also sticks a .keep in the directory so git will pick it up.
/// Returns a `DatabaseError::ProjectRootNotFound` if no Cargo.toml is found.