diff options
author | James Lopez <james@jameslopez.es> | 2016-06-20 16:31:03 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-06-20 16:31:03 +0300 |
commit | 896e09d055979cdfe6e20a8b5939c9a263f7e48a (patch) | |
tree | ecd0a544c3cfd6be28959c44463cde534a507e92 /db | |
parent | a5abec905fc69a9999887ea11335f032b4dfa957 (diff) |
started working on a migration for projects that have current import_url issues
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20160620110927_fix_no_validatable_import_url.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/db/migrate/20160620110927_fix_no_validatable_import_url.rb b/db/migrate/20160620110927_fix_no_validatable_import_url.rb new file mode 100644 index 00000000000..e56a8a0c853 --- /dev/null +++ b/db/migrate/20160620110927_fix_no_validatable_import_url.rb @@ -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 |