Command runner args, Paths and AsRef
This commit is contained in:
parent
5d5e9dfcb4
commit
3ccf64fac1
32 changed files with 696 additions and 721 deletions
|
|
@ -1,18 +1,17 @@
|
|||
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>,
|
||||
pub struct PostgreSQLDatabase<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> {
|
||||
name: N,
|
||||
seed_file: S,
|
||||
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 {
|
||||
impl<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> PostgreSQLDatabase<'a, N, S, C> {
|
||||
pub fn new(name: N, seed_file: S, command_runner: &'a C) -> Self {
|
||||
PostgreSQLDatabase {
|
||||
name,
|
||||
seed_file,
|
||||
|
|
@ -23,52 +22,68 @@ impl<'a, C: CommandRunner> PostgreSQLDatabase<'a, C> {
|
|||
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
|
||||
let b = self.command_runner.get_output(
|
||||
"su",
|
||||
&["-", "postgres", "-c", &format!("psql -t -c \"{}\"", sql)],
|
||||
args!["-", "postgres", "-c", format!("psql -t -c \"{}\"", sql)],
|
||||
)?;
|
||||
Ok(String::from_utf8(b)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C: CommandRunner> fmt::Display for PostgreSQLDatabase<'a, C> {
|
||||
impl<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> fmt::Display
|
||||
for PostgreSQLDatabase<'a, N, S, C>
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "PostgreSQL Database {}", self.name)
|
||||
write!(f, "PostgreSQL Database {}", self.name.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C: CommandRunner> Symbol for PostgreSQLDatabase<'a, C> {
|
||||
impl<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> Symbol
|
||||
for PostgreSQLDatabase<'a, N, S, C>
|
||||
{
|
||||
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
||||
Ok(
|
||||
self
|
||||
.run_sql(&format!(
|
||||
"SELECT datname FROM pg_database WHERE datname LIKE '{}'",
|
||||
self.name
|
||||
self.name.as_ref()
|
||||
))?
|
||||
.trim()
|
||||
== self.name,
|
||||
== self.name.as_ref(),
|
||||
)
|
||||
}
|
||||
|
||||
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",
|
||||
&[
|
||||
args![
|
||||
"-",
|
||||
"postgres",
|
||||
"-c",
|
||||
&format!("createdb -E UTF8 -T template0 -O {} {0}", self.name),
|
||||
format!("createuser {}", self.name.as_ref())
|
||||
],
|
||||
)?;
|
||||
self.command_runner.run_successfully(
|
||||
"su",
|
||||
&[
|
||||
args![
|
||||
"-",
|
||||
"postgres",
|
||||
"-c",
|
||||
&format!("psql '{}' < {}", self.name, self.seed_file),
|
||||
format!(
|
||||
"createdb -E UTF8 -T template0 -O {} {0}",
|
||||
self.name.as_ref()
|
||||
),
|
||||
],
|
||||
)?;
|
||||
self.command_runner.run_successfully(
|
||||
"su",
|
||||
args![
|
||||
"-",
|
||||
"postgres",
|
||||
"-c",
|
||||
format!(
|
||||
"psql '{}' < {}",
|
||||
self.name.as_ref(),
|
||||
self.seed_file.as_ref()
|
||||
),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,7 @@ use command_runner::CommandRunner;
|
|||
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: AsRef<str>, C: CommandRunner, S: Storage> {
|
||||
db_name: N,
|
||||
storage: S,
|
||||
command_runner: &'a C,
|
||||
|
|
@ -29,7 +24,7 @@ impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> DatabaseDump<'a, N, C, S>
|
|||
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
|
||||
let b = self
|
||||
.command_runner
|
||||
.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql])?;
|
||||
.get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
|
||||
Ok(String::from_utf8(b)?)
|
||||
}
|
||||
}
|
||||
|
|
@ -43,7 +38,7 @@ impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> fmt::Display for DatabaseD
|
|||
impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for DatabaseDump<'a, N, C, S> {
|
||||
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
||||
let dump_date = 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())));
|
||||
let modified_date = 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);
|
||||
}
|
||||
|
|
@ -53,9 +48,9 @@ impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for DatabaseDump<'a
|
|||
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
||||
self.command_runner.run_successfully(
|
||||
"sh",
|
||||
&[
|
||||
args![
|
||||
"-c",
|
||||
&format!(
|
||||
format!(
|
||||
"mysqldump '{}' > {}",
|
||||
self.db_name.as_ref(),
|
||||
self.storage.write_filename()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue