use std::error::Error; use std::fmt; use std::path::Path; use crate::command_runner::CommandRunner; use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct MariaDBDatabase<'a, D: AsRef, S: AsRef, C: CommandRunner> { db_name: D, seed_file: S, command_runner: &'a C, } impl<'a, D: AsRef, S: AsRef, C: CommandRunner> MariaDBDatabase<'a, D, S, C> { pub fn new(db_name: D, seed_file: S, command_runner: &'a C) -> Self { MariaDBDatabase { db_name, seed_file, command_runner, } } fn run_sql(&self, sql: &str) -> Result> { let b = self .command_runner .get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?; Ok(String::from_utf8(b)?) } } impl<'a, D: AsRef, S: AsRef, C: CommandRunner> fmt::Display for MariaDBDatabase<'a, D, S, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "MariaDB Database {}", self.db_name.as_ref()) } } impl<'a, D: AsRef, S: AsRef, C: CommandRunner> Symbol for MariaDBDatabase<'a, D, S, C> { fn target_reached(&self) -> Result> { Ok( self .run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name.as_ref()))? .trim_end() == self.db_name.as_ref(), ) } fn execute(&self) -> Result<(), Box> { self.run_sql(&format!("CREATE DATABASE {}", self.db_name.as_ref()))?; self.command_runner.run_successfully( "sh", args![ "-c", format!( "mariadb '{}' < {}", self.db_name.as_ref(), self.seed_file.as_ref().to_str().unwrap() ), ], ) } fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'b>(self: Box, runner: &'b dyn SymbolRunner) -> Box where Self: 'b, { Box::new(OwnedSymbolAction::new(runner, *self)) } } #[cfg(test)] mod test {}