CREATE TABLE `rehab_option` (
  `Id` int(11) NOT NULL,
  `Key` varchar(255) NOT NULL,
  `Value` varchar(4096) NOT NULL DEFAULT '',
  `Description` text DEFAULT NULL,
  `Access_Role` varchar(32) DEFAULT NULL,
  `Type` varchar(50) NOT NULL DEFAULT 'string',
  `Order` int(8) NOT NULL DEFAULT 0,
  `Group` varchar(255) DEFAULT NULL,
  `EditedBy` bigint(20) DEFAULT NULL,
  `Active` tinyint(1) NOT NULL DEFAULT 1,
  `Checksum` varchar(32) DEFAULT NULL,
  `Last_Edit` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `rehab_option`
  ADD PRIMARY KEY (`Id`),
  ADD UNIQUE KEY `Key` (`Key`,`Group`);

ALTER TABLE `rehab_option`
  MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT;


CREATE TABLE `rehab_option_backup` (
  `Backup_ID` int(9) NOT NULL AUTO_INCREMENT,
  `Key` varchar(255) NOT NULL,
  `Value` varchar(4096) NOT NULL DEFAULT '',
  `EditedBy` bigint(20),
  `Checksum` varchar(32),
  `Previous_Last_Edit` timestamp NULL DEFAULT NULL,
  `Last_Edit` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`Backup_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TRIGGER IF EXISTS `Option_Backup`;
DELIMITER //
CREATE TRIGGER `Option_Backup`  AFTER UPDATE ON `rehab_option`
 FOR EACH ROW
  BEGIN
	INSERT INTO `rehab_option_backup`(`Key`,`Value`,`EditedBy`,`Checksum`,`Previous_Last_Edit`) 
        VALUES ( OLD.`Key`,
                 OLD.`Value`,
                 OLD.`EditedBy`,
                 OLD.`Checksum`,
                 OLD.`Last_Edit`) ;
  END
// DELIMITER ;
