From cd4ac96617a40fdeb3aae12d042e9e790e8f8067 Mon Sep 17 00:00:00 2001 From: Tuomas Nylund Date: Thu, 26 Oct 2023 16:00:51 +0100 Subject: [PATCH] Cleanup unnecessary fields from topical event as it now uses FeaturedImageData Remove carrierwave_image from topical event as it is no longer a needed column Destroy unnecessary migration script --- ...6145623_remove_topical_event_logo_field.rb | 5 ++ db/schema.rb | 3 +- lib/tasks/migrate_topical_events.rake | 72 ------------------- 3 files changed, 6 insertions(+), 74 deletions(-) create mode 100644 db/migrate/20231026145623_remove_topical_event_logo_field.rb delete mode 100644 lib/tasks/migrate_topical_events.rake diff --git a/db/migrate/20231026145623_remove_topical_event_logo_field.rb b/db/migrate/20231026145623_remove_topical_event_logo_field.rb new file mode 100644 index 00000000000..06449d04253 --- /dev/null +++ b/db/migrate/20231026145623_remove_topical_event_logo_field.rb @@ -0,0 +1,5 @@ +class RemoveTopicalEventLogoField < ActiveRecord::Migration[7.0] + def change + remove_column :topical_events, :carrierwave_image, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e511f8482ad..6b148c5192e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -77,7 +77,7 @@ t.string "email" t.integer "call_for_evidence_response_form_id" t.text "postal_address" - t.index ["call_for_evidence_response_form_id"], name: "index_cons_participations_on_cons_response_form_id" + t.index ["call_for_evidence_response_form_id"], name: "index_cfes_participations_on_cfes_response_form_id" t.index ["edition_id"], name: "index_call_for_evidence_participations_on_edition_id" end @@ -1094,7 +1094,6 @@ t.text "description" t.string "slug" t.string "state" - t.string "carrierwave_image" t.string "logo_alt_text" t.date "start_date" t.date "end_date" diff --git a/lib/tasks/migrate_topical_events.rake b/lib/tasks/migrate_topical_events.rake deleted file mode 100644 index 2faea074a57..00000000000 --- a/lib/tasks/migrate_topical_events.rake +++ /dev/null @@ -1,72 +0,0 @@ -desc "Migrate topical events to use FeaturedImageData with assets" -task :migrate_topical_events, %i[start_id end_id] => :environment do |_, args| - puts "Migrating topical events start!" - start_id = args[:start_id] - end_id = args[:end_id] - - topical_events = TopicalEvent.where(id: start_id..end_id).where("carrierwave_image is not null and featured_image_data_id is null") - puts "Number of topical events found: #{topical_events.count}" - puts "Creating Asset for topical events records from #{start_id} to #{end_id}" - - count = 0 - asset_counter = 0 - assetable_type = FeaturedImageData.to_s - - topical_events.each do |topical_event| - all_variants = topical_event.logo.versions.keys.push(:original) - assetable = FeaturedImageData.create!( - carrierwave_image: topical_event.carrierwave_image, - featured_imageable_type: TopicalEvent.to_s, - featured_imageable_id: topical_event.id, - ) - - all_variants.each do |variant| - path = variant == :original ? topical_event.logo.path : topical_event.logo.versions[variant].path - puts "Creating Asset for path #{path}" - asset_counter += 1 if save_asset(assetable, assetable_type, variant, path) - end - - count += 1 - end - - puts "Created assets for #{count} topical events" - puts "Created asset counter #{asset_counter}" - puts "Migrating topical events finish!" -end - -def save_asset(assetable, assetable_type, variant, path) - asset_info = get_asset_data(path) - save_asset_id_to_assets(assetable.id, assetable_type, Asset.variants[variant], asset_info[:asset_manager_id], asset_info[:filename]) -rescue GdsApi::HTTPNotFound - puts "#{assetable_type} of id##{assetable.id} - could not find asset variant :#{variant} at path #{path}" - - false -end - -def asset_manager - Services.asset_manager -end - -def get_asset_data(legacy_url_path) - asset_info = {} - path = legacy_url_path.sub(/^\//, "") - response_hash = asset_manager.whitehall_asset(path).to_hash - asset_info[:asset_manager_id] = get_asset_id(response_hash) - asset_info[:filename] = get_filename(response_hash) - - asset_info -end - -def get_filename(response) - response["name"] -end - -def get_asset_id(response) - url = response["id"] - url[/\/assets\/(.*)/, 1] -end - -def save_asset_id_to_assets(assetable_id, assetable_type, variant, asset_manager_id, filename) - asset = Asset.new(asset_manager_id:, assetable_type:, assetable_id:, variant:, filename:) - asset.save! -end