ALTER TABLE `account`.`role`
	MODIFY COLUMN `hasLogin` tinyint(3) unsigned DEFAULT 1 NOT NULL;

ALTER TABLE `account`.`roleInherit`
	ADD UNIQUE( `role`, `inheritsFrom`);

ALTER TABLE `account`.`roleInherit`
	DROP PRIMARY KEY;

ALTER TABLE `account`.`roleInherit`
	ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
	ADD PRIMARY KEY (`id`);

ALTER TABLE `account`.`mailAlias`
	ADD `description` VARCHAR(255) NULL AFTER `alias`;

ALTER TABLE `account`.`mailAliasAccount`
	ADD UNIQUE( `mailAlias`, `account`);

ALTER TABLE `account`.`mailAliasAccount`
	DROP PRIMARY KEY;

ALTER TABLE `account`.`mailAliasAccount`
	ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
	ADD PRIMARY KEY (`id`);

ALTER TABLE account.ldapConfig
	ADD groupDn varchar(255) NULL;

UPDATE account.ldapConfig SET groupDn = 'ou=groups,dc=verdnatura,dc=es';

DROP PROCEDURE IF EXISTS account.user_syncPassword;

ALTER TABLE account.`user`
	MODIFY COLUMN sync tinyint(4) DEFAULT 0 NOT NULL COMMENT 'Deprecated';

CREATE TABLE account.userSync (
	name varchar(30) NOT NULL,
	CONSTRAINT userSync_PK PRIMARY KEY (name)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

USE account;

DELIMITER $$

DROP TRIGGER IF EXISTS account.user_beforeUpdate$$
CREATE DEFINER=`root`@`%` TRIGGER `user_beforeUpdate`
	BEFORE UPDATE ON `user` FOR EACH ROW
BEGIN
	IF !(NEW.`name` <=> OLD.`name`) THEN
		CALL user_checkName (NEW.`name`);
	END IF;

	IF !(NEW.`password` <=> OLD.`password`) THEN
		SET NEW.bcryptPassword = NULL;
		SET NEW.lastPassChange = NOW();
	END IF;
END$$

DROP TRIGGER IF EXISTS account.user_afterUpdate$$
CREATE DEFINER=`root`@`%` TRIGGER `user_afterUpdate`
	AFTER UPDATE ON `user` FOR EACH ROW
BEGIN
	INSERT IGNORE INTO userSync SET `name` = NEW.`name`;

	IF !(OLD.`name` <=> NEW.`name`) THEN
		INSERT IGNORE INTO userSync SET `name` = OLD.`name`;
	END IF;
	
	IF !(NEW.`role` <=> OLD.`role`)
	THEN
		INSERT INTO vn.mail SET
			`sender` = 'jgallego@verdnatura.es',
			`replyTo` = 'jgallego@verdnatura.es',
			`subject` = 'Rol modificado',
			`body` = CONCAT(myUserGetName(), ' ha modificado el rol del usuario ',
				NEW.`name`, ' de ', OLD.role, ' a ', NEW.role);
	END IF;
END$$

CREATE DEFINER=`root`@`%` TRIGGER `user_afterInsert`
	AFTER INSERT ON `user` FOR EACH ROW
BEGIN
	INSERT IGNORE INTO userSync SET `name` = NEW.`name`;
END$$

CREATE DEFINER=`root`@`%` TRIGGER `user_afterDelete`
	AFTER DELETE ON `user` FOR EACH ROW
BEGIN
	INSERT IGNORE INTO userSync SET `name` = OLD.`name`;
END$$

DROP TRIGGER IF EXISTS account.account_afterInsert$$
CREATE DEFINER=`root`@`%` TRIGGER `account_afterInsert`
	AFTER INSERT ON `account` FOR EACH ROW
BEGIN
	INSERT IGNORE INTO userSync (`name`)
		SELECT `name` FROM `user` WHERE id = NEW.id;
END$$

DROP TRIGGER IF EXISTS account.account_afterDelete$$
CREATE DEFINER=`root`@`%` TRIGGER `account_afterDelete`
	AFTER DELETE ON `account` FOR EACH ROW
BEGIN
	INSERT IGNORE INTO userSync (`name`)
		SELECT `name` FROM `user` WHERE id = OLD.id;
END$$

DROP TRIGGER IF EXISTS account.role_beforeInsert$$
CREATE DEFINER=`root`@`%` TRIGGER role_beforeInsert
	BEFORE INSERT ON `role` FOR EACH ROW
BEGIN
	CALL role_checkName(NEW.`name`);
END

DROP TRIGGER IF EXISTS account.role_beforeUpdate$$
CREATE DEFINER=`root`@`%` TRIGGER role_beforeUpdate
	BEFORE UPDATE ON `role` FOR EACH ROW
BEGIN
	IF !(NEW.`name` <=> OLD.`name`) THEN
		CALL role_checkName (NEW.`name`);
	END IF;
END$$

DELIMITER ;