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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! One trait per event of an outbox record: what a service implements when it names the event as
//! its own (`#[outbox(custom(..))]`, or `own` on its `OutboxSpec`), and `Publish`, which has no
//! default.

use std::future::Future;

use ruststream::OutgoingMessage;
use sqlx::{Database, Error};

use super::OutboxRow;
use super::database::Defaults;
use super::dispatch::{AckBy, Declared, DiscardBy, FetchBy, HeadersOf, RecoverBy, RetryBy};
use super::spec::{Declaration, OutboxTable};

/// Creates the record of a message a handler publishes under a registered name, and returns its
/// id, which the message then carries in [`OUTBOX_ID_HEADER`](super::OUTBOX_ID_HEADER).
///
/// It has no default: the service's statement lays the name, the payload and the headers out in
/// its columns. A record type without it does not register.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use chrono::{DateTime, Utc};
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
/// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
/// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
/// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
/// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
/// use ruststream_sqlx::{Outbox, outbox};
///
/// #[derive(Outbox, sqlx::FromRow)]
/// #[outbox(table = "outbox")]
/// pub struct OrderOutbox {
///     #[field(id)]
///     id: i64,
///     #[field(name)]
///     name: String,
///     #[field(payload)]
///     payload: Vec<u8>,
///     #[field(processed_at)]
///     processed_at: Option<DateTime<Utc>>,
/// }
///
/// impl outbox::Publish<Postgres> for OrderOutbox {
///     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
///         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
///             .bind(msg.name())
///             .bind(msg.payload())
///             .fetch_one(conn)
///             .await
///     }
/// }
///
/// pub fn app(pool: PgPool) -> impl App {
///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
///     RustStream::new(AppInfo::new("orders", "0.1.0"))
///         .layer(tracking.layer())
///         .publish_layer(tracking.publish_layer())
///         .with_broker(MemoryBroker::new(), |b| {
///             b.include(place).out_reply(Publish);
///             b.include(fulfil);
///             b.after_startup(Publish, tracking.republish());
///         })
/// }
/// # }
/// # fn main() {}
/// ```
#[diagnostic::on_unimplemented(
    message = "`{Self}` does not implement `outbox::Publish<{DB}>`, so nothing records what is published under its names",
    label = "no `outbox::Publish` for this record",
    note = "implement `outbox::Publish<{DB}>` for `{Self}`: its statement inserts the record and returns the id"
)]
pub trait Publish<DB: Database>: OutboxRow {
    /// Inserts the record of `msg` and returns its id.
    ///
    /// # Errors
    ///
    /// The database's error; the message is not sent, and the publish fails with it.
    fn publish(
        conn: &mut DB::Connection,
        msg: &OutgoingMessage<'_>,
    ) -> impl Future<Output = Result<Self::Id, Error>> + Send;
}

/// Takes the record `id` into work when its message reaches a subscription.
///
/// The default selects the record while it is unprocessed. `None` means the record is taken or
/// processed already: the handler does not run, and the delivery is acknowledged.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use chrono::{DateTime, Utc};
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
/// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
/// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
/// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
/// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
/// use ruststream_sqlx::{Outbox, outbox};
///
/// #[derive(Outbox, sqlx::FromRow)]
/// #[outbox(table = "outbox", custom(fetch))]
/// pub struct OrderOutbox {
///     #[field(id)]
///     id: i64,
///     #[field(name)]
///     name: String,
///     #[field(payload)]
///     payload: Vec<u8>,
///     #[field(processed_at)]
///     processed_at: Option<DateTime<Utc>>,
/// }
/// # impl outbox::Publish<Postgres> for OrderOutbox {
/// #     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
/// #         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
/// #             .bind(msg.name()).bind(msg.payload()).fetch_one(conn).await
/// #     }
/// # }
///
/// // The service's fetch also stamps when the record was taken.
/// impl outbox::Fetch<Postgres> for OrderOutbox {
///     async fn fetch(conn: &mut PgConnection, id: &i64) -> sqlx::Result<Option<Self>> {
///         sqlx::query_as(
///             "UPDATE outbox SET taken_at = now() WHERE id = $1 AND processed_at IS NULL \
///              RETURNING id, name, payload, processed_at",
///         )
///         .bind(id)
///         .fetch_optional(conn)
///         .await
///     }
/// }
///
/// pub fn app(pool: PgPool) -> impl App {
///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
///     RustStream::new(AppInfo::new("orders", "0.1.0"))
///         .layer(tracking.layer())
///         .publish_layer(tracking.publish_layer())
///         .with_broker(MemoryBroker::new(), |b| {
///             b.include(place).out_reply(Publish);
///             b.include(fulfil);
///             b.after_startup(Publish, tracking.republish());
///         })
/// }
/// # }
/// # fn main() {}
/// ```
#[diagnostic::on_unimplemented(
    message = "`{Self}` names its own fetch and does not implement `outbox::Fetch<{DB}>`",
    label = "the service's own fetch is missing",
    note = "implement `outbox::Fetch<{DB}>` for `{Self}`, or drop `fetch` from `#[outbox(custom(..))]` (or `.own::<own::Fetch>()` from its `OutboxSpec`)"
)]
pub trait Fetch<DB: Database>: OutboxRow {
    /// Takes the record `id` into work, or `None` when it is taken or processed already.
    ///
    /// # Errors
    ///
    /// The database's error; the handler does not run, and the delivery is retried.
    fn fetch(
        conn: &mut DB::Connection,
        id: &Self::Id,
    ) -> impl Future<Output = Result<Option<Self>, Error>> + Send;
}

macro_rules! outcome_event {
    (
        $(#[$doc:meta])* $trait:ident, $method:ident, $event:literal,
        message = $message:literal, note = $note:literal
    ) => {
        $(#[$doc])*
        #[diagnostic::on_unimplemented(
            message = $message,
            label = "the service's own event is missing",
            note = $note
        )]
        pub trait $trait<DB: Database>: OutboxRow {
            #[doc = concat!("Runs the `", $event, "` event for the record `id` after its handler finished.")]
            #[doc = ""]
            #[doc = "# Errors"]
            #[doc = ""]
            #[doc = "The database's error; the record stays unprocessed, and the next startup publishes it again."]
            fn $method(
                conn: &mut DB::Connection,
                id: &Self::Id,
            ) -> impl Future<Output = Result<(), Error>> + Send;
        }
    };
}

outcome_event!(
    /// Marks the record processed once its handler acknowledged the message.
    ///
    /// The default sets `processed_at` from the database's clock, or deletes the record when the
    /// struct has no `processed_at` field.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "outbox", feature = "postgres"))]
    /// # mod demo {
    /// # use chrono::{DateTime, Utc};
    /// # use ruststream::OutgoingMessage;
    /// # use ruststream::memory::prelude::*;
    /// # use serde::{Deserialize, Serialize};
    /// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
    /// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
    /// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
    /// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
    /// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
    /// use ruststream_sqlx::{Outbox, outbox};
    ///
    /// #[derive(Outbox, sqlx::FromRow)]
    /// #[outbox(table = "outbox", custom(ack))]
    /// pub struct OrderOutbox {
    ///     #[field(id)]
    ///     id: i64,
    ///     #[field(name)]
    ///     name: String,
    ///     #[field(payload)]
    ///     payload: Vec<u8>,
    ///     #[field(processed_at)]
    ///     processed_at: Option<DateTime<Utc>>,
    /// }
    /// # impl outbox::Publish<Postgres> for OrderOutbox {
    /// #     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
    /// #         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
    /// #             .bind(msg.name()).bind(msg.payload()).fetch_one(conn).await
    /// #     }
    /// # }
    ///
    /// // A processed record moves to the archive.
    /// impl outbox::Ack<Postgres> for OrderOutbox {
    ///     async fn ack(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
    ///         sqlx::query(
    ///             "WITH done AS (DELETE FROM outbox WHERE id = $1 RETURNING id, name, payload) \
    ///              INSERT INTO outbox_archive (id, name, payload) SELECT id, name, payload FROM done",
    ///         )
    ///         .bind(id)
    ///         .execute(conn)
    ///         .await?;
    ///         Ok(())
    ///     }
    /// }
    ///
    /// pub fn app(pool: PgPool) -> impl App {
    ///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
    ///     RustStream::new(AppInfo::new("orders", "0.1.0"))
    ///         .layer(tracking.layer())
    ///         .publish_layer(tracking.publish_layer())
    ///         .with_broker(MemoryBroker::new(), |b| {
    ///             b.include(place).out_reply(Publish);
    ///             b.include(fulfil);
    ///             b.after_startup(Publish, tracking.republish());
    ///         })
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    Ack, ack, "ack",
    message = "`{Self}` names its own ack and does not implement `outbox::Ack<{DB}>`",
    note = "implement `outbox::Ack<{DB}>` for `{Self}`, or drop `ack` from `#[outbox(custom(..))]` (or `.own::<own::Ack>()` from its `OutboxSpec`)"
);

outcome_event!(
    /// Runs when the handler asked for the message again.
    ///
    /// The default leaves the record unprocessed and runs no statement, so the next startup
    /// publishes it again.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "outbox", feature = "postgres"))]
    /// # mod demo {
    /// # use chrono::{DateTime, Utc};
    /// # use ruststream::OutgoingMessage;
    /// # use ruststream::memory::prelude::*;
    /// # use serde::{Deserialize, Serialize};
    /// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
    /// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
    /// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
    /// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
    /// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
    /// use ruststream_sqlx::{Outbox, outbox};
    ///
    /// #[derive(Outbox, sqlx::FromRow)]
    /// #[outbox(table = "outbox", custom(retry))]
    /// pub struct OrderOutbox {
    ///     #[field(id)]
    ///     id: i64,
    ///     #[field(name)]
    ///     name: String,
    ///     #[field(payload)]
    ///     payload: Vec<u8>,
    ///     #[field(processed_at)]
    ///     processed_at: Option<DateTime<Utc>>,
    /// }
    /// # impl outbox::Publish<Postgres> for OrderOutbox {
    /// #     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
    /// #         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
    /// #             .bind(msg.name()).bind(msg.payload()).fetch_one(conn).await
    /// #     }
    /// # }
    ///
    /// // The service counts how often a consumer asked for a record again.
    /// impl outbox::Retry<Postgres> for OrderOutbox {
    ///     async fn retry(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
    ///         sqlx::query("UPDATE outbox SET retries = retries + 1 WHERE id = $1")
    ///             .bind(id)
    ///             .execute(conn)
    ///             .await?;
    ///         Ok(())
    ///     }
    /// }
    ///
    /// pub fn app(pool: PgPool) -> impl App {
    ///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
    ///     RustStream::new(AppInfo::new("orders", "0.1.0"))
    ///         .layer(tracking.layer())
    ///         .publish_layer(tracking.publish_layer())
    ///         .with_broker(MemoryBroker::new(), |b| {
    ///             b.include(place).out_reply(Publish);
    ///             b.include(fulfil);
    ///             b.after_startup(Publish, tracking.republish());
    ///         })
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    Retry, retry, "retry",
    message = "`{Self}` names its own retry and does not implement `outbox::Retry<{DB}>`",
    note = "implement `outbox::Retry<{DB}>` for `{Self}`, or drop `retry` from `#[outbox(custom(..))]` (or `.own::<own::Retry>()` from its `OutboxSpec`)"
);

outcome_event!(
    /// Runs when the handler dropped the message.
    ///
    /// The default marks the record processed as [`Ack`] does: a dropped message is not sent
    /// again.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(all(feature = "outbox", feature = "postgres"))]
    /// # mod demo {
    /// # use chrono::{DateTime, Utc};
    /// # use ruststream::OutgoingMessage;
    /// # use ruststream::memory::prelude::*;
    /// # use serde::{Deserialize, Serialize};
    /// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
    /// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
    /// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
    /// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
    /// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
    /// use ruststream_sqlx::{Outbox, outbox};
    ///
    /// #[derive(Outbox, sqlx::FromRow)]
    /// #[outbox(table = "outbox", custom(discard))]
    /// pub struct OrderOutbox {
    ///     #[field(id)]
    ///     id: i64,
    ///     #[field(name)]
    ///     name: String,
    ///     #[field(payload)]
    ///     payload: Vec<u8>,
    ///     #[field(processed_at)]
    ///     processed_at: Option<DateTime<Utc>>,
    /// }
    /// # impl outbox::Publish<Postgres> for OrderOutbox {
    /// #     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
    /// #         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
    /// #             .bind(msg.name()).bind(msg.payload()).fetch_one(conn).await
    /// #     }
    /// # }
    ///
    /// // A dropped record is marked apart from a processed one.
    /// impl outbox::Discard<Postgres> for OrderOutbox {
    ///     async fn discard(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
    ///         sqlx::query("UPDATE outbox SET processed_at = now(), dropped = true WHERE id = $1")
    ///             .bind(id)
    ///             .execute(conn)
    ///             .await?;
    ///         Ok(())
    ///     }
    /// }
    ///
    /// pub fn app(pool: PgPool) -> impl App {
    ///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
    ///     RustStream::new(AppInfo::new("orders", "0.1.0"))
    ///         .layer(tracking.layer())
    ///         .publish_layer(tracking.publish_layer())
    ///         .with_broker(MemoryBroker::new(), |b| {
    ///             b.include(place).out_reply(Publish);
    ///             b.include(fulfil);
    ///             b.after_startup(Publish, tracking.republish());
    ///         })
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    Discard, discard, "discard",
    message = "`{Self}` names its own discard and does not implement `outbox::Discard<{DB}>`",
    note = "implement `outbox::Discard<{DB}>` for `{Self}`, or drop `discard` from `#[outbox(custom(..))]` (or `.own::<own::Discard>()` from its `OutboxSpec`)"
);

/// Selects the unprocessed records of one name, for the republish at startup.
///
/// The default selects the records of `name` whose `processed_at` is `NULL`, or every record of
/// `name` when the struct has no `processed_at` field.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use chrono::{DateTime, Utc};
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
/// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
/// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
/// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
/// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
/// use ruststream_sqlx::{Outbox, outbox};
///
/// #[derive(Outbox, sqlx::FromRow)]
/// #[outbox(table = "outbox", custom(recover))]
/// pub struct OrderOutbox {
///     #[field(id)]
///     id: i64,
///     #[field(name)]
///     name: String,
///     #[field(payload)]
///     payload: Vec<u8>,
///     #[field(processed_at)]
///     processed_at: Option<DateTime<Utc>>,
/// }
/// # impl outbox::Publish<Postgres> for OrderOutbox {
/// #     async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
/// #         sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
/// #             .bind(msg.name()).bind(msg.payload()).fetch_one(conn).await
/// #     }
/// # }
///
/// // A record published in the last minute may still be in flight; the next startup takes it.
/// impl outbox::Recover<Postgres> for OrderOutbox {
///     async fn recover(conn: &mut PgConnection, name: &str) -> sqlx::Result<Vec<Self>> {
///         sqlx::query_as(
///             "SELECT id, name, payload, processed_at FROM outbox \
///              WHERE name = $1 AND processed_at IS NULL AND created_at < now() - interval '1 minute'",
///         )
///         .bind(name)
///         .fetch_all(conn)
///         .await
///     }
/// }
///
/// pub fn app(pool: PgPool) -> impl App {
///     let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
///     RustStream::new(AppInfo::new("orders", "0.1.0"))
///         .layer(tracking.layer())
///         .publish_layer(tracking.publish_layer())
///         .with_broker(MemoryBroker::new(), |b| {
///             b.include(place).out_reply(Publish);
///             b.include(fulfil);
///             b.after_startup(Publish, tracking.republish());
///         })
/// }
/// # }
/// # fn main() {}
/// ```
#[diagnostic::on_unimplemented(
    message = "`{Self}` names its own recovery and does not implement `outbox::Recover<{DB}>`",
    label = "the service's own recovery is missing",
    note = "implement `outbox::Recover<{DB}>` for `{Self}`, or drop `recover` from `#[outbox(custom(..))]` (or `.own::<own::Recover>()` from its `OutboxSpec`)"
)]
pub trait Recover<DB: Database>: OutboxRow {
    /// The unprocessed records published under `name`.
    ///
    /// # Errors
    ///
    /// The database's error; startup fails with it.
    fn recover(
        conn: &mut DB::Connection,
        name: &str,
    ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send;
}

/// Every event of a record type on `DB`, each dispatched by its slot in the record's settings:
/// what registering a record requires. Implemented for every [`OutboxTable`] that implements
/// [`Publish`] and every event its settings name as its own. Machinery.
// No message of its own: rustc then reports the unmet bound underneath (the missing `Publish`, the
// missing event of the record's own), with that trait's message.
#[doc(hidden)]
pub trait Tracked<DB: Database>: Publish<DB> {
    /// The record's headers, moved out of it; empty without a headers column.
    fn take_headers(&mut self) -> ruststream::HeaderMap;

    /// Takes the record `id` into work.
    fn fetch_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c Self::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<Option<Self>, Error>> + Send + 'c;

    /// Marks the record `id` acknowledged.
    fn ack_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c Self::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c;

    /// Runs the retry of the record `id`.
    fn retry_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c Self::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c;

    /// Marks the record `id` dropped.
    fn discard_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c Self::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c;

    /// The unprocessed records published under `name`.
    fn recover_records<'c>(
        conn: &'c mut DB::Connection,
        name: &'c str,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send + 'c;
}

impl<DB, Record> Tracked<DB> for Record
where
    DB: Database,
    Record: OutboxTable + Publish<DB>,
    <Declared<Record> as Declaration>::Headers: HeadersOf<Record>,
    <Declared<Record> as Declaration>::OwnFetch: FetchBy<DB, Record>,
    <Declared<Record> as Declaration>::OwnAck: AckBy<DB, Record>,
    <Declared<Record> as Declaration>::OwnRetry: RetryBy<DB, Record>,
    <Declared<Record> as Declaration>::OwnDiscard: DiscardBy<DB, Record>,
    <Declared<Record> as Declaration>::OwnRecover: RecoverBy<DB, Record>,
{
    #[inline]
    fn take_headers(&mut self) -> ruststream::HeaderMap {
        <<Declared<Record> as Declaration>::Headers as HeadersOf<Record>>::take(self)
    }

    fn fetch_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c <Record as OutboxTable>::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<Option<Self>, Error>> + Send + 'c {
        <<Declared<Record> as Declaration>::OwnFetch as FetchBy<DB, Record>>::fetch(
            conn, id, defaults,
        )
    }

    fn ack_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c <Record as OutboxTable>::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c {
        <<Declared<Record> as Declaration>::OwnAck as AckBy<DB, Record>>::ack(conn, id, defaults)
    }

    fn retry_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c <Record as OutboxTable>::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c {
        <<Declared<Record> as Declaration>::OwnRetry as RetryBy<DB, Record>>::retry(
            conn, id, defaults,
        )
    }

    fn discard_record<'c>(
        conn: &'c mut DB::Connection,
        id: &'c <Record as OutboxTable>::Id,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'c {
        <<Declared<Record> as Declaration>::OwnDiscard as DiscardBy<DB, Record>>::discard(
            conn, id, defaults,
        )
    }

    fn recover_records<'c>(
        conn: &'c mut DB::Connection,
        name: &'c str,
        defaults: &'c Defaults,
    ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send + 'c {
        <<Declared<Record> as Declaration>::OwnRecover as RecoverBy<DB, Record>>::recover(
            conn, name, defaults,
        )
    }
}