-- MySQL Script generated by MySQL Workbench
-- Sat 12 May 2018 11:56:41 PM MDT
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

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='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema sitewriter
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema sitewriter
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `sitewriter` DEFAULT CHARACTER SET utf8 ;
USE `sitewriter` ;

-- -----------------------------------------------------
-- Table `sitewriter`.`sites`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`sites` (
  `siteid` INT NOT NULL AUTO_INCREMENT,
  `sitename` VARCHAR(255) NOT NULL DEFAULT '',
  `url` VARCHAR(255) NOT NULL DEFAULT '',
  `theme` VARCHAR(255) NOT NULL DEFAULT '',
  `color` VARCHAR(255) NULL,
  PRIMARY KEY (`siteid`),
  UNIQUE INDEX `siteid_UNIQUE` (`siteid` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`pages`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`pages` (
  `pageid` INT NOT NULL AUTO_INCREMENT,
  `slug` VARCHAR(255) NOT NULL,
  `siteid` INT NOT NULL,
  `title` VARCHAR(255) NOT NULL DEFAULT '',
  `parentid` INT NULL,
  `nav` VARCHAR(255) NULL DEFAULT NULL,
  `template` VARCHAR(255) NOT NULL DEFAULT 'default',
  `navorder` INT NULL,
  PRIMARY KEY (`pageid`, `slug`, `siteid`),
  UNIQUE INDEX `pageid_UNIQUE` (`pageid` ASC),
  INDEX `fk_pages_pages1_idx` (`parentid` ASC),
  INDEX `fk_pages_sites1_idx` (`siteid` ASC),
  CONSTRAINT `fk_pages_pages1`
    FOREIGN KEY (`parentid`)
    REFERENCES `sitewriter`.`pages` (`pageid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_pages_sites1`
    FOREIGN KEY (`siteid`)
    REFERENCES `sitewriter`.`sites` (`siteid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`components`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`components` (
  `compid` INT NOT NULL AUTO_INCREMENT,
  `pageid` INT NOT NULL,
  `name` VARCHAR(255) NOT NULL DEFAULT '',
  `content` LONGTEXT NULL,
  PRIMARY KEY (`compid`, `pageid`),
  UNIQUE INDEX `compid_UNIQUE` (`compid` ASC),
  INDEX `fk_components_pages_idx` (`pageid` ASC),
  CONSTRAINT `fk_components_pages`
    FOREIGN KEY (`pageid`)
    REFERENCES `sitewriter`.`pages` (`pageid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`settings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`settings` (
  `siteid` INT NOT NULL,
  `key` VARCHAR(255) NOT NULL,
  `value` TEXT NOT NULL,
  PRIMARY KEY (`siteid`, `key`),
  CONSTRAINT `fk_settings_sites1`
    FOREIGN KEY (`siteid`)
    REFERENCES `sitewriter`.`sites` (`siteid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`files`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`files` (
  `fileid` INT NOT NULL AUTO_INCREMENT,
  `siteid` INT NOT NULL,
  `name` TINYTEXT NOT NULL,
  `path` TINYTEXT NOT NULL,
  `uploaded` DATETIME NOT NULL,
  PRIMARY KEY (`fileid`, `siteid`),
  UNIQUE INDEX `fileid_UNIQUE` (`fileid` ASC),
  INDEX `fk_files_sites1_idx` (`siteid` ASC),
  CONSTRAINT `fk_files_sites1`
    FOREIGN KEY (`siteid`)
    REFERENCES `sitewriter`.`sites` (`siteid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`analytics`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`analytics` (
  `aid` INT NOT NULL AUTO_INCREMENT,
  `siteid` INT NOT NULL,
  `pageid` INT NOT NULL,
  `uuid` VARCHAR(100) NULL,
  `country` VARCHAR(255) NULL,
  `region` VARCHAR(255) NULL,
  `city` VARCHAR(255) NULL,
  `countrycode` VARCHAR(5) NULL,
  `regioncode` VARCHAR(5) NULL,
  `lat` DECIMAL(5,2) NULL,
  `lon` DECIMAL(5,2) NULL,
  `time` DATETIME NULL,
  PRIMARY KEY (`aid`, `siteid`),
  UNIQUE INDEX `aid_UNIQUE` (`aid` ASC),
  INDEX `fk_analytics_sites1_idx` (`siteid` ASC),
  INDEX `fk_analytics_pages1_idx` (`pageid` ASC),
  CONSTRAINT `fk_analytics_sites1`
    FOREIGN KEY (`siteid`)
    REFERENCES `sitewriter`.`sites` (`siteid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_analytics_pages1`
    FOREIGN KEY (`pageid`)
    REFERENCES `sitewriter`.`pages` (`pageid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`complex_components`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`complex_components` (
  `compid` INT NOT NULL AUTO_INCREMENT,
  `pageid` INT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `content` LONGTEXT NULL,
  PRIMARY KEY (`compid`, `pageid`),
  UNIQUE INDEX `compid_UNIQUE` (`compid` ASC),
  INDEX `fk_complex_components_pages1_idx` (`pageid` ASC),
  CONSTRAINT `fk_complex_components_pages1`
    FOREIGN KEY (`pageid`)
    REFERENCES `sitewriter`.`pages` (`pageid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `sitewriter`.`messages`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sitewriter`.`messages` (
  `mid` INT NOT NULL AUTO_INCREMENT,
  `siteid` INT NOT NULL,
  `name` VARCHAR(255) NULL,
  `email` VARCHAR(255) NULL,
  `message` TEXT NULL,
  `date` DATETIME NULL,
  PRIMARY KEY (`mid`, `siteid`),
  UNIQUE INDEX `mid_UNIQUE` (`mid` ASC),
  INDEX `fk_messages_sites1_idx` (`siteid` ASC),
  CONSTRAINT `fk_messages_sites1`
    FOREIGN KEY (`siteid`)
    REFERENCES `sitewriter`.`sites` (`siteid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


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