BEGIN;

ALTER TABLE `tblVersion` ADD COLUMN `module` TEXT DEFAULT NULL;

ALTER TABLE `tblDocuments` ADD COLUMN `startdate` TEXT DEFAULT NULL;

ALTER TABLE `tblDocuments` ADD COLUMN `enddate` TEXT DEFAULT NULL;

ALTER TABLE `tblDocumentContent` ADD COLUMN `lastrevisiondate` TEXT DEFAULT NULL;

ALTER TABLE `tblDocumentFiles` ADD COLUMN `fileSize` INTEGER DEFAULT NULL;

ALTER TABLE `tblDocumentFiles` ADD COLUMN `checksum` char(32) DEFAULT NULL;

ALTER TABLE `tblSessions` ADD COLUMN `data` TEXT DEFAULT NULL;

CREATE TABLE `new_tblUsers` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `login` varchar(50) default NULL,
  `pwd` varchar(255) default NULL,
  `secret` varchar(50) default NULL,
  `fullName` varchar(100) default NULL,
  `email` varchar(70) default NULL,
  `language` varchar(32) NOT NULL,
  `theme` varchar(32) NOT NULL,
  `comment` text NOT NULL,
  `role` INTEGER NOT NULL REFERENCES `tblRoles` (`id`),
  `hidden` INTEGER NOT NULL default '0',
  `pwdExpiration` TEXT default NULL,
  `loginfailures` INTEGER NOT NULL default '0',
  `disabled` INTEGER NOT NULL default '0',
  `quota` INTEGER,
  `homefolder` INTEGER default NULL REFERENCES `tblFolders` (`id`),
  UNIQUE (`login`)
);

INSERT INTO `new_tblUsers` SELECT * FROM `tblUsers`;

DROP TABLE `tblUsers`;

ALTER TABLE `new_tblUsers` RENAME TO `tblUsers`;

ALTER TABLE `tblUsers` ADD COLUMN `disabledUntil` TEXT DEFAULT NULL;

CREATE TABLE `new_tblUserPasswordRequest` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `userID` INTEGER NOT NULL default '0' REFERENCES `tblUsers` (`id`) ON DELETE CASCADE,
  `hash` varchar(255) default NULL,
  `date` TEXT NOT NULL
);

INSERT INTO `new_tblUserPasswordRequest` SELECT * FROM `tblUserPasswordRequest`;

DROP TABLE `tblUserPasswordRequest`;

ALTER TABLE `new_tblUserPasswordRequest` RENAME TO `tblUserPasswordRequest`;

CREATE TABLE `new_tblUserPasswordHistory` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `userID` INTEGER NOT NULL default '0' REFERENCES `tblUsers` (`id`) ON DELETE CASCADE,
  `pwd` varchar(255) default NULL,
  `date` TEXT NOT NULL
);

INSERT INTO `new_tblUserPasswordHistory` SELECT * FROM `tblUserPasswordHistory`;

DROP TABLE `tblUserPasswordHistory`;

ALTER TABLE `new_tblUserPasswordHistory` RENAME TO `tblUserPasswordHistory`;

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

ALTER TABLE `tblNotify` ADD COLUMN `inherit` INTEGER NOT NULL DEFAULT '0';

ALTER TABLE `tblSchedulerTask` ADD COLUMN `failures` INTEGER NOT NULL DEFAULT '0';

CREATE TABLE `tblAttributeDefinitionGroups` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `name` varchar(100) default NULL,
  `comment` text,
  UNIQUE(`name`)
);

CREATE TABLE `tblAttributeDefinitionGroupAttributeDefinition` (
  `attrdef` INTEGER REFERENCES `tblAttributeDefinitions` (`id`) ON DELETE CASCADE,
  `attrgrp` INTEGER REFERENCES `tblAttributeDefinitionGroups` (`id`) ON DELETE CASCADE,
  `sequence` double NOT NULL default '0',
  `show` INTEGER,
  UNIQUE(`attrdef`, `attrgrp`)
);

CREATE TABLE `tblFolderAttributeDefinitionGroup` (
  `folder` INTEGER REFERENCES `tblFolders` (`id`) ON DELETE CASCADE,
  `attrgrp` INTEGER REFERENCES `tblAttributeDefinitionGroups` (`id`) ON DELETE CASCADE,
  `sequence` double NOT NULL default '0',
  UNIQUE(`folder`, `attrgrp`)
);

CREATE TABLE `tblUserAttributes` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `userID` INTEGER default NULL REFERENCES `tblUsers` (`id`) ON DELETE CASCADE,
  `attrdef` INTEGER default NULL REFERENCES `tblAttributeDefinitions` (`id`),
  `value` text default NULL,
  UNIQUE (`userID`, `attrdef`)
);

CREATE TABLE `tblLinkType` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `name` text NOT NULL,
  `color` char(8) DEFAULT NULL
);

ALTER TABLE `tblDocumentLinks` ADD COLUMN `linktype` INTEGER DEFAULT NULL REFERENCES tblLinkType(`id`) ON DELETE CASCADE;

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

COMMIT;
