Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- How many Call/Lead relationships there should be, based on calls.parent_id
- select count(*) from calls
- join leads on calls.parent_id = leads.id and leads.deleted = 0
- where calls.parent_type = 'Leads' and calls.deleted = 0;
- -- How many there actually are, based on calls_leads
- select count(*) from calls
- join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
- join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
- where calls.deleted = 0 and calls.parent_type = 'Leads';
- -- difference is 1406
- -- isolate the bad ones
- select count(*) from calls
- join leads on calls.parent_id = leads.id and leads.deleted = 0
- where calls.parent_type = 'Leads' and calls.deleted = 0
- and calls.id not in (
- select calls.id from calls
- join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
- join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
- where calls.deleted = 0 and calls.parent_type = 'Leads'
- );
- -- 1499 found. Differences is probably in records that are associated to
- -- the parent_type of Contact or Lead, but also the Lead is invited. Don't
- -- worry about those. Just match up as best we can.
- insert into calls_leads (id,call_id,lead_id,date_modified,deleted)
- select calls.id,calls.id,leads.id,NOW(),0 from calls
- join leads on calls.parent_id = leads.id and leads.deleted = 0
- where calls.parent_type = 'Leads' and calls.deleted = 0
- and calls.id not in (
- select calls.id from calls
- join calls_leads on calls.id=calls_leads.call_id and calls_leads.deleted=0
- join leads on calls_leads.lead_id = leads.id and leads.deleted = 0
- where calls.deleted = 0 and calls.parent_type = 'Leads'
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement