#
# Copyright (c) 2004-2006, NFG Net Facilities Group BV, support@nfg.nl
# Copyright (c) 2006 Aaron Stone, aaron@serendipity.cx
#
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later 
# version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 

# Schema to repair broken foreign key constraints.
# Run thusly:
#    mysql -u dbmail_user -p dbmail_pass dbmail_db -f < fix_foreign_keys.mysql
#
# You will probably see a lot of errors. That's OK, because we're not sure what
# the foreign keys are called in your database (since they're broken!) and
# programmed in a couple of guesses to try to get them.
#
# The errors you can ignore will look similar to this one:
#    ERROR 1025 (HY000) at line 37: Error on rename of
#    './dbmail/dbmail_mailboxes' to './dbmail/#sql2-6487-4c' (errno: 152)
#
# After this script runs, you must run:
#    dbmail-util -by
#
# As we have dropped several of the ephemeral tables outright, they have to be
# rebuilt from the message store.
# 

SET FOREIGN_KEY_CHECKS=0;

alter table dbmail_mailboxes drop foreign key owner_idnr_fk;
alter table dbmail_mailboxes drop foreign key dbmail_mailboxes_ibfk;
alter table dbmail_mailboxes drop foreign key dbmail_mailboxes_ibfk_1;
alter table dbmail_mailboxes drop foreign key dbmail_mailboxes_ibfk_2;
alter table dbmail_mailboxes add CONSTRAINT dbmail_mailboxes_ibfk_1 
	FOREIGN KEY (`owner_idnr`) REFERENCES `dbmail_users` (`user_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_subscription drop foreign key user_id_fk;
alter table dbmail_subscription drop foreign key mailbox_id_fk;
alter table dbmail_subscription drop foreign key dbmail_subscription_ibfk_1;
alter table dbmail_subscription drop foreign key dbmail_subscription_ibfk_2;
alter table dbmail_subscription drop foreign key dbmail_subscription_ibfk_3;
alter table dbmail_subscription add CONSTRAINT dbmail_subscription_ibfk_1 
	FOREIGN KEY (`user_id`) REFERENCES `dbmail_users` (`user_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;
alter table dbmail_subscription add CONSTRAINT dbmail_subscription_ibfk_2
	FOREIGN KEY (`mailbox_id`) REFERENCES `dbmail_mailboxes` (`mailbox_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_acl drop foreign key user_id_fk;
alter table dbmail_acl drop foreign key mailbox_id_fk;
alter table dbmail_acl drop foreign key dbmail_acl_ibfk_1;
alter table dbmail_acl drop foreign key dbmail_acl_ibfk_2;
alter table dbmail_acl drop foreign key dbmail_acl_ibfk_3;
alter table dbmail_acl add CONSTRAINT dbmail_acl_ibfk_1 
	FOREIGN KEY (`user_id`) REFERENCES `dbmail_users` (`user_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;
alter table dbmail_acl add CONSTRAINT dbmail_acl_ibfk_2
	FOREIGN KEY (`mailbox_id`) REFERENCES `dbmail_mailboxes` (`mailbox_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;

	
alter table dbmail_messages drop foreign key physmessage_id_fk;
alter table dbmail_messages drop foreign key mailbox_idnr_fk;
alter table dbmail_messages drop foreign key dbmail_messages_ibfk_1;
alter table dbmail_messages drop foreign key dbmail_messages_ibfk_2;
alter table dbmail_messages drop foreign key dbmail_messages_ibfk_3;
alter table dbmail_messages add CONSTRAINT dbmail_messages_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
alter table dbmail_messages add CONSTRAINT dbmail_messages_ibfk_2 
	FOREIGN KEY (`mailbox_idnr`) REFERENCES `dbmail_mailboxes` (`mailbox_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;

	
alter table dbmail_messageblks drop foreign key physmessage_id_fk;
alter table dbmail_messageblks drop foreign key dbmail_messageblks_ibfk_1;
alter table dbmail_messageblks drop foreign key dbmail_messageblks_ibfk_2;
alter table dbmail_messageblks add CONSTRAINT dbmail_messageblks_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_sievescripts drop foreign key physmessage_id_fk;
alter table dbmail_sievescripts drop foreign key dbmail_sievescripts_ibfk_1;
alter table dbmail_sievescripts drop foreign key dbmail_sievescripts_ibfk_2;
alter table dbmail_sievescripts add CONSTRAINT dbmail_sievescripts_ibfk_1
	FOREIGN KEY (`owner_idnr`) REFERENCES `dbmail_users` (`user_idnr`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_header drop foreign key dbmail_header_ibfk_1;
alter table dbmail_header drop foreign key dbmail_header_ibfk_2;
alter table dbmail_header drop foreign key dbmail_header_ibfk_3;
alter table dbmail_header drop foreign key dbmail_header_ibfk_4;
alter table dbmail_header add CONSTRAINT `dbmail_header_ibfk_1` FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
alter table dbmail_header add CONSTRAINT `dbmail_header_ibfk_2` FOREIGN KEY (`headername_id`) REFERENCES `dbmail_headername` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
alter table dbmail_header add CONSTRAINT `dbmail_header_ibfk_3` FOREIGN KEY (`headervalue_id`) REFERENCES `dbmail_headervalue` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_subjectfield drop foreign key dbmail_subjectfield_ibfk_1;
alter table dbmail_subjectfield drop foreign key dbmail_subjectfield_ibfk_2;
alter table dbmail_subjectfield add CONSTRAINT dbmail_subjectfield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_datefield drop foreign key dbmail_datefield_ibfk_1;
alter table dbmail_datefield drop foreign key dbmail_datefield_ibfk_2;
alter table dbmail_datefield add CONSTRAINT dbmail_datefield_ibfk_1 
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_referencesfield drop foreign key dbmail_referencesfield_ibfk_1;
alter table dbmail_referencesfield drop foreign key dbmail_referencesfield_ibfk_2;
alter table dbmail_referencesfield add CONSTRAINT dbmail_referencesfield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_fromfield drop foreign key dbmail_fromfield_ibfk_1;
alter table dbmail_fromfield drop foreign key dbmail_fromfield_ibfk_2;
alter table dbmail_fromfield add CONSTRAINT dbmail_fromfield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_tofield drop foreign key dbmail_tofield_ibfk_1;
alter table dbmail_tofield drop foreign key dbmail_tofield_ibfk_2;
alter table dbmail_tofield add CONSTRAINT dbmail_tofield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_replytofield drop foreign key dbmail_replytofield_ibfk_1;
alter table dbmail_replytofield drop foreign key dbmail_replytofield_ibfk_2;
alter table dbmail_replytofield add CONSTRAINT dbmail_replytofield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_ccfield drop foreign key dbmail_datefield_ibfk_1;
alter table dbmail_ccfield drop foreign key dbmail_datefield_ibfk_2;
alter table dbmail_ccfield add CONSTRAINT dbmail_datefield_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


alter table dbmail_envelope drop foreign key dbmail_envelope_ibfk_1;
alter table dbmail_envelope drop foreign key dbmail_envelope_ibfk_2;
alter table dbmail_envelope add CONSTRAINT dbmail_envelope_ibfk_1
	FOREIGN KEY (`physmessage_id`) REFERENCES `dbmail_physmessage` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


SET FOREIGN_KEY_CHECKS=1;

