-- RainLoop Webmail initial contacts database structure

/*!40014  SET FOREIGN_KEY_CHECKS=0 */;

-- Table structure for table `rainloop_system`
CREATE TABLE IF NOT EXISTS `rainloop_system` (
	`name` varchar(50) NOT NULL,
	`value_int` int(10) UNSIGNED NOT NULL DEFAULT '0',
	`value_str` varchar(255) NOT NULL DEFAULT ''
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;

-- Table structure for table `rainloop_pab_users`
CREATE TABLE IF NOT EXISTS `rainloop_pab_users` (
	`id_user` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`email` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL,

	UNIQUE `email_unique` (`email`),
	PRIMARY KEY(`id_user`)
) /*!40000 ENGINE=INNODB */;

-- Table structure for table `rainloop_pab_contacts`
CREATE TABLE IF NOT EXISTS `rainloop_pab_contacts` (
	`id_contact` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`id_user` int(10) UNSIGNED NOT NULL,
	`display_name` varchar(255) NOT NULL DEFAULT '',
	`display_email` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL DEFAULT '',
	`display_in_list` varchar(255) NOT NULL DEFAULT '',
	`is_auto` tinyint(1) NOT NULL DEFAULT '0',
	`is_share` tinyint(1) NOT NULL DEFAULT '0',
	`changed` int(10) UNSIGNED NOT NULL DEFAULT '0',

	CONSTRAINT `id_user_fk_rainloop_pab_contacts` FOREIGN KEY (`id_user`)
		REFERENCES `rainloop_pab_users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
	PRIMARY KEY(`id_contact`)
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;

-- Table structure for table `rainloop_pab_prop`
CREATE TABLE IF NOT EXISTS `rainloop_pab_prop` (
	`id_contact` int(10) UNSIGNED NOT NULL,
	`id_user` int(10) UNSIGNED NOT NULL,
	`type` int(10) UNSIGNED NOT NULL,
	`type_custom` varchar(50) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL DEFAULT '',
	`value` varchar(255) NOT NULL DEFAULT '',
	`value_custom` varchar(255) NOT NULL DEFAULT '',
	`frec` int(10) UNSIGNED NOT NULL DEFAULT '0',

	CONSTRAINT `id_contact_fk_rainloop_pab_prop` FOREIGN KEY (`id_contact`)
		REFERENCES `rainloop_pab_contacts` (`id_contact`) ON DELETE CASCADE ON UPDATE CASCADE
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;

-- Table structure for table `rainloop_pab_tags`
CREATE TABLE IF NOT EXISTS `rainloop_pab_tags` (
	`id_user` int(10) UNSIGNED NOT NULL,
	`id_contact` int(10) UNSIGNED NOT NULL,
	`name` varchar(255) NOT NULL,

	UNIQUE `id_user_name_unique` (`id_user`, `name`),
	CONSTRAINT `id_contact_fk_rainloop_pab_tags` FOREIGN KEY (`id_contact`)
		REFERENCES `rainloop_pab_contacts` (`id_contact`) ON DELETE CASCADE ON UPDATE CASCADE
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;

-- RainLoop Webmail update contacts database structure

DELIMITER $$

DROP PROCEDURE IF EXISTS rainloop_pab_upgrade_database $$

CREATE PROCEDURE rainloop_pab_upgrade_database()
BEGIN
	DECLARE new_version INT DEFAULT 1;
	DECLARE current_version INT DEFAULT 0;
	SELECT IFNULL(MAX(`value_int`), 0) INTO current_version FROM `rainloop_system` WHERE `name` = 'rainloop-pab-db-version';

	IF current_version < 1 THEN
		ALTER TABLE `rainloop_pab_prop` ADD INDEX `id_user_id_contact_index` (`id_user`, `id_contact`);
	END IF;

--	TODO
--
-- 	IF current_version < 2 THEN
-- 		ALTER TABLE `rainloop_pab_prop` ADD INDEX `id_user_id_contact_index` (`id_user`, `id_contact`);
-- 	END IF;

	DELETE FROM `rainloop_system` WHERE `name` = 'rainloop-pab-db-version' AND `value_int` <= new_version;
	INSERT INTO `rainloop_system` (`name`, `value_int`) VALUES ('rainloop-pab-db-version', new_version);
END$$

CALL rainloop_pab_upgrade_database() $$

DROP PROCEDURE IF EXISTS rainloop_pab_upgrade_database $$

DELIMITER ;

/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
