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

Code before:

#[tracing::instrument]
fn regenerate_schema_if_file_specified(matches: &ArgMatches) -> Result<(), crate::errors::Error> {
    tracing::debug!("Regenerate schema if required");

    let config = Config::read(matches)?.print_schema;
    for config in config.all_configs.values() {
        if let Some(ref path) = config.file {
            let mut connection = InferConnection::from_matches(matches)?;
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent)
                    .map_err(|e| crate::errors::Error::IoError(e, Some(parent.to_owned())))?;
            }

            if matches.get_flag("LOCKED_SCHEMA") {
                let schema = print_schema::output_schema(&mut connection, config)?;

                let old_buf = std::fs::read_to_string(path)
                    .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;

                if schema.lines().ne(old_buf.lines()) {
                    return Err(crate::errors::Error::SchemaWouldChange(
                        path.display().to_string(),
                    ));
                }
            } else {
                let schema = print_schema::output_schema(&mut connection, config)?;
                std::fs::write(path, schema.as_bytes())
                    .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
            }
        }
    }

    Ok(())
}

fn supported_backends() -> String {
    let features = &[
        #[cfg(feature = "postgres")]
        "postgres",
        #[cfg(feature = "mysql")]
        "mysql",
        #[cfg(feature = "sqlite")]
        "sqlite",
    ];

    features.join(" ")
}

#[cfg(test)]
mod tests {

Review comment (suggestion):
The `leak()` here is probably not necessary, but as the comment says it's fine.

Code after:
}

#[tracing::instrument]
fn regenerate_schema_if_file_specified(matches: &ArgMatches) -> Result<(), crate::errors::Error> {
    tracing::debug!("Regenerate schema if required");

    let config = Config::read(matches)?.print_schema;
    for config in config.all_configs.values() {
        if let Some(ref path) = config.file {
            let mut connection = InferConnection::from_matches(matches)?;
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent)
                    .map_err(|e| crate::errors::Error::IoError(e, Some(parent.to_owned())))?;
            }

            if matches.get_flag("LOCKED_SCHEMA") {
                let schema = print_schema::output_schema(&mut connection, config)?;

                let old_buf = std::fs::read_to_string(path)
                    .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;

                if schema.lines().ne(old_buf.lines()) {
                    // it's fine to leak here, we will
                    // exit the application anyway soon
                    let label = path.file_name().expect("We have a file name here");
                    let label = label.to_string_lossy().into_owned().leak();
                    println!(
                        "{}",
                        SimpleDiff::from_str(&old_buf, &schema, label, "new schema")
                    );
                    return Err(crate::errors::Error::SchemaWouldChange(
                        path.display().to_string(),
                    ));
                }
            } else {
                let schema = print_schema::output_schema(&mut connection, config)?;
                std::fs::write(path, schema.as_bytes())
                    .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
            }
        }
    }

    Ok(())
}

fn supported_backends() -> String {
    let features = &[
        #[cfg(feature = "postgres")]
        "postgres",
        #[cfg(feature = "mysql")]
        "mysql",