use default_value for migrations source

This commit is contained in:
Stuart Hinson 2020-08-18 06:34:46 -07:00 committed by Ryan Leckey
parent a4729cdcc8
commit c91eb2b04a
2 changed files with 9 additions and 18 deletions

View File

@ -6,14 +6,11 @@ use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
const MIGRATION_FOLDER: &'static str = "migrations";
pub fn add(source_path: &Option<String>, description: &str) -> anyhow::Result<()> {
pub fn add(migration_source: &str, description: &str) -> anyhow::Result<()> {
use chrono::prelude::*;
use std::path::PathBuf;
let migration_folder = migration_path(source_path);
fs::create_dir_all(migration_folder).context("Unable to create migrations directory")?;
fs::create_dir_all(migration_source).context("Unable to create migrations directory")?;
let dt = Utc::now();
let mut file_name = dt.format("%Y%m%d%H%M%S").to_string();
@ -22,7 +19,7 @@ pub fn add(source_path: &Option<String>, description: &str) -> anyhow::Result<()
file_name.push_str(".sql");
let mut path = PathBuf::new();
path.push(migration_folder);
path.push(migration_source);
path.push(&file_name);
println!("Creating {}", style(path.display()).cyan());
@ -34,9 +31,8 @@ pub fn add(source_path: &Option<String>, description: &str) -> anyhow::Result<()
Ok(())
}
pub async fn info(source_path: &Option<String>, uri: &str) -> anyhow::Result<()> {
let migration_folder = migration_path(source_path);
let migrator = Migrator::new(Path::new(migration_folder)).await?;
pub async fn info(migration_source: &str, uri: &str) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(migration_source)).await?;
let mut conn = AnyConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
@ -59,9 +55,8 @@ pub async fn info(source_path: &Option<String>, uri: &str) -> anyhow::Result<()>
Ok(())
}
pub async fn run(source_path: &Option<String>, uri: &str) -> anyhow::Result<()> {
let migration_folder = migration_path(source_path);
let migrator = Migrator::new(Path::new(migration_folder)).await?;
pub async fn run(migration_source: &str, uri: &str) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(migration_source)).await?;
let mut conn = AnyConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
@ -90,7 +85,3 @@ pub async fn run(source_path: &Option<String>, uri: &str) -> anyhow::Result<()>
Ok(())
}
fn migration_path<'a>(source_path: &'a Option<String>) -> &'a str {
source_path.as_deref().unwrap_or(MIGRATION_FOLDER)
}

View File

@ -72,8 +72,8 @@ pub enum DatabaseCommand {
#[derive(Clap, Debug)]
pub struct MigrateOpt {
/// Path to folder containing migrations. Defaults to 'migrations'
#[clap(long)]
pub source: Option<String>,
#[clap(long, default_value = "migrations")]
pub source: String,
#[clap(subcommand)]
pub command: MigrateCommand,