-- add & populate new columns
ALTER TABLE vm_mboxes ADD domain VARCHAR(255) NOT NULL AFTER mbox;
UPDATE vm_mboxes SET vm_mboxes.domain = ( SELECT vm_domains.domain FROM vm_domains WHERE vm_domains.id = vm_mboxes.domain_id );
ALTER TABLE vm_aliases ADD mbox VARCHAR(128) NOT NULL AFTER mbox_id;
ALTER TABLE vm_aliases ADD domain VARCHAR(255) NOT NULL AFTER mbox;
UPDATE vm_aliases SET vm_aliases.mbox = ( SELECT vm_mboxes.mbox FROM vm_mboxes WHERE vm_mboxes.id = vm_aliases.mbox_id );
UPDATE vm_aliases SET vm_aliases.domain = ( SELECT vm_domains.domain FROM vm_domains, vm_mboxes WHERE vm_mboxes.id = vm_aliases.mbox_id AND vm_domains.id = vm_mboxes.domain_id );
ALTER TABLE vm_autoresponders ADD mbox VARCHAR(128) NOT NULL AFTER mbox_id;
ALTER TABLE vm_autoresponders ADD domain VARCHAR(255) NOT NULL AFTER mbox;
UPDATE vm_autoresponders SET vm_autoresponders.mbox = ( SELECT vm_mboxes.mbox FROM vm_mboxes WHERE vm_mboxes.id = vm_autoresponders.mbox_id );
UPDATE vm_autoresponders SET vm_autoresponders.domain = ( SELECT vm_domains.domain FROM vm_domains, vm_mboxes WHERE vm_mboxes.id = vm_autoresponders.mbox_id AND vm_domains.id = vm_mboxes.domain_id );
ALTER TABLE vm_filters ADD mbox VARCHAR(128) NOT NULL AFTER mbox_id;
ALTER TABLE vm_filters ADD domain VARCHAR(255) NOT NULL AFTER mbox;
UPDATE vm_filters SET vm_filters.mbox = ( SELECT vm_mboxes.mbox FROM vm_mboxes WHERE vm_mboxes.id = vm_filters.mbox_id );
UPDATE vm_filters SET vm_filters.domain = ( SELECT vm_domains.domain FROM vm_domains, vm_mboxes WHERE vm_mboxes.id = vm_filters.mbox_id AND vm_domains.id = vm_mboxes.domain_id );
ALTER TABLE vm_forwards ADD mbox VARCHAR(128) NOT NULL AFTER mbox_id;
ALTER TABLE vm_forwards ADD domain VARCHAR(255) NOT NULL AFTER mbox;
UPDATE vm_forwards SET vm_forwards.mbox = ( SELECT vm_mboxes.mbox FROM vm_mboxes WHERE vm_mboxes.id = vm_forwards.mbox_id );
UPDATE vm_forwards SET vm_forwards.domain = ( SELECT vm_domains.domain FROM vm_domains, vm_mboxes WHERE vm_mboxes.id = vm_forwards.mbox_id AND vm_domains.id = vm_mboxes.domain_id );

-- drop foreign keys
ALTER TABLE vm_mboxes DROP FOREIGN KEY domain_delete_mboxes;
ALTER TABLE vm_aliases DROP FOREIGN KEY mbox_delete_aliases;
ALTER TABLE vm_autoresponders DROP FOREIGN KEY mbox_delete_autoresponders;
ALTER TABLE vm_filters DROP FOREIGN KEY mbox_delete_filters;
ALTER TABLE vm_forwards DROP FOREIGN KEY mbox_delete_forwards;
-- update indexes
ALTER TABLE vm_domains DROP INDEX domain, ADD UNIQUE domain (domain) USING BTREE;
ALTER TABLE vm_mboxes DROP INDEX email, ADD UNIQUE email (mbox, domain) USING BTREE;
ALTER TABLE vm_aliases ADD INDEX email (mbox, domain) USING BTREE;
ALTER TABLE vm_aliases ADD UNIQUE alias (mbox, domain, alias) USING BTREE;
ALTER TABLE vm_autoresponders ADD UNIQUE email (mbox, domain) USING BTREE;
ALTER TABLE vm_filters ADD UNIQUE email (mbox, domain) USING BTREE;
ALTER TABLE vm_forwards ADD UNIQUE email (mbox, domain) USING BTREE;
-- add new foreign keys
ALTER TABLE vm_mboxes ADD CONSTRAINT domain_delete_mboxes FOREIGN KEY (domain) REFERENCES vm_domains(domain) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE vm_aliases ADD CONSTRAINT mbox_delete_aliases FOREIGN KEY (mbox, domain) REFERENCES vm_mboxes(mbox, domain) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE vm_autoresponders ADD CONSTRAINT mbox_delete_autoresponders FOREIGN KEY (mbox, domain) REFERENCES vm_mboxes(mbox, domain) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE vm_filters ADD CONSTRAINT mbox_delete_filters FOREIGN KEY (mbox, domain) REFERENCES vm_mboxes(mbox, domain) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE vm_forwards ADD CONSTRAINT mbox_delete_forwards FOREIGN KEY (mbox, domain) REFERENCES vm_mboxes(mbox, domain) ON DELETE CASCADE ON UPDATE NO ACTION;

-- delete deprecated table columns & indexes
ALTER TABLE vm_mboxes DROP domain_id;
ALTER TABLE vm_aliases DROP mbox_id;
ALTER TABLE vm_autoresponders DROP mbox_id;
ALTER TABLE vm_filters DROP mbox_id;
ALTER TABLE vm_forwards DROP mbox_id;
