From cdd28b3daca7f1ff2e751e93ce649f34600338af Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 30 Apr 2020 15:43:19 -0500 Subject: [PATCH] add creation of ActiveStorage tables --- .../cw_to_activestorage_generator.rb | 4 ++ lib/tasks/migration.rake | 53 +++++++++++++------ 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb b/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb index 64b0084d..86a2de10 100644 --- a/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb +++ b/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb @@ -2,6 +2,10 @@ class CwToActivestorageGenerator < Rails::Generators::Base source_root File.expand_path('templates', __dir__) + def install_activestorage_tables + rake 'active_storage:install' + end + def copy_uploaders if (!File.exists?('app/uploaders')) directory 'uploaders', 'app/uploaders' diff --git a/lib/tasks/migration.rake b/lib/tasks/migration.rake index defd8c39..a99a4cb4 100644 --- a/lib/tasks/migration.rake +++ b/lib/tasks/migration.rake @@ -1,30 +1,49 @@ # frozen_string_literal: true # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later + + + namespace :houdini 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" - 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 = [] Rails.application.eager_load! # find activerecord descendents - classes_with_uploaders = ActiveRecord::Base.descendants.select do |cls| - cls.uploaders&.any? - end - classes_with_uploaders.each do |cls| - cls.find_each do |i| - cls.uploaders.keys.each do |uploader_name| - 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 + UPLOADERS_TO_MIGRATE.each do |k,attributes| + klass = Object.get_const(k) + klass.where(attributes.map{|i| i.to_s + "IS NOT NULL"}.join(" OR ")) + .find_each do |record| + attributes.each do |attrib| + cw_path = record.read_attribute_before_type_cast(attrib.to_s) end - end + end end copied = results.select{|i| i[:success]}.map{|i| i[:value]}