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

use crate::command_runner::CommandRunner;
use crate::storage::Storage;
use crate::symbols::Symbol;
use std::error::Error;
#[derive(Debug)]
pub struct Database<'a, D, S, C> {
db_name: D,
seed_file: S,
command_runner: &'a C,
}
impl<'a, D, S, C: CommandRunner> Database<'a, D, S, C> {
pub fn new(db_name: D, seed_file: S, command_runner: &'a C) -> Self {
Self {
db_name,
seed_file,
command_runner,
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = self
.command_runner
.get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
Ok(String::from_utf8(b)?)
}
}
impl<D: AsRef<str>, S: Storage, C: CommandRunner> Symbol for Database<'_, D, S, C> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
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<dyn Error>> {
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.read_filename()?.to_str().unwrap()
),
],
)
}
}
#[cfg(test)]
mod test {}