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

# This migration generates missing routes for any projects and namespaces that
# don't already have a route.
#
# On GitLab.com this would insert 611 project routes, and 0 namespace routes.
# The exact number could vary per instance, so we take care of both just in
# case.
10
class GenerateMissingRoutes < ActiveRecord::Migration[4.2]
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class User < ActiveRecord::Base
    self.table_name = 'users'
  end

  class Route < ActiveRecord::Base
    self.table_name = 'routes'
  end

  module Routable
    def build_full_path
      if parent && path
        parent.build_full_path + '/' + path
      else
        path
      end
    end

    def build_full_name
      if parent && name
        parent.human_name + ' / ' + name
      else
        name
      end
    end

    def human_name
      build_full_name
    end

    def attributes_for_insert
      time = Time.zone.now

      {
        # We can't use "self.class.name" here as that would include the
        # migration namespace.
        source_type: source_type_for_route,
        source_id: id,
        created_at: time,
        updated_at: time,
        name: build_full_name,

        # The route path might already be taken. Instead of trying to generate a
        # new unique name on every conflict, we just append the row ID to the
        # route path.
        path: "#{build_full_path}-#{id}"
      }
    end
  end

  class Project < ActiveRecord::Base
    self.table_name = 'projects'

    include EachBatch
    include GenerateMissingRoutes::Routable

    belongs_to :namespace, class_name: 'GenerateMissingRoutes::Namespace'

    has_one :route,
      as: :source,
      inverse_of: :source,
      class_name: 'GenerateMissingRoutes::Route'

    alias_method :parent, :namespace
    alias_attribute :parent_id, :namespace_id

    def self.without_routes
      where(
        'NOT EXISTS (
          SELECT 1
          FROM routes
          WHERE source_type = ?
          AND source_id = projects.id
        )',
        'Project'
      )
    end

    def source_type_for_route
      'Project'
    end
  end

  class Namespace < ActiveRecord::Base
    self.table_name = 'namespaces'

    include EachBatch
    include GenerateMissingRoutes::Routable

    belongs_to :parent, class_name: 'GenerateMissingRoutes::Namespace'
    belongs_to :owner, class_name: 'GenerateMissingRoutes::User'

    has_one :route,
      as: :source,
      inverse_of: :source,
      class_name: 'GenerateMissingRoutes::Route'

    def self.without_routes
      where(
        'NOT EXISTS (
          SELECT 1
          FROM routes
          WHERE source_type = ?
          AND source_id = namespaces.id
        )',
        'Namespace'
      )
    end

    def source_type_for_route
      'Namespace'
    end
  end

  def up
    [Namespace, Project].each do |model|
      model.without_routes.each_batch(of: 100) do |batch|
        rows = batch.map(&:attributes_for_insert)

        Gitlab::Database.bulk_insert(:routes, rows)
      end
    end
  end

  def down
    # Removing routes we previously generated makes no sense.
  end
end