use crate::{Result, Settings, Status};
use std::sync::LazyLock;
use tokio::runtime::Runtime;
static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| Runtime::new().unwrap());
#[derive(Clone, Debug, Default)]
pub struct PostgreSQL {
inner: crate::postgresql::PostgreSQL,
}
impl PostgreSQL {
#[must_use]
pub fn new(settings: Settings) -> Self {
Self {
inner: crate::postgresql::PostgreSQL::new(settings),
}
}
#[must_use]
pub fn status(&self) -> Status {
self.inner.status()
}
#[must_use]
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(&self) -> Result<()> {
RUNTIME
.handle()
.block_on(async move { self.inner.stop().await })
}
pub fn create_database<S>(&self, database_name: S) -> Result<()>
where
S: AsRef<str> + std::fmt::Debug,
{
RUNTIME
.handle()
.block_on(async move { self.inner.create_database(database_name).await })
}
pub fn database_exists<S>(&self, database_name: S) -> Result<bool>
where
S: AsRef<str> + std::fmt::Debug,
{
RUNTIME
.handle()
.block_on(async move { self.inner.database_exists(database_name).await })
}
pub fn drop_database<S>(&self, database_name: S) -> Result<()>
where
S: AsRef<str> + std::fmt::Debug,
{
RUNTIME
.handle()
.block_on(async move { self.inner.drop_database(database_name).await })
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::VersionReq;
#[test]
fn test_postgresql() -> Result<()> {
let version = VersionReq::parse("=16.4.0")?;
let settings = Settings {
version,
..Settings::default()
};
let postgresql = PostgreSQL::new(settings);
let initial_statuses = [Status::NotInstalled, Status::Installed, Status::Stopped];
assert!(initial_statuses.contains(&postgresql.status()));
Ok(())
}
}