distributed 2.3.5

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#![cfg(feature = "postgres")]

#[path = "../support/ids.rs"]
mod ids;
#[path = "../support/outbox.rs"]
mod outbox_support;
#[path = "../support/postgres.rs"]
mod postgres;

use std::collections::HashMap;

use ids::unique_id;
use outbox_support::find_outbox_by_id;

use distributed::{
    sourced, Aggregate, AggregateBuilder, CommitBatch, Entity, GetStream, OutboxMessage,
    OutboxMessageStatus, OutboxStore, PostgresRepository, ReadModel, ReadModelWritePlanBuilder,
    ReadModelWritePlanCommitExt, RepositoryError, RowKey, RowPatch, RowValue, StreamIdentity,
    StreamWrite, TableSchemaRegistry, TransactionalCommit,
};
use serde::{Deserialize, Serialize};

#[derive(Default)]
struct Counter {
    entity: Entity,
    value: i32,
}

#[sourced(entity, aggregate_type = "postgres.counter")]
impl Counter {
    #[event("incremented")]
    fn increment(&mut self, id: String, by: i32) {
        self.entity.set_id(&id);
        self.value += by;
    }
}

#[derive(Default)]
struct CounterProjection {
    entity: Entity,
}

#[sourced(entity, aggregate_type = "postgres.counter_projection")]
impl CounterProjection {
    #[event("touched")]
    fn touch(&mut self, id: String) {
        self.entity.set_id(&id);
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ReadModel)]
#[table("postgres_relational_counter_views")]
struct RelationalCounterView {
    #[id]
    id: String,
    value: i64,
    #[readmodel(jsonb)]
    counts: HashMap<String, i64>,
}

async fn repository() -> Option<(postgres::PostgresTestSchema, PostgresRepository)> {
    let schema = postgres::PostgresTestSchema::create_from_env(
        "postgres_repo",
        "skipping Postgres integration test",
    )
    .await?;
    let repo = schema.repository().await;
    Some((schema, repo))
}

// Consumer inbox semantics are covered for all backends by the shared
// `persistent_repository_conformance::inbox` scenarios.

async fn bootstrap_relational_counter_table(repo: &PostgresRepository) {
    let mut registry = TableSchemaRegistry::new();
    registry.register::<RelationalCounterView>().unwrap();
    repo.bootstrap_table_schema_for_dev(&registry)
        .await
        .unwrap();
}

fn relational_counter_key(id: &str) -> RowKey {
    RowKey::new([("id", RowValue::String(id.into()))])
}

#[tokio::test]
async fn migration_is_idempotent_and_uses_postgres_column_types() {
    let Some((schema, repo)) = repository().await else {
        return;
    };
    repo.migrate().await.unwrap();

    let rows = sqlx::query(
        r#"
        SELECT column_name, udt_name
        FROM information_schema.columns
        WHERE table_schema = $1
          AND table_name = 'aggregate_events'
          AND column_name IN ('payload', 'metadata', 'recorded_at')
        "#,
    )
    .bind(schema.schema_name())
    .fetch_all(repo.pool())
    .await
    .unwrap();

    let mut columns = rows
        .into_iter()
        .map(|row| {
            (
                sqlx::Row::try_get::<String, _>(&row, "column_name").unwrap(),
                sqlx::Row::try_get::<String, _>(&row, "udt_name").unwrap(),
            )
        })
        .collect::<Vec<_>>();
    columns.sort();

    assert_eq!(
        columns,
        vec![
            ("metadata".into(), "jsonb".into()),
            ("payload".into(), "bytea".into()),
            ("recorded_at".into(), "timestamptz".into()),
        ]
    );

    let document_table: Option<String> = sqlx::query_scalar(
        r#"
        SELECT table_name
        FROM information_schema.tables
        WHERE table_schema = $1
          AND table_name = 'transactional_read_models'
        "#,
    )
    .bind(schema.schema_name())
    .fetch_optional(repo.pool())
    .await
    .unwrap();
    assert!(document_table.is_none());

    let processed_messages_table: Option<String> = sqlx::query_scalar(
        r#"
        SELECT table_name
        FROM information_schema.tables
        WHERE table_schema = $1
          AND table_name = 'read_model_processed_messages'
        "#,
    )
    .bind(schema.schema_name())
    .fetch_optional(repo.pool())
    .await
    .unwrap();
    assert!(processed_messages_table.is_none());
}

#[tokio::test]
async fn aggregate_stream_round_trips_with_metadata() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    let counter_repo = repo.clone().aggregate::<Counter>();
    let id = unique_id("counter");

    let mut counter = Counter::default();
    counter.entity.set_correlation_id("corr-postgres");
    counter.increment(id.clone(), 2).unwrap();
    counter.increment(id.clone(), 3).unwrap();

    counter_repo.commit(&mut counter).await.unwrap();

    let loaded = counter_repo.get(&id).await.unwrap().unwrap();
    assert_eq!(loaded.value, 5);
    assert_eq!(loaded.entity().events().len(), 2);
    assert_eq!(loaded.entity().events()[0].sequence, 1);
    assert_eq!(loaded.entity().events()[1].sequence, 2);
    assert_eq!(
        loaded.entity().events()[0].correlation_id(),
        Some("corr-postgres")
    );
}

// Optimistic-conflict rollback, duplicate-stream rejection, and snapshot
// identity semantics are covered for all backends by the shared
// `persistent_repository_conformance` scenarios; this main keeps only the
// Postgres-dialect raw-SQL assertions.

#[tokio::test]
async fn read_model_failure_mid_plan_rolls_back_events_and_outbox() {
    // Postgres variant of the SQLite mid-plan rollback test: a commit carrying an
    // aggregate event, an outbox row, and a two-mutation read-model plan whose
    // second mutation violates a real CHECK constraint must roll the WHOLE
    // transaction back — event, outbox row, and the first (already-applied)
    // read-model mutation all absent. Skips locally without DATABASE_URL.
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    bootstrap_relational_counter_table(&repo).await;

    // A real engine-level constraint the read-model schema is unaware of.
    sqlx::query(
        r#"ALTER TABLE "postgres_relational_counter_views"
           ADD CONSTRAINT reject_negative_value CHECK ("value" >= 0)"#,
    )
    .execute(repo.pool())
    .await
    .unwrap();

    let good_id = unique_id("midplan-good");
    let bad_id = unique_id("midplan-bad");
    let mut read_models = ReadModelWritePlanBuilder::new();
    read_models
        .upsert(&RelationalCounterView {
            id: good_id.clone(),
            value: 1,
            counts: HashMap::new(),
        })
        .unwrap();
    read_models
        .upsert(&RelationalCounterView {
            id: bad_id.clone(),
            value: -1,
            counts: HashMap::new(),
        })
        .unwrap();

    let aggregate_id = unique_id("midplan-aggregate");
    let mut projection = CounterProjection::default();
    projection.touch(aggregate_id.clone()).unwrap();
    let identity = StreamIdentity::new(CounterProjection::aggregate_type(), &aggregate_id).unwrap();

    let outbox_id = unique_id("midplan-outbox");
    let outbox_message =
        OutboxMessage::create(&outbox_id, "counter.touched", b"{}".to_vec()).unwrap();

    let err = repo
        .commit_batch(CommitBatch {
            inbox_receipts: Vec::new(),
            streams: vec![StreamWrite::new(identity.clone(), projection.entity_mut())],
            outbox_messages: vec![outbox_message],
            read_model_plans: vec![read_models.into_write_plan().unwrap()],
            snapshots: Vec::new(),
        })
        .await
        .expect_err("a mid-plan constraint violation must fail the commit");
    // A read-model CHECK-constraint violation is a deterministic, non-retryable
    // fault, so it surfaces as the permanent `Storage` variant (not `Model`):
    // re-running the identical write cannot change the outcome.
    assert!(
        matches!(
            &err,
            RepositoryError::Storage {
                retryable: false,
                ..
            }
        ),
        "expected a permanent Storage error from the constraint violation, got {err:?}"
    );
    assert!(!err.is_retryable());

    // 1. Aggregate event absent.
    assert!(
        repo.get_stream(&identity).await.unwrap().is_none(),
        "the aggregate stream must roll back"
    );

    // 2. Outbox row absent.
    assert!(
        find_outbox_by_id(&repo.outbox_store(), &outbox_id)
            .await
            .is_none(),
        "the outbox row must roll back"
    );

    // 3. First read-model mutation absent.
    let good_rows: i64 = sqlx::query_scalar(
        r#"SELECT COUNT(*) FROM "postgres_relational_counter_views" WHERE "id" = $1"#,
    )
    .bind(&good_id)
    .fetch_one(repo.pool())
    .await
    .unwrap();
    assert_eq!(good_rows, 0, "the first read-model mutation must roll back");
}

#[tokio::test]
async fn commit_batch_lowers_relational_read_model_plan_into_registered_table() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    bootstrap_relational_counter_table(&repo).await;
    let id = unique_id("relational-batch");
    let mut counts = HashMap::new();
    counts.insert("wins".to_string(), 2);
    let view = RelationalCounterView {
        id: id.clone(),
        value: 7,
        counts,
    };
    let mut session = ReadModelWritePlanBuilder::new();
    session.upsert(&view).unwrap();
    let mut projection = CounterProjection::default();
    projection.touch(id.clone()).unwrap();
    let identity = StreamIdentity::new(CounterProjection::aggregate_type(), &id).unwrap();

    repo.read_models(session)
        .commit(&mut projection)
        .await
        .unwrap();

    assert!(repo.get_stream(&identity).await.unwrap().is_some());
    let row = sqlx::query(
        r#"
        SELECT "id", "value", "counts"::text AS counts, "_sourced_version"
        FROM "postgres_relational_counter_views"
        WHERE "id" = $1
        "#,
    )
    .bind(&id)
    .fetch_one(repo.pool())
    .await
    .unwrap();
    let stored_counts: String = sqlx::Row::try_get(&row, "counts").unwrap();
    let stored_counts: serde_json::Value = serde_json::from_str(&stored_counts).unwrap();

    assert_eq!(sqlx::Row::try_get::<String, _>(&row, "id").unwrap(), id);
    assert_eq!(sqlx::Row::try_get::<i64, _>(&row, "value").unwrap(), 7);
    assert_eq!(stored_counts["wins"].as_i64(), Some(2));
    assert_eq!(
        sqlx::Row::try_get::<i64, _>(&row, "_sourced_version").unwrap(),
        1
    );
}

#[tokio::test]
async fn read_model_session_patches_and_deletes_relational_rows() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    bootstrap_relational_counter_table(&repo).await;
    let id = unique_id("relational-session");
    let mut counts = HashMap::new();
    counts.insert("wins".to_string(), 2);
    let view = RelationalCounterView {
        id: id.clone(),
        value: 7,
        counts,
    };
    let mut setup = ReadModelWritePlanBuilder::new();
    setup.upsert(&view).unwrap();
    setup.commit(&repo).await.unwrap();

    let mut patched_counts = HashMap::new();
    patched_counts.insert("wins".to_string(), 3);
    patched_counts.insert("losses".to_string(), 1);
    let patch = RowPatch::new()
        .set("value", RowValue::I64(11))
        .set_serde("counts", &patched_counts)
        .unwrap();
    let mut patch_session = ReadModelWritePlanBuilder::new();
    patch_session
        .patch::<RelationalCounterView>(relational_counter_key(&id), patch)
        .unwrap();
    patch_session.commit(&repo).await.unwrap();

    let row = sqlx::query(
        r#"
        SELECT "value", "counts"::text AS counts, "_sourced_version"
        FROM "postgres_relational_counter_views"
        WHERE "id" = $1
        "#,
    )
    .bind(&id)
    .fetch_one(repo.pool())
    .await
    .unwrap();
    let stored_counts: String = sqlx::Row::try_get(&row, "counts").unwrap();
    let stored_counts: serde_json::Value = serde_json::from_str(&stored_counts).unwrap();
    assert_eq!(sqlx::Row::try_get::<i64, _>(&row, "value").unwrap(), 11);
    assert_eq!(stored_counts["wins"].as_i64(), Some(3));
    assert_eq!(stored_counts["losses"].as_i64(), Some(1));
    assert_eq!(
        sqlx::Row::try_get::<i64, _>(&row, "_sourced_version").unwrap(),
        2
    );

    let mut delete_session = ReadModelWritePlanBuilder::new();
    delete_session
        .delete::<RelationalCounterView>(relational_counter_key(&id))
        .unwrap();
    delete_session.commit(&repo).await.unwrap();

    let remaining: i64 = sqlx::query_scalar(
        r#"
        SELECT COUNT(*)
        FROM "postgres_relational_counter_views"
        WHERE "id" = $1
        "#,
    )
    .bind(&id)
    .fetch_one(repo.pool())
    .await
    .unwrap();
    assert_eq!(remaining, 0);
}

#[tokio::test]
async fn read_model_session_persists_relational_rows() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    bootstrap_relational_counter_table(&repo).await;
    let id = unique_id("view");
    let view = RelationalCounterView {
        id: id.clone(),
        value: 42,
        counts: HashMap::new(),
    };
    let mut session = ReadModelWritePlanBuilder::new();
    session.upsert(&view).unwrap();

    let outcome = session.commit(&repo).await.unwrap();
    let row = sqlx::query(
        r#"
        SELECT "value", "_sourced_version"
        FROM "postgres_relational_counter_views"
        WHERE "id" = $1
        "#,
    )
    .bind(&id)
    .fetch_one(repo.pool())
    .await
    .unwrap();

    assert!(outcome.was_applied());
    assert_eq!(sqlx::Row::try_get::<i64, _>(&row, "value").unwrap(), 42);
    assert_eq!(
        sqlx::Row::try_get::<i64, _>(&row, "_sourced_version").unwrap(),
        1
    );
}

#[tokio::test]
async fn unsupported_codec_rows_fail_on_read() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    let id = unique_id("bad-codec");
    sqlx::query(
        r#"
        INSERT INTO aggregate_events (
          aggregate_type,
          aggregate_id,
          sequence,
          event_name,
          event_version,
          payload,
          payload_codec,
          payload_codec_version,
          metadata,
          recorded_at
        )
        VALUES ($1, $2, 1, 'BadEvent', 1, $3, 'json', 1, '{}'::jsonb, now())
        "#,
    )
    .bind("postgres.counter")
    .bind(&id)
    .bind(vec![0_u8])
    .execute(repo.pool())
    .await
    .unwrap();

    let identity = StreamIdentity::new("postgres.counter", &id).unwrap();
    let err = repo.get_stream(&identity).await.unwrap_err();

    assert!(
        matches!(
            &err,
            RepositoryError::Storage {
                retryable: false,
                ..
            }
        ),
        "unexpected error: {err}"
    );
    assert!(
        err.to_string().contains("unsupported payload codec"),
        "unexpected error: {err}"
    );
    assert!(!err.is_retryable());
}

#[tokio::test]
async fn outbox_metadata_columns_round_trip_into_message_metadata() {
    let Some((_schema, repo)) = repository().await else {
        return;
    };
    let message_id = unique_id("outbox-column-metadata");
    sqlx::query(
        r#"
        INSERT INTO outbox_messages (
          message_id,
          event_type,
          payload,
          payload_codec,
          payload_codec_version,
          metadata,
          status,
          created_at,
          next_available_at,
          attempts,
          correlation_id,
          causation_id
        )
        VALUES ($1, 'OutboxColumns', decode('00', 'hex'), 'bytes', 1, '{}'::jsonb,
                'pending', now(), now(), 0, 'corr-column', 'cause-column')
        "#,
    )
    .bind(&message_id)
    .execute(repo.pool())
    .await
    .unwrap();

    let stored = repo
        .outbox_store()
        .messages_by_status(OutboxMessageStatus::Pending, usize::MAX)
        .await
        .unwrap()
        .into_iter()
        .find(|message| message.id() == message_id)
        .unwrap();

    assert_eq!(stored.correlation_id(), Some("corr-column"));
    assert_eq!(stored.causation_id(), Some("cause-column"));
}

#[tokio::test]
async fn backend_termination_mid_commit_rolls_back_and_nothing_persists() {
    // Fault injection: a commit_batch is killed mid-transaction (the backend is
    // terminated while the commit waits on a table lock). The write must roll
    // back completely and surface as a storage error rather than hanging or
    // partially persisting.
    let Some((schema, repo)) = repository().await else {
        return;
    };
    let database_url = std::env::var("DATABASE_URL").expect("repository() checked DATABASE_URL");
    let admin = sqlx::postgres::PgPoolOptions::new()
        .max_connections(2)
        .connect(&database_url)
        .await
        .expect("admin pool connects");

    // Take an ACCESS EXCLUSIVE lock on the events table from a second
    // connection so the commit blocks mid-transaction at its first table touch.
    let mut locker = admin.acquire().await.expect("locker connection");
    let locker_pid: i32 = sqlx::query_scalar("SELECT pg_backend_pid()")
        .fetch_one(&mut *locker)
        .await
        .expect("locker pid");
    sqlx::query("BEGIN")
        .execute(&mut *locker)
        .await
        .expect("begin lock tx");
    sqlx::query(sqlx::AssertSqlSafe(format!(
        "LOCK TABLE \"{}\".aggregate_events IN ACCESS EXCLUSIVE MODE",
        schema.schema_name().replace('"', "\"\"")
    )))
    .execute(&mut *locker)
    .await
    .expect("lock aggregate_events");

    // The commit now parks on the lock inside its transaction.
    let id = unique_id("terminated");
    let commit = {
        let repo = repo.clone();
        let id = id.clone();
        tokio::spawn(async move {
            let mut counter = Counter::default();
            counter.increment(id, 1).unwrap();
            repo.aggregate::<Counter>().commit(&mut counter).await
        })
    };

    // Find the backend our lock is blocking (precisely: blocked BY locker_pid),
    // then terminate it mid-commit.
    let mut victim_pid: Option<i32> = None;
    for _ in 0..100 {
        let blocked: Option<i32> = sqlx::query_scalar(
            "SELECT pid FROM pg_stat_activity WHERE pg_blocking_pids(pid) @> ARRAY[$1] LIMIT 1",
        )
        .bind(locker_pid)
        .fetch_optional(&admin)
        .await
        .expect("pg_stat_activity lookup");
        if let Some(pid) = blocked {
            victim_pid = Some(pid);
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    }
    let victim_pid = victim_pid.expect("the commit should block on the table lock");
    let terminated: bool = sqlx::query_scalar("SELECT pg_terminate_backend($1)")
        .bind(victim_pid)
        .fetch_one(&admin)
        .await
        .expect("pg_terminate_backend");
    assert!(terminated, "the blocked commit backend was terminated");

    let err = commit
        .await
        .expect("commit task joins")
        .expect_err("a terminated backend must fail the commit");
    assert!(
        matches!(&err, RepositoryError::Storage { .. }),
        "the terminated commit surfaces as a storage error, got {err:?}"
    );
    // The termination surfaces as SQLSTATE 57P01; `is_sqlx_transient` now
    // classifies 57P01/57P02/57P03 and class-08 connection failures as
    // transient (losing the connection is an infrastructure hiccup), so the
    // error is retryable.
    assert!(
        err.is_retryable(),
        "57P01 (admin shutdown) must be retryable; got {err:?}"
    );

    // Release the lock and prove the whole transaction rolled back.
    sqlx::query("ROLLBACK")
        .execute(&mut *locker)
        .await
        .expect("release lock");
    let identity = StreamIdentity::new(Counter::aggregate_type(), &id).unwrap();
    assert!(
        repo.get_stream(&identity).await.unwrap().is_none(),
        "nothing from the killed commit may persist"
    );
}