--liquibase formatted sql

--changeset sys:access_token_definition
CREATE TABLE `access_token` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) DEFAULT NULL,
  `deviceId` varchar(32) DEFAULT NULL,
  `accessToken` varchar(36) DEFAULT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `device_token_idx` (`deviceId`,`accessToken`)
);

--changeset sys:authority_definition
CREATE TABLE `authority` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `authority` varchar(255) COLLATE utf8_bin NOT NULL,
  `name` varchar(100) COLLATE utf8_bin NOT NULL,
  `module` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  `description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `authority` (`authority`)
);

--changeset sys:department_definition
CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `createdBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `version` int(11) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `viewType` tinyint(4) NOT NULL DEFAULT '0',
  `caseOrderBy` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
);

--changeset sys:file_definition
CREATE TABLE `file` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `createdBy` varchar(30) DEFAULT NULL,
  `version` int(11) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedBy` varchar(30) DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `skey` varchar(32) NOT NULL,
  `filename` varchar(255) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `mimetype` varchar(255) NOT NULL,
  `filesize` bigint(20) NOT NULL,
  `remarks` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `skey` (`skey`)
);

--changeset sys:file_blob_definition
CREATE TABLE `file_blob` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fileId` int(11) NOT NULL,
  `bytes` longblob NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `fileId` (`fileId`)
);

--changeset sys:file_ref_definition
CREATE TABLE `file_ref` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `refType` varchar(20) NOT NULL,
  `refId` int(11) NOT NULL,
  `refCode` varchar(50) DEFAULT NULL,
  `fileId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `fileId` (`fileId`),
  KEY `refType_idx` (`refType`)
);

--changeset sys:`_roup` definition
CREATE TABLE `group` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `createdBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `version` int(11) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `name` varchar(50) COLLATE utf8_bin NOT NULL,
  `description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
);

--changeset sys:group_authority_definition
CREATE TABLE `group_authority` (
  `groupId` int(11) NOT NULL,
  `authId` int(11) NOT NULL,
  PRIMARY KEY (`groupId`,`authId`)
);

--changeset sys:id_counter_definition
CREATE TABLE `id_counter` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `count` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
);

--changeset sys:scheduled_task_log_definition
CREATE TABLE `scheduled_task_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `createdBy` varchar(30) DEFAULT NULL,
  `version` int(11) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedBy` varchar(30) DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `module` varchar(40) NOT NULL,
  `task` varchar(50) NOT NULL,
  `startDateTime` datetime NOT NULL,
  `endDateTime` datetime NOT NULL,
  `result` varchar(10) NOT NULL,
  `remarks` text,
  PRIMARY KEY (`id`)
);

--changeset sys:settings_definition
CREATE TABLE `settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `value` varchar(1000) NOT NULL,
  `category` varchar(50) DEFAULT NULL,
  `type` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name_idx` (`name`)
);

--changeset sys:`_ser` definition
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `createdBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `version` int(11) NOT NULL DEFAULT '0',
  `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedBy` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `username` varchar(30) COLLATE utf8_bin NOT NULL,
  `password` varchar(60) COLLATE utf8_bin NOT NULL,
  `locked` tinyint(1) NOT NULL DEFAULT '0',
  `expiryDate` date DEFAULT NULL,
  `name` varchar(50) COLLATE utf8_bin NOT NULL,
  `companyId` int(11) DEFAULT NULL,
  `customerId` int(11) DEFAULT NULL,
  `locale` varchar(5) COLLATE utf8_bin DEFAULT NULL,
  `fullname` varchar(90) COLLATE utf8_bin DEFAULT NULL,
  `firstname` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `lastname` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `title` varchar(60) COLLATE utf8_bin DEFAULT NULL,
  `department` varchar(60) COLLATE utf8_bin DEFAULT NULL,
  `deptId` int(11) DEFAULT NULL,
  `email` varchar(120) COLLATE utf8_bin DEFAULT NULL,
  `phone1` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `phone2` varchar(30) COLLATE utf8_bin DEFAULT NULL,
  `remarks` varchar(600) COLLATE utf8_bin DEFAULT NULL,
  `landingPage` varchar(30) COLLATE utf8_bin NOT NULL DEFAULT 'dashboard',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
);

--changeset sys:user_attempt_definition
CREATE TABLE `user_attempt` (
  `username` varchar(30) COLLATE utf8_bin NOT NULL,
  `attempt` tinyint(4) NOT NULL DEFAULT '0',
  `lastAttempted` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`username`)
);


--changeset sys:user_authority_definition
CREATE TABLE `user_authority` (
  `userId` int(11) NOT NULL,
  `authId` int(11) NOT NULL,
  PRIMARY KEY (`userId`,`authId`)
);

--changeset sys:user_group_definition 
CREATE TABLE `user_group` (
  `groupId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  PRIMARY KEY (`groupId`,`userId`)
);

