add creation of ActiveStorage tables

This commit is contained in:
Eric 2020-04-30 15:43:19 -05:00
parent c92c172f2d
commit cdd28b3dac
2 changed files with 40 additions and 17 deletions

View file

@ -2,6 +2,10 @@
class CwToActivestorageGenerator < Rails::Generators::Base class CwToActivestorageGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__) source_root File.expand_path('templates', __dir__)
def install_activestorage_tables
rake 'active_storage:install'
end
def copy_uploaders def copy_uploaders
if (!File.exists?('app/uploaders')) if (!File.exists?('app/uploaders'))
directory 'uploaders', 'app/uploaders' directory 'uploaders', 'app/uploaders'

View file

@ -1,30 +1,49 @@
# frozen_string_literal: true # frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
namespace :houdini do namespace :houdini do
namespace :migration do namespace :migration do
UPLOADERS_TO_MIGRATE = {
'Nonprofit': [
:main_image,
:second_image,
:third_image,
:background_image,
:logo
],
'Campaign': [
:main_image,
:background_image,
:banner_image
],
'Event': [
:main_image,
:background_image
],
'ImageAttachment': [
:file
],
'Profile':[
:picture
]
}
desc "Migrate your CarrierWave uploads to activestorage" desc "Migrate your CarrierWave uploads to activestorage"
task :cw_to_activestorage, [:simulate, :write_out_to_files] => [:environment] do |t, args| task :cw_to_activestorage, [:old_carrierwave_root, :simulate, :write_out_to_files] => [:environment] do |t, args|
results = [] results = []
Rails.application.eager_load! Rails.application.eager_load!
# find activerecord descendents # find activerecord descendents
classes_with_uploaders = ActiveRecord::Base.descendants.select do |cls| UPLOADERS_TO_MIGRATE.each do |k,attributes|
cls.uploaders&.any? klass = Object.get_const(k)
end klass.where(attributes.map{|i| i.to_s + "IS NOT NULL"}.join(" OR "))
classes_with_uploaders.each do |cls| .find_each do |record|
cls.find_each do |i| attributes.each do |attrib|
cls.uploaders.keys.each do |uploader_name| cw_path = record.read_attribute_before_type_cast(attrib.to_s)
upload_url = i.send("#{uploader_name.to_s}_url")
result = process({upload_url: upload_url,
uploader_name: uploader_name,
record: i,
})
if (result)
result[:value] = [cls.name].concat(result[:value])
results << result
end
end end
end end
end end
copied = results.select{|i| i[:success]}.map{|i| i[:value]} copied = results.select{|i| i[:success]}.map{|i| i[:value]}