CREATE OR REPLACE FUNCTION update_scheduled_episode_status()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.air_date < CURRENT_DATE AND NEW.status = 'upcoming' THEN
    NEW.status := 'aired';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
