From 7fa65c819769341b1247f846b375c0b38ed9d9d8 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 31 Jan 2021 13:41:12 +0300 Subject: [PATCH] feat(cli): add apply command This command provide a possibility to run sql file by name in root migra directory --- migra-cli/src/main.rs | 30 +++++++++++++++++++++++++----- migra-cli/src/opts.rs | 8 ++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index e1131e6..4b0da55 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -4,18 +4,38 @@ mod config; mod opts; use config::Config; -use opts::{AppOpt, StructOpt}; +use opts::{StructOpt, AppOpt, ApplyOpt}; +use std::path::{Path, PathBuf}; fn main() -> Result<(), Box> { let opt = AppOpt::from_args(); - dbg!(&opt); - - let config = Config::read(); - dbg!(&config); match opt { AppOpt::Init => { Config::initialize()?; + }, + AppOpt::Apply(ApplyOpt { file_name }) => { + let config = Config::read(); + + let mut client = migra_core::database::connect(&config.database.connection)?; + + let file_name = Path::new(&file_name); + let mut filepath = PathBuf::from(&config.directory); + filepath.push(file_name); + if file_name.extension().is_none() { + filepath.set_extension("sql"); + } + + let content = std::fs::read_to_string(filepath)?; + + match migra_core::database::apply_sql(&mut client, &content) { + Ok(_) => { + println!("File was applied successfully") + } + Err(err) => { + println!("{}", err) + } + } } } diff --git a/migra-cli/src/opts.rs b/migra-cli/src/opts.rs index 1d3fdfe..fb419ad 100644 --- a/migra-cli/src/opts.rs +++ b/migra-cli/src/opts.rs @@ -3,4 +3,12 @@ pub use structopt::StructOpt; #[derive(Debug, StructOpt)] pub(crate) enum AppOpt { Init, + Apply(ApplyOpt), +} + + +#[derive(Debug, StructOpt)] +pub(crate) struct ApplyOpt { + #[structopt(parse(from_str))] + pub file_name: String }