BigW Consortium Gitlab

migrate_system_uploads_to_new_folder.rb 799 Bytes
Newer Older
1 2 3 4
# frozen_string_literal: true
# rubocop:disable Metrics/LineLength
# rubocop:disable Style/Documentation

5 6 7 8 9 10
module Gitlab
  module BackgroundMigration
    class MigrateSystemUploadsToNewFolder
      include Gitlab::Database::MigrationHelpers
      attr_reader :old_folder, :new_folder

11 12 13 14
      class Upload < ActiveRecord::Base
        self.table_name = 'uploads'
        include EachBatch
      end
15

16
      def perform(old_folder, new_folder)
17
        replace_sql = replace_sql(uploads[:path], old_folder, new_folder)
18
        affected_uploads = Upload.where(uploads[:path].matches("#{old_folder}%"))
19

20 21
        affected_uploads.each_batch do |batch|
          batch.update_all("path = #{replace_sql}")
22 23 24 25 26 27 28 29 30
        end
      end

      def uploads
        Arel::Table.new('uploads')
      end
    end
  end
end