feat(cli): add apply command
This command provide a possibility to run sql file by name in root migra directory
This commit is contained in:
parent
26ee5bf26e
commit
7fa65c8197
2 changed files with 33 additions and 5 deletions
|
@ -4,18 +4,38 @@ mod config;
|
||||||
mod opts;
|
mod opts;
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use opts::{AppOpt, StructOpt};
|
use opts::{StructOpt, AppOpt, ApplyOpt};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let opt = AppOpt::from_args();
|
let opt = AppOpt::from_args();
|
||||||
dbg!(&opt);
|
|
||||||
|
|
||||||
let config = Config::read();
|
|
||||||
dbg!(&config);
|
|
||||||
|
|
||||||
match opt {
|
match opt {
|
||||||
AppOpt::Init => {
|
AppOpt::Init => {
|
||||||
Config::initialize()?;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,4 +3,12 @@ pub use structopt::StructOpt;
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
pub(crate) enum AppOpt {
|
pub(crate) enum AppOpt {
|
||||||
Init,
|
Init,
|
||||||
|
Apply(ApplyOpt),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, StructOpt)]
|
||||||
|
pub(crate) struct ApplyOpt {
|
||||||
|
#[structopt(parse(from_str))]
|
||||||
|
pub file_name: String
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue