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
2.0 KiB

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