summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180511174224_add_unique_constraint_to_project_features_project_id.rb
blob: a526001a91e60a61d752d6e00bf6a24e4ecd736c (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class AddUniqueConstraintToProjectFeaturesProjectId < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class ProjectFeature < ActiveRecord::Base
    self.table_name = 'project_features'

    include EachBatch
  end

  def up
    remove_duplicates

    add_concurrent_index :project_features, :project_id, unique: true, name: 'index_project_features_on_project_id_unique'
    remove_concurrent_index_by_name :project_features, 'index_project_features_on_project_id'
    rename_index :project_features, 'index_project_features_on_project_id_unique', 'index_project_features_on_project_id'
  end

  def down
    rename_index :project_features, 'index_project_features_on_project_id', 'index_project_features_on_project_id_old'
    add_concurrent_index :project_features, :project_id
    remove_concurrent_index_by_name :project_features, 'index_project_features_on_project_id_old'
  end

  private

  def remove_duplicates
    features = ProjectFeature
               .select('MAX(id) AS max, COUNT(id), project_id')
               .group(:project_id)
               .having('COUNT(id) > 1')

    features.each do |feature|
      ProjectFeature
        .where(project_id: feature['project_id'])
        .where('id <> ?', feature['max'])
        .each_batch { |batch| batch.delete_all }
    end
  end
end