-- MySQL Workbench Synchronization

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

ALTER TABLE `sog`.`glam_access_point` 
CHANGE COLUMN `ap_movement_radius` `ap_movement_radius` FLOAT(11) NULL DEFAULT '10' ;

ALTER TABLE `sog`.`glam_actionable` 
CHANGE COLUMN `action_type` `action_type` ENUM('DISCOVERED_KEY','DISCOVERED_LOCK','LOCK_UNSAFE','LATCH_REPLACEMENT','LOCK_REPLACEMENT','PENDING_SCHEDULE','PENDING_SRM','UNSAFE_LOCK','SHARED_LOCK','SRM_FAULT','LOCK_OUT_ZONE','SRM_OFFLINE','SRM_INACTIVE','IOT_DEV_DISCOVERED','IOT_DEV_DELETED','IOT_ALARM_ACTIVITY','DEVICE_OFFLINE','BATTERY_LOW','ASSET_DISCOVERED') NULL DEFAULT NULL ,
ADD COLUMN `asset_id` INT(11) NULL DEFAULT NULL AFTER `last_changed_by`;

ALTER TABLE `sog`.`glam_alarm_log` 
CHANGE COLUMN `alarm_log_id` `alarm_log_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ;

CREATE TABLE IF NOT EXISTS `sog`.`glam_asset` (
  `asset_id` INT(11) NOT NULL AUTO_INCREMENT,
  `asset_rf_id` BIGINT(20) NULL DEFAULT NULL,
  `asset_name` VARCHAR(64) NULL DEFAULT NULL,
  `asset_description` VARCHAR(256) NULL DEFAULT NULL,
  `asset_coordinates` POINT NULL DEFAULT NULL,
  `asset_state` ENUM('DISCOVERED','APPROVED') NULL DEFAULT 'DISCOVERED',
  `type_id` INT(11) NOT NULL,
  `is_deleted` BIT(1) NULL DEFAULT b'0',
  `created_on` DATETIME NULL DEFAULT NULL,
  `created_by` CHAR(36) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `deleted_on` DATETIME NULL DEFAULT NULL,
  `deleted_by` CHAR(36) NULL DEFAULT NULL,
  `approved_on` DATETIME NULL DEFAULT NULL,
  `approved_by` CHAR(36) NULL DEFAULT NULL,
  `asset_rf_id_deleted` BIGINT(20) NULL DEFAULT NULL,
  `created_by_key_id` CHAR(36) NULL DEFAULT NULL,
  `created_by_key_serial` INT(11) NULL DEFAULT NULL,
  PRIMARY KEY (`asset_id`),
  UNIQUE INDEX `asset_rf_id_UNIQUE` (`asset_rf_id` ASC),
  INDEX `fk_glam_asset_glam_asset_type_idx` (`type_id` ASC),
  INDEX `idx_is_deleted` (`is_deleted` ASC),
  INDEX `idx_asset_name` (`asset_name` ASC),
  CONSTRAINT `fk_glam_asset_glam_asset_type`
    FOREIGN KEY (`type_id`)
    REFERENCES `sog`.`glam_asset_type` (`type_id`)
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

CREATE TABLE IF NOT EXISTS `sog`.`glam_asset_type` (
  `type_id` INT(11) NOT NULL AUTO_INCREMENT,
  `type_name` VARCHAR(64) NULL DEFAULT NULL,
  `is_deleted` BIT(1) NULL DEFAULT b'0',
  `created_on` DATETIME NULL DEFAULT NULL,
  `created_by` CHAR(36) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `deleted_by` CHAR(36) NULL DEFAULT NULL,
  `deleted_on` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`type_id`),
  INDEX `idx_type_name` (`type_name` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

CREATE TABLE IF NOT EXISTS `sog`.`glam_asset_log` (
  `access_point_log_id` CHAR(36) NOT NULL,
  `asset_id` INT(11) NOT NULL,
  PRIMARY KEY (`access_point_log_id`),
  CONSTRAINT `fk_glam_asset_log_glam_access_point_log1`
    FOREIGN KEY (`access_point_log_id`)
    REFERENCES `sog`.`glam_access_point_log` (`access_point_log_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

ALTER TABLE `sog`.`glam_keyholder_group` 
CHANGE COLUMN `group_name` `group_name` VARCHAR(64) CHARACTER SET 'utf8' NULL DEFAULT NULL ,
CHANGE COLUMN `group_description` `group_description` VARCHAR(200) CHARACTER SET 'utf8' NULL DEFAULT NULL ;

ALTER TABLE `sog`.`glam_system_details` 
CHANGE COLUMN `system_main_colour` `system_main_colour` INT(11) NULL DEFAULT '3048945' ,
CHANGE COLUMN `system_sub_colour` `system_sub_colour` INT(11) NULL DEFAULT '7982541' ,
CHANGE COLUMN `system_unsafe_log_colour` `system_unsafe_log_colour` INT(11) NULL DEFAULT '16384000' ,
CHANGE COLUMN `system_iot_alarm_colour` `system_iot_alarm_colour` INT(11) NULL DEFAULT '16384000' ;

ALTER TABLE `sog`.`versioning` 
CHANGE COLUMN `version_string` `version_string` VARCHAR(10) CHARACTER SET 'utf8' NULL DEFAULT NULL ;

START TRANSACTION;
USE `sog`;

INSERT INTO `sog`.`glam_notification` (`notification_id`, `notification_desc`, `severity_id`, `notification_colour`) VALUES (200, 'Asset scanned', NULL, NULL);
INSERT INTO `sog`.`glam_notification` (`notification_id`, `notification_desc`, `severity_id`, `notification_colour`) VALUES (201, 'Asset discovered', NULL, NULL);
INSERT INTO `sog`.`glam_notification` (`notification_id`, `notification_desc`, `severity_id`, `notification_colour`) VALUES (202, 'Asset registered', NULL, NULL);
INSERT INTO `sog`.`glam_notification` (`notification_id`, `notification_desc`, `severity_id`, `notification_colour`) VALUES (203, 'Asset replaced by lock', NULL, NULL);

COMMIT;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
