Skip to content

Commit

Permalink
Merge pull request #4184 from lgrosz/optional-default-migration
Browse files Browse the repository at this point in the history
Adds --no-default-migration option diesel_cli
  • Loading branch information
weiznich committed Aug 23, 2024
2 parents 83da2f0 + 2ec224e commit bfe9989
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
41 changes: 30 additions & 11 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,35 @@ pub fn build_cli() -> Command {
.subcommand_required(true)
.arg_required_else_help(true);

let setup_subcommand = Command::new("setup").arg(migration_dir_arg()).about(
"Creates the migrations directory, creates the database \
let setup_subcommand = Command::new("setup")
.arg(migration_dir_arg())
.arg(no_default_migration_arg())
.about(
"Creates the migrations directory, creates the database \
specified in your DATABASE_URL, and runs existing migrations.",
);
);

let database_subcommand = Command::new("database")
.alias("db")
.arg(migration_dir_arg())
.about("A group of commands for setting up and resetting your database.")
.subcommand(Command::new("setup").about(
"Creates the database specified in your DATABASE_URL, \
and then runs any existing migrations.",
))
.subcommand(Command::new("reset").about(
"Resets your database by dropping the database specified \
in your DATABASE_URL and then running `diesel database setup`.",
))
.subcommand(
Command::new("setup")
.arg(no_default_migration_arg())
.about(
"Creates the database specified in your DATABASE_URL, and \
then runs any existing migrations.",
)
)
.subcommand(
Command::new("reset")
.arg(no_default_migration_arg())
.about(
"Resets your database by dropping the database specified \
in your DATABASE_URL and then running `diesel database \
setup`.",
)
)
.subcommand(
Command::new("drop")
.about("Drops the database specified in your DATABASE_URL.")
Expand Down Expand Up @@ -402,3 +414,10 @@ fn migration_dir_arg() -> Arg {
.value_parser(clap::value_parser!(std::path::PathBuf))
.global(true)
}

fn no_default_migration_arg() -> Arg {
Arg::new("NO_DEFAULT_MIGRATION")
.long("no-default-migration")
.help("Don't generate the default migration.")
.action(ArgAction::SetTrue)
}
8 changes: 7 additions & 1 deletion diesel_cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ pub fn setup_database(
let database_url = database_url(args)?;

create_database_if_needed(&database_url)?;
create_default_migration_if_needed(&database_url, migrations_dir)?;

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(())
}
Expand Down
26 changes: 26 additions & 0 deletions diesel_cli/tests/database_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ fn reset_works_with_migration_dir_by_env() {
assert!(db.table_exists("__diesel_schema_migrations"));
}

#[test]
fn reset_works_with_no_default_migrations_arg() {
let p = project("reset_works_with_no_default_migrations_arg")
.folder("custom_migrations")
.file(
"diesel.toml",
r#"
[migrations_directory]
dir = "custom_migrations"
"#,
)
.build();
database(&p.database_url()).create();

let result = p
.command("database")
.arg("reset")
.arg("--no-default-migration")
.run();

use std::path::Path;

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!p.has_file(Path::new("custom_migrations").join("00000000000000_diesel_initial_setup")));
}

#[test]
fn reset_sanitize_database_name() {
let p = project("name-with-dashes").folder("migrations").build();
Expand Down
28 changes: 28 additions & 0 deletions diesel_cli/tests/database_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,31 @@ fn database_setup_respects_migrations_dir_from_diesel_toml() {
);
assert!(db.table_exists("users"));
}

#[test]
fn database_setup_no_default_migrations() {
let p = project("database_setup_no_default_migrations")
.folder("custom_migrations")
.file(
"diesel.toml",
r#"
[migrations_directory]
dir = "custom_migrations"
"#,
)
.build();
let db = database(&p.database_url());

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

let result = p.command("database")
.arg("setup")
.arg("--no-default-migration")
.run();

use std::path::Path;

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!p.has_file(Path::new("custom_migrations").join("00000000000000_diesel_initial_setup")));
}
11 changes: 11 additions & 0 deletions diesel_cli/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ fn setup_creates_default_migration_file_if_project_is_otherwise_setup() {
assert!(p.has_file(&initial_migration_path));
}

#[test]
#[cfg(feature = "postgres")]
fn setup_no_default_migration() {
let p = project("setup_no_default_migration").build();

let result = p.command("setup").arg("--no-default-migration").run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!p.has_file(Path::new("migrations").join("00000000000000_diesel_initial_setup")));
}

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

0 comments on commit bfe9989

Please sign in to comment.