ALTER TABLE `qs_SideBlock` CHANGE `show` `enabled` ENUM('y','n') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'y';
ALTER TABLE `qs_SideBlock` CHANGE `blockType` `type` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'html';
ALTER TABLE `qs_SideBlock` CHANGE `hideTitle` `titleHidden` ENUM('y','n') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'n';

CREATE TABLE IF NOT EXISTS `qs_DPageItemGroup` (
`id` int(11) unsigned NOT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `title` varchar(255) NOT NULL DEFAULT '',
  `sorter` int(11) NOT NULL DEFAULT '0',
  `showSideBlock` enum('n','y') NOT NULL DEFAULT 'n'
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

DROP TABLE `qs_DPageItemGroup`;
INSERT INTO `qs_DPageItemGroup` (`id`, `name`, `title`, `sorter`, `showSideBlock`) VALUES
(1, 'ITEMS', 'Main content', 1, 'n'),
(2, 'RIGHT_ITEMS', 'Right column', 2, 'y'),
(3, 'LEFT_ITEMS', 'Left column', 0, 'y'),
(4, 'TOP_ITEMS', 'Top row', -1, 'n'),
(5, 'BOTTOM_ITEMS', 'Bottom row', 3, 'y');

ALTER TABLE `qs_DPageItemGroup`  ADD PRIMARY KEY (`id`);
ALTER TABLE `qs_DPageItemGroup` MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;

CREATE TABLE IF NOT EXISTS `qs_SideBlockPage` (
  `sideBlockId` int(11) NOT NULL DEFAULT '0',
  `pageId` int(11) NOT NULL DEFAULT '0',
  `sorter` int(11) NOT NULL DEFAULT '0',
  `pageItemGroupId` int(10) unsigned NOT NULL DEFAULT '2'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `qs_SideBlockPage`
 ADD PRIMARY KEY (`sideBlockId`,`pageId`),
 ADD KEY `pageItemGroupId` (`pageItemGroupId`),
 ADD KEY `pageId` (`pageId`);

ALTER TABLE `qs_SideBlockPage`
ADD CONSTRAINT `qs_SideBlockPage_ibfk_3` FOREIGN KEY (`pageItemGroupId`) REFERENCES `qs_DPageItemGroup` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `qs_SideBlockPage_ibfk_4` FOREIGN KEY (`sideBlockId`) REFERENCES `qs_SideBlock` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `qs_SideBlockPage_ibfk_5` FOREIGN KEY (`pageId`) REFERENCES `qs_Page` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

INSERT INTO `qs_SideBlockPage` (`sideBlockId`, `pageId`, `sorter`)
SELECT `idSideBlock`, `idPage`, `sorter`
FROM `qs_SideBlock2Page`
GROUP BY `idSideBlock`, `idPage`;

DROP TABLE `qs_SideBlock2Page`;

UPDATE qs_PageOption AS po
JOIN qs_Page AS p ON p.id = po.idPage
SET `po`.`value` = 'n'
WHERE `po`.`name` = 'showSideBlock'
  AND (`p`.`redirect` = 'y' OR `p`.`bodyTemplate` NOT IN ('default.tpl', 'full-width.tpl', 'home.tpl'));

UPDATE qs_DraftPageOption AS po
JOIN qs_DraftPage AS p ON p.id = po.idPage
SET `po`.`value` = 'n'
WHERE `po`.`name` = 'showSideBlock'
  AND (`p`.`redirect` = 'y' OR `p`.`bodyTemplate` NOT IN ('default.tpl', 'full-width.tpl', 'home.tpl'));


CREATE TABLE IF NOT EXISTS `qs_PageItemGroup` (
  `pageId` int(11) NOT NULL,
  `pageItemGroupId` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `qs_PageItemGroup`
 ADD PRIMARY KEY (`pageId`,`pageItemGroupId`), ADD KEY `pageItemGroupId` (`pageItemGroupId`);

ALTER TABLE `qs_PageItemGroup` ADD FOREIGN KEY (`pageId`) REFERENCES `qs_Page`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `qs_PageItemGroup` ADD FOREIGN KEY (`pageItemGroupId`) REFERENCES `qs_DPageItemGroup`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS `qs_DraftPageItemGroup` (
  `pageId` int(11) NOT NULL,
  `pageItemGroupId` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `qs_DraftPageItemGroup`
 ADD PRIMARY KEY (`pageId`,`pageItemGroupId`), ADD KEY `pageItemGroupId` (`pageItemGroupId`);

ALTER TABLE `qs_DraftPageItemGroup` ADD FOREIGN KEY (`pageId`) REFERENCES `qs_Page`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `qs_DraftPageItemGroup` ADD FOREIGN KEY (`pageItemGroupId`) REFERENCES `qs_DPageItemGroup`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

TRUNCATE `qs_PageItemGroup`;
INSERT INTO `qs_PageItemGroup` (`pageId`, `pageItemGroupId`)
SELECT p.id, g.id
FROM `qs_Page` AS `p`
JOIN `qs_DPageItemGroup` as `g`;

UPDATE `qs_Page` SET `bodyTemplate` = 'default.tpl' WHERE `bodyTemplate` = 'main-left-right-columns.tpl';
UPDATE `qs_DraftPage` SET `bodyTemplate` = 'default.tpl' WHERE `bodyTemplate` = 'main-left-right-columns.tpl';

DELETE `pig`
FROM `qs_PageItemGroup` AS `pig`
JOIN `qs_Page` AS `p` ON `p`.`id` = `pig`.`pageId`
WHERE `p`.`bodyTemplate` IN('left-main-columns.tpl', 'right-main-columns.tpl')
  AND `pig`.`pageItemGroupId` IN (2 /* RIGHT_ITEMS */);

DELETE `pig`
FROM `qs_DraftPageItemGroup` AS `pig`
JOIN `qs_DraftPage` AS `p` ON `p`.`id` = `pig`.`pageId`
WHERE `p`.`bodyTemplate` IN('left-main-columns.tpl', 'right-main-columns.tpl')
  AND `pig`.`pageItemGroupId` IN (2 /* RIGHT_ITEMS */);

INSERT IGNORE INTO `qs_PageItemGroup`
SELECT `p`.`id`, 3 /* LEFT_ITEMS */
FROM `qs_Page` AS `p`
WHERE `p`.`bodyTemplate` IN('right-main-columns.tpl');

INSERT IGNORE INTO `qs_DraftPageItemGroup`
SELECT `p`.`id`, 3 /* LEFT_ITEMS */
FROM `qs_DraftPage` AS `p`
WHERE `p`.`bodyTemplate` IN('right-main-columns.tpl');

UPDATE `qs_Page` SET `bodyTemplate` = 'default.tpl' WHERE `bodyTemplate` IN('left-main-columns.tpl', 'right-main-columns.tpl');
UPDATE `qs_DraftPage` SET `bodyTemplate` = 'default.tpl' WHERE `bodyTemplate` IN('left-main-columns.tpl', 'right-main-columns.tpl');

UPDATE `qs_DPageOption` SET `show` = 'y' WHERE `name` = 'showSideBlock';
UPDATE `qs_PageOption` SET `show` = 'y' WHERE `name` = 'showSideBlock' AND `value` = 'y';
UPDATE `qs_DraftPageOption` SET `show` = 'y' WHERE `name` = 'showSideBlock' AND `value` = 'y';

