-- Downgrades from version 0.4
-- WARNING: Make sure that all tables are using InnoDB engine!!!
--          If not, use: ALTER TABLE xxx ENGINE=InnoDB;

-- FIXME: well that's a howdy-do of a WARNING, written in SQL, so
--   there is no real check or fix for it...

/* MySQL bug workaround: http://bugs.mysql.com/bug.php?id=46293 */
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;

ALTER TABLE `messages` DROP FOREIGN KEY `user_id_fk_messages`;
ALTER TABLE `cache` DROP FOREIGN KEY `user_id_fk_cache`;
ALTER TABLE `contacts` DROP FOREIGN KEY `user_id_fk_contacts`;
ALTER TABLE `identities` DROP FOREIGN KEY `user_id_fk_identities`;

-- pek: the DROP INDEX fails on "Error on rename of './roundcube/#sql-dd2_170f' to
--   './roundcube/identities'" if it happens with an active foreign key constraint (
--   meaning, before the DROP FOREIGN KEY above or after the ADD CONSTRAINT on the
--   FOREIGN KEY below)
ALTER TABLE `identities` DROP INDEX `user_identities_index`;
ALTER TABLE `identities` DROP `changed`;

-- pek: compare to mysql.initial.sql from Roundcube version 0.3.1
-- pek: in general, these make the CASCADE conditional on MySQL version 4 or above;
--   not sure if the conditional is really needed; do we ship less than 4?
ALTER TABLE `messages` ADD CONSTRAINT `user_id_fk_messages` FOREIGN KEY (`user_id`)
    REFERENCES `users`(`user_id`) /*!40008 ON DELETE CASCADE ON UPDATE CASCADE */;
ALTER TABLE `cache` ADD CONSTRAINT `user_id_fk_cache` FOREIGN KEY (`user_id`)
    REFERENCES `users`(`user_id`) /*!40008 ON DELETE CASCADE ON UPDATE CASCADE */;
ALTER TABLE `contacts` ADD CONSTRAINT `user_id_fk_contacts` FOREIGN KEY (`user_id`)
    REFERENCES `users`(`user_id`) /*!40008 ON DELETE CASCADE ON UPDATE CASCADE */;
ALTER TABLE `identities` ADD CONSTRAINT `user_id_fk_identities` FOREIGN KEY (`user_id`)
    REFERENCES `users`(`user_id`) /*!40008 ON DELETE CASCADE ON UPDATE CASCADE */;

ALTER TABLE `contacts` ALTER `name` DROP DEFAULT;
ALTER TABLE `contacts` ALTER `firstname` DROP DEFAULT;
ALTER TABLE `contacts` ALTER `surname` DROP DEFAULT;

DROP TABLE contactgroups;
DROP TABLE contactgroupmembers;

/*!40014 SET FOREIGN_KEY_CHECKS=1 */;

