- @conn.
- exec("select id, created_at, closed_at, num_changes from changesets where closed_at > ((now() at time zone 'utc') - '1 hour'::interval)").
- map {|row| Changeset.new(row) }.
- select {|cs| cs.activity_between?(last_run, @now) }
+ changesets = @conn
+ .exec("select id, created_at, closed_at, num_changes from changesets where closed_at > ((now() at time zone 'utc') - '1 hour'::interval)")
+ .map { |row| Changeset.new(row) }
+ .select { |cs| cs.activity_between?(last_run, @now) }
+
+ # set for faster presence lookups by ID
+ cs_ids = Set.new(changesets.map { |c| c.id })
+
+ # but also add any changesets which have new comments
+ new_ids = @conn
+ .exec("select distinct changeset_id from changeset_comments where created_at >= '#{last_run}' and created_at < '#{@now}' and visible")
+ .map { |row| row["changeset_id"].to_i }
+ .select { |c_id| not cs_ids.include?(c_id) }
+
+ new_ids.each do |id|
+ @conn
+ .exec("select id, created_at, closed_at, num_changes from changesets where id=#{id}")
+ .map { |row| Changeset.new(row) }
+ .each { |cs| changesets << cs }
+ end
+
+ changesets.sort_by { |cs| cs.id }