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_keyholder_group` 
CHANGE COLUMN `group_name` `group_name` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL ,
CHANGE COLUMN `group_description` `group_description` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL ;

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

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') NULL DEFAULT NULL ,
ADD COLUMN `node_id` INT(11) NULL DEFAULT NULL AFTER `last_changed_by`,
ADD COLUMN `device_id` INT(11) NULL DEFAULT NULL AFTER `node_id`;

CREATE TABLE IF NOT EXISTS `sog`.`glam_device` (
  `device_id` INT(11) NOT NULL AUTO_INCREMENT,
  `device_serial` VARCHAR(45) NULL DEFAULT NULL,
  `device_name` VARCHAR(45) NULL DEFAULT NULL,
  `device_type` INT(11) NOT NULL DEFAULT 0,
  `device_battery_level` INT(11) NOT NULL DEFAULT 0,
  `device_eu_id` VARCHAR(45) NULL DEFAULT NULL,
  `device_app_id` VARCHAR(45) NULL DEFAULT NULL,
  `device_app_key` VARCHAR(45) NULL DEFAULT NULL,
  `device_net_id` VARCHAR(45) NULL DEFAULT NULL,
  `device_pac` VARCHAR(45) NULL DEFAULT NULL,
  `access_point_id` CHAR(36) NULL DEFAULT NULL,
  `device_last_updated` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `device_created_on` DATETIME NULL DEFAULT NULL,
  `device_registered` TINYINT(1) NOT NULL,
  `device_communication` INT(11) NOT NULL DEFAULT 0,
  `device_state` ENUM('ACTIVE','DELETION_PENDING','DELETED') NULL DEFAULT 'ACTIVE',
  PRIMARY KEY (`device_id`),
  INDEX `fk_glam_device_glam_access_point1_idx` (`access_point_id` ASC),
  UNIQUE INDEX `device_serial_UNIQUE` (`device_serial` ASC, `device_type` ASC),
  CONSTRAINT `fk_glam_device_glam_access_point1`
    FOREIGN KEY (`access_point_id`)
    REFERENCES `sog`.`glam_access_point` (`access_point_id`)
    ON DELETE SET NULL
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_temperature` (
  `sensor_temperature_value` FLOAT(11) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_temperature_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_temperature_glam_node1`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_humidity` (
  `sensor_humidity_value` FLOAT(11) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_humidity_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_humidity_glam_node1`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_dinput` (
  `sensor_dinput_bit_state` BIT(1) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  CONSTRAINT `fk_glam_sensor_dinput_glam_node1`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_ambient_light` (
  `sensor_ambient_light_value` FLOAT(11) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_ambient_light_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_ambient_light_glam_node1`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_node` (
  `node_id` INT(11) NOT NULL AUTO_INCREMENT,
  `node_label` VARCHAR(45) NULL DEFAULT NULL,
  `node_units` VARCHAR(45) NULL DEFAULT NULL,
  `node_alarm_status` BIT(1) NULL DEFAULT NULL,
  `node_device_id` INT(11) NULL DEFAULT NULL,
  `node_type` VARCHAR(45) NULL DEFAULT NULL,
  `node_sensor_number` INT(11) NULL DEFAULT NULL COMMENT ' /* comment truncated */ /*Used to seperate multiple sensors of the same type on a device*/',
  `node_sensor_type` ENUM('TEMPERATURE','HUMIDITY','DINPUT','GPS','AMBIENT_LIGHT','AIR_PRESSURE','ACCELEROMETER') NULL DEFAULT NULL,
  `node_state` ENUM('DISCOVERED','OPERATIONAL','DELETED') NULL DEFAULT 'DISCOVERED',
  PRIMARY KEY (`node_id`),
  INDEX `fk_glam_node_glam_device1_idx` (`node_device_id` ASC),
  CONSTRAINT `fk_glam_node_glam_device1`
    FOREIGN KEY (`node_device_id`)
    REFERENCES `sog`.`glam_device` (`device_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_air_pressure` (
  `sensor_air_pressure_value` FLOAT(11) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_ambient_light_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_ambient_light_glam_node10`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_accelerometer` (
  `sensor_accelerometer_x_value` FLOAT(11) NULL DEFAULT NULL,
  `sensor_accelerometer_y_value` FLOAT(11) NULL DEFAULT NULL,
  `sensor_accelerometer_z_value` FLOAT(11) NULL DEFAULT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_ambient_light_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_ambient_light_glam_node100`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_alarm_log` (
  `alarm_log_id` INT(10) UNSIGNED NULL DEFAULT NULL AUTO_INCREMENT,
  `alarm_log_device_id` INT(11) NOT NULL,
  `alarm_log_node_id` INT(11) NULL DEFAULT NULL,
  `alarm_log_alarm_state` ENUM('ALARM','CLEARED') NOT NULL,
  `alarm_log_alarm_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `alarm_log_alarm_category` ENUM('DEVICE','NODE','OTHER','DEVICE_OFFLINE','BATTERY_LOW') NOT NULL,
  `alarm_log_access_point_id` CHAR(36) NULL DEFAULT NULL,
  PRIMARY KEY (`alarm_log_id`),
  INDEX `idxtime` (`alarm_log_alarm_time` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_sensor_gps` (
  `sensor_gps_location` POINT NULL,
  `glam_node_id` INT(11) NOT NULL,
  PRIMARY KEY (`glam_node_id`),
  INDEX `fk_glam_sensor_humidity_glam_node1_idx` (`glam_node_id` ASC),
  CONSTRAINT `fk_glam_sensor_humidity_glam_node10`
    FOREIGN KEY (`glam_node_id`)
    REFERENCES `sog`.`glam_node` (`node_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;


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