002_deliveries.up.sql 1.3 KB

123456789101112131415161718192021222324252627
  1. -- 002_deliveries.up.sql
  2. -- Per-attempt delivery row. In M1 this is written by deliverd-fcm
  3. -- after each fake-fcm call. In M3+ it gets the full DLQ + retry
  4. -- semantics from SPEC §9.
  5. --
  6. -- For M1 the schema is intentionally minimal:
  7. -- - alert_id, individual_id, channel, target, status, attempts
  8. -- - the payload (jsonb) for replay (M8)
  9. -- Timescale conversion lands in M7.
  10. CREATE TABLE IF NOT EXISTS deliveries (
  11. id BIGSERIAL PRIMARY KEY,
  12. alert_id TEXT NOT NULL,
  13. company_id TEXT NOT NULL,
  14. individual_id TEXT NOT NULL,
  15. channel TEXT NOT NULL, -- fcm | telegram | sms | email | slack | teams | webhook
  16. target TEXT NOT NULL, -- the fcm_token, telegram_chat_id, phone_e164, …
  17. status TEXT NOT NULL DEFAULT 'pending', -- pending | sent | failed | dlq
  18. attempts INT NOT NULL DEFAULT 0,
  19. last_error TEXT,
  20. payload JSONB, -- snapshot at enqueue time, for M8 replay
  21. created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  22. sent_at TIMESTAMPTZ,
  23. next_attempt_at TIMESTAMPTZ
  24. );
  25. CREATE INDEX IF NOT EXISTS idx_deliveries_company_alert ON deliveries(company_id, alert_id);
  26. CREATE INDEX IF NOT EXISTS idx_deliveries_status ON deliveries(status) WHERE status IN ('pending','failed');