CREATE TABLE `rehab_licensekey` (
  `LicenseKey` varchar(255) NOT NULL,
  `Creation_Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Start_Date` timestamp NULL DEFAULT NULL,
  `End_Date` timestamp NULL DEFAULT NULL,
  `Activation_Date` timestamp NULL DEFAULT NULL,
  `Used` tinyint(1) NOT NULL DEFAULT 0,
  `Active` tinyint(1) NOT NULL DEFAULT 1,
  `EditedBy` bigint(20),
  `Checksum` varchar(32),
  `Last_Edit` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`LicenseKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `rehab_licensekey_activation_log` (
  `Log_ID` int(9) NOT NULL AUTO_INCREMENT,
  `LicenseKey` varchar(255) NOT NULL,
  `Activation_Date` timestamp DEFAULT CURRENT_TIMESTAMP,
  `Deactivation_Date` timestamp NULL DEFAULT NULL,
  `Username` varchar(255) NOT NULL DEFAULT "",
  `Checksum` varchar(32),
  PRIMARY KEY (`Log_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `rehab_licensekey_backup` (
  `Backup_ID` int(9) NOT NULL AUTO_INCREMENT,
  `LicenseKey` varchar(255) NOT NULL,
  `Creation_Date` timestamp NOT NULL,
  `Start_Date` timestamp  NULL DEFAULT NULL,
  `End_Date` timestamp  NULL DEFAULT NULL,
  `Activation_Date` timestamp  NULL DEFAULT NULL,
  `Used` tinyint(1) NOT NULL DEFAULT 0,
  `Active` tinyint(1) NOT NULL DEFAULT 1,	
  `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 `LicenseKey_Backup`;
DELIMITER //
CREATE TRIGGER `LicenseKey_Backup`  AFTER UPDATE ON `rehab_licensekey`
 FOR EACH ROW
  BEGIN
	INSERT INTO `rehab_licensekey_backup`(`Creation_Date`, `LicenseKey`,`Start_Date`,`End_Date`,`Activation_Date`,`Used`,`Active`,`EditedBy`,`Checksum`,`Previous_Last_Edit`) 
	VALUES (OLD.`Creation_Date`, OLD.`LicenseKey`,OLD.`Start_Date`,OLD.`End_Date`,OLD.`Activation_Date`,OLD.`Used`,OLD.`Active`,OLD.`EditedBy`,OLD.`Checksum`,OLD.`Last_Edit`) ;
  END
// DELIMITER ;
