| 1 | BEGIN; |
| 2 | |
| 3 | -- One row per event we notified someone about. The primary key is what makes |
| 4 | -- the consumer idempotent: an event delivered twice finds its row. |
| 5 | CREATE TABLE notifications ( |
| 6 | event_id uuid PRIMARY KEY, |
| 7 | user_id uuid NOT NULL, |
| 8 | recipient text NOT NULL, |
| 9 | subject text NOT NULL, |
| 10 | sent_at timestamptz NOT NULL DEFAULT now() |
| 11 | ); |
| 12 | |
| 13 | -- Messages we gave up on, and why, until someone replays them. |
| 14 | CREATE TABLE dead_letters ( |
| 15 | stream text NOT NULL, |
| 16 | seq bigint NOT NULL, |
| 17 | subject text NOT NULL, |
| 18 | -- exactly as it arrived: it might not even decode |
| 19 | payload bytea NOT NULL, |
| 20 | error text NOT NULL, |
| 21 | deliveries integer NOT NULL, |
| 22 | failed_at timestamptz NOT NULL DEFAULT now(), |
| 23 | replayed_at timestamptz, |
| 24 | PRIMARY KEY (stream, seq) |
| 25 | ); |
| 26 | |
| 27 | COMMIT; |