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.

57 lines
1.3 KiB

7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::storage::Storage;
  3. use crate::symbols::Symbol;
  4. use std::error::Error;
  5. #[derive(Debug)]
  6. pub struct Database<'a, D, S, C> {
  7. db_name: D,
  8. seed_file: S,
  9. command_runner: &'a C,
  10. }
  11. impl<'a, D, S, C: CommandRunner> Database<'a, D, S, C> {
  12. pub fn new(db_name: D, seed_file: S, command_runner: &'a C) -> Self {
  13. Self {
  14. db_name,
  15. seed_file,
  16. command_runner,
  17. }
  18. }
  19. fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
  20. let b = self
  21. .command_runner
  22. .get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
  23. Ok(String::from_utf8(b)?)
  24. }
  25. }
  26. impl<D: AsRef<str>, S: Storage, C: CommandRunner> Symbol for Database<'_, D, S, C> {
  27. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  28. Ok(
  29. self
  30. .run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name.as_ref()))?
  31. .trim_end()
  32. == self.db_name.as_ref(),
  33. )
  34. }
  35. fn execute(&self) -> Result<(), Box<dyn Error>> {
  36. self.run_sql(&format!("CREATE DATABASE {}", self.db_name.as_ref()))?;
  37. self.command_runner.run_successfully(
  38. "sh",
  39. args![
  40. "-c",
  41. format!(
  42. "mariadb '{}' < {}",
  43. self.db_name.as_ref(),
  44. self.seed_file.read_filename()?.to_str().unwrap()
  45. ),
  46. ],
  47. )
  48. }
  49. }
  50. #[cfg(test)]
  51. mod test {}