Skip to main content

Crate axa_postgres_sqlx

Crate axa_postgres_sqlx 

Source
Expand description

PostgreSQL pooling, migrations, and test resets over sqlx.

This crate holds the operations every service repeats. It does not wrap queries: a service writes its own SQL with sqlx::query!, so a reviewer reads exactly what runs.

It has the same shape as axa-postgres-seaorm, axa-postgres-diesel, and axa-postgres-toasty: a Pool, a PoolConfig, connect, ping, migrate, revert, pending, truncate, dangerously_recreate, is_unique_violation, and quote_identifier, with status besides.

use axa_postgres_sqlx::{PoolConfig, connect, migrate, sqlx};

static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate::Migrator::DEFAULT;

let pool = connect(&PoolConfig::new("postgres://localhost/app")).await?;
migrate(&pool, &MIGRATOR).await?;

Re-exports§

pub use sqlx;

Structs§

MigrationStatus
One migration and whether it has run.
PoolConfig
Pool settings.
RowSecurity
Whether row-level security can keep rows apart for the role a pool connects as.

Enums§

Conn
A connection for the length of one call. It derefs to PgConnection.
Db
Where a function gets its connection: the pool, or a connection the caller holds, usually an open transaction, so one function serves both.

Functions§

begin_with
Open a transaction with each (name, value) set for its length only, such as ("app.tenant", tenant.as_str()) for a row-level security policy that reads current_setting('app.tenant', true).
connect
Open a pool and prove it works with one connection, with the session settings on every connection.
dangerously_recreate
Drop every table and enum type in public, then migrate from nothing. This gives each test an empty schema, whatever tables it touched.
is_custom_setting
Whether name is a custom setting a service may set for row-level security: two or more parts of lower-case ASCII letters, digits, and underscores, joined by dots, such as app.tenant. A name with no dot is Postgres’s own, such as role or search_path, which this never sets.
is_stale_plan
Whether error is Postgres refusing a statement this connection prepared before a migration changed what it returns: SQLSTATE 0A000 with “cached plan must not change result type”. Each pooled connection that cached the statement fails so once, and a fresh connection does not.
is_unique_violation
Whether an error is a violated unique constraint, which a service usually reports as 409 rather than 500.
migrate
Apply pending migrations. Sqlx holds a lock while it does, so services booting together take turns.
note_stale_plan
Record error if it is a stale plan, so that every connection opened before now is retired when next acquired, from every pool connect opened in this process, and the next attempt runs on a fresh one. Returns whether it was. axa::Error calls this for every sqlx error it answers, so a service that turns sqlx errors into it needs nothing more.
pending
The names of the migrations in migrator not yet applied, in order, each as version_description.
ping
Check that the database answers.
quote_identifier
Quote a table or type name for use in SQL.
revert
Revert the newest steps migrations. Each needs a .down.sql file.
row_security
Ask Postgres whether row-level security can apply to pool’s role, and whether each of tables, such as todos or app.todos, has it enabled and forced.
status
Every migration in migrator, oldest first, with whether it has run.
truncate
Empty tables, restarting their sequences. Each name goes through quote_identifier.

Type Aliases§

Error
What this crate’s connection and query functions return on failure.
Pool
The handle a service holds: the pool.