DROP PROCEDURE IF EXISTS makeSecure;

DELIMITER $$$
CREATE PROCEDURE makeSecure(_isSecure ENUM('y', 'n'))
not deterministic modifies sql data
BEGIN

  IF _isSecure = 'y' THEN
    ALTER TABLE `ma_Page` CHANGE `isSecure` `isSecure` ENUM('n','y') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'y';
    ALTER TABLE `ma_DraftPage` CHANGE `isSecure` `isSecure` ENUM('n','y') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'y';
  ELSEIF _isSecure = 'n' THEN
    ALTER TABLE `ma_Page` CHANGE `isSecure` `isSecure` ENUM('n','y') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'n';
    ALTER TABLE `ma_DraftPage` CHANGE `isSecure` `isSecure` ENUM('n','y') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'n';
  ELSE
    SIGNAL SQLSTATE
      'ERR0R'
    SET
      MESSAGE_TEXT = "Incorrect isSecure argument. Use CALL makeSecure('y') or CALL makeSecure('n')",
      MYSQL_ERRNO = 1;
  END IF;

  UPDATE `ma_Page` SET `isSecure` = _isSecure;
  UPDATE `ma_DraftPage` SET `isSecure` = _isSecure;
  UPDATE `ma_DPageOption` SET `value` = _isSecure WHERE `name` = 'isSecure';
  UPDATE `ma_PageOption` SET `value` = _isSecure WHERE `name` = 'isSecure';
  UPDATE `ma_DraftPageOption` SET `value` = _isSecure WHERE `name` = 'isSecure';
END $$$
DELIMITER ;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

DROP PROCEDURE IF EXISTS tribeInserted;
DROP TRIGGER IF EXISTS `Tribe_ai`;

DELIMITER $$$

CREATE PROCEDURE tribeInserted(_tribeId INT(10) UNSIGNED)
not deterministic modifies sql data
BEGIN
  INSERT INTO `ma_LicenseTribe` (`licenseId`, `tribeId`, `added`, `changed`)
  SELECT `id`, _tribeId, NOW(), NOW() FROM `ma_License` WHERE 1;
END $$$

CREATE TRIGGER `Tribe_ai` AFTER INSERT
ON `ma_Tribe` FOR EACH ROW
BEGIN
    CALL tribeInserted(NEW.id);
END $$$

DELIMITER ;


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --