seed_m2.sql 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. -- seed_m2.sql
  2. -- M2 seed. Adds 2 more individuals (Bob, Carol), one group (sre:
  3. -- Alice+Bob), the M2 source row, and four subscriptions covering
  4. -- the four M2 verification scenarios.
  5. --
  6. -- Idempotent (ON CONFLICT DO NOTHING + careful ordering). Safe
  7. -- to re-run on top of seed.sql.
  8. -- ── individuals ───────────────────────────────────────────────────
  9. INSERT INTO individuals (id, company_id, full_name, email, locale, tz) VALUES
  10. ('ind-acme-002', 'acme-001', 'Bob SRE', 'bob@acme.example', 'en', 'UTC'),
  11. ('ind-acme-003', 'acme-001', 'Carol NOC', 'carol@acme.example', 'en', 'UTC')
  12. ON CONFLICT (id) DO NOTHING;
  13. -- ── tokens ────────────────────────────────────────────────────────
  14. INSERT INTO fcm_tokens (individual_id, token, device_id, locale, app_version) VALUES
  15. ('ind-acme-002', 'fake-fcm-token-acme-bob-001', 'pixel-7-bob', 'en', '1.0.0'),
  16. ('ind-acme-003', 'fake-fcm-token-acme-carol-001', 'pixel-7-carol', 'en', '1.0.0')
  17. ON CONFLICT (token) DO NOTHING;
  18. -- ── source row ────────────────────────────────────────────────────
  19. -- Pairs with BA_INGESTD_SOURCES env (HMAC auth). The env
  20. -- registers the secret; this row gives the resolver addressing
  21. -- metadata (allowed_targets + match_expr).
  22. --
  23. -- For M2 we leave allowed_targets EMPTY and rely on the
  24. -- broadcast fallback. Wait — per your Q2 answer, we hard-fail.
  25. -- So we populate allowed_targets to a real target so the
  26. -- resolver has somewhere to route.
  27. INSERT INTO sources (id, company_id, name, type, rate_limit_per_sec, allowed_targets, match_expr) VALUES
  28. ('prom-prod', 'acme-001', 'Prometheus (prod)', 'http', 100,
  29. '[
  30. {"type":"group", "id":"sre"},
  31. {"type":"individual", "id":"ind-acme-003"}
  32. ]'::jsonb,
  33. '{}'::jsonb
  34. )
  35. ON CONFLICT (company_id, id) DO NOTHING;
  36. -- ── groups ────────────────────────────────────────────────────────
  37. INSERT INTO groups (id, company_id, name, description) VALUES
  38. ('sre', 'acme-001', 'SRE Team', 'Site Reliability Engineering on-call')
  39. ON CONFLICT (company_id, id) DO NOTHING;
  40. -- ── group members ─────────────────────────────────────────────────
  41. -- sre = Alice (ind-acme-001) + Bob (ind-acme-002)
  42. INSERT INTO group_members (company_id, group_id, individual_id) VALUES
  43. ('acme-001', 'sre', 'ind-acme-001'),
  44. ('acme-001', 'sre', 'ind-acme-002')
  45. ON CONFLICT (company_id, group_id, individual_id) DO NOTHING;
  46. -- ── subscriptions ─────────────────────────────────────────────────
  47. -- Scenarios for M2_VERIFICATION.md:
  48. --
  49. -- (a) ind-acme-001 (Alice, in sre): plain fcm subscription, no
  50. -- severity filter, no quiet hours. Gets every alert.
  51. --
  52. -- (b) ind-acme-002 (Bob, in sre): fcm subscription, min_severity
  53. -- = 'critical'. Only critical / inminent_colapse alerts.
  54. --
  55. -- (c) ind-acme-003 (Carol, NOT in sre): fcm subscription with
  56. -- quiet_hours 00:00–23:59 in UTC. Skips everything except
  57. -- inminent_colapse (which bypasses quiet hours).
  58. --
  59. -- (d) ind-acme-002 (Bob) also subscribes via a routing_rule to
  60. -- demonstrate rule-driven targeting: any storage alert with
  61. -- host=db-prod-03 goes to him.
  62. INSERT INTO subscriptions (individual_id, company_id, source_id, min_severity, channel_mask, quiet_hours_start, quiet_hours_end, tz) VALUES
  63. -- (a) Alice: everything
  64. ('ind-acme-001', 'acme-001', 'prom-prod', NULL, '["fcm"]'::jsonb, NULL, NULL, 'UTC'),
  65. -- (b) Bob: critical and above
  66. ('ind-acme-002', 'acme-001', 'prom-prod', 'critical', '["fcm"]'::jsonb, NULL, NULL, 'UTC'),
  67. -- (c) Carol: quiet hours cover the whole day
  68. ('ind-acme-003', 'acme-001', 'prom-prod', NULL, '["fcm"]'::jsonb, '00:00', '23:59', 'UTC')
  69. ON CONFLICT (individual_id, source_id) DO NOTHING;
  70. -- ── routing rules ─────────────────────────────────────────────────
  71. -- Rule (d): any storage alert with data.host = db-prod-03 goes
  72. -- to Bob (ind-acme-002). Note: Bob's subscription has
  73. -- min_severity=critical, so this rule only fires for critical
  74. -- storage alerts about db-prod-03 (which is the realistic case
  75. -- for a "run this migration now" pager). The rule's match_expr
  76. -- intentionally does NOT filter on severity so we can show that
  77. -- the resolver still applies subscriptions on top of rules.
  78. INSERT INTO routing_rules (company_id, name, priority, match_expr, target) VALUES
  79. ('acme-001', 'DB prod-03 storage page', 10,
  80. '{"category":"storage","data":{"host":"db-prod-03"}}'::jsonb,
  81. '{"type":"individual","id":"ind-acme-002"}'::jsonb
  82. )
  83. ON CONFLICT (company_id, name) DO NOTHING;