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

20200325162730_schedule_backfill_push_rules_id_in_projects.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 984f4f20441ecb5d6c808dea92d03be7c29b56a0 (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
# frozen_string_literal: true

class ScheduleBackfillPushRulesIdInProjects < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  MIGRATION = 'BackfillPushRulesIdInProjects'
  BATCH_SIZE = 1_000

  class PushRules < ActiveRecord::Base
    include EachBatch

    self.table_name = 'push_rules'
  end

  def up
    # Update one record that is connected to the instance
    value_to_be_updated_to = ScheduleBackfillPushRulesIdInProjects::PushRules.find_by(is_sample: true)&.id

    if value_to_be_updated_to
      execute "UPDATE application_settings SET push_rule_id = #{value_to_be_updated_to}
        WHERE id IN (SELECT MAX(id) FROM application_settings);"
    end

    ApplicationSetting.expire

    queue_background_migration_jobs_by_range_at_intervals(ScheduleBackfillPushRulesIdInProjects::PushRules,
                                                          MIGRATION,
                                                          5.minutes,
                                                          batch_size: BATCH_SIZE)
  end

  def down
    execute "UPDATE application_settings SET push_rule_id = NULL"

    ApplicationSetting.expire
  end
end