Skip to main content

PgBackend

Struct PgBackend 

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

A PostgreSQL database behind an sqlx pool.

Implementations§

Source§

impl PgBackend

Source

pub async fn migrate( &self, migrations: &[Migration], ) -> Result<MigrateReport, MigrateError>

Apply migrations in version order, skipping those already applied, in the ledger _sqlx_migrations of the connection’s current schema. See Migrates::migrate.

Source

pub async fn verify_fresh( &self, migrations: &[Migration], ) -> Result<MigrateReport, MigrateError>

Prove that migrations install on an empty schema, in order, and leave nothing behind.

A scratch schema with a unique name is created, the migrations run in it on a pool of one connection whose search_path is that schema (so the ledger is the scratch schema’s own), and the schema is dropped afterwards, whether the run passed or failed. The report lists every version as applied. The database being installed to is never touched: not its ledger, not its tables.

Limits: a migration that names another schema explicitly, or creates an extension, role or other cluster-wide object, escapes the scratch schema; that is not detected. This is the PostgreSQL driver only.

Source

pub async fn fresh_catalog( &self, migrations: &[Migration], ) -> Result<Snapshot, MigrateError>

As verify_fresh, and read the scratch schema’s catalog before it is dropped: the schema migrations produce, from migrations and a server, without touching the target. The snapshot is stamped with the versions and checksums it was derived from.

Source

pub async fn applied_versions(&self) -> Result<Vec<i64>, MigrateError>

The versions the ledger says succeeded, ascending; empty if there is no ledger yet.

Source

pub async fn applied_checksums( &self, ) -> Result<Vec<(i64, Vec<u8>)>, MigrateError>

The ledger’s rows that succeeded, (version, checksum) ascending; empty if there is no ledger yet. Reads the same rows Migrates::migrate checks against, so a caller can compare a directory of migrations with a database without applying anything.

Source§

impl PgBackend

Source

pub async fn connect_with( secret: &ConnectionSecret, options: &ConnectOptions, ) -> Result<Self, RelError>

Connect with an explicit TLS policy and pool bounds, to a connection string held as a ConnectionSecret (resolved here, never printed).

Source

pub async fn connection_is_encrypted(&self) -> Result<bool, RelError>

Whether the connection this backend uses is encrypted, as the server reports it (pg_stat_ssl), not as the client was asked.

Source§

impl PgBackend

Source

pub async fn connect(url: &str) -> Result<Self, RelError>

Connect with default pool settings and read the server version.

Prefer PgBackend::connect_secret: a &str URL is one Debug print from a log file.

Source

pub async fn connect_secret(secret: &ConnectionSecret) -> Result<Self, RelError>

Connect with a connection string held as a persistant_relational::ConnectionSecret: resolved here, never printed, never part of an error.

Source

pub async fn from_pool(pool: PgPool) -> Result<Self, RelError>

Wrap an existing pool (a program that already owns one keeps it).

Source

pub fn pool(&self) -> &PgPool

The concrete pool: for sqlx::query! and anything this layer does not carry.

Trait Implementations§

Source§

impl Clone for PgBackend

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PgBackend

Source§

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

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

impl Migrates for PgBackend

Source§

async fn migrate( &self, migrations: &[Migration], ) -> Result<MigrateReport, MigrateError>

Apply migrations in version order, skipping those already applied. Each migration runs in its own transaction where the database can; refuses (applying nothing further) if an applied migration’s text has changed, if the database has applied a version the program does not know, or if a migration is recorded as dirty.
Source§

async fn applied_versions(&self) -> Result<Vec<i64>, MigrateError>

The versions the ledger says succeeded, ascending; empty if there is no ledger yet.
Source§

impl RelationalBackend for PgBackend

Source§

type Tx<'a> = PgTx<'a>

A transaction on this backend. Dropping it without commit rolls it back.
Source§

fn backend(&self) -> &str

The backend’s name as it appears in matrix rows (postgres).
Source§

fn version(&self) -> &str

The server version string as it appears in matrix rows (16.15).
Source§

fn dialect(&self) -> Dialect

The dialect statements must be written in.
Source§

async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, RelError>

Run a statement that returns no rows; the result is the number of rows affected.
Source§

async fn query(&self, sql: &str, params: &[Value]) -> Result<Vec<Row>, RelError>

Run a statement and return its rows.
Source§

async fn begin(&self, isolation: Isolation) -> Result<PgTx<'_>, RelError>

Begin a transaction at (at least) isolation.
Source§

fn call_bound(&self) -> Option<Duration>

The per-call timeout this backend enforces, if one is configured; None (the default) means a call waits as long as the server does. Every call that takes longer returns RelError::Timeout.
Source§

fn with_lock_held<'a>( &'a self, lock_sql: &'a str, at_most: Duration, during: Pin<Box<dyn Future<Output = ()> + Send + 'a>>, ) -> impl Future<Output = Result<(), RelError>> + Send

Test support for kv.bounded_call: take lock_sql (a statement that locks a row) in a transaction of its own, drive during while that lock is held, then roll back. during is dropped, unfinished, after at_most, so a broken bound cannot hang the test. A backend that enforces no bound returns RelError::Unsupported.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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