START TRANSACTION;

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

ALTER TABLE "tblUsers" ALTER COLUMN "pwd" TYPE varchar(255) DEFAULT NULL;

ALTER TABLE "tblUserPasswordHistory" ALTER COLUMN "pwd" varchar(255) DEFAULT NULL;

ALTER TABLE "tblUserPasswordRequest" ALTER COLUMN "hash" varchar(255) DEFAULT NULL;

ALTER TABLE "tblUsers" ADD COLUMN "webauthn" TYPE text DEFAULT NULL;

ALTER TABLE "tblUsers" ADD COLUMN "disabledUntil" TYPE TIMESTAMP DEFAULT NULL;

ALTER TABLE "tblDocuments" ADD COLUMN "startdate" TIMESTAMP default NULL;

ALTER TABLE "tblDocuments" ADD COLUMN "enddate" TIMESTAMP default NULL;

ALTER TABLE "tblDocumentContent" ADD COLUMN "lastrevisiondate" TIMESTAMP default NULL;

ALTER TABLE "tblCategory" ADD COLUMN "color" char(8) default NULL;

ALTER TABLE "tblNotify" ADD COLUMN "inherit" INTEGER NOT NULL default '0';

CREATE TABLE "tblAttributeDefinitionGroups" (
  "id" SERIAL UNIQUE,
  "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" REAL 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" REAL NOT NULL default '0',
  UNIQUE ("folder", "attrgrp")
);

CREATE TABLE "tblUserAttributes" (
  "id" SERIAL UNIQUE,
  "userID" INTEGER default NULL REFERENCES "tblUsers" ("id") ON DELETE CASCADE,
  "attrdef" INTEGER default NULL REFERENCES "tblAttributeDefinitions" ("id"),
  "value" text default NULL,
  UNIQUE (userID, attrdef)
) ;

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

COMMIT;
