2021-01-31 02:38:35 +03:00
|
|
|
#![deny(clippy::all)]
|
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
mod config;
|
2021-01-31 02:54:23 +03:00
|
|
|
mod opts;
|
2021-01-31 02:38:35 +03:00
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
use config::Config;
|
2021-01-31 13:41:12 +03:00
|
|
|
use opts::{StructOpt, AppOpt, ApplyOpt};
|
|
|
|
use std::path::{Path, PathBuf};
|
2021-01-31 02:38:35 +03:00
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2021-01-31 02:38:35 +03:00
|
|
|
let opt = AppOpt::from_args();
|
2021-01-31 03:23:43 +03:00
|
|
|
|
2021-01-31 02:38:35 +03:00
|
|
|
match opt {
|
|
|
|
AppOpt::Init => {
|
2021-01-31 03:23:43 +03:00
|
|
|
Config::initialize()?;
|
2021-01-31 13:41:12 +03:00
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 02:38:35 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-31 03:23:43 +03:00
|
|
|
|
|
|
|
Ok(())
|
2021-01-31 02:38:35 +03:00
|
|
|
}
|