PgTempDB

Struct PgTempDB 

Source
pub struct PgTempDB { /* private fields */ }
Expand description

A struct representing a handle to a local PostgreSQL server that is currently running. Upon drop or calling shutdown, the server is shut down and the directory its data is stored in is deleted. See builder struct PgTempDBBuilder for options and settings.

Implementations§

Source§

impl PgTempDB

Source

pub fn from_builder(builder: PgTempDBBuilder) -> PgTempDB

Start a PgTempDB with the parameters configured from a PgTempDBBuilder

Source

pub fn builder() -> PgTempDBBuilder

Creates a builder that can be used to configure the details of the temporary PostgreSQL server

Source

pub fn new() -> PgTempDB

Creates a new PgTempDB with default configuration and starts a PostgreSQL server.

Source

pub async fn async_new() -> PgTempDB

Creates a new PgTempDB with default configuration and starts a PostgreSQL server in an async context.

Source

pub fn dump_database(&self, path: impl AsRef<Path>)

Use pg_dump to dump the database to the provided path upon drop or Self::shutdown.

Source

pub fn load_database(&self, path: impl AsRef<Path>)

Use psql to load the database from the provided dump file. See Self::dump_database.

Source

pub fn shutdown(self)

Send a signal to the database to shutdown the server, then wait for the process to exit. Equivalent to calling drop on this struct.

We send SIGINT to the postgres process to initiate a fast shutdown (https://www.postgresql.org/docs/current/server-shutdown.html), which causes all transactions to be aborted and connections to be terminated.

NOTE: This is currently a blocking function. It sends SIGINT to the postgres server, waits for the process to exit, and also does IO to remove the temp directory.

Source

pub fn data_dir(&self) -> PathBuf

Returns the path to the data directory being used by this databaset.

Source

pub fn db_user(&self) -> &str

Returns the database username used when connecting to the postgres server.

Source

pub fn db_pass(&self) -> &str

Returns the database password used when connecting to the postgres server.

Source

pub fn db_port(&self) -> u16

Returns the port the postgres server is running on.

Source

pub fn db_name(&self) -> &str

Returns the the name of the database created.

Source

pub fn connection_string(&self) -> String

Returns a connection string that can be passed to a libpq connection function.

Example output: host=localhost port=15432 user=pgtemp password=pgtemppw-9485 dbname=pgtempdb-324

Source

pub fn connection_uri(&self) -> String

Returns a generic connection URI that can be passed to most SQL libraries’ connect methods.

Example output: postgresql://pgtemp:pgtemppw-9485@localhost:15432/pgtempdb-324

Trait Implementations§

Source§

impl Debug for PgTempDB

Source§

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

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

impl Drop for PgTempDB

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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, 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, 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.