
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- alm_mgmt_host
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `alm_mgmt_host`;

CREATE TABLE `alm_mgmt_host`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `host_addr_sel` INTEGER NOT NULL,
    `host_addr_val` VARCHAR(255) NOT NULL,
    `local` TINYINT(1) NOT NULL,
    `interface_class` VARCHAR(255) DEFAULT '' NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `alm_mgmt_host_U_1` (`name`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- alm_evt_dscr
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `alm_evt_dscr`;

CREATE TABLE `alm_evt_dscr`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `evt_spec_sel` INTEGER NOT NULL,
    `evt_spec_val` VARCHAR(255) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `alm_evt_dscr_U_1` (`evt_spec_sel`, `evt_spec_val`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- alm_man_obj
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `alm_man_obj`;

CREATE TABLE `alm_man_obj`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `inst_addr_sel` INTEGER NOT NULL,
    `inst_addr_val` VARCHAR(255) NOT NULL,
    `amh_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `alm_man_obj_U_1` (`inst_addr_sel`, `inst_addr_val`),
    INDEX `alm_man_obj_FI_1` (`amh_id`),
    CONSTRAINT `alm_man_obj_FK_1`
        FOREIGN KEY (`amh_id`)
        REFERENCES `alm_mgmt_host` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- cur_alm_cond
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `cur_alm_cond`;

CREATE TABLE `cur_alm_cond`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `amo_id` INTEGER NOT NULL,
    `aed_id` INTEGER NOT NULL,
    `evt_time` VARCHAR(255) NOT NULL,
    `rec_date` VARCHAR(255) NOT NULL,
    `alm_stat` INTEGER NOT NULL,
    `serv_infl` INTEGER NOT NULL,
    `obs_val` INTEGER,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `cur_alm_cond_U_1` (`amo_id`, `aed_id`, `alm_stat`, `serv_infl`),
    INDEX `cur_alm_cond_FI_2` (`aed_id`),
    CONSTRAINT `cur_alm_cond_FK_1`
        FOREIGN KEY (`amo_id`)
        REFERENCES `alm_man_obj` (`id`),
    CONSTRAINT `cur_alm_cond_FK_2`
        FOREIGN KEY (`aed_id`)
        REFERENCES `alm_evt_dscr` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- asev_asgt_rule
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `asev_asgt_rule`;

CREATE TABLE `asev_asgt_rule`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `precedence` INTEGER NOT NULL,
    `sev_serv_indep` INTEGER NOT NULL,
    `sev_serv_unaff` INTEGER NOT NULL,
    `sev_serv_aff` INTEGER NOT NULL,
    `evt_sel_mode` INTEGER NOT NULL,
    `asgt_evt_sel` INTEGER DEFAULT 0 NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `asev_asgt_rule_U_1` (`name`),
    UNIQUE INDEX `asev_asgt_rule_U_2` (`precedence`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- asgt_evt_sel
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `asgt_evt_sel`;

CREATE TABLE `asgt_evt_sel`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `asar_id` INTEGER NOT NULL,
    `aed_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `asgt_evt_sel_U_1` (`asar_id`, `aed_id`),
    INDEX `asgt_evt_sel_FI_2` (`aed_id`),
    CONSTRAINT `asgt_evt_sel_FK_1`
        FOREIGN KEY (`asar_id`)
        REFERENCES `asev_asgt_rule` (`id`),
    CONSTRAINT `asgt_evt_sel_FK_2`
        FOREIGN KEY (`aed_id`)
        REFERENCES `alm_evt_dscr` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- thr_asev_asgt_rule
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `thr_asev_asgt_rule`;

CREATE TABLE `thr_asev_asgt_rule`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `aed_id` INTEGER NOT NULL,
    `cnt_val_clear` INTEGER NOT NULL,
    `cnt_val_warn` INTEGER NOT NULL,
    `cnt_val_min` INTEGER NOT NULL,
    `cnt_val_maj` INTEGER NOT NULL,
    `cnt_val_crit` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `thr_asev_asgt_rule_U_1` (`name`),
    UNIQUE INDEX `thr_asev_asgt_rule_U_2` (`aed_id`),
    CONSTRAINT `thr_asev_asgt_rule_FK_1`
        FOREIGN KEY (`aed_id`)
        REFERENCES `alm_evt_dscr` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- alm_bal_mon
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `alm_bal_mon`;

CREATE TABLE `alm_bal_mon`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `uncfd_asta_bal` INTEGER DEFAULT 0 NOT NULL,
    `cfd_asta_bal` INTEGER DEFAULT 0 NOT NULL,
    `evt_sel_mode` INTEGER NOT NULL,
    `bal_evt_sel` INTEGER DEFAULT 0 NOT NULL,
    `esc_host_sel` INTEGER DEFAULT 0 NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `alm_bal_mon_U_1` (`name`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- bal_evt_sel
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `bal_evt_sel`;

CREATE TABLE `bal_evt_sel`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `abm_id` INTEGER NOT NULL,
    `aed_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `bal_evt_sel_U_1` (`abm_id`, `aed_id`),
    INDEX `bal_evt_sel_FI_2` (`aed_id`),
    CONSTRAINT `bal_evt_sel_FK_1`
        FOREIGN KEY (`abm_id`)
        REFERENCES `alm_bal_mon` (`id`),
    CONSTRAINT `bal_evt_sel_FK_2`
        FOREIGN KEY (`aed_id`)
        REFERENCES `alm_evt_dscr` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- esc_host_sel
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `esc_host_sel`;

CREATE TABLE `esc_host_sel`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `abm_id` INTEGER NOT NULL,
    `amh_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `esc_host_sel_U_1` (`abm_id`, `amh_id`),
    INDEX `esc_host_sel_FI_2` (`amh_id`),
    CONSTRAINT `esc_host_sel_FK_1`
        FOREIGN KEY (`abm_id`)
        REFERENCES `alm_bal_mon` (`id`),
    CONSTRAINT `esc_host_sel_FK_2`
        FOREIGN KEY (`amh_id`)
        REFERENCES `alm_mgmt_host` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- alm_forw_inst
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `alm_forw_inst`;

CREATE TABLE `alm_forw_inst`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `mod` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `admin_state` INTEGER NOT NULL,
    `evt_sel_mode` INTEGER NOT NULL,
    `forw_evt_sel` INTEGER DEFAULT 0 NOT NULL,
    `dest_host_sel` INTEGER DEFAULT 0 NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `alm_forw_inst_U_1` (`name`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- forw_evt_sel
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `forw_evt_sel`;

CREATE TABLE `forw_evt_sel`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `afi_id` INTEGER NOT NULL,
    `aed_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `forw_evt_sel_U_1` (`afi_id`, `aed_id`),
    INDEX `forw_evt_sel_FI_2` (`aed_id`),
    CONSTRAINT `forw_evt_sel_FK_1`
        FOREIGN KEY (`afi_id`)
        REFERENCES `alm_forw_inst` (`id`),
    CONSTRAINT `forw_evt_sel_FK_2`
        FOREIGN KEY (`aed_id`)
        REFERENCES `alm_evt_dscr` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

-- ---------------------------------------------------------------------
-- dest_host_sel
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `dest_host_sel`;

CREATE TABLE `dest_host_sel`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `afi_id` INTEGER NOT NULL,
    `amh_id` INTEGER NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `dest_host_sel_U_1` (`afi_id`, `amh_id`),
    INDEX `dest_host_sel_FI_2` (`amh_id`),
    CONSTRAINT `dest_host_sel_FK_1`
        FOREIGN KEY (`afi_id`)
        REFERENCES `alm_forw_inst` (`id`),
    CONSTRAINT `dest_host_sel_FK_2`
        FOREIGN KEY (`amh_id`)
        REFERENCES `alm_mgmt_host` (`id`)
) ENGINE=InnoDB CHARACTER SET='utf8';

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
