diff --git a/db/migrate/20220907055044_delete_duplicate_customers.rb b/db/migrate/20220907055044_delete_duplicate_customers.rb new file mode 100644 index 0000000000..2598ab4658 --- /dev/null +++ b/db/migrate/20220907055044_delete_duplicate_customers.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +class DeleteDuplicateCustomers < ActiveRecord::Migration[6.1] + class Customer < ActiveRecord::Base + belongs_to :bill_address, class_name: "SpreeAddress", dependent: :destroy + belongs_to :ship_address, class_name: "SpreeAddress", dependent: :destroy + end + + class SpreeAddress < ActiveRecord::Base + end + + class SpreeOrder < ActiveRecord::Base + end + + class Subscription < ActiveRecord::Base + end + + def up + say "#{grouped_duplicates.keys.count} customers with duplicates." + + grouped_duplicates.map do |key, customers| + chosen = customers.first + others = customers - [chosen] + + say "- #{key}" + + # We can't tell which attributes or associations are the correct ones. + # Selection has been random so far and it's no regression to randomly + # select the attributes of the first customer record. + # + # However, we do need to update references to the customer. + update_references(others, chosen) + + others.each(&:destroy!) + end + end + + def grouped_duplicates + @grouped_duplicates ||= duplicate_records.group_by do |customer| + [customer.email, customer.enterprise_id] + end + end + + def duplicate_records + customer_duplicates = <<~SQL + JOIN customers AS copy + ON customers.id != copy.id + AND customers.email = copy.email + AND customers.enterprise_id = copy.enterprise_id + SQL + + Customer.joins(customer_duplicates) + end + + def update_references(source_customers, destination_customer) + SpreeOrder.where(customer_id: source_customers.map(&:id)). + update_all(customer_id: destination_customer.id) + + Subscription.where(customer_id: source_customers.map(&:id)). + update_all(customer_id: destination_customer.id) + end +end diff --git a/db/schema.rb b/db/schema.rb index 6e3b0a3c87..6c9b1b00fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_07_13_195433) do +ActiveRecord::Schema.define(version: 2022_09_07_055044) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements"