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.
53 lines
1.9 KiB
53 lines
1.9 KiB
use std::borrow::Cow;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct PostgreSQLDatabase<'a, C: 'a + CommandRunner> {
|
|
name: Cow<'a, str>,
|
|
seed_file: Cow<'a, str>,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> PostgreSQLDatabase<'a, C> {
|
|
pub fn new(name: Cow<'a, str>, seed_file: Cow<'a, str>, command_runner: &'a C) -> Self {
|
|
PostgreSQLDatabase { name, seed_file, command_runner }
|
|
}
|
|
|
|
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
|
|
let b = try!(self.command_runner.get_output("su", &["-", "postgres", "-c", &format!("psql -t -c \"{}\"", sql)]));
|
|
Ok(try!(String::from_utf8(b)))
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for PostgreSQLDatabase<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "PostgreSQL Database {}", self.name)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> Symbol for PostgreSQLDatabase<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
Ok(try!(self.run_sql(&format!("SELECT datname FROM pg_database WHERE datname LIKE '{}'", self.name))).trim() == self.name)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("createuser {}", self.name)])?;
|
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("createdb -E UTF8 -T template0 -O {} {0}", self.name)])?;
|
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("psql '{}' < {}", self.name, self.seed_file)])
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
}
|