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.

66 lines
1.5 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
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 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 async_trait::async_trait;
  5. use std::error::Error;
  6. #[derive(Debug)]
  7. pub struct Database<'a, D, S, C> {
  8. db_name: D,
  9. seed_file: S,
  10. command_runner: &'a C,
  11. }
  12. impl<'a, D, S, C: CommandRunner> Database<'a, D, S, C> {
  13. pub fn new(db_name: D, seed_file: S, command_runner: &'a C) -> Self {
  14. Self {
  15. db_name,
  16. seed_file,
  17. command_runner,
  18. }
  19. }
  20. async 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. .await?;
  25. Ok(String::from_utf8(b)?)
  26. }
  27. }
  28. #[async_trait(?Send)]
  29. impl<D: AsRef<str>, S: Storage, C: CommandRunner> Symbol for Database<'_, D, S, C> {
  30. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  31. Ok(
  32. self
  33. .run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name.as_ref()))
  34. .await?
  35. .trim_end()
  36. == self.db_name.as_ref(),
  37. )
  38. }
  39. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  40. self
  41. .run_sql(&format!("CREATE DATABASE {}", self.db_name.as_ref()))
  42. .await?;
  43. self
  44. .command_runner
  45. .run_successfully(
  46. "sh",
  47. args![
  48. "-c",
  49. format!(
  50. "mariadb '{}' < {}",
  51. self.db_name.as_ref(),
  52. self.seed_file.read_filename()?.to_str().unwrap()
  53. ),
  54. ],
  55. )
  56. .await
  57. }
  58. }
  59. #[cfg(test)]
  60. mod test {}