mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-02 21:51:51 +08:00
Merge pull request #1766 from okriuchykhin/ok_SCI_3459
Improve speed and error handling of TinyMceAssets migration task [SCI-3459]
This commit is contained in:
commit
311f499688
1 changed files with 22 additions and 12 deletions
|
@ -5,19 +5,29 @@ namespace :tinymce_assets do
|
||||||
desc 'Migrate old TinyMCE images to new polymorphic format' \
|
desc 'Migrate old TinyMCE images to new polymorphic format' \
|
||||||
'IT SHOULD BE RUN ONE TIME ONLY'
|
'IT SHOULD BE RUN ONE TIME ONLY'
|
||||||
task migrate_tinymce_assets: :environment do
|
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)
|
ActiveRecord::Base.no_touching do
|
||||||
old_images.each do |old_image|
|
old_images = TinyMceAsset.where('step_id IS NOT NULL OR result_text_id IS NOT NULL')
|
||||||
old_format = /\[~tiny_mce_id:#{old_image.id}\]/
|
.where(object: nil)
|
||||||
new_format = "<img src='' class='img-responsive' data-mce-token='#{Base62.encode(old_image.id)}'/>"
|
.preload(:step, :result_text)
|
||||||
if old_image.step_id
|
old_images.find_each do |old_image|
|
||||||
object = old_image.step
|
ActiveRecord::Base.transaction do
|
||||||
object.description.sub!(old_format, new_format)
|
old_format = /\[~tiny_mce_id:#{old_image.id}\]/
|
||||||
else
|
new_format = "<img src='' class='img-responsive' data-mce-token='#{Base62.encode(old_image.id)}'/>"
|
||||||
object = old_image.result_text
|
if old_image.step_id
|
||||||
object.text.sub!(old_format, new_format)
|
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_id: object.id, object_type: object.class.to_s, step_id: nil, result_text_id: nil)
|
||||||
|
rescue StandardError => ex
|
||||||
|
Rails.logger.error "Failed to update TinyMceAsset id: #{old_image.id}"
|
||||||
|
Rails.logger.error ex.message
|
||||||
|
raise ActiveRecord::Rollback
|
||||||
|
end
|
||||||
end
|
end
|
||||||
object.save
|
|
||||||
old_image.update(object: object, step_id: nil, result_text_id: nil)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue