|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class MigrateCarrierwaveFilesToActiveStorage < ActiveRecord::Migration[8.0] |
| 4 | + disable_ddl_transaction! |
| 5 | + |
| 6 | + module CodeOcean |
| 7 | + class File < ActiveRecord::Base |
| 8 | + self.table_name = 'files' |
| 9 | + |
| 10 | + # Manually set class name to avoid problems with ActiveStorage's polymorphic association |
| 11 | + def self.name = 'CodeOcean::File' |
| 12 | + |
| 13 | + has_one_attached :attachment |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def up |
| 18 | + say_with_time 'Migrating CarrierWave files to ActiveStorage' do |
| 19 | + migrated = 0 |
| 20 | + skipped_existing = 0 |
| 21 | + missing = 0 |
| 22 | + |
| 23 | + scope = CodeOcean::File.unscoped.where.not(native_file: [nil, '']) |
| 24 | + total = scope.count |
| 25 | + |
| 26 | + scope.find_in_batches(batch_size: 1000).with_index do |batch, i| |
| 27 | + say "Processing batch ##{i + 1} (#{[(i * 1000) + 1, total].min}-#{[(i + 1) * 1000, total].min} of #{total})", true |
| 28 | + batch.each do |record| |
| 29 | + if record.attachment.attached? |
| 30 | + skipped_existing += 1 |
| 31 | + next |
| 32 | + end |
| 33 | + |
| 34 | + filename = record[:native_file] |
| 35 | + next if filename.blank? |
| 36 | + |
| 37 | + old_path = Rails.public_path.join('uploads', 'files', record.id.to_s, filename) |
| 38 | + |
| 39 | + unless ::File.exist?(old_path) |
| 40 | + missing += 1 |
| 41 | + next |
| 42 | + end |
| 43 | + |
| 44 | + begin |
| 45 | + File.open(old_path, 'rb') do |io| |
| 46 | + record.attachment.attach(io:, filename: filename) |
| 47 | + end |
| 48 | + migrated += 1 |
| 49 | + rescue StandardError => e |
| 50 | + warn "Failed to attach file for files.id=#{record.id}: #{e.class}: #{e.message}" |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + say "Done. Migrated: #{migrated}, already attached: #{skipped_existing}, missing source files: #{missing}", true |
| 56 | + |
| 57 | + migrated |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def down |
| 62 | + say 'No rollback for CarrierWave -> ActiveStorage file migration.' |
| 63 | + end |
| 64 | +end |
0 commit comments