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.

59 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
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 {
  15. db_name: db_name,
  16. storage: storage,
  17. command_runner: command_runner
  18. }
  19. }
  20. fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
  21. let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
  22. Ok(try!(String::from_utf8(b)))
  23. }
  24. }
  25. impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> fmt::Display for DatabaseDump<'a, N, C, S> {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "Dump MariaDB Database {}", self.db_name.as_ref())
  28. }
  29. }
  30. impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for DatabaseDump<'a, N, C, S> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. let dump_date = try!(self.storage.recent_date());
  33. 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())));
  34. if modified_date.trim_right() == "NULL" { return Ok(false); }
  35. Ok(try!(u64::from_str(modified_date.trim_right())) <= dump_date)
  36. }
  37. fn execute(&self) -> Result<(), Box<Error>> {
  38. self.command_runner.run_successfully("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name.as_ref(), self.storage.write_filename())])
  39. }
  40. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  41. Box::new(SymbolAction::new(runner, self))
  42. }
  43. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  44. Box::new(OwnedSymbolAction::new(runner, *self))
  45. }
  46. }
  47. #[cfg(test)]
  48. mod test {
  49. }