-- in order for discord chatops messages to be updated when a segment is updated,
-- we need to have postgres update the related stream timestamp when a segment is updated.

CREATE OR REPLACE FUNCTION update_stream_on_segment_update()
RETURNS TRIGGER AS $$
BEGIN
  UPDATE api.streams
  SET updated_at = NOW() 
  WHERE id IN (
    SELECT stream_id 
    FROM segments_stream_links 
    WHERE segment_id = NEW.id
  );

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;


CREATE TRIGGER trigger_update_stream
  AFTER UPDATE ON api.segments
  FOR EACH ROW
  EXECUTE FUNCTION update_stream_on_segment_update();
