@reventlessdev/reventless-postgres 3.0.0-alpha.2 → 3.0.0-alpha.200
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 +1584 -0
- package/README.md +83 -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 +4 -1
- package/lib/bs/compiler-info.json +4 -4
- 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 +38 -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 +422 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +437 -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 +193 -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 +23 -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 +240 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +232 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgChangeFeed.ast +0 -0
- package/lib/bs/src/PgChangeFeed.res +21 -9
- package/lib/bs/src/PgChangeFeed.res.mjs +6 -6
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/bs/src/PgDriver.ast +0 -0
- package/lib/bs/src/PgDriver.iast +0 -0
- package/lib/bs/src/PgDriver.res +1 -2
- 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 +44 -2
- package/lib/bs/src/PgSchema.res.mjs +13 -3
- 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 +16 -213
- 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 +372 -14
- package/lib/bs/src/QueryEnginePostgres.res.mjs +203 -15
- 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 +168 -47
- package/lib/bs/tests/PostgresIntegrationTest.res.mjs +50 -1
- 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 +335 -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 +422 -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 +193 -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 +240 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgChangeFeed.ast +0 -0
- package/lib/ocaml/PgChangeFeed.res +21 -9
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/ocaml/PgDriver.ast +0 -0
- package/lib/ocaml/PgDriver.iast +0 -0
- package/lib/ocaml/PgDriver.res +1 -2
- 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 +44 -2
- package/lib/ocaml/PostgresIntegrationTest.res +168 -47
- 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 +16 -213
- 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 +372 -14
- package/lib/ocaml/QueryEnginePostgresListPageTest.res +335 -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 +14 -14
- package/rescript.json +2 -1
- package/src/DcbEventLogStorage_Postgres.res +25 -340
- package/src/DcbEventLogStorage_Postgres.res.mjs +38 -348
- package/src/DcbEventLogStorage_Postgres_Ops.res +422 -0
- package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +437 -0
- package/src/EventLogChangeFeed.res +193 -0
- package/src/EventLogChangeFeed.res.mjs +134 -0
- package/src/EventLogStorage_Postgres.res +12 -170
- package/src/EventLogStorage_Postgres.res.mjs +23 -186
- package/src/EventLogStorage_Postgres_Ops.res +240 -0
- package/src/EventLogStorage_Postgres_Ops.res.mjs +232 -0
- package/src/PgChangeFeed.res +21 -9
- package/src/PgChangeFeed.res.mjs +6 -6
- package/src/PgDriver.res +1 -2
- package/src/PgSchema.res +44 -2
- package/src/PgSchema.res.mjs +13 -3
- package/src/QueryDbStorage_Postgres.res +16 -213
- 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 +372 -14
- package/src/QueryEnginePostgres.res.mjs +203 -15
- package/tests/PostgresIntegrationTest.res +168 -47
- package/tests/QueryEnginePostgresListPageTest.res +335 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PostgresIntegrationTest.ast +0 -0
- package/tests/PostgresIntegrationTest.res.mjs +0 -328
|
@@ -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/done/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,240 @@
|
|
|
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/done/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
|
+
/** Where a backwards read of one log starts: strictly below a position, or at
|
|
46
|
+
and below it. */
|
|
47
|
+
type bound =
|
|
48
|
+
| Below(float)
|
|
49
|
+
| AtMost(float)
|
|
50
|
+
|
|
51
|
+
/** One log's stored events newest first, across its entities, by `global_seq`,
|
|
52
|
+
narrowed to one correlation id when given. Rows are `(global_seq, aggregate
|
|
53
|
+
id, stored event)`. */
|
|
54
|
+
let readBackwards = async (
|
|
55
|
+
pool: PgDriver.pool,
|
|
56
|
+
~name: string,
|
|
57
|
+
~bound: option<bound>,
|
|
58
|
+
~limit: int,
|
|
59
|
+
~correlationId: option<string>,
|
|
60
|
+
): array<(float, string, JSON.t)> => {
|
|
61
|
+
let params = [JSON.Encode.string(name)]
|
|
62
|
+
let param = v => {
|
|
63
|
+
params->Array.push(v)
|
|
64
|
+
"$" ++ params->Array.length->Int.toString
|
|
65
|
+
}
|
|
66
|
+
let parts = ["log_name = $1"]
|
|
67
|
+
switch bound {
|
|
68
|
+
| Some(Below(p)) => parts->Array.push(`global_seq < ${param(JSON.Encode.float(p))}::bigint`)
|
|
69
|
+
| Some(AtMost(p)) => parts->Array.push(`global_seq <= ${param(JSON.Encode.float(p))}::bigint`)
|
|
70
|
+
| None => ()
|
|
71
|
+
}
|
|
72
|
+
switch correlationId {
|
|
73
|
+
| Some(id) => parts->Array.push(`(payload->>'correlationId') = ${param(JSON.Encode.string(id))}`)
|
|
74
|
+
| None => ()
|
|
75
|
+
}
|
|
76
|
+
let limitP = param(JSON.Encode.int(limit))
|
|
77
|
+
|
|
78
|
+
(
|
|
79
|
+
await pool->PgDriver.query(
|
|
80
|
+
`SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ${parts->Array.join(
|
|
81
|
+
" AND ",
|
|
82
|
+
)} ORDER BY global_seq DESC LIMIT ${limitP}::int`,
|
|
83
|
+
params,
|
|
84
|
+
)
|
|
85
|
+
)->Array.map(row => (
|
|
86
|
+
row->Dict.get("pos")->Option.mapOr(0, coerceInt)->Int.toFloat,
|
|
87
|
+
switch row->Dict.get("aggregate_id") {
|
|
88
|
+
| Some(JSON.String(id)) => id
|
|
89
|
+
| _ => ""
|
|
90
|
+
},
|
|
91
|
+
decodePayload(row),
|
|
92
|
+
))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
|
|
96
|
+
// deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
|
|
97
|
+
let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
|
|
98
|
+
string,
|
|
99
|
+
EventLog_Adapter.operations,
|
|
100
|
+
) => {
|
|
101
|
+
let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
|
|
102
|
+
// Empty append is a legitimate no-op (at-least-once retries, idempotent
|
|
103
|
+
// commands), NOT a conflict — return before the guarded insert whose 0-row
|
|
104
|
+
// result would otherwise be read as a conflict.
|
|
105
|
+
if jsons->Array.length == 0 {
|
|
106
|
+
Ok()
|
|
107
|
+
} else {
|
|
108
|
+
try {
|
|
109
|
+
let rows = await pool->PgDriver.query(
|
|
110
|
+
"INSERT INTO event_log(log_name, aggregate_id, seq_nr, payload, msg_id)
|
|
111
|
+
SELECT $1, $2, $3::bigint + (ord - 1), value, value->>'msgId'
|
|
112
|
+
FROM jsonb_array_elements($4::jsonb) WITH ORDINALITY AS t(value, ord)
|
|
113
|
+
WHERE $3::bigint = (SELECT COALESCE(MAX(seq_nr), -1) + 1
|
|
114
|
+
FROM event_log
|
|
115
|
+
WHERE log_name = $1 AND aggregate_id = $2)
|
|
116
|
+
RETURNING global_seq, msg_id",
|
|
117
|
+
[
|
|
118
|
+
JSON.Encode.string(name),
|
|
119
|
+
JSON.Encode.string(id),
|
|
120
|
+
JSON.Encode.int(seqNr),
|
|
121
|
+
JSON.Encode.string(JSON.stringify(JSON.Array(jsons))),
|
|
122
|
+
],
|
|
123
|
+
)
|
|
124
|
+
if rows->Array.length == 0 {
|
|
125
|
+
// WHERE guard rejected the seq — a genuine OCC conflict (gap or stale).
|
|
126
|
+
Error(EventLog.Conflict)
|
|
127
|
+
} else {
|
|
128
|
+
if track {
|
|
129
|
+
onAppended(
|
|
130
|
+
rows->Array.filterMap(row =>
|
|
131
|
+
switch (row->Dict.get("msg_id"), row->Dict.get("global_seq")) {
|
|
132
|
+
| (Some(JSON.String(msgId)), Some(gseq)) => Some((msgId, coerceInt(gseq)))
|
|
133
|
+
| _ => None
|
|
134
|
+
}
|
|
135
|
+
),
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
Ok()
|
|
139
|
+
}
|
|
140
|
+
} catch {
|
|
141
|
+
| exn =>
|
|
142
|
+
isUniqueViolation(exn)
|
|
143
|
+
? Error(EventLog.Conflict)
|
|
144
|
+
: Error(
|
|
145
|
+
EventLog.StorageFailure(
|
|
146
|
+
exn
|
|
147
|
+
->JsExn.fromException
|
|
148
|
+
->Option.flatMap(JsExn.message)
|
|
149
|
+
->Option.getOr("storage error"),
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let append: EventLog.append<string, JSON.t> = (seqNr, id, jsons) =>
|
|
157
|
+
appendTracked(~track=true, seqNr, id, jsons)
|
|
158
|
+
|
|
159
|
+
let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
|
|
160
|
+
(
|
|
161
|
+
await pool->PgDriver.query(
|
|
162
|
+
"SELECT payload FROM event_log
|
|
163
|
+
WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
|
|
164
|
+
ORDER BY seq_nr ASC",
|
|
165
|
+
[JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
|
|
166
|
+
)
|
|
167
|
+
)->Array.map(decodePayload)
|
|
168
|
+
|
|
169
|
+
let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
|
|
170
|
+
|
|
171
|
+
let replayStream = (id, ~fromSeq=?) =>
|
|
172
|
+
Effect.promise(() => replayArray(id, ~fromSeq?))
|
|
173
|
+
->Stream.fromEffect
|
|
174
|
+
->Stream.flatMap(arr => Stream.fromIterable(arr))
|
|
175
|
+
|
|
176
|
+
let latestSnapshot: EventLog.latestSnapshot<string> = async id =>
|
|
177
|
+
switch await pool->PgDriver.queryOne(
|
|
178
|
+
"SELECT seq_nr, state, schema_hash FROM snapshot WHERE log_name = $1 AND aggregate_id = $2",
|
|
179
|
+
[JSON.Encode.string(name), JSON.Encode.string(id)],
|
|
180
|
+
) {
|
|
181
|
+
| Some(row) =>
|
|
182
|
+
switch (row->Dict.get("seq_nr"), row->Dict.get("state"), row->Dict.get("schema_hash")) {
|
|
183
|
+
| (Some(seq), Some(state), Some(JSON.String(schemaHash))) =>
|
|
184
|
+
Ok(Some({EventLog.seqNr: coerceInt(seq), state, schemaHash}))
|
|
185
|
+
| _ => Error("snapshot row has unexpected shape")
|
|
186
|
+
}
|
|
187
|
+
| None => Ok(None)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let writeSnapshot: EventLog.writeSnapshot<string> = async (id, snap) =>
|
|
191
|
+
try {
|
|
192
|
+
let _ = await pool->PgDriver.query(
|
|
193
|
+
"INSERT INTO snapshot(log_name, aggregate_id, seq_nr, state, schema_hash)
|
|
194
|
+
VALUES ($1, $2, $3::bigint, $4::jsonb, $5)
|
|
195
|
+
ON CONFLICT (log_name, aggregate_id)
|
|
196
|
+
DO UPDATE SET seq_nr = EXCLUDED.seq_nr, state = EXCLUDED.state, schema_hash = EXCLUDED.schema_hash",
|
|
197
|
+
[
|
|
198
|
+
JSON.Encode.string(name),
|
|
199
|
+
JSON.Encode.string(id),
|
|
200
|
+
JSON.Encode.int(snap.EventLog.seqNr),
|
|
201
|
+
JSON.Encode.string(JSON.stringify(snap.state)),
|
|
202
|
+
JSON.Encode.string(snap.schemaHash),
|
|
203
|
+
],
|
|
204
|
+
)
|
|
205
|
+
Ok()
|
|
206
|
+
} catch {
|
|
207
|
+
| exn =>
|
|
208
|
+
Error(
|
|
209
|
+
exn
|
|
210
|
+
->JsExn.fromException
|
|
211
|
+
->Option.flatMap(JsExn.message)
|
|
212
|
+
->Option.getOr("snapshot write error"),
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
|
|
217
|
+
stream
|
|
218
|
+
->Stream.runCollect
|
|
219
|
+
->Effect.flatMap(jsons =>
|
|
220
|
+
Effect.promise(() =>
|
|
221
|
+
appendTracked(~track=false, startingSeqNr, id, jsons)
|
|
222
|
+
)->Effect.flatMap(result =>
|
|
223
|
+
switch result {
|
|
224
|
+
| Ok() => Effect.succeed()
|
|
225
|
+
| Error(EventLog.Conflict) => Effect.fail("conflict")
|
|
226
|
+
| Error(StorageFailure(msg)) => Effect.fail(msg)
|
|
227
|
+
}
|
|
228
|
+
)
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
let ops: EventLog_Adapter.operations = {
|
|
232
|
+
append,
|
|
233
|
+
replay,
|
|
234
|
+
replayStream,
|
|
235
|
+
appendStream,
|
|
236
|
+
latestSnapshot,
|
|
237
|
+
writeSnapshot,
|
|
238
|
+
}
|
|
239
|
+
(name, ops)
|
|
240
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -18,7 +18,12 @@
|
|
|
18
18
|
open ReventlessCore
|
|
19
19
|
open Reventless
|
|
20
20
|
|
|
21
|
-
module
|
|
21
|
+
// Runtime-pure ops module (no `@pulumi/pulumi`) — this feed is drained by the
|
|
22
|
+
// deployed change-feed relay Lambda, so its cold-start graph must stay
|
|
23
|
+
// Pulumi-free. `_Ops` carries every helper used below (mkBuilder,
|
|
24
|
+
// buildReadWhere, decodeCursor, selectColumns, rowToEvent); the deploy-time
|
|
25
|
+
// DcbEventLogStorage_Postgres only `include`s it and adds the Pulumi adapter.
|
|
26
|
+
module Dcb = DcbEventLogStorage_Postgres_Ops
|
|
22
27
|
|
|
23
28
|
// One page of the feed. `events` are in (transaction_id, position) order;
|
|
24
29
|
// `cursor` is the checkpoint to persist and to pass as the next `after` (None
|
|
@@ -44,7 +49,9 @@ let readBatch = async (
|
|
|
44
49
|
~after=after->Option.flatMap(Dcb.decodeCursor),
|
|
45
50
|
~applyFence=true,
|
|
46
51
|
)
|
|
47
|
-
let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
52
|
+
let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
|
|
53
|
+
limit,
|
|
54
|
+
)}`
|
|
48
55
|
let rows = await pool->PgDriver.query(sql, b.params)
|
|
49
56
|
let events = rows->Array.map(Dcb.rowToEvent)
|
|
50
57
|
let cursor = switch events->Array.get(events->Array.length - 1) {
|
|
@@ -58,10 +65,9 @@ let readBatch = async (
|
|
|
58
65
|
|
|
59
66
|
// The persisted checkpoint for `subscriber`, encoded as a cursor, or None if the
|
|
60
67
|
// subscriber has never committed (→ replay from the beginning).
|
|
61
|
-
let loadCheckpoint = async (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
): option<DcbTag.sequencePosition> =>
|
|
68
|
+
let loadCheckpoint = async (pool: PgDriver.pool, ~subscriber: string): option<
|
|
69
|
+
DcbTag.sequencePosition,
|
|
70
|
+
> =>
|
|
65
71
|
switch await pool->PgDriver.queryOne(
|
|
66
72
|
"SELECT lpad(last_tx::text, 20, '0') || ':' || lpad(last_position::text, 20, '0') AS cursor
|
|
67
73
|
FROM dcb_subscription WHERE subscriber = $1 AND last_position > 0",
|
|
@@ -100,8 +106,9 @@ let saveCheckpoint = async (
|
|
|
100
106
|
// dedicated client (caller owns it — release via PgDriver.unlisten). Pair with a
|
|
101
107
|
// low-frequency fallback tick in the consumer loop so a missed NOTIFY only adds
|
|
102
108
|
// latency, never a permanent stall.
|
|
103
|
-
let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
|
|
104
|
-
|
|
109
|
+
let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
|
|
110
|
+
PgDriver.client,
|
|
111
|
+
> => pool->PgDriver.listen(~channel="dcb_" ++ logName, ~onNotify=_ => onWake())
|
|
105
112
|
|
|
106
113
|
let unlisten = (client: PgDriver.client, ~logName: string): promise<unit> =>
|
|
107
114
|
client->PgDriver.unlisten(~channel="dcb_" ++ logName)
|
|
@@ -124,7 +131,12 @@ let drain = async (
|
|
|
124
131
|
let cursor = ref(await loadCheckpoint(pool, ~subscriber))
|
|
125
132
|
let continue = ref(true)
|
|
126
133
|
while continue.contents {
|
|
127
|
-
let {events, cursor: newCursor} = await readBatch(
|
|
134
|
+
let {events, cursor: newCursor} = await readBatch(
|
|
135
|
+
pool,
|
|
136
|
+
~logName,
|
|
137
|
+
~after=?cursor.contents,
|
|
138
|
+
~limit,
|
|
139
|
+
)
|
|
128
140
|
if events->Array.length == 0 {
|
|
129
141
|
continue := false
|
|
130
142
|
} else {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/PgDriver.ast
CHANGED
|
Binary file
|
package/lib/ocaml/PgDriver.iast
CHANGED
|
Binary file
|
package/lib/ocaml/PgDriver.res
CHANGED
|
@@ -47,8 +47,7 @@ type queryResult = {rows: array<dict<JSON.t>>}
|
|
|
47
47
|
|
|
48
48
|
let query = async (pool, sql, params) => (await _query(pool, sql, params)).rows
|
|
49
49
|
|
|
50
|
-
let queryOne = async (pool, sql, params) =>
|
|
51
|
-
(await _query(pool, sql, params)).rows->Array.get(0)
|
|
50
|
+
let queryOne = async (pool, sql, params) => (await _query(pool, sql, params)).rows->Array.get(0)
|
|
52
51
|
|
|
53
52
|
let exec = async (pool, sql) => {
|
|
54
53
|
let _ = await _queryText(pool, sql)
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/PgSchema.ast
CHANGED
|
Binary file
|
package/lib/ocaml/PgSchema.res
CHANGED
|
@@ -36,6 +36,23 @@ 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
|
+
// Reading one log newest first across its entities, and finding every event of
|
|
48
|
+
// one flow. The stored event is flat, so the correlation id is a top-level key.
|
|
49
|
+
"CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
|
|
50
|
+
"CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
|
|
51
|
+
"CREATE TABLE IF NOT EXISTS event_log_subscription (
|
|
52
|
+
subscriber text PRIMARY KEY,
|
|
53
|
+
last_tx xid8 NOT NULL DEFAULT '0'::xid8,
|
|
54
|
+
last_global_seq bigint NOT NULL DEFAULT 0
|
|
55
|
+
)",
|
|
39
56
|
// --- DCB event log (Phase B) ---
|
|
40
57
|
"CREATE TABLE IF NOT EXISTS dcb_event (
|
|
41
58
|
log_name text NOT NULL,
|
|
@@ -51,6 +68,7 @@ let tableStatements = [
|
|
|
51
68
|
"CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
|
|
52
69
|
"CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
|
|
53
70
|
"CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
|
|
71
|
+
"CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
|
|
54
72
|
// --- Row-lock companion (B4) ---
|
|
55
73
|
"CREATE TABLE IF NOT EXISTS dcb_scope (
|
|
56
74
|
log_name text NOT NULL,
|
|
@@ -119,6 +137,9 @@ BEGIN
|
|
|
119
137
|
INTO v_types
|
|
120
138
|
FROM jsonb_array_elements_text(qi->'eventTypes') t;
|
|
121
139
|
v_parts := array_append(v_parts, 'event_type IN (' || v_types || ')');
|
|
140
|
+
ELSIF jsonb_typeof(qi->'eventTypes') = 'array' THEN
|
|
141
|
+
-- A clause with no event type matches nothing.
|
|
142
|
+
v_parts := array_append(v_parts, 'FALSE');
|
|
122
143
|
END IF;
|
|
123
144
|
|
|
124
145
|
IF jsonb_typeof(qi->'tags') = 'array'
|
|
@@ -236,7 +257,28 @@ END;
|
|
|
236
257
|
$fn$;
|
|
237
258
|
"
|
|
238
259
|
|
|
239
|
-
|
|
260
|
+
// Classic change-feed wakeup (B2.5). A statement-level AFTER INSERT trigger fires
|
|
261
|
+
// one pg_notify per distinct log_name touched by the statement, so the classic feed
|
|
262
|
+
// gets the same near-real-time wakeup the DCB append's inline pg_notify provides —
|
|
263
|
+
// without touching EventLogStorage_Postgres.append. No-op if nobody is LISTENing; a
|
|
264
|
+
// low-frequency fallback tick in the consumer covers missed notifications.
|
|
265
|
+
let eventLogNotifyFn = "
|
|
266
|
+
CREATE OR REPLACE FUNCTION event_log_notify() RETURNS trigger
|
|
267
|
+
LANGUAGE plpgsql AS $fn$
|
|
268
|
+
BEGIN
|
|
269
|
+
PERFORM pg_notify('evlog_' || log_name, '')
|
|
270
|
+
FROM (SELECT DISTINCT log_name FROM new_rows) s;
|
|
271
|
+
RETURN NULL;
|
|
272
|
+
END;
|
|
273
|
+
$fn$;
|
|
274
|
+
|
|
275
|
+
CREATE OR REPLACE TRIGGER event_log_notify_trg
|
|
276
|
+
AFTER INSERT ON event_log
|
|
277
|
+
REFERENCING NEW TABLE AS new_rows
|
|
278
|
+
FOR EACH STATEMENT EXECUTE FUNCTION event_log_notify();
|
|
279
|
+
"
|
|
280
|
+
|
|
281
|
+
let functionStatements = [dcbConditionWhereFn, dcbAppendFn, eventLogNotifyFn]
|
|
240
282
|
|
|
241
283
|
let ensureSchema = async (pool: PgDriver.pool): unit => {
|
|
242
284
|
for i in 0 to tableStatements->Array.length - 1 {
|
|
@@ -251,6 +293,6 @@ let ensureSchema = async (pool: PgDriver.pool): unit => {
|
|
|
251
293
|
// Mirrors the reventless-local contract that reset wipes data, not structure.
|
|
252
294
|
let truncateAll = async (pool: PgDriver.pool): unit => {
|
|
253
295
|
await pool->PgDriver.exec(
|
|
254
|
-
"TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription RESTART IDENTITY",
|
|
296
|
+
"TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription, event_log_subscription RESTART IDENTITY",
|
|
255
297
|
)
|
|
256
298
|
}
|