20160302152808_remove_wrong_import_url_from_projects.rb 1.34 KB
Newer Older
James Lopez's avatar
James Lopez committed
1
class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
2

3
4
5
6
  class FakeProjectImportData
    extend AttrEncrypted
    attr_accessor :credentials
    attr_encrypted :credentials, key: Gitlab::Application.secrets.db_key_base, marshal: true, encode: true
7
8
  end

James Lopez's avatar
James Lopez committed
9
  def up
10
11
12
13
14
15
16
17
18
19
    projects_with_wrong_import_url.find_in_batches do |project_batch|
      project_batch.each do |project|
        sanitizer = Gitlab::ImportUrlSanitizer.new(project["import_url"])

        ActiveRecord::Base.transaction do
          execute("UPDATE projects SET import_url = '#{quote(sanitizer.sanitized_url)}' WHERE id = #{project['id']}")
          fake_import_data = FakeProjectImportData.new
          fake_import_data.credentials = sanitizer.credentials
          execute("UPDATE project_import_data SET encrypted_credentials = '#{quote(fake_import_data.encrypted_credentials)}' WHERE project_id = #{project['id']}")
        end
20
      end
James Lopez's avatar
James Lopez committed
21
22
23
    end
  end

24
25
26
27
  def down

  end

28
  def projects_with_wrong_import_url
James Lopez's avatar
James Lopez committed
29
    # TODO Check live with #operations for possible false positives. Also, consider regex? But may have issues MySQL/PSQL
30
31
32
33
34
    select_all("SELECT p.id, p.import_url FROM projects p WHERE p.import_url IS NOT NULL AND (p.import_url LIKE '%//%:%@%' OR p.import_url LIKE '#{"_"*40}@github.com%')")
  end

  def quote(value)
    ActiveRecord::Base.connection.quote(value)
James Lopez's avatar
James Lopez committed
35
36
  end
end