snodra_messaging_sqlx/lib.rs
1//! The SQL message store: every store of `snodra_messaging::store` over the service's
2//! own PostgreSQL (13 or later, feature `postgres`) or MySQL (8.0.17 or later, InnoDB, feature
3//! `mysql`) database through [`sqlx`], in the tables snodra-messaging-jdbc (the Kotlin twin)
4//! writes: same tables, same values, same meanings, so a Kotlin service and a Rust service can
5//! share one database.
6//!
7//! ```ignore
8//! let pool = sqlx::postgres::PgPoolOptions::new().max_connections(10).connect(&url).await?;
9//! let app = SnodraMessaging::builder(config).await?
10//! .store(Arc::new(SqlxStore::postgres(pool).with_settings(SqlxSettings::from_env()?)))
11//! /* ... */;
12//! ```
13//!
14//! - **Schema**: `db/snodra/<dialect>/V1__snodra.sql`, the files snodra-messaging-jdbc ships, byte
15//! for byte ([`schema::POSTGRESQL_V1`], [`schema::MYSQL_V1`]). `start()` applies it
16//! ([`MessageStore::init_schema`]: the server checked, then V1 under a database lock unless
17//! `snodra_schema` holds version 1 or later) unless `SNODRA_STORE_INIT_SCHEMA=false`; then it
18//! checks that the database has version 1 or later ([`MessageStore::check_schema`]).
19//! - **Transactions**: the store's transactions are transactions on connections of the pool. A
20//! handler writes its own rows in the transaction of the message it handles with [`SqlxTxExt`]
21//! (`ctx.tx.lock().await.connection::<Postgres>()`): its rows, the outbox entries and the
22//! processed-event markers commit or roll back together. What commits on its own (dead letters,
23//! processor control, the relays' and schedulers' claims, the purge) runs in a transaction of
24//! its own at READ COMMITTED; a store call outside any transaction in a short one of its own. A
25//! connection whose transaction did not end cleanly is closed, never returned to the pool.
26//! - **Wake-up** (PostgreSQL): every append runs `SELECT pg_notify('snodra_outbox', '')` in its
27//! transaction, and the relay listens (`LISTEN snodra_outbox`) on a connection of its own while
28//! it runs, so it publishes as soon as an append commits. MySQL: the relay polls.
29//! - **Retention**: what the MongoDB add-on's TTL indexes do, a purge does every
30//! [`SqlxSettings::purge_interval`], with the retentions of the `Config`.
31//!
32//! Times are the application's clock (the builder's, [`MessageStore::use_clock`]), at
33//! millisecond precision, UTC; never the database's.
34//!
35//! [`MessageStore::init_schema`]: snodra_messaging::store::MessageStore::init_schema
36//! [`MessageStore::check_schema`]: snodra_messaging::store::MessageStore::check_schema
37//! [`MessageStore::use_clock`]: snodra_messaging::store::MessageStore::use_clock
38
39#[cfg(any(feature = "postgres", feature = "mysql"))]
40mod control;
41#[cfg(any(feature = "postgres", feature = "mysql"))]
42mod db;
43#[cfg(any(feature = "postgres", feature = "mysql"))]
44mod dead_letters;
45#[cfg(any(feature = "postgres", feature = "mysql"))]
46mod external;
47#[cfg(any(feature = "postgres", feature = "mysql"))]
48mod limits;
49#[cfg(feature = "postgres")]
50mod notify;
51#[cfg(any(feature = "postgres", feature = "mysql"))]
52mod outbox;
53#[cfg(any(feature = "postgres", feature = "mysql"))]
54mod purge;
55#[cfg(any(feature = "postgres", feature = "mysql"))]
56mod sagas;
57#[cfg(any(feature = "postgres", feature = "mysql"))]
58pub mod schema;
59mod settings;
60#[cfg(any(feature = "postgres", feature = "mysql"))]
61mod store;
62
63pub use settings::SqlxSettings;
64#[cfg(any(feature = "postgres", feature = "mysql"))]
65pub use store::{SqlxStore, SqlxTxExt};