summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170815060945_remove_duplicate_mr_events.rb
blob: fdc126b8fd6e59189e373780703df744036f259e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class RemoveDuplicateMrEvents < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers
  DOWNTIME = false

  class Event < ActiveRecord::Base
    self.table_name = 'events'
  end

  def up
    base_condition = "action = 1 AND target_type = 'MergeRequest' AND created_at > '2017-08-13'"
    Event.select('target_id, count(*)')
      .where(base_condition)
      .group('target_id').having('count(*) > 1').each do |event|
      duplicates = Event.where("#{base_condition} AND target_id = #{event.target_id}").pluck(:id)
      duplicates.shift

      Event.where(id: duplicates).delete_all
    end
  end

  def down
  end
end