ruststream-sqlx 0.7.0

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! The databases the outbox's default statements run on: each builds a record's statements in the
//! built-in dialects its connections speak, and a connection picks its own.

#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
use std::collections::BTreeSet;
use std::future::Future;
#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
use std::sync::{Mutex, PoisonError};

#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
use ruststream_sqlx_dialect::{self as dialect, OutboxDialect};
use ruststream_sqlx_dialect::{StatementError, TableSpec};

#[cfg(feature = "any")]
use sqlx::Any;
#[cfg(feature = "any")]
use sqlx::AnyConnection;
#[cfg(any(
    feature = "postgres",
    feature = "mysql",
    feature = "sqlite",
    feature = "any"
))]
use sqlx::Arguments as _;
use sqlx::{Database, Error, FromRow};
#[cfg(feature = "mysql")]
use sqlx::{MySql, MySqlConnection};
#[cfg(feature = "postgres")]
use sqlx::{PgConnection, Postgres};
#[cfg(feature = "sqlite")]
use sqlx::{Sqlite, SqliteConnection};

/// The texts of the default statements of one record on one dialect, interned for the life of the
/// process. Machinery.
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct Statements {
    /// The fetch of a record by its id.
    pub(super) fetch: &'static str,
    /// The mark of a processed record, by its id: `Ack` and `Discard`.
    pub(super) mark: &'static str,
    /// The recovery of the unprocessed records of a name.
    pub(super) recover: &'static str,
}

#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
impl Statements {
    /// The statements `dialect` writes for `spec`.
    fn of(dialect: &impl OutboxDialect, spec: &TableSpec<'_>) -> Result<Self, StatementError> {
        Ok(Self {
            fetch: intern(dialect.outbox_fetch(spec)?.sql()),
            mark: intern(dialect.outbox_mark(spec)?.sql()),
            recover: intern(dialect.outbox_recover(spec)?.sql()),
        })
    }
}

/// The default statements of one record, built when the record type is registered and kept in its
/// registry node: one set per built-in dialect the record's database runs, `None` for the others.
/// Machinery.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Defaults {
    #[cfg(feature = "postgres")]
    postgres: Option<Statements>,
    #[cfg(feature = "mysql")]
    mysql: Option<Statements>,
    #[cfg(feature = "sqlite")]
    sqlite: Option<Statements>,
}

/// `text` for the life of the process, written once whatever the number of registries that build
/// it.
// Why leaked: sqlx 0.9 runs a `&'static str` as written and copies any shorter-lived text into a
// new `Arc<str>` on every run, so the per-message path needs `'static` statements. A registry
// builds them once, at construction; the set keeps each distinct text once, so building a
// registry again (a test suite, a service rebuilt in one process) leaks nothing new.
#[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
fn intern(text: &str) -> &'static str {
    static TEXTS: Mutex<BTreeSet<&'static str>> = Mutex::new(BTreeSet::new());
    let mut texts = TEXTS.lock().unwrap_or_else(PoisonError::into_inner);
    if let Some(interned) = texts.get(text) {
        return interned;
    }
    let interned: &'static str = Box::leak(text.into());
    texts.insert(interned);
    interned
}

/// The name `AnyConnection::backend_name` gives each built-in database, as sqlx names it.
#[cfg(all(feature = "any", feature = "postgres"))]
const POSTGRES_BACKEND: &str = "PostgreSQL";
#[cfg(all(feature = "any", feature = "mysql"))]
const MYSQL_BACKEND: &str = "MySQL";
#[cfg(all(feature = "any", feature = "sqlite"))]
const SQLITE_BACKEND: &str = "SQLite";

#[cfg(feature = "any")]
impl Defaults {
    /// The statements of the database behind the `AnyPool` backend named `backend`.
    fn for_backend(&self, backend: &str) -> Option<&Statements> {
        match backend {
            #[cfg(feature = "postgres")]
            POSTGRES_BACKEND => self.postgres.as_ref(),
            #[cfg(feature = "mysql")]
            MYSQL_BACKEND => self.mysql.as_ref(),
            #[cfg(feature = "sqlite")]
            SQLITE_BACKEND => self.sqlite.as_ref(),
            _ => None,
        }
    }
}

/// A sqlx database the default events of an outbox record run on.
///
/// The registry builds the statements from the record's
/// [`OutboxTable::TABLE`](super::OutboxTable::TABLE) in each built-in dialect the database's
/// connections speak, once, when the record type is registered, and a connection picks its own.
///
/// [`Postgres`](sqlx::Postgres), [`MySql`](sqlx::MySql) (MySQL and MariaDB) and
/// [`Sqlite`](sqlx::Sqlite) implement it under their features, and [`Any`](sqlx::Any) under `any`,
/// where the database the connection reached picks the text. A record's default event on a
/// backend whose dialect the crate was built without fails with [`sqlx::Error::Configuration`]
/// naming the record and the event.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # mod demo {
/// use ruststream::OutgoingMessage;
/// use ruststream_sqlx::{Outbox, OutboxDatabase, outbox};
/// use sqlx::{PgConnection, Postgres};
///
/// // outbox: id BIGSERIAL PRIMARY KEY, name TEXT, payload BYTEA; a processed record is deleted
/// #[derive(Outbox, sqlx::FromRow)]
/// #[outbox(table = "outbox")]
/// pub struct OrderEvent {
///     #[field(id)]
///     id: i64,
///     #[field(name)]
///     name: String,
///     #[field(payload)]
///     payload: Vec<u8>,
/// }
///
/// // The record of a published message is the service's own statement.
/// impl outbox::Publish<Postgres> for OrderEvent {
///     async fn publish(
///         conn: &mut PgConnection,
///         msg: &OutgoingMessage<'_>,
///     ) -> Result<i64, sqlx::Error> {
///         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
///             .bind(msg.name())
///             .bind(msg.payload())
///             .fetch_one(conn)
///             .await
///     }
/// }
///
/// // The default events run on any `OutboxDatabase`: here the mark of a processed record.
/// pub async fn mark<DB: OutboxDatabase>(
///     conn: &mut DB::Connection,
///     id: &i64,
/// ) -> Result<(), sqlx::Error>
/// where
///     OrderEvent: outbox::Ack<DB>,
/// {
///     <OrderEvent as outbox::Ack<DB>>::ack(conn, id).await
/// }
/// # }
/// # fn main() {}
/// ```
pub trait OutboxDatabase: Database {
    /// The default statements of the table `spec` describes, in each built-in dialect this
    /// database's connections may speak. Machinery.
    #[doc(hidden)]
    fn defaults(spec: &TableSpec<'_>) -> Result<Defaults, StatementError>;

    /// The statements this connection's database runs, or `None` when the crate was built without
    /// its dialect. Machinery.
    #[doc(hidden)]
    fn statements<'d>(conn: &Self::Connection, defaults: &'d Defaults) -> Option<&'d Statements>;

    /// Binds the name a recovery reads the records of. Machinery.
    #[doc(hidden)]
    fn bind_name(arguments: &mut Self::Arguments, name: &str) -> Result<(), Error>;

    /// Runs a statement. Machinery.
    #[doc(hidden)]
    fn execute<'c>(
        conn: &'c mut Self::Connection,
        sql: &'static str,
        arguments: Self::Arguments,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c;

    /// Runs a statement that reads at most one record. Machinery.
    #[doc(hidden)]
    fn fetch_optional<'c, Record>(
        conn: &'c mut Self::Connection,
        sql: &'static str,
        arguments: Self::Arguments,
    ) -> impl Future<Output = Result<Option<Record>, Error>> + Send + 'c
    where
        Record: for<'r> FromRow<'r, Self::Row> + Send + Unpin + 'c;

    /// Runs a statement that reads every record it selects. Machinery.
    #[doc(hidden)]
    fn fetch_all<'c, Record>(
        conn: &'c mut Self::Connection,
        sql: &'static str,
        arguments: Self::Arguments,
    ) -> impl Future<Output = Result<Vec<Record>, Error>> + Send + 'c
    where
        Record: for<'r> FromRow<'r, Self::Row> + Send + Unpin + 'c;
}

/// `OutboxDatabase` for one sqlx database: `$build` builds the statements of a table, and the
/// connection picks the ones `$pick` names.
#[cfg(any(
    feature = "postgres",
    feature = "mysql",
    feature = "sqlite",
    feature = "any"
))]
macro_rules! outbox_database {
    (
        $database:ty, $connection:ty,
        |$spec:ident| $build:expr,
        |$conn:ident, $defaults:ident| $pick:expr
    ) => {
        impl OutboxDatabase for $database {
            fn defaults($spec: &TableSpec<'_>) -> Result<Defaults, StatementError> {
                $build
            }

            fn statements<'d>(
                $conn: &$connection,
                $defaults: &'d Defaults,
            ) -> Option<&'d Statements> {
                $pick
            }

            fn bind_name(arguments: &mut Self::Arguments, name: &str) -> Result<(), Error> {
                arguments.add(name).map_err(Error::Encode)
            }

            async fn execute(
                conn: &mut $connection,
                sql: &'static str,
                arguments: Self::Arguments,
            ) -> Result<(), Error> {
                sqlx::query_with(sql, arguments).execute(conn).await?;
                Ok(())
            }

            async fn fetch_optional<'c, Record>(
                conn: &'c mut $connection,
                sql: &'static str,
                arguments: Self::Arguments,
            ) -> Result<Option<Record>, Error>
            where
                Record: for<'r> FromRow<'r, Self::Row> + Send + Unpin + 'c,
            {
                sqlx::query_as_with(sql, arguments)
                    .fetch_optional(conn)
                    .await
            }

            async fn fetch_all<'c, Record>(
                conn: &'c mut $connection,
                sql: &'static str,
                arguments: Self::Arguments,
            ) -> Result<Vec<Record>, Error>
            where
                Record: for<'r> FromRow<'r, Self::Row> + Send + Unpin + 'c,
            {
                sqlx::query_as_with(sql, arguments).fetch_all(conn).await
            }
        }
    };
}

#[cfg(feature = "postgres")]
outbox_database!(
    Postgres,
    PgConnection,
    |spec| Ok(Defaults {
        postgres: Some(Statements::of(&dialect::Postgres, spec)?),
        ..Defaults::default()
    }),
    |_conn, defaults| defaults.postgres.as_ref()
);
#[cfg(feature = "mysql")]
outbox_database!(
    MySql,
    MySqlConnection,
    |spec| Ok(Defaults {
        mysql: Some(Statements::of(&dialect::MySql, spec)?),
        ..Defaults::default()
    }),
    |_conn, defaults| defaults.mysql.as_ref()
);
#[cfg(feature = "sqlite")]
outbox_database!(
    Sqlite,
    SqliteConnection,
    |spec| Ok(Defaults {
        sqlite: Some(Statements::of(&dialect::Sqlite, spec)?),
        ..Defaults::default()
    }),
    |_conn, defaults| defaults.sqlite.as_ref()
);
// `Any` passes a statement to its backend as written, so the backend picks the text.
#[cfg(feature = "any")]
outbox_database!(
    Any,
    AnyConnection,
    |spec| Ok(Defaults {
        #[cfg(feature = "postgres")]
        postgres: Some(Statements::of(&dialect::Postgres, spec)?),
        #[cfg(feature = "mysql")]
        mysql: Some(Statements::of(&dialect::MySql, spec)?),
        #[cfg(feature = "sqlite")]
        sqlite: Some(Statements::of(&dialect::Sqlite, spec)?),
    }),
    |conn, defaults| defaults.for_backend(conn.backend_name())
);

/// The error of a default event on a connection whose database the crate was built without.
pub(super) fn no_outbox_statement(record: &'static str, event: &'static str) -> Error {
    Error::Configuration(
        format!("`{record}` has no {event} statement for this connection's database").into(),
    )
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "postgres")]
    use std::ptr;

    #[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
    use ruststream_sqlx_dialect::{Column, Form, OutboxDialect, StatementError, TableSpec};

    use super::no_outbox_statement;
    #[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
    use super::{Defaults, OutboxDatabase, dialect};

    #[cfg(any(feature = "postgres", feature = "mysql", feature = "sqlite"))]
    const TABLE: TableSpec<'static> = TableSpec::new("outbox", Column::new("id"), Form::RowLock)
        .group(Column::new("name"))
        .payload(Column::new("payload"))
        .processed_at(Column::new("processed_at"))
        .database_clock();

    #[test]
    fn the_missing_statement_names_the_record_and_the_event() {
        assert_eq!(
            no_outbox_statement("OrderEvent", "fetch").to_string(),
            "error with configuration: `OrderEvent` has no fetch statement for this connection's \
             database"
        );
    }

    #[cfg(all(
        feature = "any",
        feature = "postgres",
        feature = "mysql",
        feature = "sqlite"
    ))]
    #[test]
    fn an_any_backend_runs_the_statements_of_its_database() -> Result<(), StatementError> {
        use sqlx::Any;

        use super::{MYSQL_BACKEND, POSTGRES_BACKEND, SQLITE_BACKEND};

        let defaults = <Any as OutboxDatabase>::defaults(&TABLE)?;
        // The mark reads each database's clock, so its text differs on every dialect.
        let mark = |backend| {
            defaults
                .for_backend(backend)
                .map(|statements| statements.mark)
        };
        assert_eq!(
            mark(POSTGRES_BACKEND),
            Some(dialect::Postgres.outbox_mark(&TABLE)?.sql())
        );
        assert_eq!(
            mark(MYSQL_BACKEND),
            Some(dialect::MySql.outbox_mark(&TABLE)?.sql())
        );
        assert_eq!(
            mark(SQLITE_BACKEND),
            Some(dialect::Sqlite.outbox_mark(&TABLE)?.sql())
        );
        assert_eq!(mark("MSSQL"), None);
        Ok(())
    }

    #[cfg(feature = "postgres")]
    #[test]
    fn a_statement_built_twice_is_kept_once() -> Result<(), StatementError> {
        use sqlx::Postgres;

        let first = <Postgres as OutboxDatabase>::defaults(&TABLE)?;
        let second = <Postgres as OutboxDatabase>::defaults(&TABLE)?;
        let mark = |defaults: &Defaults| defaults.postgres.map(|statements| statements.mark);
        let (Some(first), Some(second)) = (mark(&first), mark(&second)) else {
            panic!("Postgres builds its own statements");
        };
        assert!(
            ptr::eq(first, second),
            "the second build reuses the first's text"
        );
        Ok(())
    }

    #[cfg(all(
        feature = "any",
        feature = "postgres",
        feature = "mysql",
        feature = "sqlite"
    ))]
    #[test]
    fn the_backend_names_are_sqlxs() {
        use sqlx::Database;

        use super::{MYSQL_BACKEND, POSTGRES_BACKEND, SQLITE_BACKEND};

        assert_eq!(POSTGRES_BACKEND, <sqlx::Postgres as Database>::NAME);
        assert_eq!(MYSQL_BACKEND, <sqlx::MySql as Database>::NAME);
        assert_eq!(SQLITE_BACKEND, <sqlx::Sqlite as Database>::NAME);
    }
}