Experiment with futures

This commit is contained in:
Adrian Heine 2020-08-16 11:08:22 +02:00
parent 907fbf95db
commit 2d3e3688fa
44 changed files with 2081 additions and 1242 deletions

View file

@ -1,8 +1,10 @@
use crate::command_runner::CommandRunner;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub struct PostgreSQLDatabase<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> {
name: N,
seed_file: S,
@ -18,11 +20,14 @@ impl<'a, N: AsRef<str>, S: AsRef<str>, C: CommandRunner> PostgreSQLDatabase<'a,
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = self.command_runner.get_output(
"su",
args!["-", "postgres", "-c", format!("psql -t -c \"{}\"", sql)],
)?;
async fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = self
.command_runner
.get_output(
"su",
args!["-", "postgres", "-c", format!("psql -t -c \"{}\"", sql)],
)
.await?;
Ok(String::from_utf8(b)?)
}
}
@ -35,54 +40,65 @@ impl<N: AsRef<str>, S: AsRef<str>, C: CommandRunner> fmt::Display
}
}
#[async_trait(?Send)]
impl<N: AsRef<str>, S: AsRef<str>, C: CommandRunner> Symbol for PostgreSQLDatabase<'_, N, S, C> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
Ok(
self
.run_sql(&format!(
"SELECT datname FROM pg_database WHERE datname LIKE '{}'",
self.name.as_ref()
))?
))
.await?
.trim()
== self.name.as_ref(),
)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.command_runner.run_successfully(
"su",
args![
"-",
"postgres",
"-c",
format!("createuser {}", self.name.as_ref())
],
)?;
self.command_runner.run_successfully(
"su",
args![
"-",
"postgres",
"-c",
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()
),
],
)
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.command_runner
.run_successfully(
"su",
args![
"-",
"postgres",
"-c",
format!("createuser {}", self.name.as_ref())
],
)
.await?;
self
.command_runner
.run_successfully(
"su",
args![
"-",
"postgres",
"-c",
format!(
"createdb -E UTF8 -T template0 -O {} {0}",
self.name.as_ref()
),
],
)
.await?;
self
.command_runner
.run_successfully(
"su",
args![
"-",
"postgres",
"-c",
format!(
"psql '{}' < {}",
self.name.as_ref(),
self.seed_file.as_ref()
),
],
)
.await
}
}