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_domain` 
ADD INDEX `fk_glam_domain_glam_domain1_idx` (`parent_domain_id` ASC) 
, DROP INDEX `fk_glam_domain_glam_domain1` ;

ALTER TABLE `sog`.`glam_domain_objholder` 
ADD INDEX `fk_glam_batch_keyholder_glam_batch1_idx` (`domain_id` ASC) ;

ALTER TABLE `sog`.`glam_domain_access_point` 
ADD INDEX `fk_glam_domain_access_point_glam_access_point1_idx` (`access_point_id` ASC) 
, ADD INDEX `fk_bl_batch_idx` (`domain_id` ASC) 
, DROP INDEX `fk_glam_domain_access_point_glam_access_point1` ;

ALTER TABLE `sog`.`glam_domain_systemuser` 
ADD INDEX `fk_glam_domain_systemuser_glam_domain1_idx` (`domain_id` ASC) 
, ADD INDEX `fk_glam_domain_systemuser_glam_systemuser1_idx` (`user_id` ASC) 
, DROP INDEX `fk_glam_domain_systemuser_glam_systemuser1` 
, DROP INDEX `fk_glam_domain_systemuser_glam_domain1` ;

ALTER TABLE `sog`.`glam_srm_stakeholder_access_object` 
ADD INDEX `fk_glam_srm_stakeholder_key_glam_srm_stakeholder1_idx` (`glam_srm_sh_id` ASC) ;

ALTER TABLE `sog`.`glam_srm_stakeholder_access_point` 
ADD INDEX `fk_glam_srm_stakeholder_lock_glam_srm_stakeholder1_idx` (`glam_srm_sh_id` ASC) ;

ALTER TABLE `sog`.`glam_system_details` 
ADD INDEX `fk_glam_system_details_glam_apn1_idx` (`default_apn_id` ASC) 
, DROP INDEX `fk_glam_system_details_glam_apn1` ;

ALTER TABLE `sog`.`glam_tag_object_key` 
ADD INDEX `fk_glam_tag_object_key_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_tag_access_point` 
ADD INDEX `fk_glam_tag_access_point_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_tag_transaction` 
ADD INDEX `fk_glam_tag_transaction_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_tag_objholder` 
ADD INDEX `fk_glam_tag_objholder_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_tag_transaction_schedule` 
ADD INDEX `fk_glam_tag_transaction_schedule_glam_transaction_schedule1_idx` (`access_point_id` ASC, `transaction_id` ASC, `user_id` ASC) 
, ADD INDEX `fk_glam_tag_transaction_schedule_glam_tag1_idx` (`tag_id` ASC) 
, DROP INDEX `fk_glam_tag_transaction_schedule_glam_transaction_schedule1` ;

ALTER TABLE `sog`.`glam_tag_domain` 
ADD INDEX `fk_glam_tag_domain_glam_tag1_idx` (`tag_id` ASC) ;

ALTER TABLE `sog`.`glam_tag_domain_access_point` 
ADD INDEX `fk_glam_tag_domain_access_point_glam_domain_access_point1_idx` (`domain_ap_domain_id` ASC, `domain_ap_access_point_id` ASC) 
, ADD INDEX `fk_glam_tag_domain_access_point_glam_tag1_idx` (`tag_id` ASC) 
, DROP INDEX `fk_glam_tag_domain_access_point_glam_domain_access_point1` ;

ALTER TABLE `sog`.`glam_tag_domain_objholder` 
ADD INDEX `fk_glam_tag_domain_objholder_glam_domain_objholder1_idx` (`domain_objholder_domain_id` ASC, `domain_objholder_user_id` ASC) 
, ADD INDEX `fk_glam_tag_domain_objholder_glam_tag1_idx` (`tag_id` ASC) 
, DROP INDEX `fk_glam_tag_domain_objholder_glam_domain_objholder1` ;

ALTER TABLE `sog`.`krf_application_role` 
ADD INDEX `fk_krf_application_role_krf_application1_idx` (`app_id` ASC) 
, DROP INDEX `fk_krf_application_role_krf_application1` ;

ALTER TABLE `sog`.`krf_user_application` 
ADD INDEX `fk_krf_user_application_krf_user1_idx` (`usr_id` ASC) 
, ADD INDEX `fk_krf_user_application_krf_application_role1_idx` (`app_role_id` ASC) 
, ADD INDEX `fk_krf_user_application_krf_application1_idx` (`app_id` ASC) 
, DROP INDEX `fk_krf_user_application_krf_application_role1` 
, DROP INDEX `fk_krf_user_application_krf_user1` ;

ALTER TABLE `sog`.`krf_user_role` 
ADD INDEX `fk_krf_user_role_krf_user1_idx` (`usr_id` ASC) 
, ADD INDEX `fk_krf_user_role_krf_role1_idx` (`role_id` ASC) 
, DROP INDEX `fk_krf_user_role_krf_user1` ;

ALTER TABLE `sog`.`krf_user_settings` 
ADD INDEX `fk_krf_user_settings_krf_user1_idx` (`usr_id` ASC) 
, DROP INDEX `fk_krf_user_settings_krf_user1` ;

ALTER TABLE `sog`.`glam_keyholder_supervisor` 
ADD INDEX `fk_glam_keyholder_supervisor_glam_keyholder_group1_idx` (`group_id` ASC) 
, ADD INDEX `fk_glam_keyholder_supervisor_glam_objholder1_idx` (`user_id` ASC) 
, DROP INDEX `fk_glam_keyholder_supervisor_glam_keyholder_group1` ;

ALTER TABLE `sog`.`glam_access_object_image` 
ADD INDEX `fk_glam_access_object_image_glam_system_details1_idx` (`system_id` ASC) 
, DROP INDEX `fk_glam_access_object_image_glam_system_details1` ;

ALTER TABLE `sog`.`glam_access_object_firmware` 
ADD INDEX `fk_glam_access_object_firmware_glam_system_details1_idx` (`system_id` ASC) 
, DROP INDEX `fk_glam_access_object_firmware_glam_system_details1` ;

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

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_objholder` (
  `usr_id` CHAR(36) NOT NULL ,
  `usr_username` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_password` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_firstname` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_lastname` VARCHAR(128) NULL DEFAULT NULL ,
  `usr_contact_cell_no` VARCHAR(16) NULL DEFAULT NULL ,
  `usr_contact_work_no` VARCHAR(16) NULL DEFAULT NULL ,
  `usr_contact_email` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_employee_no` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_is_active` BIT(1) NULL DEFAULT b'1' ,
  `usr_system_role` VARCHAR(64) NULL DEFAULT 'SYS_USER' ,
  `usr_last_login` DATETIME NULL DEFAULT NULL ,
  `usr_api_key` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_timezone_id` VARCHAR(64) NULL DEFAULT NULL ,
  `is_deleted` BIT(1) NULL DEFAULT b'0' ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `version` INT(11) NULL DEFAULT NULL ,
  `objholder_active` BIT(1) NULL DEFAULT b'1' ,
  `objholder_is_owner` BIT(1) NULL DEFAULT b'1' ,
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
  `his_action` ENUM('ADDED','UPDATED','DELETED','ASSIGN_SUPERVISOR','DEASSIGN_SUPERVISOR') NULL DEFAULT NULL ,
  `is_supervisor` BIT(1) NULL DEFAULT b'0' ,
  PRIMARY KEY (`his_id`) ,
  INDEX `is_deleted` (`is_deleted` ASC) ,
  INDEX `idx_firstname` (`usr_firstname` ASC) ,
  INDEX `idx_lastname` (`usr_lastname` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_systemuser` (
  `usr_id` CHAR(36) NOT NULL ,
  `usr_username` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_password` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_firstname` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_lastname` VARCHAR(128) NULL DEFAULT NULL ,
  `usr_contact_cell_no` VARCHAR(16) NULL DEFAULT NULL ,
  `usr_contact_work_no` VARCHAR(16) NULL DEFAULT NULL ,
  `usr_contact_email` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_employee_no` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_is_active` BIT(1) NULL DEFAULT b'1' ,
  `usr_system_role` VARCHAR(64) NULL DEFAULT 'SYS_USER' ,
  `usr_last_login` DATETIME NULL DEFAULT NULL ,
  `usr_api_key` VARCHAR(64) NULL DEFAULT NULL ,
  `usr_timezone_id` VARCHAR(64) NULL DEFAULT NULL ,
  `is_deleted` BIT(1) NULL DEFAULT b'0' ,
  `last_changed_by` CHAR(36) NULL DEFAULT NULL ,
  `last_changed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  `version` INT(11) NULL DEFAULT NULL ,
  `his_id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
  `his_action` ENUM('ADDED','UPDATED','DELETED') NULL DEFAULT NULL ,
  INDEX `is_deleted` (`is_deleted` ASC) ,
  INDEX `idx_firstname` (`usr_firstname` ASC) ,
  INDEX `idx_lastname` (`usr_lastname` ASC) ,
  PRIMARY KEY (`his_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `sog`.`glam_his_systemuser_role` (
  `his_id` BIGINT(20) NOT NULL ,
  `role_id` CHAR(36) NOT NULL ,
  PRIMARY KEY (`his_id`, `role_id`) )
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;
