sequent 8.1.1 → 8.2.0
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.
- checksums.yaml +4 -4
- data/db/migrate/20250101000000_sequent_initial_schema.rb +166 -0
- data/db/migrate/20250101000001_sequent_stored_procedures.rb +48 -0
- data/db/migrate/20250312105100_sequent_store_events_v02.rb +29 -0
- data/db/migrate/sequent/aggregate_event_type_v01.sql +7 -0
- data/db/migrate/sequent/aggregates_that_need_snapshots_v01.sql +12 -0
- data/db/migrate/sequent/command_records_v01.sql +11 -0
- data/db/migrate/sequent/delete_all_snapshots_v01.sql +9 -0
- data/db/migrate/sequent/delete_snapshots_before_v01.sql +14 -0
- data/db/migrate/sequent/enrich_command_json_v01.sql +14 -0
- data/db/migrate/sequent/enrich_event_json_v01.sql +11 -0
- data/db/migrate/sequent/event_records_v01.sql +13 -0
- data/db/migrate/sequent/load_event_v01.sql +19 -0
- data/db/migrate/sequent/load_events_v01.sql +40 -0
- data/db/migrate/sequent/load_latest_snapshots_v01.sql +12 -0
- data/db/migrate/sequent/permanently_delete_commands_without_events_v01.sql +13 -0
- data/db/migrate/sequent/permanently_delete_event_streams_v01.sql +13 -0
- data/db/migrate/sequent/save_events_trigger_v01.sql +53 -0
- data/db/migrate/sequent/select_aggregates_for_snapshotting_v01.sql +19 -0
- data/db/migrate/sequent/store_aggregates_v01.sql +39 -0
- data/db/migrate/sequent/store_command_v01.sql +21 -0
- data/db/migrate/sequent/store_events_v01.sql +37 -0
- data/db/migrate/sequent/store_events_v02.sql +37 -0
- data/db/migrate/sequent/store_snapshots_v01.sql +30 -0
- data/db/migrate/sequent/stream_records_v01.sql +7 -0
- data/db/migrate/sequent/update_types_v01.sql +32 -0
- data/db/migrate/sequent/update_unique_keys_v01.sql +33 -0
- data/db/sequent_pgsql.sql +22 -440
- data/db/structure.sql +1358 -0
- data/lib/sequent/core/command_record.rb +0 -1
- data/lib/sequent/core/event_record.rb +0 -1
- data/lib/sequent/core/event_store.rb +64 -6
- data/lib/sequent/core/helpers/message_router.rb +9 -6
- data/lib/sequent/core/persistors/active_record_persistor.rb +1 -22
- data/lib/sequent/generator/template_project/db/database.yml +1 -3
- data/lib/sequent/generator/template_project/spec/spec_helper.rb +0 -1
- data/lib/sequent/migrations/sequent_schema.rb +2 -11
- data/lib/sequent/migrations/view_schema.rb +1 -1
- data/lib/sequent/rake/migration_files.rb +69 -0
- data/lib/sequent/rake/migration_tasks.rb +71 -9
- data/lib/sequent/support/database.rb +12 -31
- data/lib/sequent/test/command_handler_helpers.rb +14 -1
- data/lib/sequent/util/dry_run.rb +8 -0
- data/lib/version.rb +1 -1
- metadata +29 -2
- data/db/migrate/20250108162754_aggregate_unique_keys.rb +0 -31
data/db/structure.sql
ADDED
@@ -0,0 +1,1358 @@
|
|
1
|
+
SET statement_timeout = 0;
|
2
|
+
SET lock_timeout = 0;
|
3
|
+
SET idle_in_transaction_session_timeout = 0;
|
4
|
+
SET client_encoding = 'UTF8';
|
5
|
+
SET standard_conforming_strings = on;
|
6
|
+
SELECT pg_catalog.set_config('search_path', '', false);
|
7
|
+
SET check_function_bodies = false;
|
8
|
+
SET xmloption = content;
|
9
|
+
SET client_min_messages = warning;
|
10
|
+
SET row_security = off;
|
11
|
+
|
12
|
+
--
|
13
|
+
-- Name: sequent_schema; Type: SCHEMA; Schema: -; Owner: -
|
14
|
+
--
|
15
|
+
|
16
|
+
CREATE SCHEMA sequent_schema;
|
17
|
+
|
18
|
+
|
19
|
+
--
|
20
|
+
-- Name: aggregate_event_type; Type: TYPE; Schema: sequent_schema; Owner: -
|
21
|
+
--
|
22
|
+
|
23
|
+
CREATE TYPE sequent_schema.aggregate_event_type AS (
|
24
|
+
aggregate_type text,
|
25
|
+
aggregate_id uuid,
|
26
|
+
events_partition_key text,
|
27
|
+
event_type text,
|
28
|
+
event_json jsonb
|
29
|
+
);
|
30
|
+
|
31
|
+
|
32
|
+
--
|
33
|
+
-- Name: aggregates_that_need_snapshots(uuid, integer); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
34
|
+
--
|
35
|
+
|
36
|
+
CREATE FUNCTION sequent_schema.aggregates_that_need_snapshots(_last_aggregate_id uuid, _limit integer) RETURNS TABLE(aggregate_id uuid)
|
37
|
+
LANGUAGE plpgsql
|
38
|
+
SET search_path TO 'sequent_schema'
|
39
|
+
AS $$
|
40
|
+
BEGIN
|
41
|
+
RETURN QUERY SELECT a.aggregate_id
|
42
|
+
FROM aggregates_that_need_snapshots a
|
43
|
+
WHERE a.snapshot_outdated_at IS NOT NULL
|
44
|
+
AND (_last_aggregate_id IS NULL OR a.aggregate_id > _last_aggregate_id)
|
45
|
+
ORDER BY 1
|
46
|
+
LIMIT _limit;
|
47
|
+
END;
|
48
|
+
$$;
|
49
|
+
|
50
|
+
|
51
|
+
--
|
52
|
+
-- Name: delete_all_snapshots(timestamp with time zone); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
53
|
+
--
|
54
|
+
|
55
|
+
CREATE PROCEDURE sequent_schema.delete_all_snapshots(IN _now timestamp with time zone DEFAULT now())
|
56
|
+
LANGUAGE plpgsql
|
57
|
+
SET search_path TO 'sequent_schema'
|
58
|
+
AS $$
|
59
|
+
BEGIN
|
60
|
+
UPDATE aggregates_that_need_snapshots
|
61
|
+
SET snapshot_outdated_at = _now
|
62
|
+
WHERE snapshot_outdated_at IS NULL;
|
63
|
+
DELETE FROM snapshot_records;
|
64
|
+
END;
|
65
|
+
$$;
|
66
|
+
|
67
|
+
|
68
|
+
--
|
69
|
+
-- Name: delete_snapshots_before(uuid, integer, timestamp with time zone); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
70
|
+
--
|
71
|
+
|
72
|
+
CREATE PROCEDURE sequent_schema.delete_snapshots_before(IN _aggregate_id uuid, IN _sequence_number integer, IN _now timestamp with time zone DEFAULT now())
|
73
|
+
LANGUAGE plpgsql
|
74
|
+
SET search_path TO 'sequent_schema'
|
75
|
+
AS $$
|
76
|
+
BEGIN
|
77
|
+
DELETE FROM snapshot_records
|
78
|
+
WHERE aggregate_id = _aggregate_id
|
79
|
+
AND sequence_number < _sequence_number;
|
80
|
+
|
81
|
+
UPDATE aggregates_that_need_snapshots
|
82
|
+
SET snapshot_outdated_at = _now
|
83
|
+
WHERE aggregate_id = _aggregate_id
|
84
|
+
AND snapshot_outdated_at IS NULL
|
85
|
+
AND NOT EXISTS (SELECT 1 FROM snapshot_records WHERE aggregate_id = _aggregate_id);
|
86
|
+
END;
|
87
|
+
$$;
|
88
|
+
|
89
|
+
|
90
|
+
SET default_tablespace = '';
|
91
|
+
|
92
|
+
--
|
93
|
+
-- Name: commands; Type: TABLE; Schema: sequent_schema; Owner: -
|
94
|
+
--
|
95
|
+
|
96
|
+
CREATE TABLE sequent_schema.commands (
|
97
|
+
id bigint NOT NULL,
|
98
|
+
created_at timestamp with time zone NOT NULL,
|
99
|
+
user_id uuid,
|
100
|
+
aggregate_id uuid,
|
101
|
+
command_type_id smallint NOT NULL,
|
102
|
+
command_json jsonb NOT NULL,
|
103
|
+
event_aggregate_id uuid,
|
104
|
+
event_sequence_number integer
|
105
|
+
)
|
106
|
+
PARTITION BY RANGE (id);
|
107
|
+
|
108
|
+
|
109
|
+
--
|
110
|
+
-- Name: enrich_command_json(sequent_schema.commands); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
111
|
+
--
|
112
|
+
|
113
|
+
CREATE FUNCTION sequent_schema.enrich_command_json(command sequent_schema.commands) RETURNS jsonb
|
114
|
+
LANGUAGE plpgsql STRICT
|
115
|
+
SET search_path TO 'sequent_schema'
|
116
|
+
AS $$
|
117
|
+
BEGIN
|
118
|
+
RETURN jsonb_build_object(
|
119
|
+
'command_type', (SELECT type FROM command_types WHERE command_types.id = command.command_type_id),
|
120
|
+
'created_at', command.created_at,
|
121
|
+
'user_id', command.user_id,
|
122
|
+
'aggregate_id', command.aggregate_id,
|
123
|
+
'event_aggregate_id', command.event_aggregate_id,
|
124
|
+
'event_sequence_number', command.event_sequence_number
|
125
|
+
)
|
126
|
+
|| command.command_json;
|
127
|
+
END
|
128
|
+
$$;
|
129
|
+
|
130
|
+
|
131
|
+
--
|
132
|
+
-- Name: events; Type: TABLE; Schema: sequent_schema; Owner: -
|
133
|
+
--
|
134
|
+
|
135
|
+
CREATE TABLE sequent_schema.events (
|
136
|
+
aggregate_id uuid NOT NULL,
|
137
|
+
partition_key text DEFAULT ''::text NOT NULL,
|
138
|
+
sequence_number integer NOT NULL,
|
139
|
+
created_at timestamp with time zone NOT NULL,
|
140
|
+
command_id bigint NOT NULL,
|
141
|
+
event_type_id smallint NOT NULL,
|
142
|
+
event_json jsonb NOT NULL,
|
143
|
+
xact_id bigint DEFAULT ((pg_current_xact_id())::text)::bigint
|
144
|
+
)
|
145
|
+
PARTITION BY RANGE (partition_key);
|
146
|
+
|
147
|
+
|
148
|
+
--
|
149
|
+
-- Name: enrich_event_json(sequent_schema.events); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
150
|
+
--
|
151
|
+
|
152
|
+
CREATE FUNCTION sequent_schema.enrich_event_json(event sequent_schema.events) RETURNS jsonb
|
153
|
+
LANGUAGE plpgsql STRICT
|
154
|
+
SET search_path TO 'sequent_schema'
|
155
|
+
AS $$
|
156
|
+
BEGIN
|
157
|
+
RETURN jsonb_build_object(
|
158
|
+
'aggregate_id', event.aggregate_id,
|
159
|
+
'sequence_number', event.sequence_number,
|
160
|
+
'created_at', event.created_at
|
161
|
+
)
|
162
|
+
|| event.event_json;
|
163
|
+
END
|
164
|
+
$$;
|
165
|
+
|
166
|
+
|
167
|
+
--
|
168
|
+
-- Name: load_event(uuid, integer); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
169
|
+
--
|
170
|
+
|
171
|
+
CREATE FUNCTION sequent_schema.load_event(_aggregate_id uuid, _sequence_number integer) RETURNS SETOF sequent_schema.aggregate_event_type
|
172
|
+
LANGUAGE plpgsql STRICT
|
173
|
+
SET search_path TO 'sequent_schema'
|
174
|
+
AS $$
|
175
|
+
BEGIN
|
176
|
+
RETURN QUERY SELECT aggregate_types.type,
|
177
|
+
a.aggregate_id,
|
178
|
+
a.events_partition_key,
|
179
|
+
event_types.type,
|
180
|
+
enrich_event_json(e)
|
181
|
+
FROM aggregates a
|
182
|
+
INNER JOIN events e ON (a.events_partition_key, a.aggregate_id) = (e.partition_key, e.aggregate_id)
|
183
|
+
INNER JOIN aggregate_types ON a.aggregate_type_id = aggregate_types.id
|
184
|
+
INNER JOIN event_types ON e.event_type_id = event_types.id
|
185
|
+
WHERE a.aggregate_id = _aggregate_id
|
186
|
+
AND e.sequence_number = _sequence_number;
|
187
|
+
END;
|
188
|
+
$$;
|
189
|
+
|
190
|
+
|
191
|
+
--
|
192
|
+
-- Name: load_events(jsonb, boolean, timestamp with time zone); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
193
|
+
--
|
194
|
+
|
195
|
+
CREATE FUNCTION sequent_schema.load_events(_aggregate_ids jsonb, _use_snapshots boolean DEFAULT true, _until timestamp with time zone DEFAULT NULL::timestamp with time zone) RETURNS SETOF sequent_schema.aggregate_event_type
|
196
|
+
LANGUAGE plpgsql
|
197
|
+
SET search_path TO 'sequent_schema'
|
198
|
+
AS $$
|
199
|
+
DECLARE
|
200
|
+
_aggregate_id aggregates.aggregate_id%TYPE;
|
201
|
+
BEGIN
|
202
|
+
FOR _aggregate_id IN SELECT * FROM jsonb_array_elements_text(_aggregate_ids) LOOP
|
203
|
+
-- Use a single query to avoid race condition with UPDATEs to the events partition key
|
204
|
+
-- in case transaction isolation level is lower than repeatable read (the default of
|
205
|
+
-- PostgreSQL is read committed).
|
206
|
+
RETURN QUERY WITH
|
207
|
+
aggregate AS (
|
208
|
+
SELECT aggregate_types.type, aggregate_id, events_partition_key
|
209
|
+
FROM aggregates
|
210
|
+
JOIN aggregate_types ON aggregate_type_id = aggregate_types.id
|
211
|
+
WHERE aggregate_id = _aggregate_id
|
212
|
+
),
|
213
|
+
snapshot AS (
|
214
|
+
SELECT *
|
215
|
+
FROM snapshot_records
|
216
|
+
WHERE _use_snapshots
|
217
|
+
AND aggregate_id = _aggregate_id
|
218
|
+
AND (_until IS NULL OR created_at < _until)
|
219
|
+
ORDER BY sequence_number DESC LIMIT 1
|
220
|
+
)
|
221
|
+
(SELECT a.*, s.snapshot_type, s.snapshot_json FROM aggregate a, snapshot s)
|
222
|
+
UNION ALL
|
223
|
+
(SELECT a.*, event_types.type, enrich_event_json(e)
|
224
|
+
FROM aggregate a
|
225
|
+
JOIN events e ON (a.events_partition_key, a.aggregate_id) = (e.partition_key, e.aggregate_id)
|
226
|
+
JOIN event_types ON e.event_type_id = event_types.id
|
227
|
+
WHERE e.sequence_number >= COALESCE((SELECT sequence_number FROM snapshot), 0)
|
228
|
+
AND (_until IS NULL OR e.created_at < _until)
|
229
|
+
ORDER BY e.sequence_number ASC);
|
230
|
+
END LOOP;
|
231
|
+
END;
|
232
|
+
$$;
|
233
|
+
|
234
|
+
|
235
|
+
--
|
236
|
+
-- Name: load_latest_snapshot(uuid); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
237
|
+
--
|
238
|
+
|
239
|
+
CREATE FUNCTION sequent_schema.load_latest_snapshot(_aggregate_id uuid) RETURNS sequent_schema.aggregate_event_type
|
240
|
+
LANGUAGE sql
|
241
|
+
SET search_path TO 'sequent_schema'
|
242
|
+
AS $$
|
243
|
+
SELECT (SELECT type FROM aggregate_types WHERE id = a.aggregate_type_id),
|
244
|
+
a.aggregate_id,
|
245
|
+
a.events_partition_key,
|
246
|
+
s.snapshot_type,
|
247
|
+
s.snapshot_json
|
248
|
+
FROM aggregates a JOIN snapshot_records s ON a.aggregate_id = s.aggregate_id
|
249
|
+
WHERE a.aggregate_id = _aggregate_id
|
250
|
+
ORDER BY s.sequence_number DESC
|
251
|
+
LIMIT 1;
|
252
|
+
$$;
|
253
|
+
|
254
|
+
|
255
|
+
--
|
256
|
+
-- Name: permanently_delete_commands_without_events(uuid); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
257
|
+
--
|
258
|
+
|
259
|
+
CREATE PROCEDURE sequent_schema.permanently_delete_commands_without_events(IN _aggregate_id uuid)
|
260
|
+
LANGUAGE plpgsql
|
261
|
+
SET search_path TO 'sequent_schema'
|
262
|
+
AS $$
|
263
|
+
BEGIN
|
264
|
+
IF _aggregate_id IS NULL THEN
|
265
|
+
RAISE EXCEPTION 'aggregate_id must be specified to delete commands';
|
266
|
+
END IF;
|
267
|
+
|
268
|
+
DELETE FROM commands
|
269
|
+
WHERE aggregate_id = _aggregate_id
|
270
|
+
AND NOT EXISTS (SELECT 1 FROM events WHERE command_id = commands.id);
|
271
|
+
END;
|
272
|
+
$$;
|
273
|
+
|
274
|
+
|
275
|
+
--
|
276
|
+
-- Name: permanently_delete_event_streams(jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
277
|
+
--
|
278
|
+
|
279
|
+
CREATE PROCEDURE sequent_schema.permanently_delete_event_streams(IN _aggregate_ids jsonb)
|
280
|
+
LANGUAGE plpgsql
|
281
|
+
SET search_path TO 'sequent_schema'
|
282
|
+
AS $$
|
283
|
+
BEGIN
|
284
|
+
DELETE FROM events
|
285
|
+
USING jsonb_array_elements_text(_aggregate_ids) AS ids (id)
|
286
|
+
JOIN aggregates ON ids.id::uuid = aggregates.aggregate_id
|
287
|
+
WHERE events.partition_key = aggregates.events_partition_key
|
288
|
+
AND events.aggregate_id = aggregates.aggregate_id;
|
289
|
+
DELETE FROM aggregates
|
290
|
+
USING jsonb_array_elements_text(_aggregate_ids) AS ids (id)
|
291
|
+
WHERE aggregates.aggregate_id = ids.id::uuid;
|
292
|
+
END;
|
293
|
+
$$;
|
294
|
+
|
295
|
+
|
296
|
+
--
|
297
|
+
-- Name: save_events_on_delete_trigger(); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
298
|
+
--
|
299
|
+
|
300
|
+
CREATE FUNCTION sequent_schema.save_events_on_delete_trigger() RETURNS trigger
|
301
|
+
LANGUAGE plpgsql
|
302
|
+
SET search_path TO 'sequent_schema'
|
303
|
+
AS $$
|
304
|
+
BEGIN
|
305
|
+
INSERT INTO saved_event_records (operation, timestamp, "user", aggregate_id, partition_key, sequence_number, created_at, event_type, event_json, command_id, xact_id)
|
306
|
+
SELECT 'D',
|
307
|
+
statement_timestamp(),
|
308
|
+
user,
|
309
|
+
o.aggregate_id,
|
310
|
+
o.partition_key,
|
311
|
+
o.sequence_number,
|
312
|
+
o.created_at,
|
313
|
+
(SELECT type FROM event_types WHERE event_types.id = o.event_type_id),
|
314
|
+
o.event_json,
|
315
|
+
o.command_id,
|
316
|
+
o.xact_id
|
317
|
+
FROM old_table o;
|
318
|
+
RETURN NULL;
|
319
|
+
END;
|
320
|
+
$$;
|
321
|
+
|
322
|
+
|
323
|
+
--
|
324
|
+
-- Name: save_events_on_update_trigger(); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
325
|
+
--
|
326
|
+
|
327
|
+
CREATE FUNCTION sequent_schema.save_events_on_update_trigger() RETURNS trigger
|
328
|
+
LANGUAGE plpgsql
|
329
|
+
SET search_path TO 'sequent_schema'
|
330
|
+
AS $$
|
331
|
+
BEGIN
|
332
|
+
INSERT INTO saved_event_records (operation, timestamp, "user", aggregate_id, partition_key, sequence_number, created_at, event_type, event_json, command_id, xact_id)
|
333
|
+
SELECT 'U',
|
334
|
+
statement_timestamp(),
|
335
|
+
user,
|
336
|
+
o.aggregate_id,
|
337
|
+
o.partition_key,
|
338
|
+
o.sequence_number,
|
339
|
+
o.created_at,
|
340
|
+
(SELECT type FROM event_types WHERE event_types.id = o.event_type_id),
|
341
|
+
o.event_json,
|
342
|
+
o.command_id,
|
343
|
+
o.xact_id
|
344
|
+
FROM old_table o LEFT JOIN new_table n ON o.aggregate_id = n.aggregate_id AND o.sequence_number = n.sequence_number
|
345
|
+
WHERE n IS NULL
|
346
|
+
-- Only save when event related information changes
|
347
|
+
OR o.created_at <> n.created_at
|
348
|
+
OR o.event_type_id <> n.event_type_id
|
349
|
+
OR o.event_json <> n.event_json;
|
350
|
+
RETURN NULL;
|
351
|
+
END;
|
352
|
+
$$;
|
353
|
+
|
354
|
+
|
355
|
+
--
|
356
|
+
-- Name: select_aggregates_for_snapshotting(integer, timestamp with time zone, timestamp with time zone); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
357
|
+
--
|
358
|
+
|
359
|
+
CREATE FUNCTION sequent_schema.select_aggregates_for_snapshotting(_limit integer, _reschedule_snapshot_scheduled_before timestamp with time zone, _now timestamp with time zone DEFAULT now()) RETURNS TABLE(aggregate_id uuid)
|
360
|
+
LANGUAGE plpgsql
|
361
|
+
SET search_path TO 'sequent_schema'
|
362
|
+
AS $$
|
363
|
+
BEGIN
|
364
|
+
RETURN QUERY WITH scheduled AS MATERIALIZED (
|
365
|
+
SELECT a.aggregate_id
|
366
|
+
FROM aggregates_that_need_snapshots AS a
|
367
|
+
WHERE snapshot_outdated_at IS NOT NULL
|
368
|
+
ORDER BY snapshot_outdated_at ASC, snapshot_sequence_number_high_water_mark DESC, aggregate_id ASC
|
369
|
+
LIMIT _limit
|
370
|
+
FOR UPDATE
|
371
|
+
) UPDATE aggregates_that_need_snapshots AS row
|
372
|
+
SET snapshot_scheduled_at = _now
|
373
|
+
FROM scheduled
|
374
|
+
WHERE row.aggregate_id = scheduled.aggregate_id
|
375
|
+
AND (row.snapshot_scheduled_at IS NULL OR row.snapshot_scheduled_at < _reschedule_snapshot_scheduled_before)
|
376
|
+
RETURNING row.aggregate_id;
|
377
|
+
END;
|
378
|
+
$$;
|
379
|
+
|
380
|
+
|
381
|
+
--
|
382
|
+
-- Name: store_aggregates(jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
383
|
+
--
|
384
|
+
|
385
|
+
CREATE PROCEDURE sequent_schema.store_aggregates(IN _aggregates_with_events jsonb)
|
386
|
+
LANGUAGE plpgsql
|
387
|
+
SET search_path TO 'sequent_schema'
|
388
|
+
AS $$
|
389
|
+
DECLARE
|
390
|
+
_aggregate jsonb;
|
391
|
+
_events jsonb;
|
392
|
+
_aggregate_id aggregates.aggregate_id%TYPE;
|
393
|
+
_events_partition_key aggregates.events_partition_key%TYPE;
|
394
|
+
_snapshot_outdated_at aggregates_that_need_snapshots.snapshot_outdated_at%TYPE;
|
395
|
+
BEGIN
|
396
|
+
FOR _aggregate, _events IN SELECT row->0, row->1 FROM jsonb_array_elements(_aggregates_with_events) AS row LOOP
|
397
|
+
_aggregate_id = _aggregate->>'aggregate_id';
|
398
|
+
|
399
|
+
_events_partition_key = COALESCE(
|
400
|
+
_aggregate->>'events_partition_key',
|
401
|
+
(SELECT events_partition_key FROM aggregates WHERE aggregate_id = _aggregate_id),
|
402
|
+
''
|
403
|
+
);
|
404
|
+
|
405
|
+
INSERT INTO aggregates (aggregate_id, created_at, aggregate_type_id, events_partition_key)
|
406
|
+
VALUES (
|
407
|
+
_aggregate_id,
|
408
|
+
(_events->0->>'created_at')::timestamptz,
|
409
|
+
(SELECT id FROM aggregate_types WHERE type = _aggregate->>'aggregate_type'),
|
410
|
+
_events_partition_key
|
411
|
+
) ON CONFLICT (aggregate_id)
|
412
|
+
DO UPDATE SET events_partition_key = EXCLUDED.events_partition_key
|
413
|
+
WHERE aggregates.events_partition_key IS DISTINCT FROM EXCLUDED.events_partition_key;
|
414
|
+
|
415
|
+
_snapshot_outdated_at = _aggregate->>'snapshot_outdated_at';
|
416
|
+
IF _snapshot_outdated_at IS NOT NULL THEN
|
417
|
+
INSERT INTO aggregates_that_need_snapshots AS row (aggregate_id, snapshot_outdated_at)
|
418
|
+
VALUES (_aggregate_id, _snapshot_outdated_at)
|
419
|
+
ON CONFLICT (aggregate_id) DO UPDATE
|
420
|
+
SET snapshot_outdated_at = LEAST(row.snapshot_outdated_at, EXCLUDED.snapshot_outdated_at)
|
421
|
+
WHERE row.snapshot_outdated_at IS DISTINCT FROM EXCLUDED.snapshot_outdated_at;
|
422
|
+
END IF;
|
423
|
+
END LOOP;
|
424
|
+
END;
|
425
|
+
$$;
|
426
|
+
|
427
|
+
|
428
|
+
--
|
429
|
+
-- Name: store_command(jsonb); Type: FUNCTION; Schema: sequent_schema; Owner: -
|
430
|
+
--
|
431
|
+
|
432
|
+
CREATE FUNCTION sequent_schema.store_command(_command jsonb) RETURNS bigint
|
433
|
+
LANGUAGE plpgsql STRICT
|
434
|
+
SET search_path TO 'sequent_schema'
|
435
|
+
AS $$
|
436
|
+
DECLARE
|
437
|
+
_id commands.id%TYPE;
|
438
|
+
_command_json jsonb = _command->'command_json';
|
439
|
+
BEGIN
|
440
|
+
INSERT INTO commands (
|
441
|
+
created_at, user_id, aggregate_id, command_type_id, command_json,
|
442
|
+
event_aggregate_id, event_sequence_number
|
443
|
+
) VALUES (
|
444
|
+
(_command->>'created_at')::timestamptz,
|
445
|
+
(_command_json->>'user_id')::uuid,
|
446
|
+
(_command_json->>'aggregate_id')::uuid,
|
447
|
+
(SELECT id FROM command_types WHERE type = _command->>'command_type'),
|
448
|
+
(_command->'command_json') - '{command_type,created_at,user_id,aggregate_id,event_aggregate_id,event_sequence_number}'::text[],
|
449
|
+
(_command_json->>'event_aggregate_id')::uuid,
|
450
|
+
NULLIF(_command_json->'event_sequence_number', 'null'::jsonb)::integer
|
451
|
+
) RETURNING id INTO STRICT _id;
|
452
|
+
RETURN _id;
|
453
|
+
END;
|
454
|
+
$$;
|
455
|
+
|
456
|
+
|
457
|
+
--
|
458
|
+
-- Name: store_events(jsonb, jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
459
|
+
--
|
460
|
+
|
461
|
+
CREATE PROCEDURE sequent_schema.store_events(IN _command jsonb, IN _aggregates_with_events jsonb)
|
462
|
+
LANGUAGE plpgsql
|
463
|
+
SET search_path TO 'sequent_schema'
|
464
|
+
AS $$
|
465
|
+
DECLARE
|
466
|
+
_command_id commands.id%TYPE;
|
467
|
+
_aggregates jsonb;
|
468
|
+
_aggregate jsonb;
|
469
|
+
_events jsonb;
|
470
|
+
_aggregate_id aggregates.aggregate_id%TYPE;
|
471
|
+
_events_partition_key aggregates.events_partition_key%TYPE;
|
472
|
+
BEGIN
|
473
|
+
CALL update_types(_command, _aggregates_with_events);
|
474
|
+
|
475
|
+
_command_id = store_command(_command);
|
476
|
+
|
477
|
+
CALL store_aggregates(_aggregates_with_events);
|
478
|
+
|
479
|
+
FOR _aggregate, _events IN SELECT row->0, row->1 FROM jsonb_array_elements(_aggregates_with_events) AS row
|
480
|
+
ORDER BY row->0->'aggregate_id', row->1->0->'event_json'->'sequence_number'
|
481
|
+
LOOP
|
482
|
+
_aggregate_id = _aggregate->>'aggregate_id';
|
483
|
+
SELECT events_partition_key INTO STRICT _events_partition_key FROM aggregates WHERE aggregate_id = _aggregate_id;
|
484
|
+
|
485
|
+
INSERT INTO events (partition_key, aggregate_id, sequence_number, created_at, command_id, event_type_id, event_json)
|
486
|
+
SELECT _events_partition_key,
|
487
|
+
_aggregate_id,
|
488
|
+
(event->'event_json'->'sequence_number')::integer,
|
489
|
+
(event->>'created_at')::timestamptz,
|
490
|
+
_command_id,
|
491
|
+
(SELECT id FROM event_types WHERE type = event->>'event_type'),
|
492
|
+
(event->'event_json') - '{aggregate_id,created_at,event_type,sequence_number}'::text[]
|
493
|
+
FROM jsonb_array_elements(_events) AS event;
|
494
|
+
END LOOP;
|
495
|
+
|
496
|
+
_aggregates = (SELECT jsonb_agg(row->0) FROM jsonb_array_elements(_aggregates_with_events) AS row);
|
497
|
+
CALL update_unique_keys(_aggregates);
|
498
|
+
END;
|
499
|
+
$$;
|
500
|
+
|
501
|
+
|
502
|
+
--
|
503
|
+
-- Name: store_snapshots(jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
504
|
+
--
|
505
|
+
|
506
|
+
CREATE PROCEDURE sequent_schema.store_snapshots(IN _snapshots jsonb)
|
507
|
+
LANGUAGE plpgsql
|
508
|
+
SET search_path TO 'sequent_schema'
|
509
|
+
AS $$
|
510
|
+
DECLARE
|
511
|
+
_aggregate_id uuid;
|
512
|
+
_snapshot jsonb;
|
513
|
+
_sequence_number snapshot_records.sequence_number%TYPE;
|
514
|
+
BEGIN
|
515
|
+
FOR _snapshot IN SELECT * FROM jsonb_array_elements(_snapshots) LOOP
|
516
|
+
_aggregate_id = _snapshot->>'aggregate_id';
|
517
|
+
_sequence_number = _snapshot->'sequence_number';
|
518
|
+
|
519
|
+
INSERT INTO aggregates_that_need_snapshots AS row (aggregate_id, snapshot_sequence_number_high_water_mark)
|
520
|
+
VALUES (_aggregate_id, _sequence_number)
|
521
|
+
ON CONFLICT (aggregate_id) DO UPDATE
|
522
|
+
SET snapshot_sequence_number_high_water_mark =
|
523
|
+
GREATEST(row.snapshot_sequence_number_high_water_mark, EXCLUDED.snapshot_sequence_number_high_water_mark),
|
524
|
+
snapshot_outdated_at = NULL,
|
525
|
+
snapshot_scheduled_at = NULL;
|
526
|
+
|
527
|
+
INSERT INTO snapshot_records (aggregate_id, sequence_number, created_at, snapshot_type, snapshot_json)
|
528
|
+
VALUES (
|
529
|
+
_aggregate_id,
|
530
|
+
_sequence_number,
|
531
|
+
(_snapshot->>'created_at')::timestamptz,
|
532
|
+
_snapshot->>'snapshot_type',
|
533
|
+
_snapshot->'snapshot_json'
|
534
|
+
);
|
535
|
+
END LOOP;
|
536
|
+
END;
|
537
|
+
$$;
|
538
|
+
|
539
|
+
|
540
|
+
--
|
541
|
+
-- Name: update_types(jsonb, jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
542
|
+
--
|
543
|
+
|
544
|
+
CREATE PROCEDURE sequent_schema.update_types(IN _command jsonb, IN _aggregates_with_events jsonb)
|
545
|
+
LANGUAGE plpgsql
|
546
|
+
SET search_path TO 'sequent_schema'
|
547
|
+
AS $$
|
548
|
+
BEGIN
|
549
|
+
IF NOT EXISTS (SELECT 1 FROM command_types t WHERE t.type = _command->>'command_type') THEN
|
550
|
+
-- Only try inserting if it doesn't exist to avoid exhausting the id sequence
|
551
|
+
INSERT INTO command_types (type)
|
552
|
+
VALUES (_command->>'command_type')
|
553
|
+
ON CONFLICT DO NOTHING;
|
554
|
+
END IF;
|
555
|
+
|
556
|
+
WITH types AS (
|
557
|
+
SELECT DISTINCT row->0->>'aggregate_type' AS type
|
558
|
+
FROM jsonb_array_elements(_aggregates_with_events) AS row
|
559
|
+
)
|
560
|
+
INSERT INTO aggregate_types (type)
|
561
|
+
SELECT type FROM types
|
562
|
+
WHERE type NOT IN (SELECT type FROM aggregate_types)
|
563
|
+
ORDER BY 1
|
564
|
+
ON CONFLICT DO NOTHING;
|
565
|
+
|
566
|
+
WITH types AS (
|
567
|
+
SELECT DISTINCT events->>'event_type' AS type
|
568
|
+
FROM jsonb_array_elements(_aggregates_with_events) AS row
|
569
|
+
CROSS JOIN LATERAL jsonb_array_elements(row->1) AS events
|
570
|
+
)
|
571
|
+
INSERT INTO event_types (type)
|
572
|
+
SELECT type FROM types
|
573
|
+
WHERE type NOT IN (SELECT type FROM event_types)
|
574
|
+
ORDER BY 1
|
575
|
+
ON CONFLICT DO NOTHING;
|
576
|
+
END;
|
577
|
+
$$;
|
578
|
+
|
579
|
+
|
580
|
+
--
|
581
|
+
-- Name: update_unique_keys(jsonb); Type: PROCEDURE; Schema: sequent_schema; Owner: -
|
582
|
+
--
|
583
|
+
|
584
|
+
CREATE PROCEDURE sequent_schema.update_unique_keys(IN _stream_records jsonb)
|
585
|
+
LANGUAGE plpgsql
|
586
|
+
SET search_path TO 'sequent_schema'
|
587
|
+
AS $$
|
588
|
+
DECLARE
|
589
|
+
_aggregate jsonb;
|
590
|
+
_aggregate_id aggregates.aggregate_id%TYPE;
|
591
|
+
_unique_keys jsonb;
|
592
|
+
BEGIN
|
593
|
+
FOR _aggregate IN SELECT aggregate FROM jsonb_array_elements(_stream_records) AS aggregate LOOP
|
594
|
+
_aggregate_id = _aggregate->>'aggregate_id';
|
595
|
+
_unique_keys = COALESCE(_aggregate->'unique_keys', '{}'::jsonb);
|
596
|
+
|
597
|
+
DELETE FROM aggregate_unique_keys AS target
|
598
|
+
WHERE target.aggregate_id = _aggregate_id
|
599
|
+
AND NOT (_unique_keys ? target.scope);
|
600
|
+
END LOOP;
|
601
|
+
|
602
|
+
FOR _aggregate IN SELECT aggregate FROM jsonb_array_elements(_stream_records) AS aggregate LOOP
|
603
|
+
_aggregate_id = _aggregate->>'aggregate_id';
|
604
|
+
_unique_keys = COALESCE(_aggregate->'unique_keys', '{}'::jsonb);
|
605
|
+
|
606
|
+
INSERT INTO aggregate_unique_keys AS target (aggregate_id, scope, key)
|
607
|
+
SELECT _aggregate_id, key, value
|
608
|
+
FROM jsonb_each(_unique_keys) AS x
|
609
|
+
ON CONFLICT (aggregate_id, scope) DO UPDATE
|
610
|
+
SET key = EXCLUDED.key
|
611
|
+
WHERE target.key <> EXCLUDED.key;
|
612
|
+
END LOOP;
|
613
|
+
EXCEPTION
|
614
|
+
WHEN unique_violation THEN
|
615
|
+
RAISE unique_violation
|
616
|
+
USING MESSAGE = 'duplicate unique key value for aggregate ' || (_aggregate->>'aggregate_type') || ' ' || _aggregate_id || ' (' || SQLERRM || ')';
|
617
|
+
END;
|
618
|
+
$$;
|
619
|
+
|
620
|
+
|
621
|
+
SET default_table_access_method = heap;
|
622
|
+
|
623
|
+
--
|
624
|
+
-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: -
|
625
|
+
--
|
626
|
+
|
627
|
+
CREATE TABLE public.ar_internal_metadata (
|
628
|
+
key character varying NOT NULL,
|
629
|
+
value character varying,
|
630
|
+
created_at timestamp(6) without time zone NOT NULL,
|
631
|
+
updated_at timestamp(6) without time zone NOT NULL
|
632
|
+
);
|
633
|
+
|
634
|
+
|
635
|
+
--
|
636
|
+
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
637
|
+
--
|
638
|
+
|
639
|
+
CREATE TABLE public.schema_migrations (
|
640
|
+
version character varying NOT NULL
|
641
|
+
);
|
642
|
+
|
643
|
+
|
644
|
+
--
|
645
|
+
-- Name: aggregate_types; Type: TABLE; Schema: sequent_schema; Owner: -
|
646
|
+
--
|
647
|
+
|
648
|
+
CREATE TABLE sequent_schema.aggregate_types (
|
649
|
+
id smallint NOT NULL,
|
650
|
+
type text NOT NULL
|
651
|
+
);
|
652
|
+
|
653
|
+
|
654
|
+
--
|
655
|
+
-- Name: aggregate_types_id_seq; Type: SEQUENCE; Schema: sequent_schema; Owner: -
|
656
|
+
--
|
657
|
+
|
658
|
+
ALTER TABLE sequent_schema.aggregate_types ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
|
659
|
+
SEQUENCE NAME sequent_schema.aggregate_types_id_seq
|
660
|
+
START WITH 1
|
661
|
+
INCREMENT BY 1
|
662
|
+
NO MINVALUE
|
663
|
+
NO MAXVALUE
|
664
|
+
CACHE 1
|
665
|
+
);
|
666
|
+
|
667
|
+
|
668
|
+
--
|
669
|
+
-- Name: aggregate_unique_keys; Type: TABLE; Schema: sequent_schema; Owner: -
|
670
|
+
--
|
671
|
+
|
672
|
+
CREATE TABLE sequent_schema.aggregate_unique_keys (
|
673
|
+
aggregate_id uuid NOT NULL,
|
674
|
+
scope text NOT NULL,
|
675
|
+
key jsonb NOT NULL
|
676
|
+
);
|
677
|
+
|
678
|
+
|
679
|
+
--
|
680
|
+
-- Name: aggregates; Type: TABLE; Schema: sequent_schema; Owner: -
|
681
|
+
--
|
682
|
+
|
683
|
+
CREATE TABLE sequent_schema.aggregates (
|
684
|
+
aggregate_id uuid NOT NULL,
|
685
|
+
events_partition_key text DEFAULT ''::text NOT NULL,
|
686
|
+
aggregate_type_id smallint NOT NULL,
|
687
|
+
created_at timestamp with time zone DEFAULT now() NOT NULL
|
688
|
+
)
|
689
|
+
PARTITION BY RANGE (aggregate_id);
|
690
|
+
|
691
|
+
|
692
|
+
--
|
693
|
+
-- Name: aggregates_default; Type: TABLE; Schema: sequent_schema; Owner: -
|
694
|
+
--
|
695
|
+
|
696
|
+
CREATE TABLE sequent_schema.aggregates_default (
|
697
|
+
aggregate_id uuid NOT NULL,
|
698
|
+
events_partition_key text DEFAULT ''::text NOT NULL,
|
699
|
+
aggregate_type_id smallint NOT NULL,
|
700
|
+
created_at timestamp with time zone DEFAULT now() NOT NULL
|
701
|
+
);
|
702
|
+
|
703
|
+
|
704
|
+
--
|
705
|
+
-- Name: aggregates_that_need_snapshots; Type: TABLE; Schema: sequent_schema; Owner: -
|
706
|
+
--
|
707
|
+
|
708
|
+
CREATE TABLE sequent_schema.aggregates_that_need_snapshots (
|
709
|
+
aggregate_id uuid NOT NULL,
|
710
|
+
snapshot_sequence_number_high_water_mark integer,
|
711
|
+
snapshot_outdated_at timestamp with time zone,
|
712
|
+
snapshot_scheduled_at timestamp with time zone
|
713
|
+
);
|
714
|
+
|
715
|
+
|
716
|
+
--
|
717
|
+
-- Name: TABLE aggregates_that_need_snapshots; Type: COMMENT; Schema: sequent_schema; Owner: -
|
718
|
+
--
|
719
|
+
|
720
|
+
COMMENT ON TABLE sequent_schema.aggregates_that_need_snapshots IS 'Contains a row for every aggregate with more events than its snapshot threshold.';
|
721
|
+
|
722
|
+
|
723
|
+
--
|
724
|
+
-- Name: COLUMN aggregates_that_need_snapshots.snapshot_sequence_number_high_water_mark; Type: COMMENT; Schema: sequent_schema; Owner: -
|
725
|
+
--
|
726
|
+
|
727
|
+
COMMENT ON COLUMN sequent_schema.aggregates_that_need_snapshots.snapshot_sequence_number_high_water_mark IS 'The highest sequence number of the stored snapshot. Kept when snapshot are deleted to more easily query aggregates that need snapshotting the most';
|
728
|
+
|
729
|
+
|
730
|
+
--
|
731
|
+
-- Name: COLUMN aggregates_that_need_snapshots.snapshot_outdated_at; Type: COMMENT; Schema: sequent_schema; Owner: -
|
732
|
+
--
|
733
|
+
|
734
|
+
COMMENT ON COLUMN sequent_schema.aggregates_that_need_snapshots.snapshot_outdated_at IS 'Not NULL indicates a snapshot is needed since the stored timestamp';
|
735
|
+
|
736
|
+
|
737
|
+
--
|
738
|
+
-- Name: COLUMN aggregates_that_need_snapshots.snapshot_scheduled_at; Type: COMMENT; Schema: sequent_schema; Owner: -
|
739
|
+
--
|
740
|
+
|
741
|
+
COMMENT ON COLUMN sequent_schema.aggregates_that_need_snapshots.snapshot_scheduled_at IS 'Not NULL indicates a snapshot is in the process of being taken';
|
742
|
+
|
743
|
+
|
744
|
+
--
|
745
|
+
-- Name: command_types; Type: TABLE; Schema: sequent_schema; Owner: -
|
746
|
+
--
|
747
|
+
|
748
|
+
CREATE TABLE sequent_schema.command_types (
|
749
|
+
id smallint NOT NULL,
|
750
|
+
type text NOT NULL
|
751
|
+
);
|
752
|
+
|
753
|
+
|
754
|
+
--
|
755
|
+
-- Name: command_records; Type: VIEW; Schema: sequent_schema; Owner: -
|
756
|
+
--
|
757
|
+
|
758
|
+
CREATE VIEW sequent_schema.command_records AS
|
759
|
+
SELECT id,
|
760
|
+
user_id,
|
761
|
+
aggregate_id,
|
762
|
+
( SELECT command_types.type
|
763
|
+
FROM sequent_schema.command_types
|
764
|
+
WHERE (command_types.id = command.command_type_id)) AS command_type,
|
765
|
+
sequent_schema.enrich_command_json(command.*) AS command_json,
|
766
|
+
created_at,
|
767
|
+
event_aggregate_id,
|
768
|
+
event_sequence_number
|
769
|
+
FROM sequent_schema.commands command;
|
770
|
+
|
771
|
+
|
772
|
+
--
|
773
|
+
-- Name: command_types_id_seq; Type: SEQUENCE; Schema: sequent_schema; Owner: -
|
774
|
+
--
|
775
|
+
|
776
|
+
ALTER TABLE sequent_schema.command_types ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
|
777
|
+
SEQUENCE NAME sequent_schema.command_types_id_seq
|
778
|
+
START WITH 1
|
779
|
+
INCREMENT BY 1
|
780
|
+
NO MINVALUE
|
781
|
+
NO MAXVALUE
|
782
|
+
CACHE 1
|
783
|
+
);
|
784
|
+
|
785
|
+
|
786
|
+
--
|
787
|
+
-- Name: commands_default; Type: TABLE; Schema: sequent_schema; Owner: -
|
788
|
+
--
|
789
|
+
|
790
|
+
CREATE TABLE sequent_schema.commands_default (
|
791
|
+
id bigint NOT NULL,
|
792
|
+
created_at timestamp with time zone NOT NULL,
|
793
|
+
user_id uuid,
|
794
|
+
aggregate_id uuid,
|
795
|
+
command_type_id smallint NOT NULL,
|
796
|
+
command_json jsonb NOT NULL,
|
797
|
+
event_aggregate_id uuid,
|
798
|
+
event_sequence_number integer
|
799
|
+
);
|
800
|
+
|
801
|
+
|
802
|
+
--
|
803
|
+
-- Name: commands_id_seq; Type: SEQUENCE; Schema: sequent_schema; Owner: -
|
804
|
+
--
|
805
|
+
|
806
|
+
ALTER TABLE sequent_schema.commands ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
|
807
|
+
SEQUENCE NAME sequent_schema.commands_id_seq
|
808
|
+
START WITH 1
|
809
|
+
INCREMENT BY 1
|
810
|
+
NO MINVALUE
|
811
|
+
NO MAXVALUE
|
812
|
+
CACHE 1
|
813
|
+
);
|
814
|
+
|
815
|
+
|
816
|
+
--
|
817
|
+
-- Name: event_types; Type: TABLE; Schema: sequent_schema; Owner: -
|
818
|
+
--
|
819
|
+
|
820
|
+
CREATE TABLE sequent_schema.event_types (
|
821
|
+
id smallint NOT NULL,
|
822
|
+
type text NOT NULL
|
823
|
+
);
|
824
|
+
|
825
|
+
|
826
|
+
--
|
827
|
+
-- Name: event_records; Type: VIEW; Schema: sequent_schema; Owner: -
|
828
|
+
--
|
829
|
+
|
830
|
+
CREATE VIEW sequent_schema.event_records AS
|
831
|
+
SELECT aggregate.aggregate_id,
|
832
|
+
event.partition_key,
|
833
|
+
event.sequence_number,
|
834
|
+
event.created_at,
|
835
|
+
type.type AS event_type,
|
836
|
+
sequent_schema.enrich_event_json(event.*) AS event_json,
|
837
|
+
event.command_id AS command_record_id,
|
838
|
+
event.xact_id
|
839
|
+
FROM ((sequent_schema.events event
|
840
|
+
JOIN sequent_schema.aggregates aggregate ON (((aggregate.aggregate_id = event.aggregate_id) AND (aggregate.events_partition_key = event.partition_key))))
|
841
|
+
JOIN sequent_schema.event_types type ON ((event.event_type_id = type.id)));
|
842
|
+
|
843
|
+
|
844
|
+
--
|
845
|
+
-- Name: event_types_id_seq; Type: SEQUENCE; Schema: sequent_schema; Owner: -
|
846
|
+
--
|
847
|
+
|
848
|
+
ALTER TABLE sequent_schema.event_types ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
|
849
|
+
SEQUENCE NAME sequent_schema.event_types_id_seq
|
850
|
+
START WITH 1
|
851
|
+
INCREMENT BY 1
|
852
|
+
NO MINVALUE
|
853
|
+
NO MAXVALUE
|
854
|
+
CACHE 1
|
855
|
+
);
|
856
|
+
|
857
|
+
|
858
|
+
--
|
859
|
+
-- Name: events_default; Type: TABLE; Schema: sequent_schema; Owner: -
|
860
|
+
--
|
861
|
+
|
862
|
+
CREATE TABLE sequent_schema.events_default (
|
863
|
+
aggregate_id uuid NOT NULL,
|
864
|
+
partition_key text DEFAULT ''::text NOT NULL,
|
865
|
+
sequence_number integer NOT NULL,
|
866
|
+
created_at timestamp with time zone NOT NULL,
|
867
|
+
command_id bigint NOT NULL,
|
868
|
+
event_type_id smallint NOT NULL,
|
869
|
+
event_json jsonb NOT NULL,
|
870
|
+
xact_id bigint DEFAULT ((pg_current_xact_id())::text)::bigint
|
871
|
+
);
|
872
|
+
|
873
|
+
|
874
|
+
--
|
875
|
+
-- Name: saved_event_records; Type: TABLE; Schema: sequent_schema; Owner: -
|
876
|
+
--
|
877
|
+
|
878
|
+
CREATE TABLE sequent_schema.saved_event_records (
|
879
|
+
operation character varying(1) NOT NULL,
|
880
|
+
"timestamp" timestamp with time zone NOT NULL,
|
881
|
+
"user" text NOT NULL,
|
882
|
+
aggregate_id uuid NOT NULL,
|
883
|
+
partition_key text DEFAULT ''::text,
|
884
|
+
sequence_number integer NOT NULL,
|
885
|
+
created_at timestamp with time zone NOT NULL,
|
886
|
+
command_id bigint NOT NULL,
|
887
|
+
event_type text NOT NULL,
|
888
|
+
event_json jsonb NOT NULL,
|
889
|
+
xact_id bigint,
|
890
|
+
CONSTRAINT saved_event_records_operation_check CHECK (((operation)::text = ANY ((ARRAY['U'::character varying, 'D'::character varying])::text[])))
|
891
|
+
);
|
892
|
+
|
893
|
+
|
894
|
+
--
|
895
|
+
-- Name: snapshot_records; Type: TABLE; Schema: sequent_schema; Owner: -
|
896
|
+
--
|
897
|
+
|
898
|
+
CREATE TABLE sequent_schema.snapshot_records (
|
899
|
+
aggregate_id uuid NOT NULL,
|
900
|
+
sequence_number integer NOT NULL,
|
901
|
+
created_at timestamp with time zone NOT NULL,
|
902
|
+
snapshot_type text NOT NULL,
|
903
|
+
snapshot_json jsonb NOT NULL
|
904
|
+
);
|
905
|
+
|
906
|
+
|
907
|
+
--
|
908
|
+
-- Name: stream_records; Type: VIEW; Schema: sequent_schema; Owner: -
|
909
|
+
--
|
910
|
+
|
911
|
+
CREATE VIEW sequent_schema.stream_records AS
|
912
|
+
SELECT aggregates.aggregate_id,
|
913
|
+
aggregates.events_partition_key,
|
914
|
+
aggregate_types.type AS aggregate_type,
|
915
|
+
aggregates.created_at
|
916
|
+
FROM (sequent_schema.aggregates
|
917
|
+
JOIN sequent_schema.aggregate_types ON ((aggregates.aggregate_type_id = aggregate_types.id)));
|
918
|
+
|
919
|
+
|
920
|
+
--
|
921
|
+
-- Name: aggregates_default; Type: TABLE ATTACH; Schema: sequent_schema; Owner: -
|
922
|
+
--
|
923
|
+
|
924
|
+
ALTER TABLE ONLY sequent_schema.aggregates ATTACH PARTITION sequent_schema.aggregates_default DEFAULT;
|
925
|
+
|
926
|
+
|
927
|
+
--
|
928
|
+
-- Name: commands_default; Type: TABLE ATTACH; Schema: sequent_schema; Owner: -
|
929
|
+
--
|
930
|
+
|
931
|
+
ALTER TABLE ONLY sequent_schema.commands ATTACH PARTITION sequent_schema.commands_default DEFAULT;
|
932
|
+
|
933
|
+
|
934
|
+
--
|
935
|
+
-- Name: events_default; Type: TABLE ATTACH; Schema: sequent_schema; Owner: -
|
936
|
+
--
|
937
|
+
|
938
|
+
ALTER TABLE ONLY sequent_schema.events ATTACH PARTITION sequent_schema.events_default DEFAULT;
|
939
|
+
|
940
|
+
|
941
|
+
--
|
942
|
+
-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
943
|
+
--
|
944
|
+
|
945
|
+
ALTER TABLE ONLY public.ar_internal_metadata
|
946
|
+
ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key);
|
947
|
+
|
948
|
+
|
949
|
+
--
|
950
|
+
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
951
|
+
--
|
952
|
+
|
953
|
+
ALTER TABLE ONLY public.schema_migrations
|
954
|
+
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
|
955
|
+
|
956
|
+
|
957
|
+
--
|
958
|
+
-- Name: aggregate_types aggregate_types_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
959
|
+
--
|
960
|
+
|
961
|
+
ALTER TABLE ONLY sequent_schema.aggregate_types
|
962
|
+
ADD CONSTRAINT aggregate_types_pkey PRIMARY KEY (id);
|
963
|
+
|
964
|
+
|
965
|
+
--
|
966
|
+
-- Name: aggregate_types aggregate_types_type_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
967
|
+
--
|
968
|
+
|
969
|
+
ALTER TABLE ONLY sequent_schema.aggregate_types
|
970
|
+
ADD CONSTRAINT aggregate_types_type_key UNIQUE (type);
|
971
|
+
|
972
|
+
|
973
|
+
--
|
974
|
+
-- Name: aggregate_unique_keys aggregate_unique_keys_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
975
|
+
--
|
976
|
+
|
977
|
+
ALTER TABLE ONLY sequent_schema.aggregate_unique_keys
|
978
|
+
ADD CONSTRAINT aggregate_unique_keys_pkey PRIMARY KEY (aggregate_id, scope);
|
979
|
+
|
980
|
+
|
981
|
+
--
|
982
|
+
-- Name: aggregate_unique_keys aggregate_unique_keys_scope_key_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
983
|
+
--
|
984
|
+
|
985
|
+
ALTER TABLE ONLY sequent_schema.aggregate_unique_keys
|
986
|
+
ADD CONSTRAINT aggregate_unique_keys_scope_key_key UNIQUE (scope, key);
|
987
|
+
|
988
|
+
|
989
|
+
--
|
990
|
+
-- Name: aggregates aggregates_events_partition_key_aggregate_id_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
991
|
+
--
|
992
|
+
|
993
|
+
ALTER TABLE ONLY sequent_schema.aggregates
|
994
|
+
ADD CONSTRAINT aggregates_events_partition_key_aggregate_id_key UNIQUE (events_partition_key, aggregate_id);
|
995
|
+
|
996
|
+
|
997
|
+
--
|
998
|
+
-- Name: aggregates_default aggregates_default_events_partition_key_aggregate_id_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
999
|
+
--
|
1000
|
+
|
1001
|
+
ALTER TABLE ONLY sequent_schema.aggregates_default
|
1002
|
+
ADD CONSTRAINT aggregates_default_events_partition_key_aggregate_id_key UNIQUE (events_partition_key, aggregate_id);
|
1003
|
+
|
1004
|
+
|
1005
|
+
--
|
1006
|
+
-- Name: aggregates aggregates_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1007
|
+
--
|
1008
|
+
|
1009
|
+
ALTER TABLE ONLY sequent_schema.aggregates
|
1010
|
+
ADD CONSTRAINT aggregates_pkey PRIMARY KEY (aggregate_id);
|
1011
|
+
|
1012
|
+
|
1013
|
+
--
|
1014
|
+
-- Name: aggregates_default aggregates_default_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1015
|
+
--
|
1016
|
+
|
1017
|
+
ALTER TABLE ONLY sequent_schema.aggregates_default
|
1018
|
+
ADD CONSTRAINT aggregates_default_pkey PRIMARY KEY (aggregate_id);
|
1019
|
+
|
1020
|
+
|
1021
|
+
--
|
1022
|
+
-- Name: aggregates_that_need_snapshots aggregates_that_need_snapshots_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1023
|
+
--
|
1024
|
+
|
1025
|
+
ALTER TABLE ONLY sequent_schema.aggregates_that_need_snapshots
|
1026
|
+
ADD CONSTRAINT aggregates_that_need_snapshots_pkey PRIMARY KEY (aggregate_id);
|
1027
|
+
|
1028
|
+
|
1029
|
+
--
|
1030
|
+
-- Name: command_types command_types_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1031
|
+
--
|
1032
|
+
|
1033
|
+
ALTER TABLE ONLY sequent_schema.command_types
|
1034
|
+
ADD CONSTRAINT command_types_pkey PRIMARY KEY (id);
|
1035
|
+
|
1036
|
+
|
1037
|
+
--
|
1038
|
+
-- Name: command_types command_types_type_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1039
|
+
--
|
1040
|
+
|
1041
|
+
ALTER TABLE ONLY sequent_schema.command_types
|
1042
|
+
ADD CONSTRAINT command_types_type_key UNIQUE (type);
|
1043
|
+
|
1044
|
+
|
1045
|
+
--
|
1046
|
+
-- Name: commands commands_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1047
|
+
--
|
1048
|
+
|
1049
|
+
ALTER TABLE ONLY sequent_schema.commands
|
1050
|
+
ADD CONSTRAINT commands_pkey PRIMARY KEY (id);
|
1051
|
+
|
1052
|
+
|
1053
|
+
--
|
1054
|
+
-- Name: commands_default commands_default_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1055
|
+
--
|
1056
|
+
|
1057
|
+
ALTER TABLE ONLY sequent_schema.commands_default
|
1058
|
+
ADD CONSTRAINT commands_default_pkey PRIMARY KEY (id);
|
1059
|
+
|
1060
|
+
|
1061
|
+
--
|
1062
|
+
-- Name: event_types event_types_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1063
|
+
--
|
1064
|
+
|
1065
|
+
ALTER TABLE ONLY sequent_schema.event_types
|
1066
|
+
ADD CONSTRAINT event_types_pkey PRIMARY KEY (id);
|
1067
|
+
|
1068
|
+
|
1069
|
+
--
|
1070
|
+
-- Name: event_types event_types_type_key; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1071
|
+
--
|
1072
|
+
|
1073
|
+
ALTER TABLE ONLY sequent_schema.event_types
|
1074
|
+
ADD CONSTRAINT event_types_type_key UNIQUE (type);
|
1075
|
+
|
1076
|
+
|
1077
|
+
--
|
1078
|
+
-- Name: events events_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1079
|
+
--
|
1080
|
+
|
1081
|
+
ALTER TABLE ONLY sequent_schema.events
|
1082
|
+
ADD CONSTRAINT events_pkey PRIMARY KEY (partition_key, aggregate_id, sequence_number);
|
1083
|
+
|
1084
|
+
|
1085
|
+
--
|
1086
|
+
-- Name: events_default events_default_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1087
|
+
--
|
1088
|
+
|
1089
|
+
ALTER TABLE ONLY sequent_schema.events_default
|
1090
|
+
ADD CONSTRAINT events_default_pkey PRIMARY KEY (partition_key, aggregate_id, sequence_number);
|
1091
|
+
|
1092
|
+
|
1093
|
+
--
|
1094
|
+
-- Name: saved_event_records saved_event_records_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1095
|
+
--
|
1096
|
+
|
1097
|
+
ALTER TABLE ONLY sequent_schema.saved_event_records
|
1098
|
+
ADD CONSTRAINT saved_event_records_pkey PRIMARY KEY (aggregate_id, sequence_number, "timestamp");
|
1099
|
+
|
1100
|
+
|
1101
|
+
--
|
1102
|
+
-- Name: snapshot_records snapshot_records_pkey; Type: CONSTRAINT; Schema: sequent_schema; Owner: -
|
1103
|
+
--
|
1104
|
+
|
1105
|
+
ALTER TABLE ONLY sequent_schema.snapshot_records
|
1106
|
+
ADD CONSTRAINT snapshot_records_pkey PRIMARY KEY (aggregate_id, sequence_number);
|
1107
|
+
|
1108
|
+
|
1109
|
+
--
|
1110
|
+
-- Name: aggregates_aggregate_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1111
|
+
--
|
1112
|
+
|
1113
|
+
CREATE INDEX aggregates_aggregate_type_id_idx ON ONLY sequent_schema.aggregates USING btree (aggregate_type_id);
|
1114
|
+
|
1115
|
+
|
1116
|
+
--
|
1117
|
+
-- Name: aggregates_default_aggregate_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1118
|
+
--
|
1119
|
+
|
1120
|
+
CREATE INDEX aggregates_default_aggregate_type_id_idx ON sequent_schema.aggregates_default USING btree (aggregate_type_id);
|
1121
|
+
|
1122
|
+
|
1123
|
+
--
|
1124
|
+
-- Name: aggregates_that_need_snapshots_outdated_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1125
|
+
--
|
1126
|
+
|
1127
|
+
CREATE INDEX aggregates_that_need_snapshots_outdated_idx ON sequent_schema.aggregates_that_need_snapshots USING btree (snapshot_outdated_at, snapshot_sequence_number_high_water_mark DESC, aggregate_id) WHERE (snapshot_outdated_at IS NOT NULL);
|
1128
|
+
|
1129
|
+
|
1130
|
+
--
|
1131
|
+
-- Name: commands_aggregate_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1132
|
+
--
|
1133
|
+
|
1134
|
+
CREATE INDEX commands_aggregate_id_idx ON ONLY sequent_schema.commands USING btree (aggregate_id);
|
1135
|
+
|
1136
|
+
|
1137
|
+
--
|
1138
|
+
-- Name: commands_command_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1139
|
+
--
|
1140
|
+
|
1141
|
+
CREATE INDEX commands_command_type_id_idx ON ONLY sequent_schema.commands USING btree (command_type_id);
|
1142
|
+
|
1143
|
+
|
1144
|
+
--
|
1145
|
+
-- Name: commands_default_aggregate_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1146
|
+
--
|
1147
|
+
|
1148
|
+
CREATE INDEX commands_default_aggregate_id_idx ON sequent_schema.commands_default USING btree (aggregate_id);
|
1149
|
+
|
1150
|
+
|
1151
|
+
--
|
1152
|
+
-- Name: commands_default_command_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1153
|
+
--
|
1154
|
+
|
1155
|
+
CREATE INDEX commands_default_command_type_id_idx ON sequent_schema.commands_default USING btree (command_type_id);
|
1156
|
+
|
1157
|
+
|
1158
|
+
--
|
1159
|
+
-- Name: commands_event_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1160
|
+
--
|
1161
|
+
|
1162
|
+
CREATE INDEX commands_event_idx ON ONLY sequent_schema.commands USING btree (event_aggregate_id, event_sequence_number);
|
1163
|
+
|
1164
|
+
|
1165
|
+
--
|
1166
|
+
-- Name: commands_default_event_aggregate_id_event_sequence_number_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1167
|
+
--
|
1168
|
+
|
1169
|
+
CREATE INDEX commands_default_event_aggregate_id_event_sequence_number_idx ON sequent_schema.commands_default USING btree (event_aggregate_id, event_sequence_number);
|
1170
|
+
|
1171
|
+
|
1172
|
+
--
|
1173
|
+
-- Name: events_command_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1174
|
+
--
|
1175
|
+
|
1176
|
+
CREATE INDEX events_command_id_idx ON ONLY sequent_schema.events USING btree (command_id);
|
1177
|
+
|
1178
|
+
|
1179
|
+
--
|
1180
|
+
-- Name: events_default_command_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1181
|
+
--
|
1182
|
+
|
1183
|
+
CREATE INDEX events_default_command_id_idx ON sequent_schema.events_default USING btree (command_id);
|
1184
|
+
|
1185
|
+
|
1186
|
+
--
|
1187
|
+
-- Name: events_event_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1188
|
+
--
|
1189
|
+
|
1190
|
+
CREATE INDEX events_event_type_id_idx ON ONLY sequent_schema.events USING btree (event_type_id);
|
1191
|
+
|
1192
|
+
|
1193
|
+
--
|
1194
|
+
-- Name: events_default_event_type_id_idx; Type: INDEX; Schema: sequent_schema; Owner: -
|
1195
|
+
--
|
1196
|
+
|
1197
|
+
CREATE INDEX events_default_event_type_id_idx ON sequent_schema.events_default USING btree (event_type_id);
|
1198
|
+
|
1199
|
+
|
1200
|
+
--
|
1201
|
+
-- Name: aggregates_default_aggregate_type_id_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1202
|
+
--
|
1203
|
+
|
1204
|
+
ALTER INDEX sequent_schema.aggregates_aggregate_type_id_idx ATTACH PARTITION sequent_schema.aggregates_default_aggregate_type_id_idx;
|
1205
|
+
|
1206
|
+
|
1207
|
+
--
|
1208
|
+
-- Name: aggregates_default_events_partition_key_aggregate_id_key; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1209
|
+
--
|
1210
|
+
|
1211
|
+
ALTER INDEX sequent_schema.aggregates_events_partition_key_aggregate_id_key ATTACH PARTITION sequent_schema.aggregates_default_events_partition_key_aggregate_id_key;
|
1212
|
+
|
1213
|
+
|
1214
|
+
--
|
1215
|
+
-- Name: aggregates_default_pkey; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1216
|
+
--
|
1217
|
+
|
1218
|
+
ALTER INDEX sequent_schema.aggregates_pkey ATTACH PARTITION sequent_schema.aggregates_default_pkey;
|
1219
|
+
|
1220
|
+
|
1221
|
+
--
|
1222
|
+
-- Name: commands_default_aggregate_id_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1223
|
+
--
|
1224
|
+
|
1225
|
+
ALTER INDEX sequent_schema.commands_aggregate_id_idx ATTACH PARTITION sequent_schema.commands_default_aggregate_id_idx;
|
1226
|
+
|
1227
|
+
|
1228
|
+
--
|
1229
|
+
-- Name: commands_default_command_type_id_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1230
|
+
--
|
1231
|
+
|
1232
|
+
ALTER INDEX sequent_schema.commands_command_type_id_idx ATTACH PARTITION sequent_schema.commands_default_command_type_id_idx;
|
1233
|
+
|
1234
|
+
|
1235
|
+
--
|
1236
|
+
-- Name: commands_default_event_aggregate_id_event_sequence_number_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1237
|
+
--
|
1238
|
+
|
1239
|
+
ALTER INDEX sequent_schema.commands_event_idx ATTACH PARTITION sequent_schema.commands_default_event_aggregate_id_event_sequence_number_idx;
|
1240
|
+
|
1241
|
+
|
1242
|
+
--
|
1243
|
+
-- Name: commands_default_pkey; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1244
|
+
--
|
1245
|
+
|
1246
|
+
ALTER INDEX sequent_schema.commands_pkey ATTACH PARTITION sequent_schema.commands_default_pkey;
|
1247
|
+
|
1248
|
+
|
1249
|
+
--
|
1250
|
+
-- Name: events_default_command_id_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1251
|
+
--
|
1252
|
+
|
1253
|
+
ALTER INDEX sequent_schema.events_command_id_idx ATTACH PARTITION sequent_schema.events_default_command_id_idx;
|
1254
|
+
|
1255
|
+
|
1256
|
+
--
|
1257
|
+
-- Name: events_default_event_type_id_idx; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1258
|
+
--
|
1259
|
+
|
1260
|
+
ALTER INDEX sequent_schema.events_event_type_id_idx ATTACH PARTITION sequent_schema.events_default_event_type_id_idx;
|
1261
|
+
|
1262
|
+
|
1263
|
+
--
|
1264
|
+
-- Name: events_default_pkey; Type: INDEX ATTACH; Schema: sequent_schema; Owner: -
|
1265
|
+
--
|
1266
|
+
|
1267
|
+
ALTER INDEX sequent_schema.events_pkey ATTACH PARTITION sequent_schema.events_default_pkey;
|
1268
|
+
|
1269
|
+
|
1270
|
+
--
|
1271
|
+
-- Name: events save_events_on_delete_trigger; Type: TRIGGER; Schema: sequent_schema; Owner: -
|
1272
|
+
--
|
1273
|
+
|
1274
|
+
CREATE TRIGGER save_events_on_delete_trigger AFTER DELETE ON sequent_schema.events REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION sequent_schema.save_events_on_delete_trigger();
|
1275
|
+
|
1276
|
+
|
1277
|
+
--
|
1278
|
+
-- Name: events save_events_on_update_trigger; Type: TRIGGER; Schema: sequent_schema; Owner: -
|
1279
|
+
--
|
1280
|
+
|
1281
|
+
CREATE TRIGGER save_events_on_update_trigger AFTER UPDATE ON sequent_schema.events REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table FOR EACH STATEMENT EXECUTE FUNCTION sequent_schema.save_events_on_update_trigger();
|
1282
|
+
|
1283
|
+
|
1284
|
+
--
|
1285
|
+
-- Name: aggregate_unique_keys aggregate_unique_keys_aggregate_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1286
|
+
--
|
1287
|
+
|
1288
|
+
ALTER TABLE ONLY sequent_schema.aggregate_unique_keys
|
1289
|
+
ADD CONSTRAINT aggregate_unique_keys_aggregate_id_fkey FOREIGN KEY (aggregate_id) REFERENCES sequent_schema.aggregates(aggregate_id) ON UPDATE CASCADE ON DELETE CASCADE;
|
1290
|
+
|
1291
|
+
|
1292
|
+
--
|
1293
|
+
-- Name: aggregates aggregates_aggregate_type_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1294
|
+
--
|
1295
|
+
|
1296
|
+
ALTER TABLE sequent_schema.aggregates
|
1297
|
+
ADD CONSTRAINT aggregates_aggregate_type_id_fkey FOREIGN KEY (aggregate_type_id) REFERENCES sequent_schema.aggregate_types(id) ON UPDATE CASCADE;
|
1298
|
+
|
1299
|
+
|
1300
|
+
--
|
1301
|
+
-- Name: aggregates_that_need_snapshots aggregates_that_need_snapshots_aggregate_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1302
|
+
--
|
1303
|
+
|
1304
|
+
ALTER TABLE ONLY sequent_schema.aggregates_that_need_snapshots
|
1305
|
+
ADD CONSTRAINT aggregates_that_need_snapshots_aggregate_id_fkey FOREIGN KEY (aggregate_id) REFERENCES sequent_schema.aggregates(aggregate_id) ON UPDATE CASCADE ON DELETE CASCADE;
|
1306
|
+
|
1307
|
+
|
1308
|
+
--
|
1309
|
+
-- Name: commands commands_command_type_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1310
|
+
--
|
1311
|
+
|
1312
|
+
ALTER TABLE sequent_schema.commands
|
1313
|
+
ADD CONSTRAINT commands_command_type_id_fkey FOREIGN KEY (command_type_id) REFERENCES sequent_schema.command_types(id) ON UPDATE CASCADE;
|
1314
|
+
|
1315
|
+
|
1316
|
+
--
|
1317
|
+
-- Name: events events_command_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1318
|
+
--
|
1319
|
+
|
1320
|
+
ALTER TABLE sequent_schema.events
|
1321
|
+
ADD CONSTRAINT events_command_id_fkey FOREIGN KEY (command_id) REFERENCES sequent_schema.commands(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
|
1322
|
+
|
1323
|
+
|
1324
|
+
--
|
1325
|
+
-- Name: events events_event_type_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1326
|
+
--
|
1327
|
+
|
1328
|
+
ALTER TABLE sequent_schema.events
|
1329
|
+
ADD CONSTRAINT events_event_type_id_fkey FOREIGN KEY (event_type_id) REFERENCES sequent_schema.event_types(id) ON UPDATE CASCADE;
|
1330
|
+
|
1331
|
+
|
1332
|
+
--
|
1333
|
+
-- Name: events events_partition_key_aggregate_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1334
|
+
--
|
1335
|
+
|
1336
|
+
ALTER TABLE sequent_schema.events
|
1337
|
+
ADD CONSTRAINT events_partition_key_aggregate_id_fkey FOREIGN KEY (partition_key, aggregate_id) REFERENCES sequent_schema.aggregates(events_partition_key, aggregate_id) ON UPDATE CASCADE ON DELETE RESTRICT;
|
1338
|
+
|
1339
|
+
|
1340
|
+
--
|
1341
|
+
-- Name: snapshot_records snapshot_records_aggregate_id_fkey; Type: FK CONSTRAINT; Schema: sequent_schema; Owner: -
|
1342
|
+
--
|
1343
|
+
|
1344
|
+
ALTER TABLE ONLY sequent_schema.snapshot_records
|
1345
|
+
ADD CONSTRAINT snapshot_records_aggregate_id_fkey FOREIGN KEY (aggregate_id) REFERENCES sequent_schema.aggregates_that_need_snapshots(aggregate_id) ON UPDATE CASCADE ON DELETE CASCADE;
|
1346
|
+
|
1347
|
+
|
1348
|
+
--
|
1349
|
+
-- PostgreSQL database dump complete
|
1350
|
+
--
|
1351
|
+
|
1352
|
+
SET search_path TO public, view_schema, sequent_schema;
|
1353
|
+
|
1354
|
+
INSERT INTO "schema_migrations" (version) VALUES
|
1355
|
+
('20250312105100'),
|
1356
|
+
('20250101000001'),
|
1357
|
+
('20250101000000');
|
1358
|
+
|