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§
- Background
- A migration of statements that cannot run in a transaction, such as
CREATE INDEX CONCURRENTLY, which sqlx’s migrator runs each file in. Name each one once and never change it: its name is what marks it done. - Migration
Status - One migration and whether it has run.
- Pool
Config - 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.
Constants§
- BACKGROUND_
LOCK - The advisory lock one process holds while it runs background migrations.
- BACKGROUND_
TABLE - The table background migrations are recorded in.
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 readscurrent_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
nameis 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 asapp.tenant. A name with no dot is Postgres’s own, such asroleorsearch_path, which this never sets. - is_
stale_ plan - Whether
erroris Postgres refusing a statement this connection prepared before a migration changed what it returns: SQLSTATE0A000with “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
409rather than500. - migrate
- Apply pending migrations. Sqlx holds a lock while it does, so services booting together take turns.
- note_
stale_ plan - Record
errorif it is a stale plan, so that every connection opened before now is retired when next acquired, from every poolconnectopened in this process, and the next attempt runs on a fresh one. Returns whether it was.axa::Errorcalls 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
migratornot yet applied, in order, each asversion_description. - pending_
background - The names of
migrationsnot yet recorded, in order, forChecks::migrations. - ping
- Check that the database answers.
- quote_
identifier - Quote a table or type name for use in SQL.
- revert
- Revert the newest
stepsmigrations. Each needs a.down.sqlfile. - row_
security - Ask Postgres whether row-level security can apply to
pool’s role, and whether each oftables, such astodosorapp.todos, has it enabled and forced. - run_
background - Run each of
migrationsnot yet recorded, in order, outside any transaction, recording each once all its statements have run. One process at a time holdsBACKGROUND_LOCK, so replicas booting together do not run one twice: a process that cannot take it answersNoneat once, and the one that holds it answers the names it ran. A failure stops the run, leaves that migration unrecorded, so the next run tries it again, and releases the lock. - status
- Every migration in
migrator, oldest first, with whether it has run. - truncate
- Empty
tables, restarting their sequences. Each name goes throughquote_identifier.