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.

56 lines
1.5 KiB

  1. use std::error::Error;
  2. use std::str::FromStr;
  3. use crate::command_runner::CommandRunner;
  4. use crate::storage::Storage;
  5. use crate::symbols::Symbol;
  6. #[derive(Debug)]
  7. pub struct Dump<'a, N, C, S> {
  8. db_name: N,
  9. storage: S,
  10. command_runner: &'a C,
  11. }
  12. impl<'a, N, C: CommandRunner, S> Dump<'a, N, C, S> {
  13. pub fn new(db_name: N, storage: S, command_runner: &'a C) -> Self {
  14. Self {
  15. db_name,
  16. storage,
  17. command_runner,
  18. }
  19. }
  20. fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
  21. let b = self
  22. .command_runner
  23. .get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
  24. Ok(String::from_utf8(b)?)
  25. }
  26. }
  27. impl<N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for Dump<'_, N, C, S> {
  28. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  29. let dump_date = self.storage.recent_date()?;
  30. let _modified_date = self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name.as_ref()))?;
  31. let modified_date = _modified_date.trim_end();
  32. Ok(modified_date != "NULL" && u64::from_str(modified_date)? <= dump_date)
  33. }
  34. fn execute(&self) -> Result<(), Box<dyn Error>> {
  35. self.command_runner.run_successfully(
  36. "sh",
  37. args![
  38. "-c",
  39. format!(
  40. "mysqldump '{}' > {}",
  41. self.db_name.as_ref(),
  42. self.storage.write_filename().to_str().unwrap()
  43. ),
  44. ],
  45. )
  46. }
  47. }
  48. #[cfg(test)]
  49. mod test {}