2021-02-13 00:39:39 +03:00
|
|
|
use crate::config::Config;
|
2021-02-15 13:06:09 +03:00
|
|
|
use crate::migration::{DatabaseMigrationManager, MigrationManager};
|
2021-02-13 00:39:39 +03:00
|
|
|
use crate::opts::ApplyCommandOpt;
|
|
|
|
use crate::StdResult;
|
2021-02-13 23:44:41 +03:00
|
|
|
use std::convert::TryFrom;
|
2021-02-13 00:39:39 +03:00
|
|
|
|
|
|
|
pub(crate) fn apply_sql(config: Config, opts: ApplyCommandOpt) -> StdResult<()> {
|
2021-02-18 12:29:09 +03:00
|
|
|
let mut manager = MigrationManager::try_from(&config)?;
|
2021-02-13 00:39:39 +03:00
|
|
|
|
2021-02-22 23:13:50 +03:00
|
|
|
let file_path = {
|
|
|
|
let mut file_path = config.directory_path().join(opts.file_name);
|
|
|
|
if file_path.extension().is_none() {
|
|
|
|
file_path.set_extension("sql");
|
|
|
|
}
|
|
|
|
file_path
|
|
|
|
};
|
2021-02-13 00:39:39 +03:00
|
|
|
|
|
|
|
let content = std::fs::read_to_string(file_path)?;
|
|
|
|
|
2021-02-15 13:06:09 +03:00
|
|
|
match manager.apply_sql(&content) {
|
2021-02-13 00:39:39 +03:00
|
|
|
Ok(_) => {
|
|
|
|
println!("File was applied successfully");
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|