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

-- ---------------------------------------------------------------------
-- lps_area
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `lps_area`;

CREATE TABLE `lps_area`
(
    `id` INTEGER NOT NULL,
    `title` VARCHAR(50) NOT NULL,
    `active` TINYINT DEFAULT 1 NOT NULL,
    `price` INTEGER DEFAULT 0 NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- lps_area_city
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `lps_area_city`;

CREATE TABLE `lps_area_city`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `id_area` INTEGER NOT NULL,
    `zipcode` VARCHAR(10) NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`),
    INDEX `fi_area_area_city` (`id_area`),
    CONSTRAINT `fk_area_area_city`
        FOREIGN KEY (`id_area`)
        REFERENCES `lps_area` (`id`)
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- lps_area_schedule
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `lps_area_schedule`;

CREATE TABLE `lps_area_schedule`
(
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `id_area` INTEGER NOT NULL,
    `day` INTEGER NOT NULL,
    `begin_time` TIME NOT NULL,
    `end_time` TIME NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`),
    INDEX `fi_area_area_schedule` (`id_area`),
    CONSTRAINT `fk_area_area_schedule`
        FOREIGN KEY (`id_area`)
        REFERENCES `lps_area` (`id`)
        ON DELETE CASCADE
) ENGINE=InnoDB;

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