use crate::{Result, Settings, Status};
use lazy_static::lazy_static;
use postgresql_archive::Version;
use tokio::runtime::Runtime;
lazy_static! {
static ref RUNTIME: Runtime = Runtime::new().unwrap();
}
#[derive(Clone, Debug, Default)]
pub struct PostgreSQL {
inner: crate::postgresql::PostgreSQL,
}
impl PostgreSQL {
pub fn new(version: Version, settings: Settings) -> Self {
Self {
inner: crate::postgresql::PostgreSQL::new(version, settings),
}
}
pub fn status(&self) -> Status {
self.inner.status()
}
pub fn version(&self) -> &Version {
self.inner.version()
}
pub fn settings(&self) -> &Settings {
self.inner.settings()
}
pub fn setup(&mut self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.setup().await })
}
pub fn start(&mut self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.start().await })
}
pub fn stop(&mut self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.stop().await })
}
pub fn create_database<S: AsRef<str>>(&mut self, database_name: S) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.create_database(database_name).await })
}
pub fn database_exists<S: AsRef<str>>(&mut self, database_name: S) -> Result<bool> {
RUNTIME
.handle()
.block_on(async move { self.inner.database_exists(database_name).await })
}
pub fn drop_database<S: AsRef<str>>(&mut self, database_name: S) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.drop_database(database_name).await })
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_postgresql() {
let version = Version::new(16, Some(2), Some(0));
let postgresql = PostgreSQL::new(version, Settings::default());
let initial_statuses = [Status::NotInstalled, Status::Installed, Status::Stopped];
assert!(initial_statuses.contains(&postgresql.status()));
assert_eq!(postgresql.version(), &version);
}
}