Cargo format

This commit is contained in:
Adrian Heine 2019-09-12 22:59:32 +02:00
parent 9bab810b91
commit 8c0224e983
44 changed files with 1784 additions and 611 deletions

View file

@ -8,16 +8,23 @@ 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
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 }
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)]));
let b = try!(self.command_runner.get_output(
"su",
&["-", "postgres", "-c", &format!("psql -t -c \"{}\"", sql)]
));
Ok(try!(String::from_utf8(b)))
}
}
@ -30,24 +37,52 @@ impl<'a, C: CommandRunner> fmt::Display for PostgreSQLDatabase<'a, C> {
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)
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)])
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 {
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 {
}
mod test {}

View file

@ -3,22 +3,33 @@ use std::fmt;
use std::str::FromStr;
use command_runner::CommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
use storage::Storage;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct DatabaseDump<'a, N, C, S> where N: 'a + AsRef<str>, C: 'a + CommandRunner, S: Storage {
pub struct DatabaseDump<'a, N, C, S>
where
N: 'a + AsRef<str>,
C: 'a + CommandRunner,
S: Storage,
{
db_name: N,
storage: S,
command_runner: &'a C
command_runner: &'a C,
}
impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> DatabaseDump<'a, N, C, S> {
pub fn new(db_name: N, storage: S, command_runner: &'a C) -> Self {
DatabaseDump { db_name, storage, command_runner }
DatabaseDump {
db_name,
storage,
command_runner,
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
let b = try!(self
.command_runner
.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
Ok(try!(String::from_utf8(b)))
}
}
@ -33,23 +44,37 @@ impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for DatabaseDump<'a
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let dump_date = try!(self.storage.recent_date());
let modified_date = try!(self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name.as_ref())));
if modified_date.trim_end() == "NULL" { return Ok(false); }
if modified_date.trim_end() == "NULL" {
return Ok(false);
}
Ok(try!(u64::from_str(modified_date.trim_end())) <= dump_date)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.command_runner.run_successfully("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name.as_ref(), self.storage.write_filename())])
self.command_runner.run_successfully(
"sh",
&[
"-c",
&format!(
"mysqldump '{}' > {}",
self.db_name.as_ref(),
self.storage.write_filename()
),
],
)
}
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 {
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 {
}
mod test {}