diff --git a/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb b/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb index 601c874bc9b..0881244ed49 100644 --- a/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb +++ b/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb @@ -4,44 +4,23 @@ module Gitlab include Gitlab::Database::MigrationHelpers attr_reader :old_folder, :new_folder + class Upload < ActiveRecord::Base + self.table_name = 'uploads' + include EachBatch + end + def perform(old_folder, new_folder) - @old_folder = old_folder - @new_folder = new_folder - replace_sql = replace_sql(uploads[:path], old_folder, new_folder) + affected_uploads = Upload.where(uploads[:path].matches("#{old_folder}%")) - while remaining_rows > 0 - sql = "UPDATE uploads "\ - "SET path = #{replace_sql} "\ - "WHERE uploads.id IN "\ - " (SELECT uploads.id FROM uploads "\ - " WHERE #{affected_uploads.to_sql} LIMIT 1000)" - connection.execute(sql) + affected_uploads.each_batch do |batch| + batch.update_all("path = #{replace_sql}") end end def uploads Arel::Table.new('uploads') end - - def remaining_rows - remaining_result = connection.exec_query("SELECT count(id) FROM uploads WHERE #{affected_uploads.to_sql}") - remaining = remaining_result.first['count'].to_i - logger.info "#{remaining} uploads remaining" - remaining - end - - def affected_uploads - uploads[:path].matches("#{old_folder}%") - end - - def connection - ActiveRecord::Base.connection - end - - def logger - Sidekiq.logger || Rails.logger || Logger.new(STDOUT) - end end end end diff --git a/lib/gitlab/path_regex.rb b/lib/gitlab/path_regex.rb index d81f825ef96..60a32d5d5ea 100644 --- a/lib/gitlab/path_regex.rb +++ b/lib/gitlab/path_regex.rb @@ -49,7 +49,6 @@ module Gitlab sent_notifications services snippets - system teams u unicorn_test diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index 89ea5ceda95..a4090b37f65 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -44,7 +44,7 @@ describe Namespace, models: true do end context "is case insensitive" do - let(:group) { build(:group, path: "System") } + let(:group) { build(:group, path: "Groups") } it { expect(group).not_to be_valid } end