pub struct TestDb { /* private fields */ }model-mysql or model-postgres or model-sqlite only.Expand description
A database for one test; see the module docs.
Implementations§
Source§impl TestDb
impl TestDb
Sourcepub fn sqlite() -> TestDbBuilder
Available on crate feature model-sqlite only.
pub fn sqlite() -> TestDbBuilder
model-sqlite only.A fresh SQLite in-memory database.
Sourcepub fn postgres(url: &str) -> TestDbBuilder
Available on crate feature model-postgres only.
pub fn postgres(url: &str) -> TestDbBuilder
model-postgres only.The PostgreSQL database at url (postgres://user:password@host/db,
sqlx’s URL syntax), each test inside a transaction that’s rolled back.
Sourcepub fn mysql(url: &str) -> TestDbBuilder
Available on crate feature model-mysql only.
pub fn mysql(url: &str) -> TestDbBuilder
model-mysql only.The MySQL database at url (mysql://user:password@host/db), each
test inside a transaction that’s rolled back.
Sourcepub fn postgres_from_env() -> Option<TestDbBuilder>
Available on crate feature model-postgres only.
pub fn postgres_from_env() -> Option<TestDbBuilder>
model-postgres only.TestDb::postgres with the URL in RWS_TEST_POSTGRES_URL; None
(after printing that the test is skipped) when it isn’t set, so a
test can return early on a machine without the server:
use rust_web_server::test_client::TestDb;
let Some(db) = TestDb::postgres_from_env() else { return Ok(()) };
let db = db.migrations("migrations").await?;Sourcepub fn mysql_from_env() -> Option<TestDbBuilder>
Available on crate feature model-mysql only.
pub fn mysql_from_env() -> Option<TestDbBuilder>
model-mysql only.TestDb::mysql with the URL in RWS_TEST_MYSQL_URL; None (after
printing that the test is skipped) when it isn’t set.
Sourcepub fn pool(&self) -> DbPool
pub fn pool(&self) -> DbPool
A clone of the pool (cheap: same connections, same database), for the app’s state.
Sourcepub fn is_transactional(&self) -> bool
pub fn is_transactional(&self) -> bool
true for PostgreSQL / MySQL, whose writes are rolled back; false
for SQLite, whose database is dropped instead.
Methods from Deref<Target = DbPool>§
Sourcepub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, DbError>
pub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, DbError>
Execute a SQL statement (INSERT / UPDATE / DELETE / DDL).
Returns the number of rows affected.
Sourcepub async fn query_rows(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<ModelRow>, DbError>
pub async fn query_rows( &self, sql: &str, params: &[Value], ) -> Result<Vec<ModelRow>, DbError>
Execute a SQL query and return untyped rows.
Sourcepub async fn query<T: Model>(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<T>, DbError>
pub async fn query<T: Model>( &self, sql: &str, params: &[Value], ) -> Result<Vec<T>, DbError>
Execute a SQL query and deserialise results into T: Model.
Sourcepub async fn query_raw(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<ModelRow>, DbError>
pub async fn query_raw( &self, sql: &str, params: &[Value], ) -> Result<Vec<ModelRow>, DbError>
Execute a SQL query returning untyped rows (alias for query_rows).
Sourcepub async fn begin(&self) -> Result<DbTransaction, DbError>
pub async fn begin(&self) -> Result<DbTransaction, DbError>
Begin a transaction. Call DbTransaction::commit to commit or let it
drop to automatically roll back.
Sourcepub async fn transaction<F, T, Fut>(&self, f: F) -> Result<T, DbError>
pub async fn transaction<F, T, Fut>(&self, f: F) -> Result<T, DbError>
Run a closure in a transaction.
The closure owns the transaction: call tx.commit() before returning
Ok. A transaction that isn’t committed — the closure returns Err,
or Ok without committing — is dropped, which rolls it back.
use rust_web_server::model::{DbPool, Value};
let pool = DbPool::memory().await?;
let result = pool.transaction(|mut tx| async move {
tx.execute("INSERT INTO t (v) VALUES (?)", &[Value::Text("hi".into())]).await?;
tx.commit().await?;
Ok(42i32)
}).await?;
assert_eq!(42, result);Sourcepub async fn migrate(&self, dir: &str) -> Result<(), DbError>
pub async fn migrate(&self, dir: &str) -> Result<(), DbError>
Run pending migrations from SQL files in dir.
Files are applied in lexicographic order. Already-applied versions are
tracked in a _schema_migrations table (created if absent). Idempotent.
Sourcepub async fn migration_status(
&self,
dir: &str,
) -> Result<Vec<MigrationStatus>, DbError>
pub async fn migration_status( &self, dir: &str, ) -> Result<Vec<MigrationStatus>, DbError>
Return the status (applied / pending) for each SQL file in dir.
Sourcepub async fn rollback_last(&self, dir: &str) -> Result<Option<String>, DbError>
pub async fn rollback_last(&self, dir: &str) -> Result<Option<String>, DbError>
Roll back the most recently applied migration in dir.
Requires a companion <version-stem>.down.sql file next to the up
migration it undoes (e.g. 0001_x.sql → 0001_x.down.sql); returns
Err if that file is missing. Returns Ok(None) if nothing is
currently applied.
Sourcepub async fn rollback(
&self,
dir: &str,
n: usize,
) -> Result<Vec<String>, DbError>
pub async fn rollback( &self, dir: &str, n: usize, ) -> Result<Vec<String>, DbError>
Roll back the last n applied migrations, most recently applied
first. Returns the versions actually rolled back — fewer than n if
fewer than n migrations were applied. Stops at the first missing
down file or SQL error; migrations already rolled back in earlier
iterations stay rolled back.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for TestDb
impl !UnwindSafe for TestDb
impl Freeze for TestDb
impl Send for TestDb
impl Sync for TestDb
impl Unpin for TestDb
impl UnsafeUnpin for TestDb
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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