# frozen_string_literal: true

class SyncTablesDefaultCharsets < ActiveRecord::Migration[6.0]
  def up
    table_changes = {
      "affiliate_credits" => "CHARACTER SET latin1",
      "affiliate_partial_refunds" => "CHARACTER SET latin1",
      "affiliates" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "affiliates_links" => "CHARACTER SET latin1",
      "asset_previews" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "balance_transactions" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "balances" => "CHARACTER SET latin1",
      "bank_accounts" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "banks" => "CHARACTER SET latin1",
      "base_variants" => "CHARACTER SET utf8mb4",
      "base_variants_product_files" => "CHARACTER SET latin1",
      "base_variants_purchases" => "CHARACTER SET latin1",
      "bins" => "CHARACTER SET latin1",
      "blocked_ips" => "CHARACTER SET latin1",
      "categories" => "CHARACTER SET latin1",
      "comments" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "computed_sales_analytics_days" => "CHARACTER SET latin1",
      "consumption_events" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "credit_cards" => "CHARACTER SET latin1",
      "credits" => "CHARACTER SET latin1",
      "custom_domains" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "delayed_emails" => "CHARACTER SET latin1",
      "devices" => "CHARACTER SET latin1",
      "disputes" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "dropbox_files" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "dynamic_product_page_switch_assignments" => "CHARACTER SET latin1",
      "dynamic_product_page_switches" => "CHARACTER SET latin1",
      "email_infos" => "CHARACTER SET utf8",
      "event_test_path_assignments" => "CHARACTER SET latin1",
      "events" => "CHARACTER SET latin1",
      "failed_purchases" => "CHARACTER SET latin1",
      "followers" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "friendly_id_slugs" => "CHARACTER SET latin1",
      "gifts" => "CHARACTER SET utf8mb4",
      "import_jobs" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "imported_customers" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "installment_events" => "CHARACTER SET latin1",
      "installment_rules" => "CHARACTER SET latin1",
      "installments" => "CHARACTER SET utf8mb4",
      "invites" => "CHARACTER SET utf8mb4",
      "large_sellers" => "CHARACTER SET latin1",
      "licenses" => "CHARACTER SET utf8",
      "links" => "CHARACTER SET utf8mb4",
      "megaphone_states" => "CHARACTER SET latin1",
      "merchant_accounts" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "messages" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "oauth_access_grants" => "CHARACTER SET latin1",
      "oauth_access_tokens" => "CHARACTER SET latin1",
      "oauth_applications" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "offer_codes" => "CHARACTER SET utf8mb4",
      "parents_children" => "CHARACTER SET latin1",
      "payment_options" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "payments" => "CHARACTER SET latin1",
      "payments_balances" => "CHARACTER SET latin1",
      "preorder_links" => "CHARACTER SET latin1",
      "preorders" => "CHARACTER SET latin1",
      "prices" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "processed_audios" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "product_categorizations" => "CHARACTER SET latin1",
      "product_files" => "CHARACTER SET utf8mb4",
      "product_files_archives" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "product_files_files_archives" => "CHARACTER SET latin1",
      "product_review_stats" => "CHARACTER SET latin1",
      "product_reviews" => "CHARACTER SET latin1",
      "product_taggings" => "CHARACTER SET latin1",
      "profile_tags" => "CHARACTER SET latin1",
      "purchase_sales_tax_infos" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "purchases" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "recommended_purchase_infos" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "recurring_services" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "refunds" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "resource_subscriptions" => "CHARACTER SET latin1",
      "sent_email_infos" => "CHARACTER SET latin1",
      "service_charges" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "shipments" => "CHARACTER SET utf8mb4",
      "shipping_destinations" => "CHARACTER SET utf8mb4",
      "skus_variants" => "CHARACTER SET latin1",
      "stamped_pdfs" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "subscriptions" => "CHARACTER SET latin1",
      "subtitle_files" => "CHARACTER SET utf8mb4",
      "tags" => "CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",
      "third_party_analytics" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "tos_agreements" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "transcoded_videos" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "url_redirects" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "user_categorizations" => "CHARACTER SET latin1",
      "user_compliance_info" => "CHARACTER SET utf8",
      "user_compliance_info_requests" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "users" => "CHARACTER SET utf8mb4",
      "variant_categories" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci",
      "versions" => "CHARACTER SET latin1",
      "workflows" => "CHARACTER SET utf8mb4",
      "zip_tax_rates" => "CHARACTER SET utf8 COLLATE utf8_unicode_ci"
    }

    safety_assured do
      table_changes.each do |target_table_name, change_snippet|
        exec_query "ALTER TABLE #{target_table_name} CONVERT TO #{change_snippet}"
      end

      exec_query "ALTER TABLE product_files_archives CHANGE url url varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT ''"
      exec_query "ALTER TABLE transcoded_videos CHANGE original_video_key original_video_key varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT ''"
    end
  end
end
