ruststream-sqlx 0.7.1

SQL databases for the RustStream messaging framework through sqlx: a transactional outbox over any RustStream broker and task queues in Postgres, MySQL/MariaDB and SQLite tables.
Documentation
//! The service of `examples/sqlx_time_database.rs` without the macros: `.clock::<DatabaseClock>()`
//! makes the statements read the database's clock, and `Clock<DatabaseClock>` goes into the type.
//!
//! ```text
//! cargo run -p ruststream-sqlx --example manual_sqlx_time_database --features inbox,sqlite,chrono,testing
//! ```

use ruststream::OutgoingMessage;
use ruststream::schemars::JsonSchema;
use ruststream_sqlx::dialect::{Column, KeyPart};
use ruststream_sqlx::keys::InboxContext;
use ruststream_sqlx::spec::{Advisory, Attempt, Clock, Payload, ProcessedAt, RetryAfter};
use ruststream_sqlx::{AttemptRow, InboxSpec, InboxTable, PayloadRow};
use serde::{Deserialize, Serialize};
use sqlx::{Sqlite, SqliteConnection};

// --8<-- [start:database_clock]
use std::time::Duration;

use chrono::{DateTime, Utc};
use ruststream_sqlx::DatabaseClock;
use ruststream_sqlx::prelude::*;
use sqlx::Connection;
use sqlx::sqlite::SqlitePoolOptions;

/// One row of `statement_jobs`, written by hosts whose clocks drift. The advisory lock form,
/// because the lease form reads the host's clock.
#[derive(sqlx::FromRow)]
struct SendStatement {
    id: i64,
    attempt: i16,
    payload: Vec<u8>,
}

impl InboxTable for SendStatement {
    type Id = i64;
    type Table = InboxSpec<(
        Clock<DatabaseClock>,
        Advisory,
        Attempt,
        RetryAfter<DateTime<Utc>>,
        ProcessedAt<DateTime<Utc>>,
        Payload,
    )>;
    const TABLE: Self::Table = InboxSpec::new("statement_jobs", Column::new("id").generated())
        .clock::<DatabaseClock>()
        .advisory(&[KeyPart::Literal("statement_jobs-"), KeyPart::Column("id")])
        .attempt(Column::new("attempt").generated())
        .retry_after(Column::new("retry_after"))
        .processed_at(Column::new("processed_at"))
        .payload(Column::new("payload"));

    fn id(&self) -> &i64 {
        &self.id
    }
}

impl AttemptRow for SendStatement {
    type Attempt = i16;

    fn attempt(&self) -> &i16 {
        &self.attempt
    }
}

impl PayloadRow for SendStatement {
    type Column = Vec<u8>;

    fn payload(&self) -> &[u8] {
        &self.payload
    }
}

struct SendOne;

impl Handle<Account, (), (), InboxContext> for SendOne {
    async fn handle(
        &self,
        account: &Account,
        _outs: &(),
        ctx: &mut Context<'_, InboxContext>,
    ) -> Result<(), HandlerOutcome> {
        let Ctx(attempt) = Ctx::<keys::Attempt>::from_context(ctx).await?;
        if render(account, attempt).await {
            return Ok(());
        }
        // A second by the database's clock, whichever host claimed the row.
        Err(HandlerOutcome::retry_after(Duration::from_secs(1)))
    }
}

/// The service on the database `url` names.
fn service(url: &str) -> RustStream {
    let schema_url = url.to_owned();
    // Nothing connects here: the broker builds its pool when the service starts.
    let broker = SqlxBroker::from_url(url, SqlitePoolOptions::new());
    RustStream::new(AppInfo::new("statements", "0.1.0"))
        // The table, created before the subscription checks it.
        .on_startup(async move |()| {
            let mut conn = SqliteConnection::connect(&schema_url).await?;
            sqlx::raw_sql(SCHEMA).execute(&mut conn).await?;
            conn.close().await
        })
        .with_broker(broker.route::<SendStatement>("statements"), |b| {
            let send = subscriber("statements", SendOne)
                .map_source(|_| InboxQueue::<SendStatement>::new("statements"));
            b.include(send.build());
        })
}
// --8<-- [end:database_clock]

/// A stand-in for the ledger: the month's figures are not closed at the first attempt.
async fn render(account: &Account, attempt: Option<u64>) -> bool {
    tokio::task::yield_now().await;
    tracing::info!(account.id, ?attempt, "rendering a statement");
    attempt != Some(1)
}

#[ruststream::app]
fn app() -> impl App {
    service("sqlite:statements.db?mode=rwc")
}

const SCHEMA: &str = "CREATE TABLE IF NOT EXISTS statement_jobs (
    id INTEGER PRIMARY KEY,
    attempt INTEGER NOT NULL DEFAULT 1,
    retry_after TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%f+00:00', 'now')),
    processed_at TEXT,
    payload BLOB NOT NULL
)";

impl Publish<Sqlite> for SendStatement {
    async fn publish(
        conn: &mut SqliteConnection,
        message: &OutgoingMessage<'_>,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("INSERT INTO statement_jobs (payload) VALUES (?)")
            .bind(message.payload())
            .execute(conn)
            .await?;
        Ok(())
    }
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing, JsonSchema)]
#[schemars(crate = "ruststream::schemars")]
#[outgoing(name = "statements")]
struct Account {
    id: u64,
}

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::time::Duration;

    use ruststream::testing::TestApp;
    use ruststream_sqlx::prelude::*;
    use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
    use sqlx::{Connection, Sqlite, SqliteConnection, SqlitePool};

    use super::{Account, SCHEMA, service};

    // An in-memory database every connection naming it shares, alive while one of them is open:
    // the test holds one beside the pool it reads the table through.
    async fn database(
        name: &str,
    ) -> Result<(String, SqlitePool, SqliteConnection), Box<dyn Error>> {
        let url = format!("sqlite:file:{name}?mode=memory&cache=shared");
        let options: SqliteConnectOptions = url.parse()?;
        let keeper = SqliteConnection::connect_with(&options).await?;
        let pool = SqlitePoolOptions::new()
            .min_connections(1)
            .idle_timeout(None)
            .connect_with(options)
            .await?;
        sqlx::raw_sql(SCHEMA).execute(&pool).await?;
        Ok((url, pool, keeper))
    }

    // The database's clock is real time, which a paused tokio clock does not move, so the test
    // runs on the real clock and lets the second pass.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_delay_on_the_database_clock_brings_the_row_back() -> Result<(), Box<dyn Error>> {
        let (url, pool, _keeper) = database("manual_sqlx_time_database").await?;
        let tb = TestApp::start_live(service(&url)).await?;
        let account = Account { id: 7 };
        tb.broker::<SqlxBroker<Sqlite>>()
            .message(&account)
            .publish()
            .await?;
        tb.broker::<SqlxBroker<Sqlite>>()
            .subscriber("statements")
            .assert_called_once()
            .settled(HandlerOutcome::retry_after(Duration::from_secs(1)));

        tb.advance(Duration::from_secs(1)).await?;

        tb.broker::<SqlxBroker<Sqlite>>()
            .subscriber("statements")
            .assert_called(2)
            .settled(HandlerOutcome::ack());
        let marked: bool =
            sqlx::query_scalar("SELECT processed_at IS NOT NULL FROM statement_jobs")
                .fetch_one(&pool)
                .await?;
        assert!(marked, "the database's clock marked the row");
        Ok(())
    }
}