-- MySQL Script generated by MySQL Workbench
-- Fri 20 Feb 2015 05:52:59 PM EET
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering


-- -----------------------------------------------------
-- backup nodeLinkT data
-- -----------------------------------------------------

/* run once when setting up new structure
start transaction;

create table __nodeLinkBackupT
select 
  nodeLinkTypeId,
  srcNodeId,
  dstNodeId,
  metric,
  added
from nodeLinkT
where metric < 2;

commit;
*/

-- -----------------------------------------------------
-- Table `nodeLinkT`
-- -----------------------------------------------------
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='STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES';

DROP TABLE IF EXISTS `nodeLinkT` ;

CREATE TABLE IF NOT EXISTS `nodeLinkT` (
  `_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `nodeLinkTypeId` INT UNSIGNED NOT NULL,
  `srcNodeId` INT UNSIGNED NOT NULL,
  `dstNodeId` INT UNSIGNED NOT NULL,
  `metric` INT UNSIGNED NOT NULL DEFAULT 1,
  `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`_id`),
  UNIQUE INDEX `nodelinktypeid_srcnodeid_dstnodeid_uq` (`nodeLinkTypeId` ASC, `srcNodeId` ASC, `dstNodeId` ASC),
  INDEX `nodelinktypeid_dstnodeid_srcnodeid_idx` (`nodeLinkTypeId` ASC, `dstNodeId` ASC, `srcNodeId` ASC),
  INDEX `nodelinktypeid_srcnodeid_metric_idx` (`nodeLinkTypeId` ASC, `srcNodeId` ASC, `metric` ASC),
  INDEX `nodelinktypeid_dstnodeid_metric_idx` (`nodeLinkTypeId` ASC, `dstNodeId` ASC, `metric` ASC),
  INDEX `fk_nodeLinkT_nodeT1_idx` (`srcNodeId` ASC),
  INDEX `fk_nodeLinkT_nodeT2_idx` (`dstNodeId` ASC),
  CONSTRAINT `fk_nodeLinkT_nodeLinkTypeT1`
    FOREIGN KEY (`nodeLinkTypeId`)
    REFERENCES `nodeLinkTypeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_nodeLinkT_nodeT1`
    FOREIGN KEY (`srcNodeId`)
    REFERENCES `nodeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_nodeLinkT_nodeT2`
    FOREIGN KEY (`dstNodeId`)
    REFERENCES `nodeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- restore nodeLinkT data
-- -----------------------------------------------------

start transaction;

insert nodeLinkT(nodeLinkTypeId, srcNodeId, dstNodeId, metric, added)
select 
  nodeLinkTypeId,
  srcNodeId,
  dstNodeId,
  metric,
  added
from __nodeLinkBackupT;

select __nodeLink_RefreshExtraLinks();

commit;

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