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

Code before:
        };

        let mut conn = result.map_err(|err| crate::errors::Error::ConnectionError {
            error: err,
            url: database_url,
        })?;

        conn.set_instrumentation(|event: InstrumentationEvent<'_>| {
            if let InstrumentationEvent::FinishQuery { query, error, .. } = event {
                if let Some(err) = error {
                    tracing::error!(?query, ?err, "Failed to execute query");
                } else {
                    tracing::debug!(?query);
                }
            }
        });
        Ok(conn)
    }
}

pub fn reset_database(
    args: &ArgMatches,
    migrations_dir: &Path,
) -> Result<(), crate::errors::Error> {
    drop_database(&database_url(args)?)?;
    setup_database(args, migrations_dir, false)
}

pub fn setup_database(
    args: &ArgMatches,
    migrations_dir: &Path,
    default_migrations: bool,
) -> Result<(), crate::errors::Error> {
    let database_url = database_url(args)?;

    create_database_if_needed(&database_url)?;
    if default_migrations {
        create_default_migration_if_needed(&database_url, migrations_dir)?;
    }
    create_schema_table_and_run_migrations_if_needed(&database_url, migrations_dir)?;
    Ok(())
}

pub fn drop_database_command(args: &ArgMatches) -> Result<(), crate::errors::Error> {
    drop_database(&database_url(args)?)
}

/// Creates the database specified in the connection url. It returns an error
/// it was unable to create the database.
fn create_database_if_needed(database_url: &str) -> Result<(), crate::errors::Error> {
    match Backend::for_url(database_url) {

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

Code after:
        };

        let mut conn = result.map_err(|err| crate::errors::Error::ConnectionError {
            error: err,
            url: database_url,
        })?;

        conn.set_instrumentation(|event: InstrumentationEvent<'_>| {
            if let InstrumentationEvent::FinishQuery { query, error, .. } = event {
                if let Some(err) = error {
                    tracing::error!(?query, ?err, "Failed to execute query");
                } else {
                    tracing::debug!(?query);
                }
            }
        });
        Ok(conn)
    }
}

pub fn reset_database(
    args: &ArgMatches,
    migrations_dir: &Path,
) -> Result<(), crate::errors::Error> {
    drop_database(&database_url(args)?)?;
    setup_database(args, migrations_dir)
}

pub fn setup_database(
    args: &ArgMatches,
    migrations_dir: &Path,
) -> Result<(), crate::errors::Error> {
    let database_url = database_url(args)?;

    create_database_if_needed(&database_url)?;

    let default_migrations = !args.get_flag("NO_DEFAULT_MIGRATION");

    if default_migrations {
        create_default_migration_if_needed(&database_url, migrations_dir)?;
    }

    create_schema_table_and_run_migrations_if_needed(&database_url, migrations_dir)?;
    Ok(())
}

pub fn drop_database_command(args: &ArgMatches) -> Result<(), crate::errors::Error> {
    drop_database(&database_url(args)?)
}

/// Creates the database specified in the connection url. It returns an error