 ALTER TABLE  `qs_Admin` ENGINE = INNODB;
 ALTER TABLE  `qs_Application` ENGINE = INNODB;                     
 ALTER TABLE  `qs_Cart` ENGINE = INNODB;                            
 ALTER TABLE  `qs_CartItem` ENGINE = INNODB;                        
 ALTER TABLE  `qs_CartItemGiftCard` ENGINE = INNODB;                
 ALTER TABLE  `qs_CartItemPromo` ENGINE = INNODB;                   
 ALTER TABLE  `qs_DContactReason` ENGINE = INNODB;                  
 ALTER TABLE  `qs_DCountry` ENGINE = INNODB;                        
 ALTER TABLE  `qs_DCountryState` ENGINE = INNODB;                   
 ALTER TABLE  `qs_DFileType` ENGINE = INNODB;                       
 ALTER TABLE  `qs_DFilter` ENGINE = INNODB;                         
 ALTER TABLE  `qs_DFormFieldType` ENGINE = INNODB;                  
 ALTER TABLE  `qs_DFormFieldType2Filter` ENGINE = INNODB;           
 ALTER TABLE  `qs_DFormFieldType2Validate` ENGINE = INNODB;         
 ALTER TABLE  `qs_DGiftCardStatus` ENGINE = INNODB;                 
 ALTER TABLE  `qs_DLanguage` ENGINE = INNODB;                       
 ALTER TABLE  `qs_DNotFoundMode` ENGINE = INNODB;                   
 ALTER TABLE  `qs_DPageHandler` ENGINE = INNODB;                    
 ALTER TABLE  `qs_DPageItemGroup` ENGINE = INNODB;                  
 ALTER TABLE  `qs_DPageItemType` ENGINE = INNODB;                   
 ALTER TABLE  `qs_DPageOption` ENGINE = INNODB;                     
 ALTER TABLE  `qs_DPromoType` ENGINE = INNODB;                      
 ALTER TABLE  `qs_DSlideEffect` ENGINE = INNODB;                    
 ALTER TABLE  `qs_DSlideType` ENGINE = INNODB;                      
 ALTER TABLE  `qs_DState` ENGINE = INNODB;                          
 ALTER TABLE  `qs_DValidate` ENGINE = INNODB;                       
 ALTER TABLE  `qs_DraftHtmlBlock` ENGINE = INNODB;                  
 ALTER TABLE  `qs_DraftHtmlBlockContent` ENGINE = INNODB;           
 ALTER TABLE  `qs_DraftPage` ENGINE = INNODB;                       
 ALTER TABLE  `qs_DraftPageContent` ENGINE = INNODB;                
 ALTER TABLE  `qs_DraftPageItem` ENGINE = INNODB;                   
 ALTER TABLE  `qs_DraftPageItemConfig` ENGINE = INNODB;             
 ALTER TABLE  `qs_DraftPageOption` ENGINE = INNODB;                 
 ALTER TABLE  `qs_Faq` ENGINE = INNODB;                             
 ALTER TABLE  `qs_FaqCategory` ENGINE = INNODB;                     
 ALTER TABLE  `qs_File` ENGINE = INNODB;                            
 ALTER TABLE  `qs_FormContact` ENGINE = INNODB;                     
 ALTER TABLE  `qs_FormNewsletter` ENGINE = INNODB;                  
 ALTER TABLE  `qs_GiftCard` ENGINE = INNODB;                        
 ALTER TABLE  `qs_HtmlBlock` ENGINE = INNODB;                       
 ALTER TABLE  `qs_HtmlBlockContent` ENGINE = INNODB;                
 ALTER TABLE  `qs_LockedObject` ENGINE = INNODB;                    
 ALTER TABLE  `qs_Log` ENGINE = INNODB;                             
 ALTER TABLE  `qs_News` ENGINE = INNODB;                            
 ALTER TABLE  `qs_Newsletter` ENGINE = INNODB;                      
 ALTER TABLE  `qs_NotFound` ENGINE = INNODB;                        
 ALTER TABLE  `qs_Page` ENGINE = INNODB;                            
 ALTER TABLE  `qs_PageContent` ENGINE = INNODB;                     
 ALTER TABLE  `qs_PageIndex` ENGINE = INNODB;                       
 ALTER TABLE  `qs_PageItem` ENGINE = INNODB;                        
 ALTER TABLE  `qs_PageItemConfig` ENGINE = INNODB;                  
 ALTER TABLE  `qs_PageOption` ENGINE = INNODB;                      
 ALTER TABLE  `qs_Partner` ENGINE = INNODB;                         
 ALTER TABLE  `qs_Post` ENGINE = INNODB;                            
 ALTER TABLE  `qs_Post2Tag` ENGINE = INNODB;                        
 ALTER TABLE  `qs_PostCategory` ENGINE = INNODB;                    
 ALTER TABLE  `qs_PostComment` ENGINE = INNODB;                     
 ALTER TABLE  `qs_PostTag` ENGINE = INNODB;                         
 ALTER TABLE  `qs_Product` ENGINE = INNODB;                         
 ALTER TABLE  `qs_Product2Category` ENGINE = INNODB;                
 ALTER TABLE  `qs_ProductCategory` ENGINE = INNODB;                 
 ALTER TABLE  `qs_ProductImage` ENGINE = INNODB;                    
 ALTER TABLE  `qs_Promo` ENGINE = INNODB;                           
 ALTER TABLE  `qs_Redirection` ENGINE = INNODB;                     
 ALTER TABLE  `qs_SanityCheckAppFile` ENGINE = INNODB;              
 ALTER TABLE  `qs_SanityCheckFileRegistry` ENGINE = INNODB;         
 ALTER TABLE  `qs_SanityCheckUserFile` ENGINE = INNODB;             
 ALTER TABLE  `qs_Settings` ENGINE = INNODB;                        
 ALTER TABLE  `qs_SettingsCategory` ENGINE = INNODB;                
 ALTER TABLE  `qs_SettingsOptions` ENGINE = INNODB;                 
 ALTER TABLE  `qs_ShippingPrice` ENGINE = INNODB;                   
 ALTER TABLE  `qs_SideBlock` ENGINE = INNODB;                       
 ALTER TABLE  `qs_SideBlock2Page` ENGINE = INNODB;                  
 ALTER TABLE  `qs_Slide` ENGINE = INNODB;                           
 ALTER TABLE  `qs_SlideHotSpot` ENGINE = INNODB;                    
 ALTER TABLE  `qs_SocialLink` ENGINE = INNODB;                      
 ALTER TABLE  `qs_Testimonial` ENGINE = INNODB;                     
 ALTER TABLE  `qs_Tool` ENGINE = INNODB;                            
 ALTER TABLE  `qs_Transaction` ENGINE = INNODB;                     
 ALTER TABLE  `qs_User` ENGINE = INNODB;                            
 ALTER TABLE  `qs_UserAddress` ENGINE = INNODB;                     
 ALTER TABLE  `qs_ViewControllerLog` ENGINE = INNODB;               
 ALTER TABLE  `qs_ViewControllerLogSession` ENGINE = INNODB;        
 ALTER TABLE  `qs_WideSlide` ENGINE = INNODB;                       


-- changes for modules

-- faq
ALTER TABLE  `qs_Faq` CHANGE  `categoryId`  `categoryId` INT( 11 ) NULL DEFAULT NULL;
UPDATE `qs_Faq` SET `categoryId` = NULL  WHERE `categoryId` = 0;
ALTER TABLE  `qs_Faq` ADD FOREIGN KEY (  `categoryId` ) REFERENCES  `qs_FaqCategory` (
`id`
) ON DELETE RESTRICT ON UPDATE CASCADE ;


-- cart
ALTER TABLE  `qs_CartItem` ADD FOREIGN KEY (  `cartId` ) REFERENCES  `qs_Cart` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_CartItemGiftCard` ADD FOREIGN KEY (  `cartId` ) REFERENCES  `qs_Cart` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_CartItemPromo` ADD FOREIGN KEY (  `cartId` ) REFERENCES  `qs_Cart` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_Cart` ADD FOREIGN KEY (  `transactionId` ) REFERENCES  `qs_Transaction` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;


-- FormField
ALTER TABLE  `qs_DFormFieldType2Filter` ADD FOREIGN KEY (  `fieldType` ) REFERENCES  `qs_DFormFieldType` (
`name`
) ON DELETE RESTRICT ON UPDATE RESTRICT ;

ALTER TABLE  `qs_DFormFieldType2Validate` ADD FOREIGN KEY (  `fieldType` ) REFERENCES  `qs_DFormFieldType` (
`name`
) ON DELETE RESTRICT ON UPDATE RESTRICT ;


-- cms
-- -- draft pages
ALTER TABLE  `qs_DraftPageItem` ADD FOREIGN KEY (  `idPage` ) REFERENCES  `qs_DraftPage` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;


-- blog
ALTER TABLE  `qs_Post2Tag` ADD FOREIGN KEY (  `idPost` ) REFERENCES  `qs_Post` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_Post2Tag` ADD FOREIGN KEY (  `idTag` ) REFERENCES  `qs_PostTag` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_PostComment` CHANGE  `idPost`  `idPost` INT( 11 ) NULL;
UPDATE  `qs_PostComment` SET  `idPost` = null  WHERE `idPost` = 0;
ALTER TABLE  `qs_PostComment` ADD FOREIGN KEY (  `idPost` ) REFERENCES  `qs_Post` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_Post` ADD INDEX  `categoryId` (  `categoryId` );

ALTER TABLE  `qs_Post` ADD FOREIGN KEY (  `categoryId` ) REFERENCES  `qs_PostCategory` (
`id`
) ON DELETE RESTRICT ON UPDATE CASCADE ;


-- products !!! продукти зразу не пропатчились !!!
ALTER TABLE  `qs_Product2Category` ADD INDEX  `productId` (  `productId` );
ALTER TABLE  `qs_Product2Category` ADD INDEX  `categoryId` (  `categoryId` );

ALTER TABLE  `qs_Product2Category` ADD FOREIGN KEY (  `productId` ) REFERENCES  `qs_Product` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `qs_Product2Category` ADD FOREIGN KEY (  `categoryId` ) REFERENCES  `qs_ProductCategory` (
`id`
) ON DELETE RESTRICT ON UPDATE CASCADE ;

ALTER TABLE  `qs_ProductImage` CHANGE  `productId`  `productId` INT( 11 ) NOT NULL;
ALTER TABLE`qs_ProductImage` ADD FOREIGN KEY (`productId`) REFERENCES `qs_Product`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE  `qs_Promo` CHANGE  `productId`  `productId` INT( 11 ) NULL DEFAULT NULL;
ALTER TABLE  `qs_Promo` ADD INDEX  `productId` (  `productId` );
update `qs_Promo` set `productId` = null where `productId` = 0;
update qs_Promo as promo
left join qs_Product as product
    on promo.productId = product.id
set promo.productId = null
where product.id is null;
ALTER TABLE `qs_Promo` ADD FOREIGN KEY (`productId`) REFERENCES `qs_Product` (`id`) ON DELETE set null ON UPDATE CASCADE;


-- Settings
ALTER TABLE  `qs_SettingsOptions` ADD FOREIGN KEY (  `name` ) REFERENCES  `qs_Settings` (
`name`
) ON DELETE CASCADE ON UPDATE CASCADE ;
ALTER TABLE `qs_Settings` ADD FOREIGN KEY (`idCategory`) REFERENCES `qs_SettingsCategory`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;


-- SideBlock
DELETE FROM `qs_SideBlock2Page` WHERE `idPage` NOT IN (SELECT `id` FROM `qs_Page`);

ALTER TABLE  `qs_SideBlock2Page` ADD FOREIGN KEY (`idPage`) REFERENCES qs_Page (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE  `qs_SideBlock2Page` ADD FOREIGN KEY (  `idSideBlock` ) REFERENCES  `qs_SideBlock` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;


-- Slides
ALTER TABLE `qs_SlideHotSpot` ADD CONSTRAINT `fk_SlideHotSpotSlideId` FOREIGN KEY(`slideId`) REFERENCES `qs_Slide`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- User
ALTER TABLE  `qs_User` CHANGE  `id`  `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `qs_UserAddress` CHANGE `userId` `userId` INT(11) unsigned NOT NULL;
DELETE FROM `qs_UserAddress` WHERE `userId` NOT IN (select `id` from `qs_User`);
ALTER TABLE `qs_UserAddress` ADD CONSTRAINT `fk_UserId` FOREIGN KEY (`userId`) REFERENCES `qs_User`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE  `qs_GiftCard` CHANGE  `userId`  `userId` INT( 11 ) UNSIGNED NOT NULL;
ALTER TABLE `qs_GiftCard` ADD CONSTRAINT `fk_GiftCardUser` FOREIGN KEY (`userId`) REFERENCES `qs_User` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

--  ViewControllerLog / ViewControllerLogSession 
ALTER TABLE  `qs_ViewControllerLog` CHANGE  `sessionId`  `sessionId` CHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
ALTER TABLE  `qs_ViewControllerLog` ADD INDEX  `sessionId` (  `sessionId` );
ALTER TABLE  `qs_ViewControllerLog` ADD FOREIGN KEY (  `sessionId` ) REFERENCES  `qs_ViewControllerLogSession` (
`id`
) ON DELETE CASCADE ON UPDATE CASCADE ;
