mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-12-10 06:06:24 +08:00
Migration for old tinymce assets [SCI-3176] (#1663)
* adding rake task * Addign test for update images * Adding tests for tiny mce model
This commit is contained in:
parent
92d8e42a92
commit
6ac620feb3
7 changed files with 77 additions and 124 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class AddColumnSavedToTinyMceAsset < ActiveRecord::Migration[5.1]
|
class AddColumnSavedToTinyMceAsset < ActiveRecord::Migration[5.1]
|
||||||
def change
|
def change
|
||||||
add_column :tiny_mce_assets, :saved, :boolean, default: true
|
add_column :tiny_mce_assets, :saved, :boolean, default: true
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class AddPolymorphicToTinyMceAsset < ActiveRecord::Migration[5.1]
|
class AddPolymorphicToTinyMceAsset < ActiveRecord::Migration[5.1]
|
||||||
def change
|
def change
|
||||||
add_reference :tiny_mce_assets, :object, polymorphic: true
|
add_reference :tiny_mce_assets, :object, polymorphic: true
|
||||||
|
|
|
||||||
24
lib/tasks/migrate_old_tinymce_assets.rake
Normal file
24
lib/tasks/migrate_old_tinymce_assets.rake
Normal file
|
|
@ -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 = "<img src='' class='img-responsive' data-mce-token='#{Base62.encode(old_image.id)}'/>"
|
||||||
|
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
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :result do
|
factory :result do
|
||||||
name { Faker::Name.unique.name }
|
name { Faker::Name.unique.name }
|
||||||
my_module { create(:my_module) }
|
user
|
||||||
|
my_module
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
10
spec/factories/tinymce_assets.rb
Normal file
10
spec/factories/tinymce_assets.rb
Normal file
|
|
@ -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
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe TinyMceAsset, type: :model do
|
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_file_size }
|
||||||
it { should have_db_column :image_updated_at }
|
it { should have_db_column :image_updated_at }
|
||||||
it { should have_db_column :estimated_size }
|
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 :team_id }
|
||||||
it { should have_db_column :result_text_id }
|
|
||||||
it { should have_db_column :created_at }
|
it { should have_db_column :created_at }
|
||||||
it { should have_db_column :updated_at }
|
it { should have_db_column :updated_at }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Relations' do
|
describe 'Relations' do
|
||||||
it { should belong_to :team }
|
it { should belong_to :team }
|
||||||
it { should belong_to :step }
|
it { should belong_to :object }
|
||||||
it { should belong_to :result_text }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Should be a valid object' do
|
describe 'Should be a valid object' do
|
||||||
it { should validate_presence_of :estimated_size }
|
it { should validate_presence_of :estimated_size }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'Methods' do
|
||||||
|
let(:result_text) do
|
||||||
|
create :result_text,
|
||||||
|
text: '<img data-mce-token=1 src="fake-path"/>'
|
||||||
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue