A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::str::FromStr;
use command_runner::CommandRunner;
use symbols::Symbol;
use storage::Storage;
pub struct DatabaseDump<'a, S> where S: Storage {
db_name: Cow<'a, str>,
storage: S,
command_runner: &'a CommandRunner
}
impl<'a, S> DatabaseDump<'a, S> where S: Storage {
pub fn new(db_name: Cow<'a, str>, storage: S, command_runner: &'a CommandRunner) -> Self {
DatabaseDump {
db_name: db_name,
storage: storage,
command_runner: command_runner
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-B", "-e", sql]));
if output.status.code() != Some(0) {
return Err(try!(String::from_utf8(output.stderr)).into());
}
Ok(try!(String::from_utf8(output.stdout)))
}
}
impl<'a, S> fmt::Display for DatabaseDump<'a, S> where S: Storage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MariaDB Database {}", self.db_name)
}
}
impl<'a, S> Symbol for DatabaseDump<'a, S> where S: Storage {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let dump_date = try!(self.storage.recent_date());
let modified_date = try!(self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name)));
Ok(try!(u64::from_str(modified_date.trim_right())) <= dump_date)
}
fn execute(&self) -> Result<(), Box<Error>> {
try!(self.command_runner.run_with_args("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name, self.storage.write_filename())]));
Ok(())
}
}
#[cfg(test)]
mod test {
}