Cluster

Struct Cluster 

Source
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: PathBuf

The data directory of the cluster.

Corresponds to the PGDATA environment variable.

§strategy: Strategy

How to select the PostgreSQL installation to use with this cluster.

Implementations§

Source§

impl Cluster

Source

pub fn new<P: AsRef<Path>, S: Into<Strategy>>( datadir: P, strategy: S, ) -> Result<Self, ClusterError>

Represent a cluster at the given path.

Source

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.

Source

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.

Source

pub fn pidfile(&self) -> PathBuf

Return the path to the PID file used in this cluster.

The PID file does not necessarily exist.

Source

pub fn logfile(&self) -> PathBuf

Return the path to the log file used in this cluster.

The log file does not necessarily exist.

Source

pub fn create(&self) -> Result<State, ClusterError>

Create the cluster if it does not already exist.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn databases(&self) -> Result<Vec<String>, ClusterError>

The names of databases in this cluster.

Source

pub fn createdb(&self, database: &str) -> Result<State, ClusterError>

Create the named database.

Returns Unmodified if the database already exists, otherwise it returns Modified.

Source

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.

Source

pub fn stop(&self) -> Result<State, ClusterError>

Stop the cluster if it’s running.

Source

pub fn destroy(&self) -> Result<State, ClusterError>

Destroy the cluster if it exists, after stopping it.

Trait Implementations§

Source§

impl AsRef<Path> for Cluster

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Cluster

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FacetExclusive for Cluster

Source§

impl FacetFree for Cluster

Source§

type FacetFree<'a> = ClusterFree<'a>

Source§

fn facet_free(&self) -> Self::FacetFree<'_>

Source§

impl FacetShared for Cluster

Source§

impl Subject for Cluster

Cluster can be coordinated.

Source§

type Error = ClusterError

Source§

type Options<'a> = &'a [(Parameter<'a>, Value)]

Source§

fn start(&self, options: Self::Options<'_>) -> Result<State, Self::Error>

Source§

fn stop(&self) -> Result<State, Self::Error>

Source§

fn destroy(&self) -> Result<State, Self::Error>

Source§

fn exists(&self) -> Result<bool, Self::Error>

Source§

fn running(&self) -> Result<bool, Self::Error>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> QuoteExt for T
where T: ?Sized,

Source§

fn push_quoted<'q, Q, S>(&mut self, _q: Q, s: S)
where Q: QuoteInto<T>, S: Into<Quotable<'q>>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,