Skip to content

Commit f688624

Browse files
committed
add migration of files from carrierwave to activestorage
1 parent ccf63fe commit f688624

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class RemoveNativeFileFromFiles < ActiveRecord::Migration[8.0]
4+
def up
5+
if column_exists?(:files, :native_file)
6+
remove_column :files, :native_file, :string
7+
end
8+
end
9+
10+
def down
11+
unless column_exists?(:files, :native_file)
12+
add_column :files, :native_file, :string
13+
end
14+
end
15+
end

db/schema.rb

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)