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

-- ---------------------------------------------------------------------
-- user
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user`
(
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `name` TEXT NOT NULL,
    `login` VARCHAR(50) NOT NULL,
    `password` VARCHAR(50) NOT NULL,
    `email` TEXT NOT NULL,
    `phone` TEXT NOT NULL,
    `title` TEXT NOT NULL,
    `address` TEXT NOT NULL,
    `photo` TEXT NOT NULL,
    `active` TINYINT(1) NOT NULL,
    `superuser` TINYINT(1) NOT NULL,
    `last_login` DATETIME NOT NULL,
    `owner_id` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `user_I_1` (`login`),
    INDEX `user_FI_1` (`owner_id`),
    CONSTRAINT `user_FK_1`
        FOREIGN KEY (`owner_id`)
        REFERENCES `sysgroup` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- role
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `role`;

CREATE TABLE `role`
(
    `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
    `name` TEXT NOT NULL,
    `privileges` TEXT NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- role_grant_role
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `role_grant_role`;

CREATE TABLE `role_grant_role`
(
    `role_id` BIGINT(20) DEFAULT 0 NOT NULL,
    `grant_role_id` BIGINT(20) DEFAULT 0 NOT NULL,
    PRIMARY KEY (`role_id`,`grant_role_id`),
    INDEX `role_grant_role_FI_1` (`grant_role_id`),
    CONSTRAINT `role_grant_role_FK_1`
        FOREIGN KEY (`grant_role_id`)
        REFERENCES `role` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT `role_grant_role_FK_2`
        FOREIGN KEY (`role_id`)
        REFERENCES `role` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- user_role
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `user_role`;

CREATE TABLE `user_role`
(
    `role_id` BIGINT(20) DEFAULT 0 NOT NULL,
    `user_id` BIGINT(20) DEFAULT 0 NOT NULL,
    PRIMARY KEY (`role_id`,`user_id`),
    INDEX `user_role_FI_1` (`user_id`),
    CONSTRAINT `user_role_FK_1`
        FOREIGN KEY (`user_id`)
        REFERENCES `user` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT `user_role_FK_2`
        FOREIGN KEY (`role_id`)
        REFERENCES `role` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- sysgroup
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `sysgroup`;

CREATE TABLE `sysgroup`
(
    `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
    `name` TEXT NOT NULL,
    `lft` INTEGER(11) NOT NULL,
    `rgt` INTEGER(11) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- user_group
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `user_group`;

CREATE TABLE `user_group`
(
    `group_id` BIGINT(20) DEFAULT 0 NOT NULL,
    `user_id` BIGINT(20) DEFAULT 0 NOT NULL,
    PRIMARY KEY (`group_id`,`user_id`),
    INDEX `user_id` (`user_id`),
    CONSTRAINT `user_group_ibfk_1`
        FOREIGN KEY (`user_id`)
        REFERENCES `user` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT `user_group_ibfk_2`
        FOREIGN KEY (`group_id`)
        REFERENCES `sysgroup` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- user_diagram
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `user_diagram`;

CREATE TABLE `user_diagram`
(
    `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    `path` TEXT NOT NULL,
    `user_id` BIGINT(20),
    PRIMARY KEY (`id`),
    INDEX `user_diagram_FI_1` (`user_id`),
    CONSTRAINT `user_diagram_FK_1`
        FOREIGN KEY (`user_id`)
        REFERENCES `user` (`id`)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

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