Skip to content

Instantly share code, notes, and snippets.

@pecha7x
Created February 15, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pecha7x/db48d83e713d532608721b4831f4d3f0 to your computer and use it in GitHub Desktop.
Save pecha7x/db48d83e713d532608721b4831f4d3f0 to your computer and use it in GitHub Desktop.
t
sql =
<<-SQL
(select person_id from abc_duplicates
LEFT JOIN people ON abc_duplicates.person_id = people.id
where abc_duplicates.created_at > now()-interval'48 hours'
and people.company_id in (select id from companies where group_id=99))
SQL
results = ActiveRecord::Base.connection.exec_query(sql)
ids = results.map{|r| r['person_id']}
incorrect_merged_ids = []
correct_merged_ids = []
need_check_manually = []
Person.where(id: ids).each do |person|
chngs = person.audits.where(action: 'update').where("created_at > now()-interval'48 hours' AND audited_changes ~ 'first_name:|email:|phone_mobile:'")
if chngs.size == 0
correct_merged_ids << person.id
elsif chngs.size > 1
need_check_manually << person.id
else
changes = chngs.last.audited_changes.slice('email', 'phone_mobile', 'first_name', 'last_name')
keys = changes.keys
if keys.include?('last_name')
incorrect_merged_ids << person.id
else
if person.last_name.blank?
need_check_manually << person.id
end
end
end
end;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment