Skip to content

Commit 807f500

Browse files
committed
Refactor functions to get or create the migrations directory
The `migrations_dir` function doesn't handle errors anymore but returns a Result. This allows `create_migrations_dir` to match the result and create the missing migrations directory if it doesn't exist. Functions calling `migrations_dir` should now handle the error. See issue diesel-rs#2163
1 parent 6a09698 commit 807f500

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

diesel_cli/src/main.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
8181
match matches.subcommand() {
8282
("run", Some(_)) => {
8383
let database_url = database::database_url(matches);
84-
let dir = migrations_dir(matches);
84+
let dir = migrations_dir(matches).unwrap_or_else(handle_error);
8585
call_with_conn!(
8686
database_url,
8787
migrations::run_pending_migrations_in_directory(&dir, &mut stdout())
@@ -90,7 +90,7 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
9090
}
9191
("revert", Some(_)) => {
9292
let database_url = database::database_url(matches);
93-
let dir = migrations_dir(matches);
93+
let dir = migrations_dir(matches).unwrap_or_else(handle_error);
9494
call_with_conn!(
9595
database_url,
9696
migrations::revert_latest_migration_in_directory(&dir)
@@ -99,13 +99,13 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
9999
}
100100
("redo", Some(_)) => {
101101
let database_url = database::database_url(matches);
102-
let dir = migrations_dir(matches);
102+
let dir = migrations_dir(matches).unwrap_or_else(handle_error);
103103
call_with_conn!(database_url, redo_latest_migration(&dir));
104104
regenerate_schema_if_file_specified(matches)?;
105105
}
106106
("list", Some(_)) => {
107107
let database_url = database::database_url(matches);
108-
let dir = migrations_dir(matches);
108+
let dir = migrations_dir(matches).unwrap_or_else(handle_error);
109109
let mut migrations =
110110
call_with_conn!(database_url, migrations::mark_migrations_in_directory(&dir))?;
111111

@@ -132,7 +132,9 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
132132
let migration_name = args.value_of("MIGRATION_NAME").unwrap();
133133
let version = migration_version(args);
134134
let versioned_name = format!("{}_{}", version, migration_name);
135-
let migration_dir = migrations_dir(matches).join(versioned_name);
135+
let migration_dir = migrations_dir(matches)
136+
.unwrap_or_else(handle_error)
137+
.join(versioned_name);
136138
fs::create_dir(&migration_dir).unwrap();
137139

138140
match args.value_of("MIGRATION_FORMAT") {
@@ -206,11 +208,15 @@ fn migrations_dir_from_config(matches: &ArgMatches) -> Result<PathBuf, Migration
206208
}
207209
}
208210

209-
fn migrations_dir(matches: &ArgMatches) -> PathBuf {
210-
migrations_dir_from_cli(matches)
211+
fn migrations_dir(matches: &ArgMatches) -> Result<PathBuf, MigrationError> {
212+
let migrations_dir = migrations_dir_from_cli(matches)
211213
.or_else(|| env::var("MIGRATION_DIRECTORY").map(PathBuf::from).ok())
212-
.or_else(|| migrations_dir_from_config(matches).ok())
213-
.unwrap_or_else(|| migrations::find_migrations_directory().unwrap_or_else(handle_error))
214+
.or_else(|| migrations_dir_from_config(matches).ok());
215+
216+
match migrations_dir {
217+
Some(dir) => Ok(dir),
218+
None => migrations::find_migrations_directory(),
219+
}
214220
}
215221

216222
fn run_setup_command(matches: &ArgMatches) {
@@ -220,22 +226,15 @@ fn run_setup_command(matches: &ArgMatches) {
220226
database::setup_database(matches, &migrations_dir).unwrap_or_else(handle_error);
221227
}
222228

229+
/// Checks if the migration directory exists, else creates it.
230+
/// For more information see the `migrations_dir` function.
223231
fn create_migrations_dir(matches: &ArgMatches) -> DatabaseResult<PathBuf> {
224-
let dir = matches
225-
.value_of("MIGRATION_DIRECTORY")
226-
.map(PathBuf::from)
227-
.or_else(|| env::var("MIGRATION_DIRECTORY").map(PathBuf::from).ok())
228-
.unwrap_or_else(|| {
229-
let migrations_dir_from_config = migrations_dir_from_config(matches);
230-
let project_root = find_project_root().unwrap_or_else(handle_error);
231-
232-
// Retreives and transforms the path of the migrations
233-
// directory from the configuration file or returns the
234-
// default one.
235-
migrations_dir_from_config
236-
.ok()
237-
.unwrap_or_else(|| project_root.join("migrations"))
238-
});
232+
let dir = match migrations_dir(matches) {
233+
Ok(dir) => dir,
234+
Err(_) => find_project_root()
235+
.unwrap_or_else(handle_error)
236+
.join("migrations"),
237+
};
239238

240239
if !dir.exists() {
241240
create_migrations_directory(&dir)?;
@@ -258,11 +257,11 @@ fn create_config_file(matches: &ArgMatches) -> DatabaseResult<()> {
258257
fn run_database_command(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
259258
match matches.subcommand() {
260259
("setup", Some(args)) => {
261-
let migrations_dir = migrations_dir(args);
260+
let migrations_dir = migrations_dir(args).unwrap_or_else(handle_error);
262261
database::setup_database(args, &migrations_dir)?;
263262
}
264263
("reset", Some(args)) => {
265-
let migrations_dir = migrations_dir(args);
264+
let migrations_dir = migrations_dir(args).unwrap_or_else(handle_error);
266265
database::reset_database(args, &migrations_dir)?;
267266
regenerate_schema_if_file_specified(matches)?;
268267
}

0 commit comments

Comments
 (0)