20170503140202_turn_nested_groups_into_regular_groups_for_mysql.rb 3.78 KB
Newer Older
1 2 3 4 5 6 7
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

# This migration depends on code external to it. For example, it relies on
# updating a namespace to also rename directories (uploads, GitLab pages, etc).
# The alternative is to copy hundreds of lines of code into this migration,
# adjust them where needed, etc; something which doesn't work well at all.
8
class TurnNestedGroupsIntoRegularGroupsForMysql < ActiveRecord::Migration[4.2]
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  include Gitlab::Database::MigrationHelpers

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

  def run_migration?
    Gitlab::Database.mysql?
  end

  def up
    return unless run_migration?

    # For all sub-groups we need to give the right people access. We do this as
    # follows:
    #
    # 1. Get all the ancestors for the current namespace
    # 2. Get all the members of these namespaces, along with their higher access
    #    level
    # 3. Give these members access to the current namespace
    Namespace.unscoped.where('parent_id IS NOT NULL').find_each do |namespace|
      rows = []
      existing = namespace.members.pluck(:user_id)

      all_members_for(namespace).each do |member|
        next if existing.include?(member[:user_id])

        rows << {
          access_level: member[:access_level],
          source_id: namespace.id,
          source_type: 'Namespace',
          user_id: member[:user_id],
          notification_level: 3, # global
          type: 'GroupMember',
          created_at: Time.current,
          updated_at: Time.current
        }
      end

      bulk_insert_members(rows)

      # This method relies on the parent to determine the proper path.
      # Because we reset "parent_id" this method will not return the right path
      # when moving namespaces.
      full_path_was = namespace.send(:full_path_was)

      namespace.define_singleton_method(:full_path_was) { full_path_was }

      namespace.update!(parent_id: nil, path: new_path_for(namespace))
    end
  end

  def down
    # There is no way to go back from regular groups to nested groups.
  end

  # Generates a new (unique) path for a namespace.
  def new_path_for(namespace)
    counter = 1
    base = namespace.full_path.tr('/', '-')
    new_path = base

    while Namespace.unscoped.where(path: new_path).exists?
      new_path = base + "-#{counter}"
      counter += 1
    end

    new_path
  end

  # Returns an Array containing all the ancestors of the current namespace.
  #
  # This method is not particularly efficient, but it's probably still faster
  # than using the "routes" table. Most importantly of all, it _only_ depends
  # on the namespaces table and the "parent_id" column.
  def ancestors_for(namespace)
    ancestors = []
    current = namespace

    while current&.parent_id
      # We're using find_by(id: ...) here to deal with cases where the
      # parent_id may point to a missing row.
90 91
      current = Namespace.unscoped.select([:id, :parent_id])
        .find_by(id: current.parent_id)
92 93 94 95 96 97 98 99 100 101

      ancestors << current.id if current
    end

    ancestors
  end

  # Returns a relation containing all the members that have access to any of
  # the current namespace's parent namespaces.
  def all_members_for(namespace)
102 103 104 105 106
    Member
      .unscoped
      .select(['user_id', 'MAX(access_level) AS access_level'])
      .where(source_type: 'Namespace', source_id: ancestors_for(namespace))
      .group(:user_id)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  end

  def bulk_insert_members(rows)
    return if rows.empty?

    keys = rows.first.keys

    tuples = rows.map do |row|
      row.map { |(_, value)| connection.quote(value) }
    end

    execute <<-EOF.strip_heredoc
    INSERT INTO members (#{keys.join(', ')})
    VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
    EOF
  end
end