started working on a migration for projects that have current import_url issues

This commit is contained in:
James Lopez 2016-06-20 15:31:03 +02:00
parent a5abec905f
commit 896e09d055
2 changed files with 59 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# AddressableUrlValidator
#
# Custom validator for URLs. This is a
# Custom validator for URLs. This is a
#
# By default, only URLs for http, https, ssh, and git protocols will be considered valid.
# Provide a `:protocols` option to configure accepted protocols.
@ -22,12 +22,6 @@ class AddressableUrlValidator < ActiveModel::EachValidator
end
end
private
def default_options
@default_options ||= { protocols: %w(http https ssh git) }
end
def valid_url?(value)
return false unless value
@ -38,6 +32,12 @@ class AddressableUrlValidator < ActiveModel::EachValidator
false
end
private
def default_options
@default_options ||= { protocols: %w(http https ssh git) }
end
def valid_uri?(value)
Addressable::URI.parse(value).is_a?(Addressable::URI)
end

View File

@ -0,0 +1,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