UPDATE: CRM-18526 fixes this issue - it's just been checked in. To bypass this problem, upgrade to CiviCRM 4.7.8 or higher.
John is correct about this being related to CRM-18345. There are two problems with the upgrade code - and the SET FOREIGN_KEY_CHECKS=0
others posted only solves one of them.
I've posted my analysis here, and I expect that someone will fix this issue. However, if you're experiencing this now and want to upgrade, here's what to do. I'm assuming that folks reading this will feel comfortable editing a file on their server, but aren't generally comfortable playing around with MySQL constraints:
First, restore your database to a state before the backup!
When you upgrade, look at the error.
Problem 1
Does the SQL statement that failed have the words DROP FOREIGN KEY
in it? If so, the problem is that you don't have the foreign keys. In this case, it's safe to delete the code that tries to remove the foreign key. Open up the file <civicrmroot>/CRM/Upgrade/Incremental/sql/4.7.5.mysql.tpl
. Find the command ALTER TABLE civicrm_mailing_event_queue DROP FOREIGN KEY FK_civicrm_mailing_event_queue_email_id
(or whichever command corresponds to the error you're getting) and delete it. Reload the page and the upgrade will continue. It's very likely that if you don't have one foreign key, you don't have all four - so expect to repeat this three times.
Problem 2 Does your SQL statement that failed have the words ADD CONSTRAINT
in it? If so, you've already encountered the problem this code is trying to fix - but the code doesn't work if you've already encountered the issue. To resolve this, add the line SET FOREIGN KEY CHECKS=0
on the line above the command that's giving you an error, and SET FOREIGN KEY CHECKS=1
on the line directly below it.
Don't forget - these commands are very similar, so if you think that this solution hasn't solved your problem, make sure you're looking at an identical error and not a very similar one!