START TRANSACTION;

ALTER TABLE `tblVersion` ADD COLUMN `module` varchar(255) DEFAULT NULL;

ALTER TABLE `tblUsers` CHANGE `pwd` `pwd` varchar(255) DEFAULT NULL;

ALTER TABLE `tblUserPasswordHistory` CHANGE `pwd` `pwd` varchar(255) DEFAULT NULL;

ALTER TABLE `tblUserPasswordRequest` CHANGE `hash` `hash` varchar(255) DEFAULT NULL;

ALTER TABLE `tblUsers` ADD COLUMN `disabledUntil` datetime DEFAULT NULL AFTER `disabled`;

ALTER TABLE `tblDocuments` ADD COLUMN `enddate` datetime DEFAULT NULL AFTER `sequence`;

ALTER TABLE `tblDocuments` ADD COLUMN `startdate` datetime DEFAULT NULL AFTER `sequence`;

ALTER TABLE `tblDocumentContent` ADD COLUMN `lastrevisiondate` datetime DEFAULT NULL AFTER `revisiondate`;
  
ALTER TABLE `tblDocumentFiles` ADD COLUMN `checksum` char(32) DEFAULT NULL AFTER `mimeType`;

ALTER TABLE `tblDocumentFiles` ADD COLUMN `fileSize` bigint(20) DEFAULT NULL AFTER `mimeType`;

ALTER TABLE `tblCategory` ADD COLUMN `color` char(8) DEFAULT NULL AFTER `name`;

ALTER TABLE `tblNotify` ADD COLUMN `inherit` smallint(1) NOT NULL DEFAULT '0';

ALTER TABLE `tblSchedulerTask` ADD COLUMN `failures` int(11) NOT NULL DEFAULT '0';

CREATE TABLE `tblAttributeDefinitionGroups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `comment` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tblAttributeDefinitionGroupAttributeDefinition` (
  `attrdef` int(11),
  `attrgrp` int(11),
  `sequence` double NOT NULL DEFAULT '0',
  `show` int(11),
  UNIQUE KEY `attrdef` (`attrdef`,`attrgrp`),
  CONSTRAINT `tblAttributeDefinitionGroupAttributeDefinition_attrdef` FOREIGN KEY (`attrdef`) REFERENCES `tblAttributeDefinitions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `tblAttributeDefinitionGroupAttributeDefinition_attrgrp` FOREIGN KEY (`attrgrp`) REFERENCES `tblAttributeDefinitionGroups` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tblFolderAttributeDefinitionGroup` (
  `folder` int(11),
  `attrgrp` int(11),
  `sequence` double NOT NULL DEFAULT '0',
  UNIQUE KEY `folder` (`folder`,`attrgrp`),
  CONSTRAINT `tblFolderAttributeDefinitionGroup_folder` FOREIGN KEY (`folder`) REFERENCES `tblFolders` (`id`) ON DELETE CASCADE,
  CONSTRAINT `tblFolderAttributeDefinitionGroup_attrgrp` FOREIGN KEY (`attrgrp`) REFERENCES `tblAttributeDefinitionGroups` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tblUserAttributes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userID` int(11) DEFAULT NULL,
  `attrdef` int(11) DEFAULT NULL,
  `value` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `userID` (`userID`,`attrdef`),
  KEY `tblUserAttributes_attrdef` (`attrdef`),
  CONSTRAINT `tblUserAttributes_attrdef` FOREIGN KEY (`attrdef`) REFERENCES `tblAttributeDefinitions` (`id`),
  CONSTRAINT `tblUserAttributes_userid` FOREIGN KEY (`userID`) REFERENCES `tblUsers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tblLinkType` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `color` char(8) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `tblDocumentLinks` ADD COLUMN `linktype` int(11) DEFAULT NULL, ADD FOREIGN KEY tblDocumentLinks_linktype(`linktype`) REFERENCES tblLinkType(`id`) ON DELETE CASCADE;

UPDATE `tblVersion` set `major`=6, `minor`=1, `subminor`=0, `module`='core';

COMMIT;

