BEGIN;

CREATE TABLE `new_tblUsers` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `login` varchar(50) default NULL,
  `pwd` varchar(255) default NULL,
  `webauthn` varchar(1024) 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`;

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`;

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`)
);

UPDATE tblVersion set major=6, minor=1, subminor=0;

COMMIT;
