Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20171215113714_populate_can_push_from_deploy_keys_projects.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec0427a8e5ae9f8e48a1de304e7f444c54e6ef01 (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
44
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class PopulateCanPushFromDeployKeysProjects < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  # Set this constant to true if this migration requires downtime.
  DOWNTIME = false
  disable_ddl_transaction!

  class DeploysKeyProject < ActiveRecord::Base
    include EachBatch

    self.table_name = 'deploy_keys_projects'
  end

  def up
    DeploysKeyProject.each_batch(of: 10_000) do |batch|
      start_id, end_id = batch.pluck('MIN(id), MAX(id)').first

      execute <<-EOF
        UPDATE deploy_keys_projects
        SET can_push = keys.can_push
        FROM keys
        WHERE deploy_key_id = keys.id
        AND deploy_keys_projects.id BETWEEN #{start_id} AND #{end_id}
      EOF
    end
  end

  def down
    DeploysKeyProject.each_batch(of: 10_000) do |batch|
      start_id, end_id = batch.pluck('MIN(id), MAX(id)').first

      execute <<-EOF
        UPDATE keys
        SET can_push = deploy_keys_projects.can_push
        FROM deploy_keys_projects
        WHERE deploy_keys_projects.deploy_key_id = keys.id
        AND deploy_keys_projects.id BETWEEN #{start_id} AND #{end_id}
      EOF
    end
  end
end