-- CREATE TABLE "smd__autofeed_preset" -------------------------
CREATE TABLE `smd__autofeed_preset`(
	`presetID` Int( 11 ) UNSIGNED AUTO_INCREMENT NOT NULL,
	`settings` JSON NOT NULL,
	`createDate` Timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY ( `presetID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_presetID" -------------------------------
CREATE INDEX `index_presetID` ON `smd__autofeed_preset`( `presetID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_createDate13" ---------------------------
CREATE INDEX `index_createDate13` ON `smd__autofeed_preset`( `createDate` );
-- -------------------------------------------------------------

-- CREATE FIELD "name" -----------------------------------------
ALTER TABLE `smd__autofeed_preset` ADD COLUMN `name` VarChar( 128 ) NOT NULL;
-- -------------------------------------------------------------

-- CREATE INDEX "index_name4" ----------------------------------
CREATE INDEX `index_name4` ON `smd__autofeed_preset`( `name` );
-- -------------------------------------------------------------

-- CREATE TABLE "smd__organization_has_autofeed_preset" --------
CREATE TABLE `smd__organization_has_autofeed_preset`(
	`organizationID` Int( 11 ) UNSIGNED NOT NULL,
	`presetID` Int( 11 ) UNSIGNED NOT NULL,
	PRIMARY KEY ( `organizationID`, `presetID` ),
	CONSTRAINT `unique` UNIQUE( `organizationID`, `presetID` ) )
ENGINE = InnoDB;
-- -------------------------------------------------------------

-- CREATE INDEX "index_presetID1" ------------------------------
CREATE INDEX `index_presetID1` ON `smd__organization_has_autofeed_preset`( `presetID` );
-- -------------------------------------------------------------

-- CREATE INDEX "index_organizationID10" -----------------------
CREATE INDEX `index_organizationID10` ON `smd__organization_has_autofeed_preset`( `organizationID` );
-- -------------------------------------------------------------

-- CREATE LINK "smd__autofeed_preset_smd__organization_has_autofeed_preset"
ALTER TABLE `smd__organization_has_autofeed_preset`
	ADD CONSTRAINT `smd__autofeed_preset_smd__organization_has_autofeed_preset` FOREIGN KEY ( `presetID` )
	REFERENCES `smd__autofeed_preset`( `presetID` )
	ON DELETE Cascade
	ON UPDATE Cascade;
-- -------------------------------------------------------------

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

SELECT @moduleID:=moduleID AS moduleID FROM cms__module WHERE name = 'module.post';

INSERT INTO `cms__resource`(
    `resourceID`,
    `moduleID`,
    `name`,
    `description`
) VALUES (
    'post.autoFeedPreset.list',
    @moduleID,
    'post.autoFeedPreset.list.name',
    'post.autoFeedPreset.list.description'
 );

 INSERT INTO `cms__resource`(
    `resourceID`,
    `moduleID`,
    `name`,
    `description`
) VALUES (
    'post.autoFeedPreset.create',
    @moduleID,
    'post.autoFeedPreset.create.name',
    'post.autoFeedPreset.create.description'
 );

INSERT INTO `cms__resource`(
    `resourceID`,
    `moduleID`,
    `name`,
    `description`
) VALUES (
    'post.autoFeedPreset.update',
    @moduleID,
    'post.autoFeedPreset.update.name',
    'post.autoFeedPreset.update.description'
 );

INSERT INTO `cms__resource`(
    `resourceID`,
    `moduleID`,
    `name`,
    `description`
) VALUES (
    'post.autoFeedPreset.remove',
    @moduleID,
    'post.autoFeedPreset.remove.name',
    'post.autoFeedPreset.remove.description'
 );

INSERT INTO `cms__resource`(
    `resourceID`,
    `moduleID`,
    `name`,
    `description`
) VALUES (
    'post.autoFeedPreset.bulkDelete',
    @moduleID,
    'post.autoFeedPreset.bulkDelete.name',
    'post.autoFeedPreset.bulkDelete.description'
 );

 ALTER TABLE smd__autofeed_preset ADD COLUMN autoFeedCategoryID INT(11) AS (settings->>'$.categoryID'), ADD COLUMN autoFeedPeriod VARCHAR(32) AS (settings->>'$.period');