Last active
August 5, 2022 15:58
-
-
Save jinjagit/4cfebd037f367d3cd1c144be674793e5 to your computer and use it in GitHub Desktop.
Task to update text_images (with dry_run as default)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
namespace :fix_existing_tenants do | |
desc 'Update text_images if referenced in HomePage.bottom_info_section_multiloc' | |
# Usage: | |
# Dry run (no changes): rake fix_existing_tenants:update_home_page_text_images | |
# Execute (changes): rake fix_existing_tenants:update_home_page_text_images['execute'] | |
task :update_home_page_text_images, [:execute] => [:environment] do |_t, args| | |
args.with_defaults(execute: false) | |
dry_run = true unless args[:execute] == 'execute' | |
n = Tenant.all.count | |
successful = 0 | |
failed = 0 | |
errors = [] | |
puts "\nProcessing #{n} tenants..." | |
Tenant.all.each_with_index do |tenant, i| | |
puts "#{i + 1}). Processing tenant #{tenant.host}..." | |
Apartment::Tenant.switch(tenant.schema_name) do | |
info = HomePage.first.bottom_info_section_multiloc | |
image_refs = [] | |
# Add any text_image refs in the HomePage.first.bottom_info_section_multiloc to refs array | |
if info.present? | |
info.each do |_key, value| # check every value as could have different images for different locales | |
split = value.gsub('"', '').split("image-text-reference=") | |
if split.count > 1 | |
split.each_with_index do |s, i| # Make sure we get all references, not just the first in a value | |
image_refs << s[0, 36] if i > 0 | |
end | |
end | |
end | |
end | |
image_refs.uniq! | |
text_images = [] | |
# Find text_image records for each ref | |
image_refs.each do |ref| | |
text_images << TextImage.find_by(text_reference: ref) | |
end | |
text_images.compact! | |
if dry_run | |
if image_refs.count == text_images.count | |
if image_refs.count > 0 | |
puts "✅ *********************" | |
successful += 1 | |
else | |
puts "✅" | |
end | |
else | |
puts "❌" | |
failed += 1 | |
end | |
else | |
failures = 0 | |
text_images.each do |text_image| | |
begin | |
text_image.imageable_type = 'HomePage' | |
text_image.imageable_id = HomePage.first.id | |
text_image.save! | |
puts "Updated: #{tenant.host} text_image #{text_image.id} with imageable_type: #{text_image.imageable_type} and imageable_id: #{HomePage.first.id}" | |
rescue StandardError => e | |
failures += 1 | |
puts " Error! could not save TextImage record - #{e}." | |
errors << { host: host, message: "could not save TextImage record - #{e}." } | |
end | |
end | |
if failures > 0 | |
puts "❌" | |
failed += 1 | |
else | |
print "✅ " | |
print " updated #{text_images.count} text_images for tenant #{tenant.host}" if text_images.count > 0 | |
print "\n" | |
successful += 1 if text_images.count > 0 | |
end | |
end | |
end | |
end | |
puts "Total: #{n} tenants, #{successful} successful updates (or matching of refs if dry_run), #{failed} failed" | |
if errors.count > 0 | |
puts "Errors:" | |
errors.each do |error| | |
puts " #{error[:host]}: #{error[:message]}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
screenshot of end of dry_run on Europe cluster (no failures found in whole log):