SET FOREIGN_KEY_CHECKS = 0;

SET AUTOCOMMIT = 0;
START TRANSACTION;

ALTER TABLE `katao_invoice_product` ADD `quantity_delivered` INTEGER default 0 AFTER `quantity`;
UPDATE `katao_invoice_product` SET `quantity_delivered` = `quantity`;

ALTER TABLE `katao_invoice_product` ADD `katao_cart_product_id` INTEGER  NOT NULL AFTER `katao_product_id`;
ALTER TABLE `katao_invoice_product` ADD INDEX `katao_invoice_product_FI_3` (`katao_cart_product_id`);
ALTER TABLE `katao_invoice_product` ADD CONSTRAINT `katao_invoice_product_FK_3` FOREIGN KEY (`katao_cart_product_id`) REFERENCES `katao_cart_product` (`id`) ON DELETE CASCADE;

ALTER TABLE `katao_invoice_product` ADD `katao_order_product_id` INTEGER  NOT NULL AFTER `katao_cart_product_id`;
ALTER TABLE `katao_invoice_product` ADD INDEX `katao_invoice_product_FI_4` (`katao_order_product_id`);
ALTER TABLE `katao_invoice_product` ADD CONSTRAINT `katao_invoice_product_FK_4` FOREIGN KEY (`katao_order_product_id`) REFERENCES `katao_order_product` (`id`) ON DELETE CASCADE;

SET FOREIGN_KEY_CHECKS = 1;

COMMIT;