008_dlq.down.sql 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- 008_dlq.down.sql
  2. -- Reverse M8 DLQ additions. Drop the retention policy
  3. -- first, then convert the hypertable back to a plain
  4. -- table via the shadow-table rename pattern (see
  5. -- 006_timescale.down.sql for the rationale).
  6. -- 1. Remove the retention policy.
  7. SELECT remove_retention_policy('deliveries_dlq', if_exists => true);
  8. -- 2. Create a plain shadow table with the original
  9. -- (pre-hypertable) layout: BIGSERIAL PK, no chunks.
  10. CREATE TABLE IF NOT EXISTS deliveries_dlq_plain (
  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,
  16. target TEXT NOT NULL,
  17. original_subject TEXT NOT NULL,
  18. attempts INT NOT NULL,
  19. last_error TEXT NOT NULL,
  20. payload JSONB,
  21. discarded BOOLEAN NOT NULL DEFAULT false,
  22. discarded_at TIMESTAMPTZ,
  23. discarded_by TEXT,
  24. created_at TIMESTAMPTZ NOT NULL DEFAULT now()
  25. );
  26. -- 3. Copy the live data over. Timescale will reject
  27. -- SELECT FROM a hypertable that doesn't have
  28. -- move_data/copy_data logic; here we use a plain
  29. -- SELECT, which works on hypertables (you just lose
  30. -- the chunk-aware planner).
  31. INSERT INTO deliveries_dlq_plain
  32. (id, alert_id, company_id, individual_id, channel, target,
  33. original_subject, attempts, last_error, payload,
  34. discarded, discarded_at, discarded_by, created_at)
  35. SELECT
  36. id, alert_id, company_id, individual_id, channel, target,
  37. original_subject, attempts, last_error, payload,
  38. discarded, discarded_at, discarded_by, created_at
  39. FROM deliveries_dlq
  40. ON CONFLICT (id) DO NOTHING;
  41. -- 4. Drop the hypertable, rename the plain shadow.
  42. DROP TABLE IF EXISTS deliveries_dlq CASCADE;
  43. ALTER TABLE deliveries_dlq_plain RENAME TO deliveries_dlq;
  44. -- 5. Recreate the operator-UI indexes.
  45. CREATE INDEX IF NOT EXISTS idx_dlq_company_created
  46. ON deliveries_dlq(company_id, created_at DESC)
  47. WHERE discarded = false;
  48. CREATE INDEX IF NOT EXISTS idx_dlq_alert
  49. ON deliveries_dlq(alert_id);