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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
use std::time::Duration;
use sqlx::{PgPool, Postgres, Row, Transaction};
use crate::{
Aggregate, AggregateRoot, Event, EventData, EventStoreError, RecordedEvent,
snapshot::{SNAPSHOT_CATEGORY, Snapshot, SnapshotEnvelope},
stream::{ExpectedVersion, ReadDirection, StreamQuery},
};
/// Whether a transactional load should take a `FOR UPDATE` row-lock on the
/// stream entry. See [`TransactionScope::load_for_update`].
enum LoadLock {
None,
ForUpdate,
}
/// Transaction-scoped advisory-lock key guarding schema migration. Arbitrary
/// but stable: the ASCII bytes of "mire".
const MIGRATE_LOCK_KEY: i64 = 0x6d_69_72_65;
/// Page size for full-stream hydration reads. Hydration pages until a short
/// page so a long stream is never silently truncated (review C1/SNAP-1).
const HYDRATE_PAGE_SIZE: i64 = 1000;
/// Map a queried row into a [`RecordedEvent`]. `transaction_id` is selected as
/// text (`xid8::text`) because sqlx has no native xid8 decoder.
///
/// Fallible: a missing column or type mismatch (`try_get`) surfaces as
/// `Database` rather than panicking in library code (CORE-16), and a
/// `transaction_id` that fails to parse surfaces as `StreamCorruption`
/// rather than silently defaulting to `0` — which would rewind a
/// subscription cursor to the epoch and re-deliver the entire log (CORE-15).
fn map_event(row: &sqlx::postgres::PgRow) -> Result<RecordedEvent, EventStoreError> {
let to_db = |e: sqlx::Error| EventStoreError::Database(e);
let transaction_id_text: String = row.try_get("transaction_id").map_err(to_db)?;
let stream_id: String = row.try_get("stream_id").map_err(to_db)?;
let global_position: i64 = row.try_get("global_position").map_err(to_db)?;
let transaction_id = transaction_id_text.parse().map_err(|_| {
EventStoreError::StreamCorruption {
stream_id: stream_id.clone(),
recorded_version: -1,
read_count: -1,
detail: format!("event at position {global_position} has unparseable transaction_id '{transaction_id_text}'"),
}
})?;
Ok(RecordedEvent {
global_position,
stream_id,
stream_version: row.try_get("stream_version").map_err(to_db)?,
event_type: row.try_get("event_type").map_err(to_db)?,
data: row.try_get("data").map_err(to_db)?,
metadata: row.try_get("metadata").map_err(to_db)?,
transaction_id,
created_at: row.try_get("created_at").map_err(to_db)?,
})
}
#[derive(Clone)]
pub struct EventStore {
pool: PgPool,
}
impl EventStore {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
pub fn pool(&self) -> &PgPool {
&self.pool
}
/// Apply the schema migrations. Idempotent — safe to call at every boot.
///
/// **Run this once at startup, before serving traffic.** The migration
/// runs `CREATE INDEX` / `ALTER TABLE`, which take table-level locks
/// (`ShareLock` / `AccessExclusiveLock`) that conflict with in-flight
/// writers — even on the already-applied no-op path, because Postgres
/// grabs the lock *before* the `IF NOT EXISTS` check. So a migrate that
/// races a live writer to the same table would otherwise block until
/// that writer's transaction ends. To keep that from becoming an
/// invisible, unbounded hang (a paused writer connection shows as
/// `idle in transaction`, so Postgres's deadlock detector never breaks
/// the cycle), the DDL runs under a bounded `lock_timeout`: if a lock
/// can't be taken in time, migrate fails fast with a retryable
/// [`DbErrorKind::LockTimeout`] error instead of wedging.
///
/// The advisory lock that serialises concurrent migrators is acquired
/// *before* the timeout is armed, so multiple replicas booting at once
/// still queue cleanly rather than timing each other out.
pub async fn migrate(&self) -> Result<(), EventStoreError> {
self.migrate_with_lock_timeout(Duration::from_secs(15)).await
}
/// [`migrate`](Self::migrate) with an explicit DDL `lock_timeout`. Tests
/// use a short value so a contended migrate surfaces quickly instead of
/// stalling the suite; production uses the 15s default.
pub async fn migrate_with_lock_timeout(
&self,
lock_timeout: Duration,
) -> Result<(), EventStoreError> {
// Serialize concurrent migrations (parallel tests sharing a database,
// or several service replicas booting at once). `CREATE TABLE IF NOT
// EXISTS` is not concurrency-safe — simultaneous runs race on the
// pg_type catalog and one fails with a duplicate-key error — so we take
// a transaction-scoped advisory lock before applying the schema.
//
// The advisory lock is acquired with NO `lock_timeout` (default 0 =
// wait forever) so genuine migrator serialisation is never spuriously
// aborted; the bounded timeout is armed only for the DDL that follows.
let mut tx = self.pool.begin().await?;
sqlx::query("SELECT pg_advisory_xact_lock($1)")
.bind(MIGRATE_LOCK_KEY)
.execute(&mut *tx)
.await?;
// Arm a bounded lock_timeout for the DDL below (transaction-local).
// A `CREATE INDEX` / `ALTER TABLE` that can't take its table lock —
// because a live writer holds a conflicting row lock — now errors
// with SQLSTATE 55P03 (→ DbErrorKind::LockTimeout, retryable) rather
// than blocking forever behind a connection Postgres sees as idle.
// `set_config(name, value, is_local=true)` is the parameterised
// equivalent of `SET LOCAL lock_timeout` — no dynamic SQL string.
sqlx::query("SELECT set_config('lock_timeout', $1, true)")
.bind(lock_timeout.as_millis().max(1).to_string())
.execute(&mut *tx)
.await?;
sqlx::raw_sql(include_str!("../migrations/001_event_store.sql"))
.execute(&mut *tx)
.await?;
// 002: replica-coordination schema (subscription fence + leases),
// append-only with idempotent ALTER so pre-lease databases upgrade
// correctly (review MIG-1). Applied under the same advisory lock.
sqlx::raw_sql(include_str!("../migrations/002_projection_leases.sql"))
.execute(&mut *tx)
.await?;
// 003: denormalize stream_category onto es_events (plan §P4.5). DDL
// only here — ADD COLUMN (nullable) + the composite cursor index.
sqlx::raw_sql(include_str!("../migrations/003_event_category.sql"))
.execute(&mut *tx)
.await?;
// Backfill stream_category on pre-existing rows in bounded batches,
// still under the advisory lock so concurrent migrators don't double
// it. Blocking on first boot after upgrade is acceptable (pre-1.0,
// small installs — plan §P4.5 "bias to the simpler blocking
// variant"). Batched (not one giant UPDATE) to bound lock/WAL per
// statement; resumable because each pass targets only NULL rows, so a
// crash mid-backfill just resumes on the next migrate. New writes from
// this version already stamp the column, so once this drains there are
// no NULLs and read_category_after can trust the column without a join.
loop {
let updated = sqlx::query(
"UPDATE es_events e
SET stream_category = s.stream_category
FROM es_streams s
WHERE e.stream_id = s.stream_id
AND e.global_position IN (
SELECT global_position FROM es_events
WHERE stream_category IS NULL
ORDER BY global_position
LIMIT 5000
)",
)
.execute(&mut *tx)
.await?
.rows_affected();
if updated == 0 {
break;
}
}
tx.commit().await?;
Ok(())
}
pub async fn load<A: Aggregate>(
&self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
let stream_id = format!("{}-{}", A::stream_category(), id);
let stream_row = sqlx::query("SELECT stream_version FROM es_streams WHERE stream_id = $1")
.bind(&stream_id)
.fetch_optional(&self.pool)
.await?;
let Some(stream_row) = stream_row else {
return Ok(None);
};
let version: i64 = stream_row.get("stream_version");
let events = self.read_stream_all(&stream_id, 0).await?;
Ok(Some(AggregateRoot::hydrate(stream_id, &events, version)?))
}
pub async fn load_or_default<A: Aggregate>(
&self,
id: &str,
) -> Result<AggregateRoot<A>, EventStoreError> {
match self.load::<A>(id).await? {
Some(root) => Ok(root),
None => Ok(AggregateRoot::new(id)),
}
}
/// Cheap presence check: returns `true` iff a stream row exists for
/// `A::stream_category()-id`. Single round-trip — does not fetch
/// events. Prefer this over discarding the result of [`load`] when
/// you just need to know whether the aggregate exists.
pub async fn stream_exists<A: Aggregate>(&self, id: &str) -> Result<bool, EventStoreError> {
let stream_id = format!("{}-{}", A::stream_category(), id);
let exists: bool =
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM es_streams WHERE stream_id = $1)")
.bind(&stream_id)
.fetch_one(&self.pool)
.await?;
Ok(exists)
}
pub async fn save<A: Aggregate>(
&self,
root: &mut AggregateRoot<A>,
) -> Result<(), EventStoreError> {
if !root.has_pending() {
return Ok(());
}
let events = root.take_pending();
let expected = if root.version == 0 {
ExpectedVersion::NoStream
} else {
ExpectedVersion::Exact(root.version)
};
// Pending events are only dropped once the append *succeeds*. On any
// failure (transient DB error, conflict, cancellation) they are
// returned to the root so a retry — which the error type explicitly
// advertises via `is_retryable()` — re-attempts the same write
// rather than silently succeeding as a no-op (review C8/CORE-3).
let metadata = match serde_json::to_value(&root.metadata) {
Ok(m) => m,
Err(e) => {
root.restore_pending(events);
return Err(e.into());
}
};
let wrapped: Vec<Event<A::Event>> = events
.into_iter()
.map(|e| Event::new(e).with_metadata(metadata.clone()))
.collect();
match self
.append(&root.stream_id, A::stream_category(), expected, &wrapped)
.await
{
Ok(new_version) => {
root.version = new_version;
Ok(())
}
Err(e) => {
root.restore_pending(wrapped.into_iter().map(|w| w.event).collect());
Err(e)
}
}
}
/// Append `events` to `stream_id` in a **single atomic statement** —
/// no client-driven `BEGIN`/`COMMIT`.
///
/// The version fence (OCC on `stream_version`) is the whole reason we
/// don't need a pessimistic transaction here: the CAS and the event
/// inserts run as one data-modifying CTE, which Postgres executes in an
/// implicit transaction. That is atomic, costs one round trip instead
/// of three, holds the `es_streams` row lock only for the statement's
/// own execution, and — critically — never leaves the connection
/// `idle in transaction`. A multi-statement `BEGIN/…/COMMIT` parks the
/// connection between statements holding locks, where Postgres's
/// deadlock detector is blind to it; a single statement is always
/// `active` and part of the lock wait-graph, so any contention is
/// detected and broken by Postgres rather than hanging (review
/// CORE-5/CORE-12, and the migrate-vs-writer wedge that motivated this).
///
/// Composable transactional writes (read-before-write via
/// [`TransactionScope`], or the saga runner appending events + routing
/// rows together) still use [`append_in_tx`](Self::append_in_tx),
/// which layers onto a caller-owned transaction.
///
/// Semantic note (breaking, pre-1.0): `Exact(0)` on a missing stream
/// returns `ConcurrencyConflict { expected: 0, actual: 0 }`. Callers
/// wanting "create stream and write at version 0" must use `NoStream`.
pub async fn append<E: EventData>(
&self,
stream_id: &str,
category: &str,
expected: ExpectedVersion,
events: &[Event<E>],
) -> Result<i64, EventStoreError> {
if events.is_empty() {
// An empty append still asserts the expected version: callers
// use it as a fence/precondition (review CORE-14).
let actual = Self::read_current_version_on(&self.pool, stream_id).await?;
match expected {
ExpectedVersion::Exact(v) if v != actual => {
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: v,
actual,
});
}
ExpectedVersion::NoStream if actual != 0 => {
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: 0,
actual,
});
}
_ => {}
}
return Ok(actual);
}
let n_events = events.len() as i64;
// Serialize all payloads up front, before issuing the statement —
// no work happens while the row lock is held.
let mut event_types: Vec<String> = Vec::with_capacity(events.len());
let mut datas: Vec<serde_json::Value> = Vec::with_capacity(events.len());
let mut metadatas: Vec<serde_json::Value> = Vec::with_capacity(events.len());
for event in events {
event_types.push(event.event.event_type().to_string());
datas.push(serde_json::to_value(&event.event)?);
metadatas.push(event.metadata.clone());
}
// One data-modifying CTE per expected-version mode. Each:
// 1. advances `es_streams` (the version CAS / create), RETURNING
// the new high-water version, then
// 2. inserts the N events at the allocated `(base+ord)` slots,
// and the final SELECT returns the new version — or NULL when the
// CAS matched no row (a conflict). Postgres runs every
// data-modifying CTE to completion regardless of whether the final
// SELECT reads it, so the inserts always happen.
//
// `WITH ORDINALITY` numbers the unnested events 1..N; the
// `CROSS JOIN` against the (0-or-1-row) version CTE means a failed
// CAS inserts zero events.
let new_version: Option<i64> = match expected {
// $1=expected version, $2=n, $3=stream_id, $4=types, $5=data, $6=meta, $7=category
ExpectedVersion::Exact(v) => {
sqlx::query_scalar(
"WITH bump AS (
UPDATE es_streams
SET stream_version = $1 + $2, updated_at = now()
WHERE stream_id = $3 AND stream_version = $1
RETURNING stream_version
),
ins AS (
INSERT INTO es_events
(stream_id, stream_version, event_type, data, metadata, stream_category)
SELECT $3, $1 + t.ord, t.etype, t.data, t.meta, $7
FROM bump
CROSS JOIN unnest($4::text[], $5::jsonb[], $6::jsonb[])
WITH ORDINALITY AS t(etype, data, meta, ord)
RETURNING 1
)
SELECT (SELECT stream_version FROM bump) AS new_version",
)
.bind(v)
.bind(n_events)
.bind(stream_id)
.bind(&event_types)
.bind(&datas)
.bind(&metadatas)
.bind(category)
.fetch_one(&self.pool)
.await?
}
// $1=n, $2=stream_id, $3=types, $4=data, $5=meta, $6=category
ExpectedVersion::NoStream => {
sqlx::query_scalar(
"WITH created AS (
INSERT INTO es_streams (stream_id, stream_category, stream_version)
VALUES ($2, $6, $1)
ON CONFLICT (stream_id) DO NOTHING
RETURNING stream_version
),
ins AS (
INSERT INTO es_events
(stream_id, stream_version, event_type, data, metadata, stream_category)
SELECT $2, t.ord, t.etype, t.data, t.meta, $6
FROM created
CROSS JOIN unnest($3::text[], $4::jsonb[], $5::jsonb[])
WITH ORDINALITY AS t(etype, data, meta, ord)
RETURNING 1
)
SELECT (SELECT stream_version FROM created) AS new_version",
)
.bind(n_events)
.bind(stream_id)
.bind(&event_types)
.bind(&datas)
.bind(&metadatas)
.bind(category)
.fetch_one(&self.pool)
.await?
}
// $1=n, $2=stream_id, $3=types, $4=data, $5=meta, $6=category.
// `ON CONFLICT DO UPDATE` is the create-race fix in one shot:
// insert the stream at version N, or atomically increment an
// existing row by N. Either way the upsert returns the new
// high-water version; events land at `(new - N + ord)`.
ExpectedVersion::Any => {
sqlx::query_scalar(
"WITH upsert AS (
INSERT INTO es_streams (stream_id, stream_category, stream_version)
VALUES ($2, $6, $1)
ON CONFLICT (stream_id) DO UPDATE
SET stream_version = es_streams.stream_version + $1,
updated_at = now()
RETURNING stream_version
),
ins AS (
INSERT INTO es_events
(stream_id, stream_version, event_type, data, metadata, stream_category)
SELECT $2, (SELECT stream_version FROM upsert) - $1 + t.ord,
t.etype, t.data, t.meta, $6
FROM unnest($3::text[], $4::jsonb[], $5::jsonb[])
WITH ORDINALITY AS t(etype, data, meta, ord)
RETURNING 1
)
SELECT (SELECT stream_version FROM upsert) AS new_version",
)
.bind(n_events)
.bind(stream_id)
.bind(&event_types)
.bind(&datas)
.bind(&metadatas)
.bind(category)
.fetch_one(&self.pool)
.await?
}
};
match new_version {
Some(v) => Ok(v),
None => {
// The CAS matched no row: read the committed version to
// build a precise conflict (expected 0 for NoStream).
let actual = Self::read_current_version_on(&self.pool, stream_id).await?;
let expected_v = match expected {
ExpectedVersion::Exact(v) => v,
_ => 0,
};
Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: expected_v,
actual,
})
}
}
}
/// Read the current `stream_version` (0 if the stream doesn't exist)
/// on any executor — pool or connection.
async fn read_current_version_on<'e, X>(
executor: X,
stream_id: &str,
) -> Result<i64, EventStoreError>
where
X: sqlx::Executor<'e, Database = Postgres>,
{
let v: Option<i64> =
sqlx::query_scalar("SELECT stream_version FROM es_streams WHERE stream_id = $1")
.bind(stream_id)
.fetch_optional(executor)
.await?;
Ok(v.unwrap_or(0))
}
/// Append `events` to `stream_id`, advancing the stream version
/// atomically.
///
/// **No explicit `SELECT ... FOR UPDATE`** — the version check is folded
/// into the conditional `UPDATE` (or, for fresh streams, the
/// `INSERT ... ON CONFLICT DO NOTHING`), saving one round trip. Note
/// this does **not** make concurrent writers lock-free: Postgres holds
/// a row lock on the `es_streams` row from the moment this `UPDATE`
/// executes until the transaction commits or rolls back, so a second
/// writer to the *same* stream blocks until the first finishes, then
/// re-evaluates the `WHERE` against the committed row (EvalPlanQual at
/// READ COMMITTED) and surfaces `ConcurrencyConflict` on a mismatch.
/// Writers to *different* streams never contend. The
/// `UNIQUE (stream_id, stream_version)` invariant on `es_events` is the
/// backup guarantee against any residual schedule anomaly.
///
/// The lock window currently spans payload serialization, all per-event
/// inserts, and the commit fsync; shortening it (serialize-first, batch
/// insert) is tracked as review CORE-5/CORE-12.
///
/// Semantic note (breaking, pre-1.0): `Exact(0)` on a missing stream
/// now returns `ConcurrencyConflict { expected: 0, actual: 0 }`.
/// Callers wanting "create stream and write at version 0" must use
/// `NoStream` (which is what they should have been doing).
pub async fn append_in_tx<E: EventData>(
tx: &mut Transaction<'_, Postgres>,
stream_id: &str,
category: &str,
expected: ExpectedVersion,
events: &[Event<E>],
) -> Result<i64, EventStoreError> {
if events.is_empty() {
// An empty append still asserts the expected version: callers
// use it as a fence/precondition. Read the current version and
// verify it matches `expected` rather than blindly returning it
// (review CORE-14).
let actual = Self::read_current_version_in_tx(tx, stream_id).await?;
match expected {
ExpectedVersion::Exact(v) if v != actual => {
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: v,
actual,
});
}
ExpectedVersion::NoStream if actual != 0 => {
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: 0,
actual,
});
}
_ => {}
}
return Ok(actual);
}
let n_events = events.len() as i64;
// Allocate the version range via CAS. `new_version` is the
// highest version slot this writer owns; the allocated range is
// `(new_version - n_events .. new_version]`.
let new_version = match expected {
ExpectedVersion::Exact(v) => {
let updated: Option<i64> = sqlx::query_scalar(
"UPDATE es_streams
SET stream_version = $1 + $2,
updated_at = now()
WHERE stream_id = $3
AND stream_version = $1
RETURNING stream_version",
)
.bind(v)
.bind(n_events)
.bind(stream_id)
.fetch_optional(&mut **tx)
.await?;
match updated {
Some(new_v) => new_v,
None => {
let actual = Self::read_current_version_in_tx(tx, stream_id).await?;
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: v,
actual,
});
}
}
}
ExpectedVersion::Any => {
// Try a blind increment first; fall through to INSERT if
// the row doesn't exist yet.
let bumped: Option<i64> = sqlx::query_scalar(
"UPDATE es_streams
SET stream_version = stream_version + $1,
updated_at = now()
WHERE stream_id = $2
RETURNING stream_version",
)
.bind(n_events)
.bind(stream_id)
.fetch_optional(&mut **tx)
.await?;
match bumped {
Some(v) => v,
None => {
// No row — try to create. ON CONFLICT covers the
// race where a peer creates between our UPDATE
// and our INSERT.
let inserted: Option<i64> = sqlx::query_scalar(
"INSERT INTO es_streams (stream_id, stream_category, stream_version)
VALUES ($1, $2, $3)
ON CONFLICT (stream_id) DO NOTHING
RETURNING stream_version",
)
.bind(stream_id)
.bind(category)
.bind(n_events)
.fetch_optional(&mut **tx)
.await?;
match inserted {
Some(v) => v,
None => {
// Lost the create race — retry the
// blind increment, guaranteed to find a
// row this time.
sqlx::query_scalar(
"UPDATE es_streams
SET stream_version = stream_version + $1,
updated_at = now()
WHERE stream_id = $2
RETURNING stream_version",
)
.bind(n_events)
.bind(stream_id)
.fetch_one(&mut **tx)
.await?
}
}
}
}
}
ExpectedVersion::NoStream => {
let inserted: Option<i64> = sqlx::query_scalar(
"INSERT INTO es_streams (stream_id, stream_category, stream_version)
VALUES ($1, $2, $3)
ON CONFLICT (stream_id) DO NOTHING
RETURNING stream_version",
)
.bind(stream_id)
.bind(category)
.bind(n_events)
.fetch_optional(&mut **tx)
.await?;
match inserted {
Some(v) => v,
None => {
let actual = Self::read_current_version_in_tx(tx, stream_id).await?;
return Err(EventStoreError::ConcurrencyConflict {
stream_id: stream_id.to_string(),
expected: 0,
actual,
});
}
}
}
};
// Insert all events into the allocated range in ONE statement
// (`unnest … WITH ORDINALITY`) rather than N per-event inserts.
// Inside a caller-owned transaction the `es_streams` row lock is
// held from the CAS above until the caller commits; batching the
// inserts shortens that window from N round trips to one (review
// CORE-5/CORE-12). Payloads are serialised before the statement so
// no encoding work happens while the lock is held. The base slot is
// `new_version - n_events`, so event `ord` (1-based) lands at
// `base + ord`. The UNIQUE (stream_id, stream_version) constraint
// remains the backstop against any CAS-missed slot collision.
let base = new_version - n_events;
let mut event_types: Vec<String> = Vec::with_capacity(events.len());
let mut datas: Vec<serde_json::Value> = Vec::with_capacity(events.len());
let mut metadatas: Vec<serde_json::Value> = Vec::with_capacity(events.len());
for event in events {
event_types.push(event.event.event_type().to_string());
datas.push(serde_json::to_value(&event.event)?);
metadatas.push(event.metadata.clone());
}
sqlx::query(
"INSERT INTO es_events (stream_id, stream_version, event_type, data, metadata, stream_category)
SELECT $1, $2 + t.ord, t.etype, t.data, t.meta, $6
FROM unnest($3::text[], $4::jsonb[], $5::jsonb[])
WITH ORDINALITY AS t(etype, data, meta, ord)",
)
.bind(stream_id)
.bind(base)
.bind(&event_types)
.bind(&datas)
.bind(&metadatas)
.bind(category)
.execute(&mut **tx)
.await?;
Ok(new_version)
}
async fn read_current_version_in_tx(
tx: &mut Transaction<'_, Postgres>,
stream_id: &str,
) -> Result<i64, EventStoreError> {
let row = sqlx::query("SELECT stream_version FROM es_streams WHERE stream_id = $1")
.bind(stream_id)
.fetch_optional(&mut **tx)
.await?;
Ok(row.map(|r| r.get("stream_version")).unwrap_or(0))
}
pub async fn read_stream(
&self,
stream_id: &str,
query: StreamQuery,
) -> Result<Vec<RecordedEvent>, EventStoreError> {
Self::read_stream_with(&self.pool, stream_id, query).await
}
pub async fn read_stream_with<'e, E>(
executor: E,
stream_id: &str,
query: StreamQuery,
) -> Result<Vec<RecordedEvent>, EventStoreError>
where
E: sqlx::Executor<'e, Database = Postgres>,
{
// Two static query strings (sqlx 0.9 requires `&'static str`): the only
// difference is the sort direction, which is never user input.
let sql: &'static str = match query.direction {
ReadDirection::Forward => {
"SELECT global_position, stream_id, stream_version, event_type, data, metadata, transaction_id::text AS transaction_id, created_at
FROM es_events
WHERE stream_id = $1 AND stream_version >= $2
ORDER BY stream_version ASC
LIMIT $3"
}
ReadDirection::Backward => {
"SELECT global_position, stream_id, stream_version, event_type, data, metadata, transaction_id::text AS transaction_id, created_at
FROM es_events
WHERE stream_id = $1 AND stream_version >= $2
ORDER BY stream_version DESC
LIMIT $3"
}
};
let rows = sqlx::query(sql)
.bind(stream_id)
.bind(query.from_version)
.bind(query.limit)
.fetch_all(executor)
.await?;
rows.iter().map(map_event).collect()
}
/// Read the entire forward history of `stream_id` from `from_version`
/// upward, paging in [`HYDRATE_PAGE_SIZE`] chunks until exhausted.
///
/// Hydration must never use a single row-limited read: a stream longer
/// than the limit would fold to truncated state while still reporting
/// the full `stream_version`, and the resulting bad command decision
/// would pass the optimistic-concurrency check on save (review
/// C1/SNAP-1). The user-facing [`read_stream`](Self::read_stream) keeps
/// its explicit limit for paging APIs; every hydration path uses this.
pub async fn read_stream_all(
&self,
stream_id: &str,
from_version: i64,
) -> Result<Vec<RecordedEvent>, EventStoreError> {
let mut all = Vec::new();
let mut next = from_version;
loop {
let page = Self::read_stream_with(
&self.pool,
stream_id,
StreamQuery {
direction: ReadDirection::Forward,
from_version: next,
limit: HYDRATE_PAGE_SIZE,
},
)
.await?;
let len = page.len() as i64;
if let Some(last) = page.last() {
next = last.stream_version + 1;
}
all.extend(page);
if len < HYDRATE_PAGE_SIZE {
break;
}
}
Ok(all)
}
/// Transaction-scoped counterpart to [`read_stream_all`](Self::read_stream_all),
/// paging within the caller's transaction.
async fn read_stream_all_in_tx(
tx: &mut Transaction<'_, Postgres>,
stream_id: &str,
from_version: i64,
) -> Result<Vec<RecordedEvent>, EventStoreError> {
let mut all = Vec::new();
let mut next = from_version;
loop {
let page = Self::read_stream_with(
&mut **tx,
stream_id,
StreamQuery {
direction: ReadDirection::Forward,
from_version: next,
limit: HYDRATE_PAGE_SIZE,
},
)
.await?;
let len = page.len() as i64;
if let Some(last) = page.last() {
next = last.stream_version + 1;
}
all.extend(page);
if len < HYDRATE_PAGE_SIZE {
break;
}
}
Ok(all)
}
/// Read committed events across all streams after the `(transaction_id,
/// position)` cursor, in transaction-then-position order. Events from
/// still-in-flight transactions are excluded, so the result is gapless and
/// safe to checkpoint past. This backs catch-up subscriptions.
pub async fn read_all_after(
&self,
after_transaction_id: u64,
after_position: i64,
limit: i64,
) -> Result<Vec<RecordedEvent>, EventStoreError> {
let rows = sqlx::query(
"SELECT global_position, stream_id, stream_version, event_type, data, metadata, transaction_id::text AS transaction_id, created_at
FROM es_events
WHERE (
(transaction_id = $1::text::xid8 AND global_position > $2)
OR transaction_id > $1::text::xid8
)
AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
ORDER BY transaction_id ASC, global_position ASC
LIMIT $3",
)
.bind(after_transaction_id.to_string())
.bind(after_position)
.bind(limit)
.fetch_all(&self.pool)
.await?;
rows.iter().map(map_event).collect()
}
/// Like [`read_all_after`](Self::read_all_after) but restricted to one
/// stream category.
pub async fn read_category_after(
&self,
category: &str,
after_transaction_id: u64,
after_position: i64,
limit: i64,
) -> Result<Vec<RecordedEvent>, EventStoreError> {
let rows = sqlx::query(
// No es_streams join: stream_category is denormalized onto
// es_events (plan §P4.5), so this is a single-table range scan on
// idx_es_events_category_cursor. Backfilled + stamped on write, so
// the column is non-null for every row by the time reads run.
"SELECT e.global_position, e.stream_id, e.stream_version, e.event_type, e.data, e.metadata, e.transaction_id::text AS transaction_id, e.created_at
FROM es_events e
WHERE e.stream_category = $1
AND (
(e.transaction_id = $2::text::xid8 AND e.global_position > $3)
OR e.transaction_id > $2::text::xid8
)
AND e.transaction_id < pg_snapshot_xmin(pg_current_snapshot())
ORDER BY e.transaction_id ASC, e.global_position ASC
LIMIT $4",
)
.bind(category)
.bind(after_transaction_id.to_string())
.bind(after_position)
.bind(limit)
.fetch_all(&self.pool)
.await?;
rows.iter().map(map_event).collect()
}
/// A coarse health signal for one subscription: the newest event's global
/// position minus the subscription's last checkpoint. A growing value means
/// the projection is falling behind.
///
/// Note this compares against the *global* max position, so a
/// category-scoped subscription will report non-zero lag whenever other
/// categories receive events — treat it as a trend, not an exact backlog.
pub async fn projection_lag(&self, subscription_id: &str) -> Result<i64, EventStoreError> {
let max_position: i64 =
sqlx::query_scalar("SELECT COALESCE(MAX(global_position), 0) FROM es_events")
.fetch_one(&self.pool)
.await?;
let last_position: i64 = sqlx::query_scalar(
"SELECT COALESCE(
(SELECT last_position FROM es_subscriptions WHERE subscription_id = $1),
0
)",
)
.bind(subscription_id)
.fetch_one(&self.pool)
.await?;
Ok(max_position - last_position)
}
/// Load an aggregate, seeding from its latest snapshot when one is present
/// and current. Falls back to a full replay if there is no snapshot, the
/// snapshot's version no longer matches, or it fails to deserialize —
/// snapshots are a disposable optimisation, never the source of truth.
/// Returns `None` if the stream does not exist.
pub async fn load_snapshotted<A: Snapshot>(
&self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
let stream_id = format!("{}-{}", A::stream_category(), id);
let stream_row = sqlx::query("SELECT stream_version FROM es_streams WHERE stream_id = $1")
.bind(&stream_id)
.fetch_optional(&self.pool)
.await?;
let Some(stream_row) = stream_row else {
return Ok(None);
};
let current_version: i64 = stream_row.get("stream_version");
// Latest snapshot for this stream (highest version, read backwards).
let snapshot_stream = format!("{stream_id}-snapshot");
let latest = self
.read_stream(
&snapshot_stream,
StreamQuery {
direction: ReadDirection::Backward,
from_version: 0,
limit: 1,
},
)
.await?;
if let Some(recorded) = latest.first()
&& let Ok(envelope) = serde_json::from_value::<SnapshotEnvelope>(recorded.data.clone())
&& envelope.snapshot_version == A::SNAPSHOT_VERSION
&& let Ok(state) = serde_json::from_value::<A>(envelope.state)
{
let mut root = AggregateRoot::from_snapshot(stream_id.clone(), state, envelope.version);
if envelope.version < current_version {
let tail = self
.read_stream_all(&stream_id, envelope.version + 1)
.await?;
Self::apply_tail(&mut root, &tail)?;
}
root.version = current_version;
return Ok(Some(root));
}
// Fall back to a full replay.
let events = self.read_stream_all(&stream_id, 0).await?;
Ok(Some(AggregateRoot::hydrate(
stream_id,
&events,
current_version,
)?))
}
/// Snapshot-aware counterpart to [`load_or_default`]: seeds from the
/// latest snapshot if one is current, replaying only the tail; falls
/// back to a full replay otherwise, or returns a fresh `AggregateRoot`
/// if the stream doesn't exist.
pub async fn load_or_default_snapshotted<A: Snapshot>(
&self,
id: &str,
) -> Result<AggregateRoot<A>, EventStoreError> {
match self.load_snapshotted::<A>(id).await? {
Some(root) => Ok(root),
None => Ok(AggregateRoot::new(id)),
}
}
/// Save pending events like [`save`](Self::save), then write a snapshot if
/// the new version crosses a [`SNAPSHOT_FREQUENCY`](Snapshot::SNAPSHOT_FREQUENCY)
/// boundary. This is the snapshot-aware save path.
pub async fn save_snapshotting<A: Snapshot>(
&self,
root: &mut AggregateRoot<A>,
) -> Result<(), EventStoreError> {
let old_version = root.version;
self.save(root).await?;
let freq = A::SNAPSHOT_FREQUENCY;
if freq > 0 && root.version / freq > old_version / freq {
self.save_snapshot(root).await?;
}
Ok(())
}
/// Explicitly write a snapshot of `root`'s current state into its snapshot
/// stream. Idempotent in effect: only the latest snapshot is ever read.
pub async fn save_snapshot<A: Snapshot>(
&self,
root: &AggregateRoot<A>,
) -> Result<(), EventStoreError> {
let envelope = SnapshotEnvelope {
snapshot_version: A::SNAPSHOT_VERSION,
version: root.version,
state: serde_json::to_value(&root.state)?,
};
let snapshot_stream = format!("{}-snapshot", root.stream_id);
self.append::<SnapshotEnvelope>(
&snapshot_stream,
SNAPSHOT_CATEGORY,
ExpectedVersion::Any,
&[Event::new(envelope)],
)
.await?;
Ok(())
}
/// Deserialize and fold `tail` events onto a snapshot-seeded root.
/// Shared by the snapshot load paths (pool and transaction).
fn apply_tail<A: Aggregate>(
root: &mut AggregateRoot<A>,
tail: &[RecordedEvent],
) -> Result<(), EventStoreError>
where
A::Event: serde::de::DeserializeOwned,
{
for tail_event in tail {
let event =
serde_json::from_value::<A::Event>(tail_event.data.clone()).map_err(|source| {
EventStoreError::Deserialization {
stream_id: tail_event.stream_id.clone(),
global_position: tail_event.global_position,
event_type: tail_event.event_type.clone(),
source,
}
})?;
root.state.apply(&event);
}
Ok(())
}
pub async fn begin_transaction(&self) -> Result<TransactionScope<'_>, EventStoreError> {
let tx = self.pool.begin().await?;
Ok(TransactionScope {
store: self,
tx,
committed_events: Vec::new(),
})
}
}
/// **Read-before-write escape hatch.** In a strict event-sourced design,
/// commands read from projections (CQRS) and write to aggregates. The
/// `TransactionScope` exists for edge cases where that separation is
/// impractical: cross-aggregate invariants enforced in a single deployment,
/// recovery tooling, one-shot migrations. **Prefer a projection-driven
/// design** wherever you can — a long-lived `TransactionScope` on a hot
/// stream blocks other writers and couples your read path to the event
/// store's transaction lifecycle.
///
/// If you find yourself reaching for [`load_for_update`](Self::load_for_update)
/// in steady-state command code, that's a signal the projection boundary is
/// in the wrong place.
pub struct TransactionScope<'a> {
#[allow(dead_code)]
store: &'a EventStore,
tx: Transaction<'static, Postgres>,
committed_events: Vec<RecordedEvent>,
}
impl<'a> TransactionScope<'a> {
/// Load `A`'s aggregate within this transaction. Returns `None` if the
/// stream does not exist. Does **not** lock the stream row — concurrent
/// writers to this stream are still possible until [`save`](Self::save)
/// takes its own `FOR UPDATE`.
pub async fn load<A: Aggregate>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
self.load_with::<A>(id, LoadLock::None).await
}
/// Load `A`'s aggregate within this transaction and take a `FOR UPDATE`
/// row-lock on its stream entry. Returns `None` if the stream does not
/// exist; in that case no lock is acquired (there is no row to lock),
/// and the standard optimistic-concurrency check at save time still
/// guards the create path.
///
/// Use this when a *different* stream's write depends on this
/// aggregate's state and must not race a concurrent writer to *this*
/// stream. Without the lock, you observe a snapshot that can be
/// invalidated before commit.
///
/// Hot-path warning: holds the row lock until the transaction commits
/// or rolls back. Long-running scopes block other writers to the same
/// stream.
pub async fn load_for_update<A: Aggregate>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
self.load_with::<A>(id, LoadLock::ForUpdate).await
}
pub async fn load_or_default<A: Aggregate>(
&mut self,
id: &str,
) -> Result<AggregateRoot<A>, EventStoreError> {
match self.load::<A>(id).await? {
Some(root) => Ok(root),
None => Ok(AggregateRoot::new(id)),
}
}
/// Like [`load_or_default`](Self::load_or_default) but takes
/// `FOR UPDATE` on the stream row when one exists. See
/// [`load_for_update`](Self::load_for_update) for caveats.
pub async fn load_or_default_for_update<A: Aggregate>(
&mut self,
id: &str,
) -> Result<AggregateRoot<A>, EventStoreError> {
match self.load_for_update::<A>(id).await? {
Some(root) => Ok(root),
None => Ok(AggregateRoot::new(id)),
}
}
async fn load_with<A: Aggregate>(
&mut self,
id: &str,
lock: LoadLock,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
let stream_id = format!("{}-{}", A::stream_category(), id);
let sql = match lock {
LoadLock::None => "SELECT stream_version FROM es_streams WHERE stream_id = $1",
LoadLock::ForUpdate => {
"SELECT stream_version FROM es_streams WHERE stream_id = $1 FOR UPDATE"
}
};
let stream_row = sqlx::query(sql)
.bind(&stream_id)
.fetch_optional(&mut *self.tx)
.await?;
let Some(row) = stream_row else {
return Ok(None);
};
let version: i64 = row.get("stream_version");
let events = EventStore::read_stream_all_in_tx(&mut self.tx, &stream_id, 0).await?;
Ok(Some(AggregateRoot::hydrate(stream_id, &events, version)?))
}
/// Snapshot-aware load within the transaction. Mirrors
/// [`EventStore::load_snapshotted`], reading the snapshot and tail
/// through the scope's transaction.
///
/// **Consistency caveat:** a default sqlx transaction runs at READ
/// COMMITTED, where *each statement* takes a fresh snapshot — so the
/// stream-version, snapshot, and tail reads here do **not** observe a
/// single point-in-time view, and a concurrent writer between them can
/// yield a `(state, version)` pair where `state` reflects more events
/// than `version`. The optimistic-concurrency check at save time still
/// prevents log corruption, but a cross-stream decision made on the
/// returned pair can be inconsistent. For a true point-in-time read,
/// take `load_for_update` (locks the stream row) or run the scope at
/// REPEATABLE READ. (Review CORE-9/SNAP-3.)
pub async fn load_snapshotted<A: Snapshot>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError> {
let stream_id = format!("{}-{}", A::stream_category(), id);
let stream_row = sqlx::query("SELECT stream_version FROM es_streams WHERE stream_id = $1")
.bind(&stream_id)
.fetch_optional(&mut *self.tx)
.await?;
let Some(stream_row) = stream_row else {
return Ok(None);
};
let current_version: i64 = stream_row.get("stream_version");
let snapshot_stream = format!("{stream_id}-snapshot");
let latest = EventStore::read_stream_with(
&mut *self.tx,
&snapshot_stream,
StreamQuery {
direction: ReadDirection::Backward,
from_version: 0,
limit: 1,
},
)
.await?;
if let Some(recorded) = latest.first()
&& let Ok(envelope) = serde_json::from_value::<SnapshotEnvelope>(recorded.data.clone())
&& envelope.snapshot_version == A::SNAPSHOT_VERSION
&& let Ok(state) = serde_json::from_value::<A>(envelope.state)
{
let mut root = AggregateRoot::from_snapshot(stream_id.clone(), state, envelope.version);
if envelope.version < current_version {
let tail = EventStore::read_stream_all_in_tx(
&mut self.tx,
&stream_id,
envelope.version + 1,
)
.await?;
EventStore::apply_tail(&mut root, &tail)?;
}
root.version = current_version;
return Ok(Some(root));
}
let events = EventStore::read_stream_all_in_tx(&mut self.tx, &stream_id, 0).await?;
Ok(Some(AggregateRoot::hydrate(
stream_id,
&events,
current_version,
)?))
}
pub async fn save<A: Aggregate>(
&mut self,
root: &mut AggregateRoot<A>,
) -> Result<(), EventStoreError> {
if !root.has_pending() {
return Ok(());
}
let events = root.take_pending();
let old_version = root.version;
let expected = if old_version == 0 {
ExpectedVersion::NoStream
} else {
ExpectedVersion::Exact(old_version)
};
// Restore pending on failure so the root stays retryable; the events
// are only consumed once the in-transaction append succeeds (C8).
let metadata = match serde_json::to_value(&root.metadata) {
Ok(m) => m,
Err(e) => {
root.restore_pending(events);
return Err(e.into());
}
};
let wrapped: Vec<Event<A::Event>> = events
.into_iter()
.map(|e| Event::new(e).with_metadata(metadata.clone()))
.collect();
let new_version = match EventStore::append_in_tx(
&mut self.tx,
&root.stream_id,
A::stream_category(),
expected,
&wrapped,
)
.await
{
Ok(v) => v,
Err(e) => {
root.restore_pending(wrapped.into_iter().map(|w| w.event).collect());
return Err(e);
}
};
root.version = new_version;
let recorded =
EventStore::read_stream_all_in_tx(&mut self.tx, &root.stream_id, old_version + 1)
.await?;
self.committed_events.extend(recorded);
Ok(())
}
pub async fn append<E: EventData>(
&mut self,
stream_id: &str,
category: &str,
expected: ExpectedVersion,
events: &[Event<E>],
) -> Result<i64, EventStoreError> {
EventStore::append_in_tx(&mut self.tx, stream_id, category, expected, events).await
}
pub fn tx(&mut self) -> &mut Transaction<'static, Postgres> {
&mut self.tx
}
pub fn take_committed_events(&mut self) -> Vec<RecordedEvent> {
std::mem::take(&mut self.committed_events)
}
pub async fn commit(self) -> Result<CommittedEvents, EventStoreError> {
self.tx.commit().await?;
Ok(CommittedEvents {
events: self.committed_events,
})
}
pub async fn rollback(self) -> Result<(), EventStoreError> {
self.tx.rollback().await?;
Ok(())
}
}
pub struct CommittedEvents {
pub events: Vec<RecordedEvent>,
}
impl CommittedEvents {
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
}