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

Code before:
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            redo_migrations(&mut conn, dir, args)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("list", _) => {
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            list_migrations(&mut conn, dir)?;
        }
        ("pending", _) => {
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            let result = MigrationHarness::has_pending_migration(&mut conn, dir)
                .map_err(crate::errors::Error::MigrationError)?;
            println!("{result:?}");
        }
        ("generate", args) => {
            let dir = migrations_dir(matches)?;
            let mut lock = RwLock::new(migration_folder_lock(dir)?);
            // This blocks until we can get the lock
            // Will throw an error if we receive a termination signal
            let _ = lock.write().map_err(|err| {
                crate::errors::Error::FailedToAcquireMigrationFolderLock(err.to_string())
            })?;

            let migration_name = args
                .get_one::<String>("MIGRATION_NAME")
                .expect("Clap ensure this argument is set");

            let (up_sql, down_sql) = if let Some(diff_schema) = args.get_one::<String>("SCHEMA_RS")
            {
                let config = Config::read(matches)?;
                let mut print_schema =
                    if let Some(schema_key) = args.get_one::<String>("schema-key") {
                        config
                            .print_schema
                            .all_configs
                            .get(schema_key)
                            .ok_or(crate::errors::Error::NoSchemaKeyFound(schema_key.clone()))?
                    } else {
                        config
                            .print_schema
                            .all_configs
                            .get("default")

Review comment (suggestion):
I would rather like to clone the `dir` here and not call `migrations_dir` again in line 140. Also I would prefer to use a better fitting name, like for example `migration_dir` as variable name here.

Code after:
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            redo_migrations(&mut conn, dir, args)?;
            regenerate_schema_if_file_specified(matches)?;
        }
        ("list", _) => {
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            list_migrations(&mut conn, dir)?;
        }
        ("pending", _) => {
            let mut conn = InferConnection::from_matches(matches)?;
            let dir = migrations_dir(matches)?;
            let dir = FileBasedMigrations::from_path(dir)
                .map_err(|e| crate::errors::Error::MigrationError(Box::new(e)))?;
            let result = MigrationHarness::has_pending_migration(&mut conn, dir)
                .map_err(crate::errors::Error::MigrationError)?;
            println!("{result:?}");
        }
        ("generate", args) => {
            let migrations_folder = migrations_dir(matches)?;
            let mut lock = RwLock::new(migration_folder_lock(migrations_folder.clone())?);
            // This blocks until we can get the lock
            // Will throw an error if we receive a termination signal
            let _ = lock.write().map_err(|err| {
                crate::errors::Error::FailedToAcquireMigrationFolderLock(
                    migrations_folder.clone(),
                    err.to_string(),
                )
            })?;

            let migration_name = args
                .get_one::<String>("MIGRATION_NAME")
                .expect("Clap ensure this argument is set");

            let (up_sql, down_sql) = if let Some(diff_schema) = args.get_one::<String>("SCHEMA_RS")
            {
                let config = Config::read(matches)?;
                let mut print_schema =
                    if let Some(schema_key) = args.get_one::<String>("schema-key") {
                        config
                            .print_schema
                            .all_configs
                            .get(schema_key)
                            .ok_or(crate::errors::Error::NoSchemaKeyFound(schema_key.clone()))?
                    } else {
                        config