-- CREATE TABLE "smd__autofeed_settings" -----------------------
CREATE TABLE `smd__autofeed_settings` (
	`settingID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`organizationID` Int( 11 ) UNSIGNED NOT NULL,
	`socialSiteID` Int( 11 ) UNSIGNED NOT NULL,
	`timezone` VarChar( 64 ) NOT NULL,
	`status` Enum( 'paused', 'active' ) NOT NULL,
	PRIMARY KEY ( `settingID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_organizationID" -------------------------
CREATE INDEX `index_organizationID` ON `smd__autofeed_settings`( `organizationID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_socialSiteID" ---------------------------
CREATE INDEX `index_socialSiteID` ON `smd__autofeed_settings`( `socialSiteID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_status" ---------------------------------
CREATE INDEX `index_status` ON `smd__autofeed_settings`( `status` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__autofeed_post_times" ---------------------
CREATE TABLE `smd__autofeed_post_time` (
	`postTimeID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`days` Set( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'weekend', 'weekdays', 'everyday') NOT NULL,
	`time` Time NOT NULL,
	`categoryID` Int( 11 ) UNSIGNED NULL,
	`settingID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `postTimeID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_day" ------------------------------------
CREATE INDEX `index_day` ON `smd__autofeed_post_times`( `days` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_time" -----------------------------------
CREATE INDEX `index_time` ON `smd__autofeed_post_times`( `time` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_organizationID1" ------------------------
CREATE INDEX `index_organizationID1` ON `smd__autofeed_post_times`( `settingID` );
-- -------------------------------------------------------------

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

-- CREATE TABLE "smd__autofeed_post_time_has_post" -------------
CREATE TABLE `smd__autofeed_post_time_has_posts` (
	`postTimeID` Int( 11 ) UNSIGNED NOT NULL,
	`postID` Int( 255 ) UNSIGNED NOT NULL,
	`position` Int( 3 ) NOT NULL,
	PRIMARY KEY ( `postTimeID`, `postID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_postTimeID" -----------------------------
CREATE INDEX `index_postTimeID` ON `smd__autofeed_post_time_has_post`( `postTimeID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_postID3" --------------------------------
CREATE INDEX `index_postID3` ON `smd__autofeed_post_time_has_post`( `postID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_position4" ------------------------------
CREATE INDEX `index_position4` ON `smd__autofeed_post_time_has_post`( `position` );
-- -------------------------------------------------------------

-- CREATE LINK "smd__autofeed_settings_smd__autofeed_post_times_settingID_settingID"
ALTER TABLE `smd__autofeed_post_times`
	ADD CONSTRAINT `smd__autofeed_settings_smd__autofeed_post_times_settingID_settingID` FOREIGN KEY ( `settingID` )
	REFERENCES `smd__autofeed_settings`( `settingID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__organization_smd__autofeed_settings_organizationID_organizationID"
ALTER TABLE `smd__autofeed_settings`
	ADD CONSTRAINT `smd__organization_smd__autofeed_settings_organizationID_organizationID` FOREIGN KEY ( `organizationID` )
	REFERENCES `smd__organization`( `organizationID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__category_smd__autofeed_post_times_categoryID_categoryID"
ALTER TABLE `smd__autofeed_post_times`
	ADD CONSTRAINT `smd__category_smd__autofeed_post_times_categoryID_categoryID` FOREIGN KEY ( `categoryID` )
	REFERENCES `smd__category`( `categoryID` )
	ON DELETE Set NULL
	ON UPDATE Cascade;
-- -------------------------------------------------------------

-- CREATE LINK "smd__autofeed_post_times_smd__autofeed_post_time_has_post"
ALTER TABLE `smd__autofeed_post_time_has_post`
	ADD CONSTRAINT `smd__autofeed_post_times_smd__autofeed_post_time_has_post` FOREIGN KEY ( `postTimeID` )
	REFERENCES `smd__autofeed_post_times`( `postTimeID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

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