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,ALLOW_INVALID_DATES';

ALTER TABLE `sog`.`glam_notification` ADD COLUMN `notification_colour` INT(11) NULL DEFAULT NULL  AFTER `severity_id` ;

ALTER TABLE `sog`.`glam_srm_stakeholder` ADD COLUMN `tag_id` INT(10) UNSIGNED NULL DEFAULT NULL  AFTER `sh_share_system_status` , 
  ADD CONSTRAINT `fk_glam_srm_stakeholder_glam_tag1`
  FOREIGN KEY (`tag_id` )
  REFERENCES `sog`.`glam_tag` (`tag_id` )
  ON DELETE SET NULL
  ON UPDATE CASCADE
, ADD INDEX `fk_glam_srm_stakeholder_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_system_details` ADD COLUMN `system_allow_no_start_date` BIT(1) NULL DEFAULT b'0'  AFTER `system_home_coordinates` , ADD COLUMN `system_allow_no_end_date` BIT(1) NULL DEFAULT b'0'  AFTER `system_allow_no_start_date` , ADD COLUMN `system_allow_no_coordinates` BIT(1) NULL DEFAULT b'0'  AFTER `system_allow_no_end_date` , ADD COLUMN `system_srm_enabled` BIT(1) NULL DEFAULT b'0'  AFTER `system_allow_no_coordinates` , ADD COLUMN `system_limit_keys` BIT(1) NULL DEFAULT b'0'  AFTER `system_srm_enabled` , ADD COLUMN `system_lid_tracking_enabled` BIT(1) NULL DEFAULT b'0'  AFTER `system_limit_keys` , ADD COLUMN `system_acl_enabled` BIT(1) NULL DEFAULT b'0'  AFTER `system_lid_tracking_enabled` , ADD COLUMN `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP  AFTER `system_acl_enabled` , ADD COLUMN `last_changed_by` CHAR(36) NULL DEFAULT NULL  AFTER `last_changed_on` , ADD COLUMN `system_main_colour` INT(11) NULL DEFAULT 0x2E85F1  AFTER `last_changed_by` , ADD COLUMN `system_sub_colour` INT(11) NULL DEFAULT 0x79CDCD  AFTER `system_main_colour` ;

ALTER TABLE `sog`.`glam_access_object_key` ADD COLUMN `key_allow_dynamic_transactions` BIT(1) NULL DEFAULT b'0'  AFTER `key_transaction_limit` ;

ALTER TABLE `sog`.`glam_his_access_object_key` ADD COLUMN `key_allow_dynamic_transactions` BIT(1) NULL DEFAULT b'0'  AFTER `key_transaction_limit` ;

ALTER TABLE `sog`.`glam_supervisor_schedule` CHANGE COLUMN `transaction_location` `transaction_location` POINT NULL  ;

ALTER TABLE `sog`.`glam_point` CHANGE COLUMN `point_location` `point_location` POINT NULL  ;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_dynamic_schedule` (
  `dyn_schedule_id` CHAR(36) NOT NULL ,
  `dyn_point_id` CHAR(36) NULL DEFAULT NULL ,
  `dyn_domain_id` INT(10) UNSIGNED NULL DEFAULT NULL ,
  `dyn_oh_tag_id` INT(10) UNSIGNED NULL DEFAULT NULL ,
  `dyn_ap_tag_id` INT(10) UNSIGNED NULL DEFAULT NULL ,
  `dyn_schedule_operation` ENUM('ACCESS','ACCESS_COMMISSION') NULL DEFAULT 'ACCESS' ,
  `dyn_transaction_start` INT(11) NULL DEFAULT NULL ,
  `dyn_transaction_end` INT(11) NULL DEFAULT NULL ,
  `dyn_transaction_radius` FLOAT(11) NULL DEFAULT NULL ,
  `dyn_date_recursion` VARCHAR(128) NULL DEFAULT NULL ,
  `dyn_transaction_location` POINT NULL DEFAULT NULL ,
  `dyn_domain_perspective` ENUM('AP','OH','BOTH') NULL DEFAULT 'AP' ,
  `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  `dyn_schedule_description` VARCHAR(64) NULL DEFAULT NULL ,
  `dyn_is_suspended` BIT(1) NULL DEFAULT b'0' ,
  PRIMARY KEY (`dyn_schedule_id`) ,
  INDEX `fk_glam_dynamic_schedule_glam_point1_idx` (`dyn_point_id` ASC) ,
  INDEX `fk_glam_dynamic_schedule_glam_domain1_idx` (`dyn_domain_id` ASC) ,
  INDEX `fk_glam_dynamic_schedule_glam_tag1_idx` (`dyn_oh_tag_id` ASC) ,
  INDEX `fk_glam_dynamic_schedule_glam_tag2_idx` (`dyn_ap_tag_id` ASC) ,
  INDEX `fk_glam_dynamic_schedule_glam_start_date_idx` (`dyn_transaction_start` ASC) ,
  INDEX `fk_glam_dynamic_schedule_glam_end_date_idx` (`dyn_transaction_end` ASC) ,
  CONSTRAINT `fk_glam_dynamic_schedule_glam_point1`
    FOREIGN KEY (`dyn_point_id` )
    REFERENCES `sog`.`glam_point` (`point_id` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_dynamic_schedule_glam_domain1`
    FOREIGN KEY (`dyn_domain_id` )
    REFERENCES `sog`.`glam_domain` (`domain_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_dynamic_schedule_glam_tag1`
    FOREIGN KEY (`dyn_oh_tag_id` )
    REFERENCES `sog`.`glam_tag` (`tag_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_dynamic_schedule_glam_tag2`
    FOREIGN KEY (`dyn_ap_tag_id` )
    REFERENCES `sog`.`glam_tag` (`tag_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_report` (
  `report_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `report_name` VARCHAR(64) NOT NULL ,
  `report_description` VARCHAR(256) NULL DEFAULT NULL ,
  `report_url` VARCHAR(256) NULL DEFAULT 'report/glam/lock_status.rptdesign' ,
  `report_params` VARCHAR(1024) NULL DEFAULT NULL ,
  PRIMARY KEY (`report_id`) ,
  UNIQUE INDEX `report_name_UNIQUE` (`report_name` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_viewable_report` (
  `glam_report_report_id` INT(11) NOT NULL ,
  `app_role_id` CHAR(36) NULL DEFAULT NULL ,
  PRIMARY KEY (`glam_report_report_id`) ,
  INDEX `fk_krf_application_role_idx` (`app_role_id` ASC) ,
  CONSTRAINT `fk_glam_viewable_report_glam_report1`
    FOREIGN KEY (`glam_report_report_id` )
    REFERENCES `sog`.`glam_report` (`report_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_krf_application_role`
    FOREIGN KEY (`app_role_id` )
    REFERENCES `sog`.`krf_application_role` (`app_role_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_report_mailer` (
  `mailer_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `report_id` INT(11) NULL DEFAULT NULL ,
  `user_id` CHAR(36) NULL DEFAULT NULL ,
  `rm_start_datetime` INT(11) NULL DEFAULT NULL ,
  `rm_end_datetime` INT(11) NULL DEFAULT NULL ,
  `rm_params` VARCHAR(1024) NULL DEFAULT NULL ,
  `rm_mail_at` VARCHAR(128) NULL DEFAULT NULL ,
  PRIMARY KEY (`mailer_id`) ,
  INDEX `fk_report_mailer_report_idx` (`report_id` ASC) ,
  INDEX `fk_report_mailer_user_idx` (`user_id` ASC) ,
  CONSTRAINT `fk_report_mailer_report`
    FOREIGN KEY (`report_id` )
    REFERENCES `sog`.`glam_report` (`report_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_report_mailer_user`
    FOREIGN KEY (`user_id` )
    REFERENCES `sog`.`glam_systemuser` (`user_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_dynamic_schedule` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
  `dyn_schedule_id` CHAR(36) NULL DEFAULT NULL ,
  `dyn_point_id` CHAR(36) NULL DEFAULT NULL ,
  `dyn_domain_id` INT(11) NULL DEFAULT NULL ,
  `dyn_oh_tag_id` INT(11) NULL DEFAULT NULL ,
  `dyn_ap_tag_id` INT(11) NULL DEFAULT NULL ,
  `dyn_schedule_operation` ENUM('ACCESS','ACCESS_COMMISSION') NULL DEFAULT 'ACCESS' ,
  `dyn_transaction_start` INT(11) NULL DEFAULT NULL ,
  `dyn_transaction_end` INT(11) NULL DEFAULT NULL ,
  `dyn_transaction_radius` FLOAT(11) NULL DEFAULT NULL ,
  `dyn_date_recursion` VARCHAR(128) NULL DEFAULT NULL ,
  `dyn_transaction_location` POINT NULL DEFAULT NULL ,
  `dyn_domain_perspective` ENUM('AP','OH','BOTH') NULL DEFAULT 'AP' ,
  `his_action` ENUM('ADDED','UPDATED','DELETED','SUSPEND','UNSUSPEND') NULL DEFAULT 'ADDED' ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `dyn_schedule_description` VARCHAR(64) NULL DEFAULT NULL ,
  `dyn_is_suspended` BIT(1) NULL DEFAULT b'0' ,
  PRIMARY KEY (`his_id`) ,
  INDEX `original_pk` (`dyn_schedule_id` ASC) ,
  INDEX `his_action` (`his_action` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_dyn_sched_oh` (
  `dyn_schedule_id` CHAR(36) NOT NULL ,
  `user_id` CHAR(36) NOT NULL ,
  `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  PRIMARY KEY (`dyn_schedule_id`, `user_id`) ,
  INDEX `fk_glam_dyn_sched_oh_glam_objholder1_idx` (`user_id` ASC) ,
  CONSTRAINT `fk_glam_dyn_sched_oh_glam_dynamic_schedule1`
    FOREIGN KEY (`dyn_schedule_id` )
    REFERENCES `sog`.`glam_dynamic_schedule` (`dyn_schedule_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_dyn_sched_oh_glam_objholder1`
    FOREIGN KEY (`user_id` )
    REFERENCES `sog`.`glam_objholder` (`user_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_dyn_sched_ap` (
  `dyn_schedule_id` CHAR(36) NOT NULL ,
  `access_point_id` CHAR(36) NOT NULL ,
  `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  PRIMARY KEY (`dyn_schedule_id`, `access_point_id`) ,
  INDEX `fk_glam_dyn_sched_ap_glam_access_point1_idx` (`access_point_id` ASC) ,
  CONSTRAINT `fk_glam_dyn_sched_ap_glam_dynamic_schedule1`
    FOREIGN KEY (`dyn_schedule_id` )
    REFERENCES `sog`.`glam_dynamic_schedule` (`dyn_schedule_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_dyn_sched_ap_glam_access_point1`
    FOREIGN KEY (`access_point_id` )
    REFERENCES `sog`.`glam_access_point` (`access_point_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_dyn_sched_oh` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
  `dyn_schedule_id` CHAR(36) NULL DEFAULT NULL ,
  `user_id` 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 ,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT 'ADDED' ,
  PRIMARY KEY (`his_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_dyn_sched_ap` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
  `dyn_schedule_id` CHAR(36) NULL DEFAULT NULL ,
  `access_point_id` 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 ,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT 'ADDED' ,
  PRIMARY KEY (`his_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

INSERT INTO `sog`.`glam_notification` (notification_id,notification_desc) values (37,'Possible spoofing detected.');


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