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_ext_exam_center` 
ADD COLUMN `his_id` BIGINT(20) NULL DEFAULT NULL AFTER `is_deleted`;

ALTER TABLE `sog`.`glam_ext_exam_schedule` 
ADD COLUMN `his_id` BIGINT(20) NULL DEFAULT NULL AFTER `last_changed_on`;

ALTER TABLE `sog`.`glam_ext_exam_timetable` 
ADD COLUMN `his_id` BIGINT(20) NULL DEFAULT NULL AFTER `is_deleted`;

CREATE TABLE IF NOT EXISTS `sog`.`glam_his_point` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `point_id` CHAR(36) NOT NULL,
  `point_desc` VARCHAR(255) NULL DEFAULT NULL,
  `point_radius` FLOAT(11) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `location_required` BIT(1) NULL DEFAULT NULL,
  `grace_period_start` INT(11) NULL DEFAULT NULL,
  `grace_period_end` INT(11) NULL DEFAULT NULL,
  `gsm_coverage` ENUM('UNKNOWN','POOR','GOOD') NULL DEFAULT NULL,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL,
  PRIMARY KEY (`his_id`),
  INDEX `idx_point_id` (`point_id` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC),
  INDEX `idx_his_action` (`his_action` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_his_ext_exam_center` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `point_id` CHAR(36) NOT NULL,
  `center_address` VARCHAR(256) NULL DEFAULT NULL,
  `center_tel` VARCHAR(16) NULL DEFAULT NULL,
  `center_abbreviation` VARCHAR(8) NULL DEFAULT NULL,
  `center_timezone` VARCHAR(128) NULL DEFAULT NULL,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL,
  `point_his_id` BIGINT(20) NOT NULL,
  PRIMARY KEY (`his_id`),
  INDEX `fk_glam_his_ext_exam_center_glam_his_point1_idx` (`point_his_id` ASC),
  INDEX `idx_point_id` (`point_id` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC),
  CONSTRAINT `fk_glam_his_ext_exam_center_glam_his_point1`
    FOREIGN KEY (`point_his_id`)
    REFERENCES `sog`.`glam_his_point` (`his_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_ext_exam_timetable` (
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `table_id` CHAR(36) NOT NULL,
  `table_start_time` INT(11) NULL DEFAULT NULL,
  `table_end_time` INT(11) NULL DEFAULT NULL,
  `table_subject` VARCHAR(128) NULL DEFAULT NULL,
  `table_abbreviation` VARCHAR(8) NULL DEFAULT NULL,
  `table_group_id` CHAR(36) NULL DEFAULT NULL,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL,
  PRIMARY KEY (`his_id`),
  INDEX `idx_start_end_time` (`table_start_time` ASC, `table_end_time` ASC),
  INDEX `idx_table_id` (`table_id` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE TABLE IF NOT EXISTS `sog`.`glam_his_ext_exam_schedule` (
  `his_id` INT(11) NOT NULL AUTO_INCREMENT,
  `exam_sched_id` INT(11) NOT NULL,
  `exam_center_his_id` BIGINT(20) NOT NULL,
  `timetable_his_id` BIGINT(20) NOT NULL,
  `sched_comment` VARCHAR(256) NULL DEFAULT NULL,
  `objholder_id` CHAR(36) NULL DEFAULT NULL,
  `sched_status` ENUM('UNKNOWN','PENDING','READY','CREATED','INVALID_OH','INVALID_AP','DELETED') NULL DEFAULT NULL,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL,
  PRIMARY KEY (`his_id`),
  INDEX `fk_glam_his_ext_exam_schedule_glam_his_ext_exam_center1_idx` (`exam_center_his_id` ASC),
  INDEX `fk_glam_his_ext_exam_schedule_glam_his_ext_exam_timetable1_idx` (`timetable_his_id` ASC),
  INDEX `idx_his_action` (`his_action` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC),
  CONSTRAINT `fk_glam_his_ext_exam_schedule_glam_his_ext_exam_center1`
    FOREIGN KEY (`exam_center_his_id`)
    REFERENCES `sog`.`glam_his_ext_exam_center` (`his_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_glam_his_ext_exam_schedule_glam_his_ext_exam_timetable1`
    FOREIGN KEY (`timetable_his_id`)
    REFERENCES `sog`.`glam_his_ext_exam_timetable` (`his_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_ext_exam_schedule_ap` (
  `exam_schedule_his_id` INT(11) NOT NULL,
  `access_point_id` CHAR(36) NOT 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 NULL,
  INDEX `fk_glam_his_ext_exam_schedule_ap_glam_his_ext_exam_schedule_idx` (`exam_schedule_his_id` ASC),
  PRIMARY KEY (`exam_schedule_his_id`),
  INDEX `idx_access_point` (`access_point_id` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC),
  CONSTRAINT `fk_glam_his_ext_exam_schedule_ap_glam_his_ext_exam_schedule1`
    FOREIGN KEY (`exam_schedule_his_id`)
    REFERENCES `sog`.`glam_his_ext_exam_schedule` (`his_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_ext_exam_schedule_oh` (
  `exam_schedule_his_id` INT(11) NOT NULL,
  `objholder_id` CHAR(36) NOT NULL,
  `last_changed_on` TIMESTAMP NULL DEFAULT NULL,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL,
  INDEX `fk_glam_his_ext_exam_schedule_oh_glam_his_ext_exam_schedule_idx` (`exam_schedule_his_id` ASC),
  PRIMARY KEY (`exam_schedule_his_id`),
  INDEX `idx_objholder_id` (`objholder_id` ASC),
  INDEX `idx_last_changed_on` (`last_changed_on` ASC),
  CONSTRAINT `fk_glam_his_ext_exam_schedule_oh_glam_his_ext_exam_schedule1`
    FOREIGN KEY (`exam_schedule_his_id`)
    REFERENCES `sog`.`glam_his_ext_exam_schedule` (`his_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;
