Advertisement
matthewpoer

Fixing calls_leads to match calls.parent_id

Apr 26th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.67 KB | None | 0 0
  1. -- How many Call/Lead relationships there should be, based on calls.parent_id
  2. select count(*) from calls
  3. join leads on calls.parent_id = leads.id and leads.deleted = 0
  4. where calls.parent_type = 'Leads' and calls.deleted = 0;
  5.  
  6. -- How many there actually are, based on calls_leads
  7. select count(*) from calls
  8. join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
  9. join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
  10. where calls.deleted = 0 and calls.parent_type = 'Leads';
  11.  
  12. -- difference is 1406
  13.  
  14. -- isolate the bad ones
  15. select count(*) from calls
  16. join leads on calls.parent_id = leads.id and leads.deleted = 0
  17. where calls.parent_type = 'Leads' and calls.deleted = 0
  18. and calls.id not in (
  19.     select calls.id from calls
  20.     join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
  21.     join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
  22.     where calls.deleted = 0 and calls.parent_type = 'Leads'
  23. );
  24.  
  25. -- 1499 found. Differences is probably in records that are associated to
  26. -- the parent_type of Contact or Lead, but also the Lead is invited. Don't
  27. -- worry about those. Just match up as best we can.
  28. insert into calls_leads (id,call_id,lead_id,date_modified,deleted)
  29. select calls.id,calls.id,leads.id,NOW(),0 from calls
  30. join leads on calls.parent_id = leads.id and leads.deleted = 0
  31. where calls.parent_type = 'Leads' and calls.deleted = 0
  32. and calls.id not in (
  33.     select calls.id from calls
  34.     join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
  35.     join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
  36.     where calls.deleted = 0 and calls.parent_type = 'Leads'
  37. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement