Add source param to command line

This commit is contained in:
Stuart Hinson 2020-08-17 20:46:18 -07:00 committed by Ryan Leckey
parent 9d71a7f372
commit a4729cdcc8
3 changed files with 21 additions and 10 deletions

View File

@ -31,9 +31,9 @@ hint: This command only works in the manifest directory of a Cargo package."#
match opt.command {
Command::Migrate(migrate) => match migrate.command {
MigrateCommand::Add { description } => migrate::add(&description)?,
MigrateCommand::Run => migrate::run(&database_url).await?,
MigrateCommand::Info => migrate::info(&database_url).await?,
MigrateCommand::Add { description } => migrate::add(&migrate.source, &description)?,
MigrateCommand::Run => migrate::run(&migrate.source, &database_url).await?,
MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?,
},
Command::Database(database) => match database.command {

View File

@ -8,11 +8,12 @@ use std::path::Path;
const MIGRATION_FOLDER: &'static str = "migrations";
pub fn add(description: &str) -> anyhow::Result<()> {
pub fn add(source_path: &Option<String>, 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_folder).context("Unable to create migrations directory")?;
let dt = Utc::now();
let mut file_name = dt.format("%Y%m%d%H%M%S").to_string();
@ -21,7 +22,7 @@ pub fn add(description: &str) -> anyhow::Result<()> {
file_name.push_str(".sql");
let mut path = PathBuf::new();
path.push(MIGRATION_FOLDER);
path.push(migration_folder);
path.push(&file_name);
println!("Creating {}", style(path.display()).cyan());
@ -33,8 +34,9 @@ pub fn add(description: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn info(uri: &str) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
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?;
let mut conn = AnyConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
@ -57,8 +59,9 @@ pub async fn info(uri: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn run(uri: &str) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
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?;
let mut conn = AnyConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
@ -87,3 +90,7 @@ pub async fn run(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

@ -71,6 +71,10 @@ pub enum DatabaseCommand {
/// Group of commands for creating and running migrations.
#[derive(Clap, Debug)]
pub struct MigrateOpt {
/// Path to folder containing migrations. Defaults to 'migrations'
#[clap(long)]
pub source: Option<String>,
#[clap(subcommand)]
pub command: MigrateCommand,
}