
# 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;

-- ---------------------------------------------------------------------
-- beds24_product_info
-- ---------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS `beds24_product_info`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `room_id` INTEGER NOT NULL,
    `product_id` INTEGER NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`),
    INDEX `FI_beds24_pi_product_id` (`product_id`),
    CONSTRAINT `fk_beds24_pi_product_id`
        FOREIGN KEY (`product_id`)
        REFERENCES `product` (`id`)
        ON UPDATE RESTRICT
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- beds24_booking_info
-- ---------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS `beds24_booking_info`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `cart_item_id` INTEGER NOT NULL,
    `room_id` INTEGER NOT NULL,
    `start_date` DATE NOT NULL,
    `end_date` DATE NOT NULL,
    `adults` INTEGER(2) NOT NULL,
    `children` INTEGER(2) NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `FI_beds24_booking_info_cart_item` (`cart_item_id`),
    CONSTRAINT `fk_beds24_booking_info_cart_item`
        FOREIGN KEY (`cart_item_id`)
        REFERENCES `cart_item` (`id`)
        ON UPDATE RESTRICT
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- beds24_booking_order_product
-- ---------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS `beds24_booking_order_product`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `order_id` INTEGER NOT NULL,
    `order_product_id` INTEGER NOT NULL,
    `room_id` INTEGER NOT NULL,
    `start_date` DATE NOT NULL,
    `end_date` DATE NOT NULL,
    `adults` INTEGER(2) NOT NULL,
    `children` INTEGER(2) NOT NULL,
    `beds_24_booking_id` VARCHAR(128) NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `FI_beds24_booking_order_product` (`order_product_id`),
    INDEX `FI_beds24_booking_order_product_order` (`order_id`),
    CONSTRAINT `fk_beds24_booking_order_product`
        FOREIGN KEY (`order_product_id`)
        REFERENCES `order_product` (`id`)
        ON UPDATE RESTRICT
        ON DELETE CASCADE,
    CONSTRAINT `fk_beds24_booking_order_product_order`
        FOREIGN KEY (`order_id`)
        REFERENCES `order` (`id`)
        ON UPDATE RESTRICT
        ON DELETE CASCADE
) ENGINE=InnoDB;

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