summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/migrate_system_uploads_to_new_folder.rb
blob: ef50fe4adb14683cf3ec3088361de819613b89e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
# rubocop:disable Style/Documentation

module Gitlab
  module BackgroundMigration
    class MigrateSystemUploadsToNewFolder
      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)
        replace_sql = replace_sql(uploads[:path], old_folder, new_folder)
        affected_uploads = Upload.where(uploads[:path].matches("#{old_folder}%"))

        affected_uploads.each_batch do |batch|
          batch.update_all("path = #{replace_sql}")
        end
      end

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