Skip to content

Commit c6aedff

Browse files
committed
Clean the code for the revert feature
1 parent 35aba07 commit c6aedff

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

diesel_cli/src/cli.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,13 @@ fn migration_dir_arg<'a, 'b>() -> Arg<'a, 'b> {
236236
}
237237

238238
fn migration_revert_number_validator(val: String) -> Result<(), String> {
239-
240239
if val == "all" {
241240
return Ok(());
242241
}
243242

244-
let number = match val.parse::<i64>() {
245-
Ok(number) => number,
246-
Err(_) => return Err("Cannot parse <REVERT_NUMBER>. The input must be an integer or 'all'.".to_string())
247-
};
243+
let number = val.parse::<i64>().map_err(|_| {
244+
"Cannot parse <REVERT_NUMBER>. The input must be an integer or 'all'.".to_string()
245+
})?;
248246

249247
if number < 1 {
250248
return Err("<REVERT_NUMBER> must be at least equal to 1.".to_string());

diesel_cli/src/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,24 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
7575
let database_url = database::database_url(matches);
7676
let dir = migrations_dir(matches).unwrap_or_else(handle_error);
7777

78-
match args.value_of("REVERT_NUMBER") {
79-
Some("all") => {
78+
// We can unwrap since the argument is validated from the cli module.
79+
// The value is required and is a number greater than 0 or "all".
80+
let number = args.value_of("REVERT_NUMBER").unwrap();
81+
82+
// Test if we have an integer or "all"
83+
match number.parse::<i64>() {
84+
Ok(number) => {
8085
call_with_conn!(
8186
database_url,
82-
migrations::revert_all_migrations_in_directory(&dir)
87+
migrations::revert_latest_migrations_in_directory(&dir, number)
8388
)?;
8489
}
85-
Some(n) => {
86-
let x = n.parse::<i64>().unwrap_or_else(handle_error);
87-
90+
_ => {
8891
call_with_conn!(
8992
database_url,
90-
migrations::revert_latest_migrations_in_directory(&dir, x)
93+
migrations::revert_all_migrations_in_directory(&dir)
9194
)?;
9295
}
93-
None => unreachable!("REVERT_NUMBER has a default value"),
9496
}
9597

9698
regenerate_schema_if_file_specified(matches)?;

diesel_cli/tests/migration_revert.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::support::{database, project};
22

33
#[test]
44
fn migration_revert_runs_the_last_migration_down() {
5-
let p = project("migration_revert_runs_the_last_migration_down").folder("migrations").build();
5+
let p = project("migration_revert_runs_the_last_migration_down")
6+
.folder("migrations")
7+
.build();
68
let db = database(&p.database_url());
79

810
p.create_migration(
@@ -131,7 +133,9 @@ fn migration_revert_respects_migration_dir_from_diesel_toml() {
131133

132134
#[test]
133135
fn migration_revert_runs_the_last_two_migration_down() {
134-
let p = project("migration_revert_runs_the_last_two_migration_down").folder("migrations").build();
136+
let p = project("migration_revert_runs_the_last_two_migration_down")
137+
.folder("migrations")
138+
.build();
135139
let db = database(&p.database_url());
136140

137141
p.create_migration(
@@ -182,7 +186,9 @@ fn migration_revert_runs_the_last_two_migration_down() {
182186

183187
#[test]
184188
fn migration_revert_all_runs_the_migrations_down() {
185-
let p = project("migration_revert_all_runs_the_migrations_down").folder("migrations").build();
189+
let p = project("migration_revert_all_runs_the_migrations_down")
190+
.folder("migrations")
191+
.build();
186192
let db = database(&p.database_url());
187193

188194
p.create_migration(
@@ -239,7 +245,9 @@ fn migration_revert_all_runs_the_migrations_down() {
239245

240246
#[test]
241247
fn migration_revert_with_zero_should_throw_an_error() {
242-
let p = project("migration_revert_with_zero_should_throw_an_error").folder("migrations").build();
248+
let p = project("migration_revert_with_zero_should_throw_an_error")
249+
.folder("migrations")
250+
.build();
243251

244252
// Make sure the project is setup
245253
p.command("setup").run();
@@ -263,7 +271,9 @@ fn migration_revert_with_zero_should_throw_an_error() {
263271

264272
#[test]
265273
fn migration_revert_with_an_invalid_input_should_throw_an_error() {
266-
let p = project("migration_revert_with_an_invalid_input_should_throw_an_error").folder("migrations").build();
274+
let p = project("migration_revert_with_an_invalid_input_should_throw_an_error")
275+
.folder("migrations")
276+
.build();
267277

268278
// Make sure the project is setup
269279
p.command("setup").run();

0 commit comments

Comments
 (0)