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

20160620110927_fix_no_validatable_import_url.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e56a8a0c8530918895931155dd289282d2e4eae2 (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
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class FixNoValidatableImportUrl < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers
  class SqlBatches

    attr_reader :results, :query

    def initialize(batch_size: 100, query:)
      @offset = 0
      @batch_size = batch_size
      @query = query
      @results = []
    end

    def next
      @results = ActiveRecord::Base.connection.execute(batched_sql)
      @offset += @batch_size
      @results.any?
    end

    private

    def batched_sql
      "#{@query} OFFSET #{@offset} LIMIT #{@batch_size}"
    end
  end

  def up
    invalid_import_url_project_ids.each { |project_id| cleanup_import_url(project_id) }
  end

  def invalid_import_url_project_ids
    ids = []
    batches = SqlBatches.new(query: "SELECT id, import_url FROM projects WHERE import_url IS NOT NULL")

    while batches.nexts
      ids += batches.results.map { |result| invalid_url?(result[:import_url]) ? result[:id] : nil }
    end

    ids.compact
  end

  def invalid_url?(url)
    AddressableUrlValidator.new({ attributes: 1 }).valid_url?(url)
  end

  def cleanup_import_url(project_id)
    execute("UPDATE projects SET mirror = false, import_url = NULL WHERE id = #{project_id}")
  end
end