@reventlessdev/reventless-postgres 3.0.0-alpha.1 → 3.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +78 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessPostgres.cmi +0 -0
- package/lib/bs/ReventlessPostgres.cmt +0 -0
- package/lib/bs/ReventlessPostgres.mlmap +5 -0
- package/lib/bs/compiler-info.json +1 -1
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.res +25 -340
- package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogChangeFeed.ast +0 -0
- package/lib/bs/src/EventLogChangeFeed.res +184 -0
- package/lib/bs/src/EventLogChangeFeed.res.mjs +134 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.res +12 -170
- package/lib/bs/src/EventLogStorage_Postgres.res.mjs +20 -186
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res +180 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgSchema.ast +0 -0
- package/lib/bs/src/PgSchema.res +36 -2
- package/lib/bs/src/PgSchema.res.mjs +9 -2
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.res +10 -212
- package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +27 -191
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +229 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryEnginePostgres.ast +0 -0
- package/lib/bs/src/QueryEnginePostgres.res +259 -0
- package/lib/bs/src/QueryEnginePostgres.res.mjs +165 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.res +68 -0
- package/lib/bs/tests/PostgresIntegrationTest.res.mjs +49 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res +312 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +461 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.res +25 -340
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +350 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogChangeFeed.ast +0 -0
- package/lib/ocaml/EventLogChangeFeed.res +184 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.res +12 -170
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.res +180 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgSchema.ast +0 -0
- package/lib/ocaml/PgSchema.res +36 -2
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PostgresIntegrationTest.ast +0 -0
- package/lib/ocaml/PostgresIntegrationTest.res +68 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.res +10 -212
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +229 -0
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgres.ast +0 -0
- package/lib/ocaml/QueryEnginePostgres.res +259 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest.res +312 -0
- package/lib/ocaml/ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/ReventlessPostgres.mlmap +5 -0
- package/package.json +8 -8
- package/src/DcbEventLogStorage_Postgres.res +25 -340
- package/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
- package/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
- package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
- package/src/EventLogChangeFeed.res +184 -0
- package/src/EventLogChangeFeed.res.mjs +134 -0
- package/src/EventLogStorage_Postgres.res +12 -170
- package/src/EventLogStorage_Postgres.res.mjs +20 -186
- package/src/EventLogStorage_Postgres_Ops.res +180 -0
- package/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
- package/src/PgSchema.res +36 -2
- package/src/PgSchema.res.mjs +9 -2
- package/src/QueryDbStorage_Postgres.res +10 -212
- package/src/QueryDbStorage_Postgres.res.mjs +27 -191
- package/src/QueryDbStorage_Postgres_Ops.res +229 -0
- package/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
- package/src/QueryEnginePostgres.res +259 -0
- package/src/QueryEnginePostgres.res.mjs +165 -0
- package/tests/PostgresIntegrationTest.res +68 -0
- package/tests/PostgresIntegrationTest.res.mjs +49 -0
- package/tests/QueryEnginePostgresListPageTest.res +312 -0
- package/tests/QueryEnginePostgresListPageTest.res.mjs +461 -0
|
@@ -1,176 +1,18 @@
|
|
|
1
|
-
//
|
|
2
|
-
// EventLogStorage_Sqlite; the only structural difference is that every operation
|
|
3
|
-
// is async (pg has no synchronous client) and `payload jsonb` returns already
|
|
4
|
-
// parsed, so there is no per-row JSON.parse.
|
|
1
|
+
// Deploy/local wrapper over the runtime-pure EventLogStorage_Postgres_Ops.
|
|
5
2
|
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
3
|
+
// The runtime ops (and helpers like `countAll`) live in
|
|
4
|
+
// EventLogStorage_Postgres_Ops, which imports NO `@pulumi/pulumi` so the deployed
|
|
5
|
+
// Lambda import graph stays clean (the aggregate entry point pulls the ops in
|
|
6
|
+
// unconditionally at cold start — a deploy-time dep here would fail resolution on
|
|
7
|
+
// real Lambda). This module `include`s that pure module and adds only the
|
|
8
|
+
// deploy-time `EventLog_Adapter` shape (`operations` wrapped in `Pulumi.Output`),
|
|
9
|
+
// keeping the historic `makeStorage` 3-tuple `(name, ops, adapter)` API unchanged.
|
|
10
|
+
// See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
14
11
|
|
|
15
12
|
open ReventlessCore
|
|
13
|
+
include EventLogStorage_Postgres_Ops
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
type onAppended = array<(string, int)> => unit
|
|
20
|
-
let noTracking: onAppended = _ => ()
|
|
21
|
-
|
|
22
|
-
let decodePayload = (row: dict<JSON.t>): JSON.t =>
|
|
23
|
-
row->Dict.get("payload")->Option.getOr(JSON.Encode.null)
|
|
24
|
-
|
|
25
|
-
let coerceInt = (v: JSON.t): int =>
|
|
26
|
-
switch v {
|
|
27
|
-
| JSON.Number(n) => Float.toInt(n)
|
|
28
|
-
| JSON.String(s) => s->Int.fromString->Option.getOr(0)
|
|
29
|
-
| _ => 0
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Total events across all aggregates. Seeds the event-tap counter at startup so
|
|
33
|
-
// timeline numbering continues across restarts.
|
|
34
|
-
let countAll = async (pool: PgDriver.pool): int =>
|
|
35
|
-
switch await pool->PgDriver.queryOne("SELECT COUNT(*)::bigint AS c FROM event_log", []) {
|
|
36
|
-
| Some(row) => row->Dict.get("c")->Option.map(coerceInt)->Option.getOr(0)
|
|
37
|
-
| None => 0
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let isUniqueViolation = (exn: exn): bool => {
|
|
41
|
-
let msg = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("")
|
|
42
|
-
// pg surfaces the SQLSTATE on `.code`; the message also carries the constraint.
|
|
43
|
-
msg->String.includes("duplicate key") ||
|
|
44
|
-
msg->String.includes("unique") ||
|
|
45
|
-
msg->String.includes("23505")
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let makeStorage = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking) => {
|
|
49
|
-
let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
|
|
50
|
-
// Empty append is a legitimate no-op (at-least-once retries, idempotent
|
|
51
|
-
// commands), NOT a conflict — return before the guarded insert whose 0-row
|
|
52
|
-
// result would otherwise be read as a conflict.
|
|
53
|
-
if jsons->Array.length == 0 {
|
|
54
|
-
Ok()
|
|
55
|
-
} else {
|
|
56
|
-
try {
|
|
57
|
-
let rows = await pool->PgDriver.query(
|
|
58
|
-
"INSERT INTO event_log(log_name, aggregate_id, seq_nr, payload, msg_id)
|
|
59
|
-
SELECT $1, $2, $3::bigint + (ord - 1), value, value->>'msgId'
|
|
60
|
-
FROM jsonb_array_elements($4::jsonb) WITH ORDINALITY AS t(value, ord)
|
|
61
|
-
WHERE $3::bigint = (SELECT COALESCE(MAX(seq_nr), -1) + 1
|
|
62
|
-
FROM event_log
|
|
63
|
-
WHERE log_name = $1 AND aggregate_id = $2)
|
|
64
|
-
RETURNING global_seq, msg_id",
|
|
65
|
-
[
|
|
66
|
-
JSON.Encode.string(name),
|
|
67
|
-
JSON.Encode.string(id),
|
|
68
|
-
JSON.Encode.int(seqNr),
|
|
69
|
-
JSON.Encode.string(JSON.stringify(JSON.Array(jsons))),
|
|
70
|
-
],
|
|
71
|
-
)
|
|
72
|
-
if rows->Array.length == 0 {
|
|
73
|
-
// WHERE guard rejected the seq — a genuine OCC conflict (gap or stale).
|
|
74
|
-
Error(EventLog.Conflict)
|
|
75
|
-
} else {
|
|
76
|
-
if track {
|
|
77
|
-
onAppended(
|
|
78
|
-
rows->Array.filterMap(row =>
|
|
79
|
-
switch (row->Dict.get("msg_id"), row->Dict.get("global_seq")) {
|
|
80
|
-
| (Some(JSON.String(msgId)), Some(gseq)) => Some((msgId, coerceInt(gseq)))
|
|
81
|
-
| _ => None
|
|
82
|
-
}
|
|
83
|
-
),
|
|
84
|
-
)
|
|
85
|
-
}
|
|
86
|
-
Ok()
|
|
87
|
-
}
|
|
88
|
-
} catch {
|
|
89
|
-
| exn =>
|
|
90
|
-
isUniqueViolation(exn)
|
|
91
|
-
? Error(EventLog.Conflict)
|
|
92
|
-
: Error(
|
|
93
|
-
EventLog.StorageFailure(
|
|
94
|
-
exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
|
|
95
|
-
),
|
|
96
|
-
)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
let append: EventLog.append<string, JSON.t> = (seqNr, id, jsons) =>
|
|
102
|
-
appendTracked(~track=true, seqNr, id, jsons)
|
|
103
|
-
|
|
104
|
-
let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
|
|
105
|
-
(await pool->PgDriver.query(
|
|
106
|
-
"SELECT payload FROM event_log
|
|
107
|
-
WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
|
|
108
|
-
ORDER BY seq_nr ASC",
|
|
109
|
-
[JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
|
|
110
|
-
))->Array.map(decodePayload)
|
|
111
|
-
|
|
112
|
-
let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
|
|
113
|
-
|
|
114
|
-
let replayStream = (id, ~fromSeq=?) =>
|
|
115
|
-
Effect.promise(() => replayArray(id, ~fromSeq?))
|
|
116
|
-
->Stream.fromEffect
|
|
117
|
-
->Stream.flatMap(arr => Stream.fromIterable(arr))
|
|
118
|
-
|
|
119
|
-
let latestSnapshot: EventLog.latestSnapshot<string> = async id =>
|
|
120
|
-
switch await pool->PgDriver.queryOne(
|
|
121
|
-
"SELECT seq_nr, state, schema_hash FROM snapshot WHERE log_name = $1 AND aggregate_id = $2",
|
|
122
|
-
[JSON.Encode.string(name), JSON.Encode.string(id)],
|
|
123
|
-
) {
|
|
124
|
-
| Some(row) =>
|
|
125
|
-
switch (row->Dict.get("seq_nr"), row->Dict.get("state"), row->Dict.get("schema_hash")) {
|
|
126
|
-
| (Some(seq), Some(state), Some(JSON.String(schemaHash))) =>
|
|
127
|
-
Ok(Some({EventLog.seqNr: coerceInt(seq), state, schemaHash}))
|
|
128
|
-
| _ => Error("snapshot row has unexpected shape")
|
|
129
|
-
}
|
|
130
|
-
| None => Ok(None)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
let writeSnapshot: EventLog.writeSnapshot<string> = async (id, snap) =>
|
|
134
|
-
try {
|
|
135
|
-
let _ = await pool->PgDriver.query(
|
|
136
|
-
"INSERT INTO snapshot(log_name, aggregate_id, seq_nr, state, schema_hash)
|
|
137
|
-
VALUES ($1, $2, $3::bigint, $4::jsonb, $5)
|
|
138
|
-
ON CONFLICT (log_name, aggregate_id)
|
|
139
|
-
DO UPDATE SET seq_nr = EXCLUDED.seq_nr, state = EXCLUDED.state, schema_hash = EXCLUDED.schema_hash",
|
|
140
|
-
[
|
|
141
|
-
JSON.Encode.string(name),
|
|
142
|
-
JSON.Encode.string(id),
|
|
143
|
-
JSON.Encode.int(snap.EventLog.seqNr),
|
|
144
|
-
JSON.Encode.string(JSON.stringify(snap.state)),
|
|
145
|
-
JSON.Encode.string(snap.schemaHash),
|
|
146
|
-
],
|
|
147
|
-
)
|
|
148
|
-
Ok()
|
|
149
|
-
} catch {
|
|
150
|
-
| exn =>
|
|
151
|
-
Error(exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("snapshot write error"))
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
|
|
155
|
-
stream
|
|
156
|
-
->Stream.runCollect
|
|
157
|
-
->Effect.flatMap(jsons =>
|
|
158
|
-
Effect.promise(() => appendTracked(~track=false, startingSeqNr, id, jsons))->Effect.flatMap(result =>
|
|
159
|
-
switch result {
|
|
160
|
-
| Ok() => Effect.succeed()
|
|
161
|
-
| Error(EventLog.Conflict) => Effect.fail("conflict")
|
|
162
|
-
| Error(StorageFailure(msg)) => Effect.fail(msg)
|
|
163
|
-
}
|
|
164
|
-
)
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
let ops: EventLog_Adapter.operations = {
|
|
168
|
-
append,
|
|
169
|
-
replay,
|
|
170
|
-
replayStream,
|
|
171
|
-
appendStream,
|
|
172
|
-
latestSnapshot,
|
|
173
|
-
writeSnapshot,
|
|
174
|
-
}
|
|
15
|
+
let makeStorage = (~pool: PgDriver.pool, ~name: string, ~opts, ~onAppended=noTracking) => {
|
|
16
|
+
let (name, ops) = makeOps(~pool, ~name, ~opts, ~onAppended)
|
|
175
17
|
(name, ops, {EventLog_Adapter.resources: [], operations: Pulumi.Output.make(ops)})
|
|
176
18
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Runtime-pure classic (OCC) event-log operations for the Postgres backend.
|
|
2
|
+
//
|
|
3
|
+
// This module holds everything the DEPLOYED Lambda runtime needs and imports NO
|
|
4
|
+
// `@pulumi/pulumi` — deploy-time-only. The deployed aggregate entry point pulls
|
|
5
|
+
// these ops in unconditionally at cold start, so any deploy-time dependency here
|
|
6
|
+
// leaks into the Lambda import graph and fails resolution (`@pulumi/pulumi` exists
|
|
7
|
+
// in no Lambda — not the layer, not `/var/runtime`). The thin
|
|
8
|
+
// `EventLogStorage_Postgres` wrapper adds the deploy-time `Pulumi.Output`-shaped
|
|
9
|
+
// adapter on top for local/deploy consumers; the runtime imports `makeOps` here.
|
|
10
|
+
// See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
|
|
11
|
+
|
|
12
|
+
open ReventlessCore
|
|
13
|
+
|
|
14
|
+
// Injected by reventless-local to feed the projection-checkpoint low-watermark
|
|
15
|
+
// (ProjectionPending, aggregate axis). Standalone/deploy use passes nothing.
|
|
16
|
+
type onAppended = array<(string, int)> => unit
|
|
17
|
+
let noTracking: onAppended = _ => ()
|
|
18
|
+
|
|
19
|
+
let decodePayload = (row: dict<JSON.t>): JSON.t =>
|
|
20
|
+
row->Dict.get("payload")->Option.getOr(JSON.Encode.null)
|
|
21
|
+
|
|
22
|
+
let coerceInt = (v: JSON.t): int =>
|
|
23
|
+
switch v {
|
|
24
|
+
| JSON.Number(n) => Float.toInt(n)
|
|
25
|
+
| JSON.String(s) => s->Int.fromString->Option.getOr(0)
|
|
26
|
+
| _ => 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Total events across all aggregates. Seeds the event-tap counter at startup so
|
|
30
|
+
// timeline numbering continues across restarts.
|
|
31
|
+
let countAll = async (pool: PgDriver.pool): int =>
|
|
32
|
+
switch await pool->PgDriver.queryOne("SELECT COUNT(*)::bigint AS c FROM event_log", []) {
|
|
33
|
+
| Some(row) => row->Dict.get("c")->Option.map(coerceInt)->Option.getOr(0)
|
|
34
|
+
| None => 0
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let isUniqueViolation = (exn: exn): bool => {
|
|
38
|
+
let msg = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("")
|
|
39
|
+
// pg surfaces the SQLSTATE on `.code`; the message also carries the constraint.
|
|
40
|
+
msg->String.includes("duplicate key") ||
|
|
41
|
+
msg->String.includes("unique") ||
|
|
42
|
+
msg->String.includes("23505")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
|
|
46
|
+
// deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
|
|
47
|
+
let makeOps = (
|
|
48
|
+
~pool: PgDriver.pool,
|
|
49
|
+
~name: string,
|
|
50
|
+
~opts as _,
|
|
51
|
+
~onAppended=noTracking,
|
|
52
|
+
): (string, EventLog_Adapter.operations) => {
|
|
53
|
+
let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
|
|
54
|
+
// Empty append is a legitimate no-op (at-least-once retries, idempotent
|
|
55
|
+
// commands), NOT a conflict — return before the guarded insert whose 0-row
|
|
56
|
+
// result would otherwise be read as a conflict.
|
|
57
|
+
if jsons->Array.length == 0 {
|
|
58
|
+
Ok()
|
|
59
|
+
} else {
|
|
60
|
+
try {
|
|
61
|
+
let rows = await pool->PgDriver.query(
|
|
62
|
+
"INSERT INTO event_log(log_name, aggregate_id, seq_nr, payload, msg_id)
|
|
63
|
+
SELECT $1, $2, $3::bigint + (ord - 1), value, value->>'msgId'
|
|
64
|
+
FROM jsonb_array_elements($4::jsonb) WITH ORDINALITY AS t(value, ord)
|
|
65
|
+
WHERE $3::bigint = (SELECT COALESCE(MAX(seq_nr), -1) + 1
|
|
66
|
+
FROM event_log
|
|
67
|
+
WHERE log_name = $1 AND aggregate_id = $2)
|
|
68
|
+
RETURNING global_seq, msg_id",
|
|
69
|
+
[
|
|
70
|
+
JSON.Encode.string(name),
|
|
71
|
+
JSON.Encode.string(id),
|
|
72
|
+
JSON.Encode.int(seqNr),
|
|
73
|
+
JSON.Encode.string(JSON.stringify(JSON.Array(jsons))),
|
|
74
|
+
],
|
|
75
|
+
)
|
|
76
|
+
if rows->Array.length == 0 {
|
|
77
|
+
// WHERE guard rejected the seq — a genuine OCC conflict (gap or stale).
|
|
78
|
+
Error(EventLog.Conflict)
|
|
79
|
+
} else {
|
|
80
|
+
if track {
|
|
81
|
+
onAppended(
|
|
82
|
+
rows->Array.filterMap(row =>
|
|
83
|
+
switch (row->Dict.get("msg_id"), row->Dict.get("global_seq")) {
|
|
84
|
+
| (Some(JSON.String(msgId)), Some(gseq)) => Some((msgId, coerceInt(gseq)))
|
|
85
|
+
| _ => None
|
|
86
|
+
}
|
|
87
|
+
),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
Ok()
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
| exn =>
|
|
94
|
+
isUniqueViolation(exn)
|
|
95
|
+
? Error(EventLog.Conflict)
|
|
96
|
+
: Error(
|
|
97
|
+
EventLog.StorageFailure(
|
|
98
|
+
exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
|
|
99
|
+
),
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let append: EventLog.append<string, JSON.t> = (seqNr, id, jsons) =>
|
|
106
|
+
appendTracked(~track=true, seqNr, id, jsons)
|
|
107
|
+
|
|
108
|
+
let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
|
|
109
|
+
(await pool->PgDriver.query(
|
|
110
|
+
"SELECT payload FROM event_log
|
|
111
|
+
WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
|
|
112
|
+
ORDER BY seq_nr ASC",
|
|
113
|
+
[JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
|
|
114
|
+
))->Array.map(decodePayload)
|
|
115
|
+
|
|
116
|
+
let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
|
|
117
|
+
|
|
118
|
+
let replayStream = (id, ~fromSeq=?) =>
|
|
119
|
+
Effect.promise(() => replayArray(id, ~fromSeq?))
|
|
120
|
+
->Stream.fromEffect
|
|
121
|
+
->Stream.flatMap(arr => Stream.fromIterable(arr))
|
|
122
|
+
|
|
123
|
+
let latestSnapshot: EventLog.latestSnapshot<string> = async id =>
|
|
124
|
+
switch await pool->PgDriver.queryOne(
|
|
125
|
+
"SELECT seq_nr, state, schema_hash FROM snapshot WHERE log_name = $1 AND aggregate_id = $2",
|
|
126
|
+
[JSON.Encode.string(name), JSON.Encode.string(id)],
|
|
127
|
+
) {
|
|
128
|
+
| Some(row) =>
|
|
129
|
+
switch (row->Dict.get("seq_nr"), row->Dict.get("state"), row->Dict.get("schema_hash")) {
|
|
130
|
+
| (Some(seq), Some(state), Some(JSON.String(schemaHash))) =>
|
|
131
|
+
Ok(Some({EventLog.seqNr: coerceInt(seq), state, schemaHash}))
|
|
132
|
+
| _ => Error("snapshot row has unexpected shape")
|
|
133
|
+
}
|
|
134
|
+
| None => Ok(None)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let writeSnapshot: EventLog.writeSnapshot<string> = async (id, snap) =>
|
|
138
|
+
try {
|
|
139
|
+
let _ = await pool->PgDriver.query(
|
|
140
|
+
"INSERT INTO snapshot(log_name, aggregate_id, seq_nr, state, schema_hash)
|
|
141
|
+
VALUES ($1, $2, $3::bigint, $4::jsonb, $5)
|
|
142
|
+
ON CONFLICT (log_name, aggregate_id)
|
|
143
|
+
DO UPDATE SET seq_nr = EXCLUDED.seq_nr, state = EXCLUDED.state, schema_hash = EXCLUDED.schema_hash",
|
|
144
|
+
[
|
|
145
|
+
JSON.Encode.string(name),
|
|
146
|
+
JSON.Encode.string(id),
|
|
147
|
+
JSON.Encode.int(snap.EventLog.seqNr),
|
|
148
|
+
JSON.Encode.string(JSON.stringify(snap.state)),
|
|
149
|
+
JSON.Encode.string(snap.schemaHash),
|
|
150
|
+
],
|
|
151
|
+
)
|
|
152
|
+
Ok()
|
|
153
|
+
} catch {
|
|
154
|
+
| exn =>
|
|
155
|
+
Error(exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("snapshot write error"))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
|
|
159
|
+
stream
|
|
160
|
+
->Stream.runCollect
|
|
161
|
+
->Effect.flatMap(jsons =>
|
|
162
|
+
Effect.promise(() => appendTracked(~track=false, startingSeqNr, id, jsons))->Effect.flatMap(result =>
|
|
163
|
+
switch result {
|
|
164
|
+
| Ok() => Effect.succeed()
|
|
165
|
+
| Error(EventLog.Conflict) => Effect.fail("conflict")
|
|
166
|
+
| Error(StorageFailure(msg)) => Effect.fail(msg)
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
let ops: EventLog_Adapter.operations = {
|
|
172
|
+
append,
|
|
173
|
+
replay,
|
|
174
|
+
replayStream,
|
|
175
|
+
appendStream,
|
|
176
|
+
latestSnapshot,
|
|
177
|
+
writeSnapshot,
|
|
178
|
+
}
|
|
179
|
+
(name, ops)
|
|
180
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/PgSchema.ast
CHANGED
|
Binary file
|
package/lib/ocaml/PgSchema.res
CHANGED
|
@@ -36,6 +36,19 @@ let tableStatements = [
|
|
|
36
36
|
schema_hash text NOT NULL,
|
|
37
37
|
PRIMARY KEY (log_name, aggregate_id)
|
|
38
38
|
)",
|
|
39
|
+
// --- Classic change feed (B2.5) ---
|
|
40
|
+
// Stamp every classic event with its committing xid8 so the feed can apply the
|
|
41
|
+
// same xmin read barrier the DCB feed uses — global_seq (an IDENTITY assigned at
|
|
42
|
+
// INSERT, before commit) is unsafe as a feed cursor on its own (a late-committing
|
|
43
|
+
// lower global_seq would be skipped). Idempotent; the volatile default auto-stamps
|
|
44
|
+
// every future insert with no change to EventLogStorage_Postgres.append.
|
|
45
|
+
"ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
|
|
46
|
+
"CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
|
|
47
|
+
"CREATE TABLE IF NOT EXISTS event_log_subscription (
|
|
48
|
+
subscriber text PRIMARY KEY,
|
|
49
|
+
last_tx xid8 NOT NULL DEFAULT '0'::xid8,
|
|
50
|
+
last_global_seq bigint NOT NULL DEFAULT 0
|
|
51
|
+
)",
|
|
39
52
|
// --- DCB event log (Phase B) ---
|
|
40
53
|
"CREATE TABLE IF NOT EXISTS dcb_event (
|
|
41
54
|
log_name text NOT NULL,
|
|
@@ -236,7 +249,28 @@ END;
|
|
|
236
249
|
$fn$;
|
|
237
250
|
"
|
|
238
251
|
|
|
239
|
-
|
|
252
|
+
// Classic change-feed wakeup (B2.5). A statement-level AFTER INSERT trigger fires
|
|
253
|
+
// one pg_notify per distinct log_name touched by the statement, so the classic feed
|
|
254
|
+
// gets the same near-real-time wakeup the DCB append's inline pg_notify provides —
|
|
255
|
+
// without touching EventLogStorage_Postgres.append. No-op if nobody is LISTENing; a
|
|
256
|
+
// low-frequency fallback tick in the consumer covers missed notifications.
|
|
257
|
+
let eventLogNotifyFn = "
|
|
258
|
+
CREATE OR REPLACE FUNCTION event_log_notify() RETURNS trigger
|
|
259
|
+
LANGUAGE plpgsql AS $fn$
|
|
260
|
+
BEGIN
|
|
261
|
+
PERFORM pg_notify('evlog_' || log_name, '')
|
|
262
|
+
FROM (SELECT DISTINCT log_name FROM new_rows) s;
|
|
263
|
+
RETURN NULL;
|
|
264
|
+
END;
|
|
265
|
+
$fn$;
|
|
266
|
+
|
|
267
|
+
CREATE OR REPLACE TRIGGER event_log_notify_trg
|
|
268
|
+
AFTER INSERT ON event_log
|
|
269
|
+
REFERENCING NEW TABLE AS new_rows
|
|
270
|
+
FOR EACH STATEMENT EXECUTE FUNCTION event_log_notify();
|
|
271
|
+
"
|
|
272
|
+
|
|
273
|
+
let functionStatements = [dcbConditionWhereFn, dcbAppendFn, eventLogNotifyFn]
|
|
240
274
|
|
|
241
275
|
let ensureSchema = async (pool: PgDriver.pool): unit => {
|
|
242
276
|
for i in 0 to tableStatements->Array.length - 1 {
|
|
@@ -251,6 +285,6 @@ let ensureSchema = async (pool: PgDriver.pool): unit => {
|
|
|
251
285
|
// Mirrors the reventless-local contract that reset wipes data, not structure.
|
|
252
286
|
let truncateAll = async (pool: PgDriver.pool): unit => {
|
|
253
287
|
await pool->PgDriver.exec(
|
|
254
|
-
"TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription RESTART IDENTITY",
|
|
288
|
+
"TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription, event_log_subscription RESTART IDENTITY",
|
|
255
289
|
)
|
|
256
290
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -233,4 +233,72 @@ switch processEnv->Dict.get("PG_URL") {
|
|
|
233
233
|
expect(again)->toBe(0)
|
|
234
234
|
})
|
|
235
235
|
})
|
|
236
|
+
|
|
237
|
+
describe("classic change feed (event_log, B2.5)", () => {
|
|
238
|
+
testPromise("drains classic events in commit order, with fields, then checkpoints", async () => {
|
|
239
|
+
let (_n, ops, _s) = EventLogStorage_Postgres.makeStorage(~pool, ~name="cf-orders", ~opts)
|
|
240
|
+
let _ = await ops.append(0, "agg-1", [evJson("m0", 1), evJson("m1", 2)])
|
|
241
|
+
let _ = await ops.append(0, "agg-2", [evJson("m2", 3)])
|
|
242
|
+
|
|
243
|
+
let collected = ref([])
|
|
244
|
+
let processed = await EventLogChangeFeed.drain(
|
|
245
|
+
pool,
|
|
246
|
+
~logName="cf-orders",
|
|
247
|
+
~subscriber="cf-sub",
|
|
248
|
+
~handle=async evs => collected := collected.contents->Array.concat(evs),
|
|
249
|
+
)
|
|
250
|
+
expect(processed)->toBe(3)
|
|
251
|
+
expect(collected.contents->Array.length)->toBe(3)
|
|
252
|
+
|
|
253
|
+
// Fields round-trip from the stored row.
|
|
254
|
+
let first = collected.contents->Array.getUnsafe(0)
|
|
255
|
+
expect(first.aggregateId)->toBe("agg-1")
|
|
256
|
+
expect(first.seqNr)->toBe(0)
|
|
257
|
+
expect(first.msgId)->toEqual(Some("m0"))
|
|
258
|
+
|
|
259
|
+
// Cursors are strictly increasing in drain order.
|
|
260
|
+
let cursors = collected.contents->Array.map(e => e.cursor)
|
|
261
|
+
let increasing = ref(true)
|
|
262
|
+
for i in 1 to cursors->Array.length - 1 {
|
|
263
|
+
if cursors->Array.getUnsafe(i) <= cursors->Array.getUnsafe(i - 1) {
|
|
264
|
+
increasing := false
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
expect(increasing.contents)->toBe(true)
|
|
268
|
+
|
|
269
|
+
// Second drain from the saved checkpoint sees nothing new.
|
|
270
|
+
let again = await EventLogChangeFeed.drain(
|
|
271
|
+
pool,
|
|
272
|
+
~logName="cf-orders",
|
|
273
|
+
~subscriber="cf-sub",
|
|
274
|
+
~handle=async _ => (),
|
|
275
|
+
)
|
|
276
|
+
expect(again)->toBe(0)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
testPromise("concurrent appends across aggregates: every event drained exactly once", async () => {
|
|
280
|
+
let (_n, ops, _s) = EventLogStorage_Postgres.makeStorage(~pool, ~name="cf-concurrent", ~opts)
|
|
281
|
+
let n = 20
|
|
282
|
+
// One event per aggregate, all dispatched before awaiting — genuinely
|
|
283
|
+
// concurrent on the pool, so global_seq is assigned in interleaved/undefined
|
|
284
|
+
// commit order. The xmin fence must still drain every event exactly once.
|
|
285
|
+
let promises =
|
|
286
|
+
Array.make(~length=n, 0)->Array.mapWithIndex((_, i) =>
|
|
287
|
+
ops.append(0, "agg-" ++ Int.toString(i), [evJson("m" ++ Int.toString(i), i)])
|
|
288
|
+
)
|
|
289
|
+
let _ = await Promise.all(promises)
|
|
290
|
+
|
|
291
|
+
let collected = ref([])
|
|
292
|
+
let _ = await EventLogChangeFeed.drain(
|
|
293
|
+
pool,
|
|
294
|
+
~logName="cf-concurrent",
|
|
295
|
+
~subscriber="cf-conc-sub",
|
|
296
|
+
~handle=async evs => collected := collected.contents->Array.concat(evs),
|
|
297
|
+
)
|
|
298
|
+
// No skips: all N events present. No dupes: N distinct cursors.
|
|
299
|
+
expect(collected.contents->Array.length)->toBe(n)
|
|
300
|
+
let distinctCursors = collected.contents->Array.map(e => e.cursor)->Set.fromArray
|
|
301
|
+
expect(distinctCursors->Set.size)->toBe(n)
|
|
302
|
+
})
|
|
303
|
+
})
|
|
236
304
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|