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

7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::str::FromStr;
  5. use command_runner::CommandRunner;
  6. use symbols::Symbol;
  7. use storage::Storage;
  8. pub struct DatabaseDump<'a, S> where S: Storage {
  9. db_name: Cow<'a, str>,
  10. storage: S,
  11. command_runner: &'a CommandRunner
  12. }
  13. impl<'a, S> DatabaseDump<'a, S> where S: Storage {
  14. pub fn new(db_name: Cow<'a, str>, storage: S, command_runner: &'a CommandRunner) -> Self {
  15. DatabaseDump {
  16. db_name: db_name,
  17. storage: storage,
  18. command_runner: command_runner
  19. }
  20. }
  21. fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
  22. let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-B", "-e", sql]));
  23. if output.status.code() != Some(0) {
  24. return Err(try!(String::from_utf8(output.stderr)).into());
  25. }
  26. Ok(try!(String::from_utf8(output.stdout)))
  27. }
  28. }
  29. impl<'a, S> fmt::Display for DatabaseDump<'a, S> where S: Storage {
  30. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  31. write!(f, "MariaDB Database {}", self.db_name)
  32. }
  33. }
  34. impl<'a, S> Symbol for DatabaseDump<'a, S> where S: Storage {
  35. fn target_reached(&self) -> Result<bool, Box<Error>> {
  36. let dump_date = try!(self.storage.recent_date());
  37. let modified_date = try!(self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name)));
  38. Ok(try!(u64::from_str(modified_date.trim_right())) <= dump_date)
  39. }
  40. fn execute(&self) -> Result<(), Box<Error>> {
  41. try!(self.command_runner.run_with_args("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name, self.storage.write_filename())]));
  42. Ok(())
  43. }
  44. }
  45. #[cfg(test)]
  46. mod test {
  47. }