From 6ac620feb379d723e28e600771360850c2743f48 Mon Sep 17 00:00:00 2001 From: aignatov-bio <47317017+aignatov-bio@users.noreply.github.com> Date: Fri, 19 Apr 2019 15:24:54 +0200 Subject: [PATCH] Migration for old tinymce assets [SCI-3176] (#1663) * adding rake task * Addign test for update images * Adding tests for tiny mce model --- ...2521_add_column_saved_to_tiny_mce_asset.rb | 1 - ...92130_add_polymorphic_to_tiny_mce_asset.rb | 1 - lib/tasks/migrate_old_tinymce_assets.rake | 24 ++++ lib/tasks/tiny_mce_asset.rake | 117 ------------------ spec/factories/result.rb | 3 +- spec/factories/tinymce_assets.rb | 10 ++ spec/models/tiny_mce_asset_spec.rb | 45 ++++++- 7 files changed, 77 insertions(+), 124 deletions(-) create mode 100644 lib/tasks/migrate_old_tinymce_assets.rake delete mode 100644 lib/tasks/tiny_mce_asset.rake create mode 100644 spec/factories/tinymce_assets.rb diff --git a/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb b/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb index 2f206c174..fb150257d 100644 --- a/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb +++ b/db/migrate/20190307102521_add_column_saved_to_tiny_mce_asset.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - class AddColumnSavedToTinyMceAsset < ActiveRecord::Migration[5.1] def change add_column :tiny_mce_assets, :saved, :boolean, default: true diff --git a/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb b/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb index 552702235..ad2acc098 100644 --- a/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb +++ b/db/migrate/20190308092130_add_polymorphic_to_tiny_mce_asset.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - class AddPolymorphicToTinyMceAsset < ActiveRecord::Migration[5.1] def change add_reference :tiny_mce_assets, :object, polymorphic: true diff --git a/lib/tasks/migrate_old_tinymce_assets.rake b/lib/tasks/migrate_old_tinymce_assets.rake new file mode 100644 index 000000000..57731c645 --- /dev/null +++ b/lib/tasks/migrate_old_tinymce_assets.rake @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# rubocop:disable Metrics/LineLength +namespace :tinymce_assets do + desc 'Migrate old TinyMCE images to new polymorphic format' \ + 'IT SHOULD BE RUN ONE TIME ONLY' + task migrate_tinymce_assets: :environment do + old_images = TinyMceAsset.where('step_id IS NOT NULL OR result_text_id IS NOT NULL').where(object: nil) + old_images.each do |old_image| + old_format = /\[~tiny_mce_id:#{old_image.id}\]/ + new_format = "" + if old_image.step_id + object = old_image.step + object.description.sub!(old_format, new_format) + else + object = old_image.result_text + object.text.sub!(old_format, new_format) + end + object.save + old_image.update(object: object, step_id: nil, result_text_id: nil) + end + end +end +# rubocop:enable Metrics/LineLength diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake deleted file mode 100644 index ecd64d88b..000000000 --- a/lib/tasks/tiny_mce_asset.rake +++ /dev/null @@ -1,117 +0,0 @@ -namespace :tiny_mce_asset do - REGEX = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ - desc 'Remove obsolete images that were created on new steps or '\ - 'results and the step/result didn\'t get saved.' - task remove_obsolete_images: :environment do - TinyMceAsset.where('step_id IS ? AND ' \ - 'result_text_id IS ? AND created_at < ?', - nil, nil, 7.days.ago).destroy_all - end - - desc 'Generate new tiny_mce_assets and replace old assets in RTE for ' \ - 'steps. Assign the last printed id if the script crashes or ' \ - 'id + 1 if there is a problematic asset' - task :regenerate_step_images, [:last_id] => :environment do |_, args| - replaced_images = 0 - failed_attempts = 0 - all_images = TinyMceAsset.where.not(step: nil).count - failed_attempts_ids = [] - puts 'Start processing steps...' - params = { batch_size: 100 } - if args.present? && args[:last_id].present? - # fetch all steps and sort them asc - params[:start] = args[:last_id].to_i - end - Step.find_each(params) do |step| - next unless step.description && step.description.match(REGEX) - team = step.protocol.team - puts "******************************* \n\n\n\n" - puts "Processing step id => [#{step.id}] \n\n\n\n" - puts '*******************************' - step.description.gsub!(REGEX) do |el| - match = el.match(REGEX) - old_img = TinyMceAsset.find_by_id(match[1]) - # skip other processing and deletes tiny_mce tag - # if image is not in database - next unless old_img - new_img = TinyMceAsset.create(image: old_img.image, - team: team, - reference: step) - if new_img - # This image will be removed by `remove_obsolete_images` rake task - # until all the steps are not updated we still need this image - # in case it appears on some other step - old_img.update_attributes(result_text_id: nil, step_id: nil) - replaced_images += 1 - "[~tiny_mce_id:#{new_img.id}]" - else - failed_attempts += 1 - failed_attempts_ids << old_img.id - "[~tiny_mce_id:#{old_img.id}]" # return the old img - end - end - step.save - end - puts 'Completed processing steps...' - - puts '----------- TASK REPORT -----------------' - puts "All images: #{all_images}" - puts "Recreated images: #{replaced_images}" - puts "Failed attempts: #{failed_attempts}" - puts "TinyMceAsset ids of failed attempts: #{failed_attempts_ids}" - puts '-----------------------------------------' - end - - desc 'Generate new tiny_mce_assets and replace old assets in RTE ' \ - 'for results. Assign the last printed id if the script crashes or ' \ - 'id + 1 if there is a problematic asset' - task :regenerate_results_images, [:last_id] => :environment do |_, args| - replaced_images = 0 - failed_attempts = 0 - all_images = TinyMceAsset.where.not(result_text: nil).count - failed_attempts_ids = [] - params = { batch_size: 100 } - if args.present? && args[:last_id].present? - params[:start] = args[:last_id].to_i - end - - puts 'Start processing result_texts...' - ResultText.find_each(params) do |result_text| - next unless result_text.text && result_text.text.match(REGEX) - team = result_text.result.my_module.protocol.team - puts "******************************************* \n\n\n\n" - puts "Processing result_text id => [#{result_text.id}] \n\n\n\n" - puts '*******************************************' - result_text.text.gsub!(REGEX) do |el| - match = el.match(REGEX) - old_img = TinyMceAsset.find_by_id(match[1]) - # skip other processing and deletes tiny_mce tag - # if image is not in database - next unless old_img - new_img = TinyMceAsset.create(image: old_img.image, - team: team, - reference: result_text) - if new_img - # This image will be removed by `remove_obsolete_images` rake task - # until all the steps are not updated we still need this image - # in case it appears on some other step - old_img.update_attributes(result_text_id: nil, step_id: nil) - replaced_images += 1 - "[~tiny_mce_id:#{new_img.id}]" - else - failed_attempts += 1 - failed_attempts_ids << old_img.id - "[~tiny_mce_id:#{old_img.id}]" # return the old img - end - end - result_text.save - end - puts 'Completed processing result_texts...' - puts '----------- TASK REPORT -----------------' - puts "All images: #{all_images}" - puts "Recreated images: #{replaced_images}" - puts "Failed attempts: #{failed_attempts}" - puts "TinyMceAsset ids of failed attempts: #{failed_attempts_ids}" - puts '-----------------------------------------' - end -end diff --git a/spec/factories/result.rb b/spec/factories/result.rb index c7ae2d613..c0a52e451 100644 --- a/spec/factories/result.rb +++ b/spec/factories/result.rb @@ -3,6 +3,7 @@ FactoryBot.define do factory :result do name { Faker::Name.unique.name } - my_module { create(:my_module) } + user + my_module end end diff --git a/spec/factories/tinymce_assets.rb b/spec/factories/tinymce_assets.rb new file mode 100644 index 000000000..fd4ea64de --- /dev/null +++ b/spec/factories/tinymce_assets.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :tiny_mce_asset do + association :team, factory: :team + image_file_name 'sample_file.jpg' + image_content_type 'image/jpeg' + image_file_size 69 + end +end diff --git a/spec/models/tiny_mce_asset_spec.rb b/spec/models/tiny_mce_asset_spec.rb index c97b1f8cd..b8a555d2c 100644 --- a/spec/models/tiny_mce_asset_spec.rb +++ b/spec/models/tiny_mce_asset_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' describe TinyMceAsset, type: :model do @@ -11,20 +13,55 @@ describe TinyMceAsset, type: :model do it { should have_db_column :image_file_size } it { should have_db_column :image_updated_at } it { should have_db_column :estimated_size } - it { should have_db_column :step_id } + it { should have_db_column :object_id } + it { should have_db_column :object_type } it { should have_db_column :team_id } - it { should have_db_column :result_text_id } it { should have_db_column :created_at } it { should have_db_column :updated_at } end describe 'Relations' do it { should belong_to :team } - it { should belong_to :step } - it { should belong_to :result_text } + it { should belong_to :object } end describe 'Should be a valid object' do it { should validate_presence_of :estimated_size } end + + describe 'Methods' do + let(:result_text) do + create :result_text, + text: '' + end + let(:image) { create :tiny_mce_asset, id: 1 } + + describe '#update_images' do + it 'save new image' do + new_image = image + new_result_text = result_text + TinyMceAsset.update_images(new_result_text, [Base62.encode(new_image.id)].to_s) + updated_image = TinyMceAsset.find(new_image.id) + expect(updated_image.object_type).to eq 'ResultText' + expect(ResultText.find(new_result_text.id).text).not_to include 'fake-path' + end + end + + describe '#generate_url' do + it 'create new url' do + image + expect(TinyMceAsset.generate_url(result_text.text)).to include 'sample_file.jpg' + end + end + + describe '#reload_images' do + it 'change image token in description' do + new_result_text = result_text + TinyMceAsset.update_images(new_result_text, [Base62.encode(image.id)].to_s) + create :tiny_mce_asset, id: 2, object: new_result_text + TinyMceAsset.reload_images([[1, 2]]) + expect(ResultText.find(new_result_text.id).text).to include 'data-mce-token="2"' + end + end + end end