summaryrefslogtreecommitdiff
path: root/db/post_migrate/20191118211629_migrate_ops_feature_flags_scopes_target_user_ids.rb
blob: 47622a216d91fb87196c433e10b205c51185e2b4 (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
# frozen_string_literal: true

class MigrateOpsFeatureFlagsScopesTargetUserIds < ActiveRecord::Migration[5.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class OperationsFeatureFlagScope < ActiveRecord::Base
    include EachBatch
    self.table_name = 'operations_feature_flag_scopes'
    self.inheritance_column = :_type_disabled
  end

  ###
  # 2019-11-26
  #
  # There are about 1000 rows in the operations_feature_flag_scopes table on gitlab.com.
  # This migration will update about 30 of them.
  # https://gitlab.com/gitlab-org/gitlab/merge_requests/20325#note_250742098
  #
  # This should take a few seconds to run.
  # https://gitlab.com/gitlab-org/gitlab/merge_requests/20325#note_254871603
  #
  ###
  def up
    OperationsFeatureFlagScope.where("strategies @> ?", [{ 'name': 'userWithId' }].to_json).each_batch do |scopes|
      scopes.each do |scope|
        if scope.active
          default_strategy = scope.strategies.find { |s| s['name'] == 'default' }

          if default_strategy.present?
            scope.update({ strategies: [default_strategy] })
          end
        else
          user_with_id_strategy = scope.strategies.find { |s| s['name'] == 'userWithId' }

          scope.update({
            active: true,
            strategies: [user_with_id_strategy]
          })
        end
      end
    end
  end

  def down
    # This is not reversible.
    # The old Target Users feature required the same list of user ids to be applied to each environment scope.
    # Now we allow the list of user ids to differ for each scope.
  end
end