pub struct Cluster {
pub datadir: PathBuf,
pub strategy: Strategy,
}Expand description
Representation of a PostgreSQL cluster.
The cluster may not yet exist on disk. It may exist but be stopped, or it
may be running. The methods here can be used to create, start, introspect,
stop, and destroy the cluster. There’s no protection against concurrent
changes to the cluster made by other processes, but the functions in the
coordinate module may help.
Fields§
§datadir: PathBufThe data directory of the cluster.
Corresponds to the PGDATA environment variable.
strategy: StrategyHow to select the PostgreSQL installation to use with this cluster.
Implementations§
Source§impl Cluster
impl Cluster
Sourcepub fn new<P: AsRef<Path>, S: Into<Strategy>>(
datadir: P,
strategy: S,
) -> Result<Self, ClusterError>
pub fn new<P: AsRef<Path>, S: Into<Strategy>>( datadir: P, strategy: S, ) -> Result<Self, ClusterError>
Represent a cluster at the given path.
Sourcepub fn running(&self) -> Result<bool, ClusterError>
pub fn running(&self) -> Result<bool, ClusterError>
Check if this cluster is running.
Convenient call-through to status; only returns
true when the cluster is definitely running.
Sourcepub fn status(&self) -> Result<ClusterStatus, ClusterError>
pub fn status(&self) -> Result<ClusterStatus, ClusterError>
Check the status of this cluster.
Tries to distinguish carefully between “definitely running”, “definitely
not running”, “missing”, and “don’t know”. The latter results in
ClusterError.
Sourcepub fn pidfile(&self) -> PathBuf
pub fn pidfile(&self) -> PathBuf
Return the path to the PID file used in this cluster.
The PID file does not necessarily exist.
Sourcepub fn logfile(&self) -> PathBuf
pub fn logfile(&self) -> PathBuf
Return the path to the log file used in this cluster.
The log file does not necessarily exist.
Sourcepub fn create(&self) -> Result<State, ClusterError>
pub fn create(&self) -> Result<State, ClusterError>
Create the cluster if it does not already exist.
Sourcepub fn start(
&self,
options: &[(Parameter<'_>, Value)],
) -> Result<State, ClusterError>
pub fn start( &self, options: &[(Parameter<'_>, Value)], ) -> Result<State, ClusterError>
Start the cluster if it’s not already running, with the given options.
Returns State::Unmodified if the cluster is already running, meaning
the given options were NOT applied.
Sourcepub fn pool(&self, database: Option<&str>) -> Result<PgPool, ClusterError>
pub fn pool(&self, database: Option<&str>) -> Result<PgPool, ClusterError>
Create a lazy SQLx pool for this cluster.
Although it’s possible to call this anywhere, at runtime it needs a Tokio context to work, e.g.:
let tokio = tokio::runtime::Runtime::new()?;
let rows = tokio.block_on(async {
let pool = cluster.pool(None)?;
let rows = sqlx::query("SELECT 1").fetch_all(&pool).await?;
Ok::<_, ClusterError>(rows)
})?;When the database is not specified, connects to DATABASE_POSTGRES.
Sourcepub fn shell(&self, database: Option<&str>) -> Result<ExitStatus, ClusterError>
pub fn shell(&self, database: Option<&str>) -> Result<ExitStatus, ClusterError>
Run psql against this cluster, in the given database.
When the database is not specified, connects to DATABASE_POSTGRES.
Sourcepub fn exec<T: AsRef<OsStr>>(
&self,
database: Option<&str>,
command: T,
args: &[T],
) -> Result<ExitStatus, ClusterError>
pub fn exec<T: AsRef<OsStr>>( &self, database: Option<&str>, command: T, args: &[T], ) -> Result<ExitStatus, ClusterError>
Run the given command against this cluster.
The command is run with the PGDATA, PGHOST, and PGDATABASE
environment variables set appropriately.
When the database is not specified, uses DATABASE_POSTGRES.
Sourcepub fn databases(&self) -> Result<Vec<String>, ClusterError>
pub fn databases(&self) -> Result<Vec<String>, ClusterError>
The names of databases in this cluster.
Sourcepub fn createdb(&self, database: &str) -> Result<State, ClusterError>
pub fn createdb(&self, database: &str) -> Result<State, ClusterError>
Create the named database.
Returns Unmodified if the database already exists, otherwise it
returns Modified.
Sourcepub fn dropdb(&self, database: &str) -> Result<State, ClusterError>
pub fn dropdb(&self, database: &str) -> Result<State, ClusterError>
Drop the named database.
Returns Unmodified if the database does not exist, otherwise it
returns Modified.
Sourcepub fn stop(&self) -> Result<State, ClusterError>
pub fn stop(&self) -> Result<State, ClusterError>
Stop the cluster if it’s running.
Sourcepub fn destroy(&self) -> Result<State, ClusterError>
pub fn destroy(&self) -> Result<State, ClusterError>
Destroy the cluster if it exists, after stopping it.
Trait Implementations§
Source§impl FacetExclusive for Cluster
impl FacetExclusive for Cluster
type FacetExclusive<'a> = ClusterExclusive<'a>
fn facet_exclusive(&self) -> Self::FacetExclusive<'_>
Source§impl FacetFree for Cluster
impl FacetFree for Cluster
type FacetFree<'a> = ClusterFree<'a>
fn facet_free(&self) -> Self::FacetFree<'_>
Source§impl Subject for Cluster
Cluster can be coordinated.
impl Subject for Cluster
Cluster can be coordinated.
type Error = ClusterError
type Options<'a> = &'a [(Parameter<'a>, Value)]
fn start(&self, options: Self::Options<'_>) -> Result<State, Self::Error>
fn stop(&self) -> Result<State, Self::Error>
fn destroy(&self) -> Result<State, Self::Error>
fn exists(&self) -> Result<bool, Self::Error>
fn running(&self) -> Result<bool, Self::Error>
Auto Trait Implementations§
impl Freeze for Cluster
impl RefUnwindSafe for Cluster
impl Send for Cluster
impl Sync for Cluster
impl Unpin for Cluster
impl !UnwindSafe for Cluster
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more