pub enum Database {
Sqlite(Pool<Sqlite>),
Postgres(Pool<Postgres>),
}Expand description
The connection pool, and the only way to reach it.
The pool is private to this crate: everything else goes through a table
module, Database::transaction or Database::pool_stats. That is what
keeps SQL — and the dialect it is written in — in one crate.
Variants§
Implementations§
Source§impl Database
impl Database
Sourcepub async fn transaction(&self) -> Result<Tx, Error>
pub async fn transaction(&self) -> Result<Tx, Error>
Begins a transaction.
Sourcepub async fn write_transaction(&self) -> Result<Tx, Error>
pub async fn write_transaction(&self) -> Result<Tx, Error>
Begins a transaction that holds the write lock from its first
statement (BEGIN IMMEDIATE), for one that reads and then writes on
what it read while another process may be writing.
A plain transaction is deferred: it takes a read
snapshot at its first SELECT and asks for the write lock only at its
first write. In WAL mode, if another connection committed in between,
that upgrade fails at once with SQLITE_BUSY_SNAPSHOT — busy_timeout
cannot help, since waiting would not make the snapshot current. Taking
the lock up front makes the transaction wait its turn under
busy_timeout instead, and then read what it writes against.
Sourcepub fn pool_stats(&self) -> PoolStats
pub fn pool_stats(&self) -> PoolStats
The pool’s size and idle count, read now rather than tracked.
Sourcepub async fn close(&self)
pub async fn close(&self)
Closes the pool: every later query fails with PoolClosed.
Waits for checked-out connections to be returned. Also how a test simulates the database going away underneath a running server.
Sourcepub async fn open(url: &str) -> Result<Database, Error>
pub async fn open(url: &str) -> Result<Database, Error>
Opens the database at url, whose scheme picks the backend.
Does not migrate.
sqlite: creates the file if it is not there yet, and pins the two
pragmas the schema depends on. postgres:/postgresql: expects the
database to exist — creating one is a privileged act an operator
performs, not something a server does to a cluster it was pointed at.
Any other scheme is refused by name here, rather than as a driver error
several frames down.
Applying the schema is a separate, named act: migrate,
acme-proxy migrate, or the worker role at startup. It used to happen
here, which meant every subcommand — audit list, completions, a
health check — silently upgraded the schema of whatever database it was
pointed at, and two processes starting together raced MIGRATOR::run
with no lock between them (SQLite gives sqlx none; PostgreSQL does,
an advisory lock, so there the one-owner rule is belt and braces).
A caller that needs the schema present asks
pending_migrations and refuses by name, or
uses connect_and_migrate.
Sourcepub async fn migrate(&self) -> Result<(), Error>
pub async fn migrate(&self) -> Result<(), Error>
Applies every embedded migration that has not run yet.
Idempotent: sqlx tracks each file by version and checksum, so running
this against an up-to-date database does nothing.
Sourcepub async fn pending_migrations(&self) -> Result<Vec<i64>, Error>
pub async fn pending_migrations(&self) -> Result<Vec<i64>, Error>
The versions of the embedded migrations this database has not applied.
Empty means the schema is current. What the roles that must not migrate check before serving, so an unmigrated database stops them by name rather than failing later as a missing table.
A database with no _sqlx_migrations table has applied nothing — that
is a freshly created file, not an error.
Sourcepub async fn connect_in_memory() -> Result<Database, Error>
pub async fn connect_in_memory() -> Result<Database, Error>
Builds a throwaway in-memory database with migrations applied. Pinned to
a single connection so the whole test shares one in-memory database
(each SQLite connection otherwise gets its own).
Source§impl Database
impl Database
Sourcepub async fn transfer_to(
&self,
target: &Database,
) -> Result<TransferReport, Error>
pub async fn transfer_to( &self, target: &Database, ) -> Result<TransferReport, Error>
Copies every row of this database into target.
The whole copy is one transaction on the target, so a failure anywhere leaves it exactly as it was rather than half-populated. The source is only read.
The caller is responsible for the two things this cannot check: that
target’s schema is current (ask Database::pending_migrations) and
that nothing is writing to the source. See the module doc.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Database
impl !UnwindSafe for Database
impl Freeze for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl UnsafeUnpin for Database
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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