snodra-messaging-sql 0.5.0__py3-none-any.whl
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.
- snodra_messaging_sql-0.5.0.dist-info/METADATA +129 -0
- snodra_messaging_sql-0.5.0.dist-info/RECORD +9 -0
- snodra_messaging_sql-0.5.0.dist-info/WHEEL +4 -0
- snodra_messaging_sql-0.5.0.dist-info/licenses/LICENSE +202 -0
- snodra_sql/__init__.py +37 -0
- snodra_sql/py.typed +0 -0
- snodra_sql/schema/mysql/V1__snodra.sql +212 -0
- snodra_sql/schema/postgresql/V1__snodra.sql +211 -0
- snodra_sql/sql.py +1708 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
-- snodra-messaging message store, schema version 1 (PostgreSQL 13 or later).
|
|
2
|
+
-- Shared by the Kotlin add-on (snodra-messaging-jdbc) and the Rust one (snodra-messaging-sqlx): both read and write these
|
|
3
|
+
-- tables the same way. Idempotent: it can run again over itself. Frozen once released: later changes are V2, V3, ...
|
|
4
|
+
-- Times are UTC instants at millisecond precision, stamped by the application (never now()).
|
|
5
|
+
-- Identifiers and positions use the "C" collation: compared and ordered by their bytes.
|
|
6
|
+
-- *_hash columns hold the lowercase hex SHA-256 of a value's UTF-8 bytes: they carry the uniqueness and the lookups of values
|
|
7
|
+
-- with no length bound (marker ids, idempotency keys, start keys, record keys, associations); the value itself is in a
|
|
8
|
+
-- TEXT column next to it. Bounded columns are checked by the libraries before any write (see the module README).
|
|
9
|
+
|
|
10
|
+
CREATE TABLE IF NOT EXISTS snodra_schema (
|
|
11
|
+
version INTEGER NOT NULL PRIMARY KEY,
|
|
12
|
+
applied_at TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
-- The outbox: events appended in the service's transaction, relayed to the event store afterwards.
|
|
16
|
+
CREATE TABLE IF NOT EXISTS snodra_outbox (
|
|
17
|
+
id VARCHAR(255) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
18
|
+
type VARCHAR(255) COLLATE "C" NOT NULL,
|
|
19
|
+
event_key TEXT NULL,
|
|
20
|
+
headers JSONB NOT NULL,
|
|
21
|
+
payload JSONB NOT NULL,
|
|
22
|
+
created_at TIMESTAMPTZ(3) NOT NULL,
|
|
23
|
+
seq BIGINT NOT NULL,
|
|
24
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
25
|
+
sent_at TIMESTAMPTZ(3) NULL,
|
|
26
|
+
failed_at TIMESTAMPTZ(3) NULL,
|
|
27
|
+
last_error TEXT NULL,
|
|
28
|
+
owner TEXT NULL,
|
|
29
|
+
lease_until TIMESTAMPTZ(3) NULL
|
|
30
|
+
);
|
|
31
|
+
CREATE INDEX IF NOT EXISTS snodra_outbox_pending ON snodra_outbox (created_at, seq) WHERE sent_at IS NULL AND failed_at IS NULL;
|
|
32
|
+
CREATE INDEX IF NOT EXISTS snodra_outbox_sent ON snodra_outbox (sent_at) WHERE sent_at IS NOT NULL;
|
|
33
|
+
|
|
34
|
+
-- Processed-event markers: one per id (processor:<name>:<eventId>, saga:<type>:<eventId>, ingress:<source>:<key>, ...).
|
|
35
|
+
-- A stage-1 marker also carries its processor, source, segment and position (position_num: the position as a number when
|
|
36
|
+
-- it is a canonical decimal one, so a reset deletes a range by value).
|
|
37
|
+
CREATE TABLE IF NOT EXISTS snodra_processed_events (
|
|
38
|
+
id_hash CHAR(64) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
39
|
+
id TEXT NOT NULL,
|
|
40
|
+
type TEXT NOT NULL,
|
|
41
|
+
processed_at TIMESTAMPTZ(3) NOT NULL,
|
|
42
|
+
processor VARCHAR(64) COLLATE "C" NULL,
|
|
43
|
+
source VARCHAR(255) COLLATE "C" NULL,
|
|
44
|
+
segment VARCHAR(128) COLLATE "C" NULL,
|
|
45
|
+
position VARCHAR(128) COLLATE "C" NULL,
|
|
46
|
+
position_num BIGINT NULL
|
|
47
|
+
);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS snodra_processed_events_range ON snodra_processed_events (processor, source, segment, position_num) WHERE processor IS NOT NULL;
|
|
49
|
+
CREATE INDEX IF NOT EXISTS snodra_processed_events_processed_at ON snodra_processed_events (processed_at);
|
|
50
|
+
|
|
51
|
+
-- Dead letters, sequenced per (processor, key). id = <processor>:<source>:<segment>:<position> (at most 578).
|
|
52
|
+
CREATE TABLE IF NOT EXISTS snodra_dead_letters (
|
|
53
|
+
id VARCHAR(578) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
54
|
+
processor VARCHAR(64) COLLATE "C" NOT NULL,
|
|
55
|
+
source VARCHAR(255) COLLATE "C" NOT NULL,
|
|
56
|
+
segment VARCHAR(128) COLLATE "C" NOT NULL,
|
|
57
|
+
position VARCHAR(128) COLLATE "C" NOT NULL,
|
|
58
|
+
position_num BIGINT NULL,
|
|
59
|
+
event_key TEXT NULL,
|
|
60
|
+
event_key_hash CHAR(64) COLLATE "C" NULL,
|
|
61
|
+
event_id TEXT NULL,
|
|
62
|
+
event_type TEXT NULL,
|
|
63
|
+
headers JSONB NOT NULL,
|
|
64
|
+
payload TEXT NULL,
|
|
65
|
+
payload_binary BYTEA NULL,
|
|
66
|
+
reason VARCHAR(32) NOT NULL,
|
|
67
|
+
error_type TEXT NOT NULL,
|
|
68
|
+
error_message TEXT NOT NULL,
|
|
69
|
+
blocked_by TEXT NULL,
|
|
70
|
+
attempts INTEGER NOT NULL,
|
|
71
|
+
parked_at TIMESTAMPTZ(3) NOT NULL,
|
|
72
|
+
last_attempt_at TIMESTAMPTZ(3) NOT NULL,
|
|
73
|
+
expire_at TIMESTAMPTZ(3) NOT NULL,
|
|
74
|
+
replay BOOLEAN NOT NULL DEFAULT FALSE
|
|
75
|
+
);
|
|
76
|
+
CREATE INDEX IF NOT EXISTS snodra_dead_letters_key ON snodra_dead_letters (processor, event_key_hash, position_num);
|
|
77
|
+
CREATE INDEX IF NOT EXISTS snodra_dead_letters_parked ON snodra_dead_letters (processor, parked_at);
|
|
78
|
+
CREATE INDEX IF NOT EXISTS snodra_dead_letters_expire ON snodra_dead_letters (expire_at);
|
|
79
|
+
|
|
80
|
+
-- One row per event processor: its pause, the reset operation holding its lock, its open replay ranges
|
|
81
|
+
-- (a JSON array of {"source","segment","from","until"} strings, in order).
|
|
82
|
+
CREATE TABLE IF NOT EXISTS snodra_processor_control (
|
|
83
|
+
processor VARCHAR(64) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
84
|
+
paused BOOLEAN NOT NULL DEFAULT FALSE,
|
|
85
|
+
paused_at TIMESTAMPTZ(3) NULL,
|
|
86
|
+
paused_by TEXT NULL,
|
|
87
|
+
op_id TEXT NULL,
|
|
88
|
+
op_kind TEXT NULL,
|
|
89
|
+
op_lease_until TIMESTAMPTZ(3) NULL,
|
|
90
|
+
op_prepared_at TIMESTAMPTZ(3) NULL,
|
|
91
|
+
replay JSONB NOT NULL,
|
|
92
|
+
updated_at TIMESTAMPTZ(3) NULL
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
-- Idempotent external calls and ingress (@IdempotentHandler): one record per idempotency key (id). reply and error are JSON
|
|
96
|
+
-- text, kept as written.
|
|
97
|
+
CREATE TABLE IF NOT EXISTS snodra_external_calls (
|
|
98
|
+
id_hash CHAR(64) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
99
|
+
id TEXT NOT NULL,
|
|
100
|
+
status VARCHAR(32) NOT NULL,
|
|
101
|
+
reply TEXT NULL,
|
|
102
|
+
error TEXT NULL,
|
|
103
|
+
attempts INTEGER NOT NULL,
|
|
104
|
+
updated_at TIMESTAMPTZ(3) NOT NULL,
|
|
105
|
+
lease_until TIMESTAMPTZ(3) NULL,
|
|
106
|
+
owner TEXT NULL
|
|
107
|
+
);
|
|
108
|
+
CREATE INDEX IF NOT EXISTS snodra_external_calls_updated ON snodra_external_calls (updated_at);
|
|
109
|
+
|
|
110
|
+
-- Saga instances. state is the saga as JSON text, kept as written. live_start_key_hash is the start key's hash
|
|
111
|
+
-- while the saga is live: its unique index lets one live saga hold a start key.
|
|
112
|
+
CREATE TABLE IF NOT EXISTS snodra_sagas (
|
|
113
|
+
id VARCHAR(255) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
114
|
+
type VARCHAR(255) COLLATE "C" NOT NULL,
|
|
115
|
+
state TEXT NOT NULL,
|
|
116
|
+
start_key TEXT NULL,
|
|
117
|
+
start_key_hash CHAR(64) COLLATE "C" NULL,
|
|
118
|
+
ended BOOLEAN NOT NULL,
|
|
119
|
+
ended_at TIMESTAMPTZ(3) NULL,
|
|
120
|
+
version BIGINT NOT NULL,
|
|
121
|
+
created_at TIMESTAMPTZ(3) NOT NULL,
|
|
122
|
+
updated_at TIMESTAMPTZ(3) NOT NULL,
|
|
123
|
+
live_start_key_hash CHAR(64) COLLATE "C" GENERATED ALWAYS AS (CASE WHEN ended THEN NULL ELSE start_key_hash END) STORED
|
|
124
|
+
);
|
|
125
|
+
CREATE UNIQUE INDEX IF NOT EXISTS snodra_sagas_live_start_key ON snodra_sagas (live_start_key_hash);
|
|
126
|
+
CREATE INDEX IF NOT EXISTS snodra_sagas_type_ended ON snodra_sagas (type, ended);
|
|
127
|
+
CREATE INDEX IF NOT EXISTS snodra_sagas_ended_at ON snodra_sagas (ended_at);
|
|
128
|
+
|
|
129
|
+
-- A saga's associations key = value, in order (idx from 0): what routes events to it. assoc_hash = SHA-256 of
|
|
130
|
+
-- key, a zero byte, value.
|
|
131
|
+
CREATE TABLE IF NOT EXISTS snodra_saga_associations (
|
|
132
|
+
saga_id VARCHAR(255) COLLATE "C" NOT NULL REFERENCES snodra_sagas (id) ON DELETE CASCADE,
|
|
133
|
+
idx INTEGER NOT NULL,
|
|
134
|
+
assoc_key TEXT NOT NULL,
|
|
135
|
+
assoc_value TEXT NOT NULL,
|
|
136
|
+
assoc_hash CHAR(64) COLLATE "C" NOT NULL,
|
|
137
|
+
PRIMARY KEY (saga_id, idx)
|
|
138
|
+
);
|
|
139
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_associations_hash ON snodra_saga_associations (assoc_hash);
|
|
140
|
+
|
|
141
|
+
-- The saga-side outbox: commands and events a saga recorded, sent in seq order per saga.
|
|
142
|
+
CREATE TABLE IF NOT EXISTS snodra_saga_dispatch (
|
|
143
|
+
id VARCHAR(255) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
144
|
+
saga_id VARCHAR(255) COLLATE "C" NOT NULL,
|
|
145
|
+
seq BIGINT NOT NULL,
|
|
146
|
+
kind VARCHAR(16) NOT NULL,
|
|
147
|
+
type VARCHAR(255) COLLATE "C" NOT NULL,
|
|
148
|
+
payload TEXT NOT NULL,
|
|
149
|
+
headers JSONB NOT NULL,
|
|
150
|
+
routing_key TEXT NULL,
|
|
151
|
+
created_at TIMESTAMPTZ(3) NOT NULL,
|
|
152
|
+
dispatched_at TIMESTAMPTZ(3) NULL,
|
|
153
|
+
attempts INTEGER NOT NULL,
|
|
154
|
+
next_attempt_at TIMESTAMPTZ(3) NULL,
|
|
155
|
+
failed_at TIMESTAMPTZ(3) NULL,
|
|
156
|
+
failure_reason VARCHAR(32) NULL,
|
|
157
|
+
failure_message TEXT NULL,
|
|
158
|
+
notice_pending BOOLEAN NULL,
|
|
159
|
+
notice_attempts INTEGER NULL,
|
|
160
|
+
notice_next_at TIMESTAMPTZ(3) NULL
|
|
161
|
+
);
|
|
162
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_dispatch_saga_seq ON snodra_saga_dispatch (saga_id, seq);
|
|
163
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_dispatch_pending ON snodra_saga_dispatch (saga_id, seq) WHERE dispatched_at IS NULL AND failed_at IS NULL;
|
|
164
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_dispatch_notice ON snodra_saga_dispatch (notice_next_at) WHERE notice_pending;
|
|
165
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_dispatch_dispatched ON snodra_saga_dispatch (dispatched_at) WHERE dispatched_at IS NOT NULL;
|
|
166
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_dispatch_failed ON snodra_saga_dispatch (failed_at) WHERE failed_at IS NOT NULL;
|
|
167
|
+
|
|
168
|
+
-- One dispatch relay instance per saga at a time.
|
|
169
|
+
CREATE TABLE IF NOT EXISTS snodra_saga_dispatch_leases (
|
|
170
|
+
saga_id VARCHAR(255) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
171
|
+
owner TEXT NOT NULL,
|
|
172
|
+
lease_until TIMESTAMPTZ(3) NOT NULL
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
-- Saga deadlines. payload is JSON text (any JSON value), kept as written.
|
|
176
|
+
CREATE TABLE IF NOT EXISTS snodra_deadlines (
|
|
177
|
+
id VARCHAR(255) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
178
|
+
saga_type VARCHAR(255) COLLATE "C" NOT NULL,
|
|
179
|
+
saga_id VARCHAR(255) COLLATE "C" NOT NULL,
|
|
180
|
+
name VARCHAR(255) COLLATE "C" NOT NULL,
|
|
181
|
+
payload TEXT NULL,
|
|
182
|
+
payload_type TEXT NULL,
|
|
183
|
+
due_at TIMESTAMPTZ(3) NOT NULL,
|
|
184
|
+
correlation_id TEXT NOT NULL,
|
|
185
|
+
status VARCHAR(16) NOT NULL,
|
|
186
|
+
claimed_by TEXT NULL,
|
|
187
|
+
claimed_until TIMESTAMPTZ(3) NULL,
|
|
188
|
+
finished_at TIMESTAMPTZ(3) NULL
|
|
189
|
+
);
|
|
190
|
+
CREATE INDEX IF NOT EXISTS snodra_deadlines_due ON snodra_deadlines (status, due_at);
|
|
191
|
+
CREATE INDEX IF NOT EXISTS snodra_deadlines_saga ON snodra_deadlines (saga_id, status);
|
|
192
|
+
CREATE INDEX IF NOT EXISTS snodra_deadlines_finished ON snodra_deadlines (finished_at) WHERE finished_at IS NOT NULL;
|
|
193
|
+
|
|
194
|
+
-- Saga events parked after their last attempt. id = <sagaType>:<eventId>.
|
|
195
|
+
CREATE TABLE IF NOT EXISTS snodra_saga_failures (
|
|
196
|
+
id_hash CHAR(64) COLLATE "C" NOT NULL PRIMARY KEY,
|
|
197
|
+
id TEXT NOT NULL,
|
|
198
|
+
saga_type VARCHAR(255) COLLATE "C" NOT NULL,
|
|
199
|
+
event_id TEXT NOT NULL,
|
|
200
|
+
event_type TEXT NOT NULL,
|
|
201
|
+
payload TEXT NOT NULL,
|
|
202
|
+
headers JSONB NOT NULL,
|
|
203
|
+
error TEXT NOT NULL,
|
|
204
|
+
attempts INTEGER NOT NULL,
|
|
205
|
+
first_failed_at TIMESTAMPTZ(3) NOT NULL,
|
|
206
|
+
last_failed_at TIMESTAMPTZ(3) NOT NULL
|
|
207
|
+
);
|
|
208
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_failures_type ON snodra_saga_failures (saga_type, last_failed_at);
|
|
209
|
+
CREATE INDEX IF NOT EXISTS snodra_saga_failures_last ON snodra_saga_failures (last_failed_at);
|
|
210
|
+
|
|
211
|
+
INSERT INTO snodra_schema (version) VALUES (1) ON CONFLICT (version) DO NOTHING;
|