-- CREATE TABLE "smd__post_template" ---------------------------
CREATE TABLE `smd__post_template` (
	`templateID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`headline` VarChar( 255 ) NULL,
	`subHeadline` VarChar( 255 ) NULL,
	`mainCopy` Text NOT NULL,
	`userID` Int( 11 ) UNSIGNED NOT NULL,
	`partnerID` Int( 11 ) UNSIGNED NULL,
	PRIMARY KEY ( `templateID` ),
	CONSTRAINT `unique_templateID` UNIQUE( `templateID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_userID1" --------------------------------
CREATE INDEX `index_userID1` ON `smd__post_template`( `userID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_partnerID2" -----------------------------
CREATE INDEX `index_partnerID2` ON `smd__post_template`( `partnerID` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__post_template_has_tags" ------------------
CREATE TABLE `smd__post_template_has_tags` (
	`templateID` Int( 11 ) UNSIGNED NOT NULL,
	`tagID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `templateID`, `tagID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_tagID1" ---------------------------------
CREATE INDEX `index_tagID1` ON `smd__post_template_has_tags`( `tagID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_postID" ---------------------------------
CREATE INDEX `index_postID` ON `smd__post_template_has_tags`( `templateID` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__category" --------------------------------
CREATE TABLE `smd__category` (
	`categoryID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`name` VarChar( 64 ) NOT NULL,
	`partnerID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `categoryID` ),
	CONSTRAINT `unique_categoryID` UNIQUE( `categoryID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_name1" ----------------------------------
CREATE INDEX `index_name1` ON `smd__category`( `name` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_partnerID3" -----------------------------
CREATE INDEX `index_partnerID3` ON `smd__category`( `partnerID` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__post_template_has_categories" ------------
CREATE TABLE `smd__post_template_has_categories` (
	`templateID` Int( 11 ) UNSIGNED NOT NULL,
	`categoryID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `templateID`, `categoryID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_categoryID" -----------------------------
CREATE INDEX `index_categoryID` ON `smd__post_template_has_categories`( `categoryID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_postID1" --------------------------------
CREATE INDEX `index_postID1` ON `smd__post_template_has_categories`( `templateID` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__post" ------------------------------------
CREATE TABLE `smd__post` (
	`postID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`templateID` Int( 11 ) UNSIGNED NOT NULL,
	`userID` Int( 11 ) UNSIGNED NOT NULL,
	`activeFrom` Timestamp NULL,
	`activeTo` Timestamp NULL,
	`status` Enum( 'active', 'pending', 'inactive' ) NOT NULL DEFAULT 'pending',
	PRIMARY KEY ( `postID` ),
	CONSTRAINT `unique_postID` UNIQUE( `postID` ),
	CONSTRAINT `unique_templateID` UNIQUE( `templateID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_userID" ---------------------------------
CREATE INDEX `index_userID` ON `smd__post`( `userID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_activeFrom" -----------------------------
CREATE INDEX `index_activeFrom` ON `smd__post`( `activeFrom` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_activeTo" -------------------------------
CREATE INDEX `index_activeTo` ON `smd__post`( `activeTo` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__post_has_files" --------------------------
CREATE TABLE `smd__post_has_files` (
	`postID` Int( 11 ) UNSIGNED NOT NULL,
	`fileID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `postID`, `fileID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_documentID" -----------------------------
CREATE INDEX `index_documentID` ON `smd__post_has_files`( `fileID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_postID2" --------------------------------
CREATE INDEX `index_postID2` ON `smd__post_has_files`( `postID` );
-- -------------------------------------------------------------

-- CREATE LINK "smd__partner_smd__post_template_partnerID_partnerID"
ALTER TABLE `smd__post_template`
	ADD CONSTRAINT `smd__partner_smd__post_template_partnerID_partnerID` FOREIGN KEY ( `partnerID` )
	REFERENCES `smd__partner`( `partnerID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__post_template_smd__post_templateID_templateID"
ALTER TABLE `smd__post`
	ADD CONSTRAINT `smd__post_template_smd__post_templateID_templateID` FOREIGN KEY ( `templateID` )
	REFERENCES `smd__post_template`( `templateID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "cms__user_smd__post_template_userID_userID" ----
ALTER TABLE `smd__post_template`
	ADD CONSTRAINT `cms__user_smd__post_template_userID_userID` FOREIGN KEY ( `userID` )
	REFERENCES `cms__user`( `userID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__post_template_smd__post_template_has_tags_templateID"
ALTER TABLE `smd__post_template_has_tags`
	ADD CONSTRAINT `smd__post_template_smd__post_template_has_tags_templateID` FOREIGN KEY ( `templateID` )
	REFERENCES `smd__post_template`( `templateID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__post_template_smd__post_template_has_categories_templateID"
ALTER TABLE `smd__post_template_has_categories`
	ADD CONSTRAINT `smd__post_template_smd__post_template_has_categories_templateID` FOREIGN KEY ( `templateID` )
	REFERENCES `smd__post_template`( `templateID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "cms__user_smd__post_userID_userID" -------------
ALTER TABLE `smd__post`
	ADD CONSTRAINT `cms__user_smd__post_userID_userID` FOREIGN KEY ( `userID` )
	REFERENCES `cms__user`( `userID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__post_smd__post_has_files_postID_postID"
ALTER TABLE `smd__post_has_files`
	ADD CONSTRAINT `smd__post_smd__post_has_files_postID_postID` FOREIGN KEY ( `postID` )
	REFERENCES `smd__post`( `postID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "cms__file_smd__post_has_files_fileID_fileID" ---
ALTER TABLE `smd__post_has_files`
	ADD CONSTRAINT `cms__file_smd__post_has_files_fileID_fileID` FOREIGN KEY ( `fileID` )
	REFERENCES `cms__file`( `fileID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__partner_smd__category_partnerID_partnerID"
ALTER TABLE `smd__category`
	ADD CONSTRAINT `smd__partner_smd__category_partnerID_partnerID` FOREIGN KEY ( `partnerID` )
	REFERENCES `smd__partner`( `partnerID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__category_smd__post_has_categories_categoryID_categoryID"
ALTER TABLE `smd__post_template_has_categories`
	ADD CONSTRAINT `smd__category_smd__post_has_categories_categoryID_categoryID` FOREIGN KEY ( `categoryID` )
	REFERENCES `smd__category`( `categoryID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "cms__tag_smd__post_has_tags_tagID_tagID" -------
ALTER TABLE `smd__post_template_has_tags`
	ADD CONSTRAINT `cms__tag_smd__post_has_tags_tagID_tagID` FOREIGN KEY ( `tagID` )
	REFERENCES `cms__tag`( `tagID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------