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

20200701070435_add_default_value_stream_to_groups_with_group_stages.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 971eb3c489f5d437bf7590b5c766b55a0668b0ea (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
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

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

  DOWNTIME = false

  disable_ddl_transaction!

  class Group < ActiveRecord::Base
    def self.find_sti_class(typename)
      if typename == 'Group'
        Group
      else
        super
      end
    end
    self.table_name = 'namespaces'
    has_many :group_value_streams
    has_many :group_stages
  end

  class GroupValueStream < ActiveRecord::Base
    self.table_name = 'analytics_cycle_analytics_group_value_streams'
    has_many :group_stages
    belongs_to :group
  end

  class GroupStage < ActiveRecord::Base
    self.table_name = 'analytics_cycle_analytics_group_stages'
    belongs_to :group_value_stream
  end

  def up
    Group.where(type: 'Group').joins(:group_stages).distinct.find_each do |group|
      Group.transaction do
        group_value_stream = group.group_value_streams.first_or_create!(name: 'default')
        group.group_stages.update_all(group_value_stream_id: group_value_stream.id)
      end
    end

    change_column_null :analytics_cycle_analytics_group_stages, :group_value_stream_id, false
  end

  def down
    change_column_null :analytics_cycle_analytics_group_stages, :group_value_stream_id, true

    GroupValueStream.where(name: 'default').includes(:group_stages).find_each do |value_stream|
      GroupValueStream.transaction do
        value_stream.group_stages.update_all(group_value_stream_id: nil)
        value_stream.destroy!
      end
    end
  end
end