USE `vn`;

CREATE TABLE `vn`.`zoneWarehouse` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `zoneFk` int(11) NOT NULL,
  `warehouseFk` smallint(6) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `zoneFk` (`zoneFk`,`warehouseFk`),
  KEY `warehouseFk` (`warehouseFk`),
  CONSTRAINT `zoneWarehouse_ibfk_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE CASCADE ON UPDATE CASCADE/*,
CONSTRAINT `zoneWarehouse_ibfk_2` FOREIGN KEY (`warehouseFk`) REFERENCES `vn`.`warehouse` (`id`)  ON DELETE CASCADE ON UPDATE CASCADE*/
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

CREATE TABLE `vn`.`zoneEvent` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zoneFk` int(11) NOT NULL,
  `from` date DEFAULT NULL,
  `to` date DEFAULT NULL,
  `weekDays` set('mon','tue','wed','thu','fri','sat','sun') NOT NULL,
  `hour` datetime DEFAULT NULL,
  `travelingDays` int(11) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `bonus` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `zoneFk` (`zoneFk`),
  CONSTRAINT `zoneEvent_ibfk_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;

ALTER TABLE `vn`.`zoneEvent` 
  ADD COLUMN `type` ENUM('day', 'indefinitely', 'range') NOT NULL AFTER `zoneFk`,
  ADD COLUMN `dated` DATE NULL DEFAULT NULL AFTER `type`,
  CHANGE COLUMN `from` `started` DATE NULL DEFAULT NULL ,
  CHANGE COLUMN `to` `ended` DATE NULL DEFAULT NULL ;

CREATE TABLE `vn`.`zoneExclusion` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zoneFk` int(11) NOT NULL,
  `day` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `zoneFk_2` (`zoneFk`,`day`),
  KEY `zoneFk` (`zoneFk`),
  CONSTRAINT `zoneExclusion_ibfk_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
ALTER TABLE `vn`.`zone` 
DROP FOREIGN KEY `fk_zone_1`;
ALTER TABLE `vn`.`zone` 
CHANGE COLUMN `warehouseFk` `warehouseFk` SMALLINT(6) UNSIGNED NULL DEFAULT NULL ;
ALTER TABLE `vn`.`zone` 
ADD CONSTRAINT `fk_zone_1`
  FOREIGN KEY (`warehouseFk`)
  REFERENCES `vn`.`warehouse` (`id`)
  ON DELETE NO ACTION
  ON UPDATE CASCADE;

ALTER TABLE `vn`.`zoneExclusion` 
  CHANGE COLUMN `day` `dated` DATE NOT NULL ;
