#
# MySQL upgrade script for upgrade database from version 2.0.x to 2.0.x
#

SET FOREIGN_KEY_CHECKS = 0;

#-----------------------------------------------------------------------------
#-- Drafts
#-----------------------------------------------------------------------------

ALTER TABLE draft ADD `quantity` INTEGER  NOT NULL AFTER `description`;
ALTER TABLE draft ADD `price` FLOAT  AFTER `quantity`;
ALTER TABLE draft ADD `delivery_price` FLOAT AFTER `price`;
ALTER TABLE draft ADD `points` FLOAT AFTER `discount`;
ALTER TABLE draft ADD `in_user_cart` INTEGER default 0 AFTER `data`;
ALTER TABLE draft ADD `custom_message` VARCHAR(1000) AFTER `in_user_cart`;

#-----------------------------------------------------------------------------
#-- draft_work_message
#-----------------------------------------------------------------------------

CREATE TABLE `draft_work_message`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`draft_id` INTEGER  NOT NULL,
	`work_message_id` INTEGER,
	PRIMARY KEY (`id`),
	UNIQUE KEY `unique_draft_work_message` (`draft_id`, `work_message_id`),
	CONSTRAINT `draft_work_message_FK_1`
		FOREIGN KEY (`draft_id`)
		REFERENCES `draft` (`id`)
		ON DELETE CASCADE,
	INDEX `draft_work_message_FI_2` (`work_message_id`),
	CONSTRAINT `draft_work_message_FK_2`
		FOREIGN KEY (`work_message_id`)
		REFERENCES `work_message` (`id`)
		ON DELETE SET NULL
)Type=InnoDB;

#-----------------------------------------------------------------------------
#-- Orders
#-----------------------------------------------------------------------------

CREATE TABLE `order1`
(
	`id` INTEGER  NOT NULL AUTO_INCREMENT,
	`hierarchy_id` INTEGER,
	`hierarchy_history_id` INTEGER  NOT NULL,
	`sf_guard_user_id` INTEGER,
	`user_history_id` INTEGER  NOT NULL,
	`created_at` DATETIME,
	`updated_at` DATETIME,
	PRIMARY KEY (`id`),
	INDEX `order1_FI_1` (`hierarchy_id`),
	CONSTRAINT `order1_FK_1`
		FOREIGN KEY (`hierarchy_id`)
		REFERENCES `hierarchy` (`id`)
		ON DELETE SET NULL,
	INDEX `order1_FI_2` (`hierarchy_history_id`),
	CONSTRAINT `order1_FK_2`
		FOREIGN KEY (`hierarchy_history_id`)
		REFERENCES `hierarchy_history` (`id`),
	INDEX `order1_FI_3` (`sf_guard_user_id`),
	CONSTRAINT `order1_FK_3`
		FOREIGN KEY (`sf_guard_user_id`)
		REFERENCES `sf_guard_user` (`id`)
		ON DELETE SET NULL,
	INDEX `order1_FI_4` (`user_history_id`),
	CONSTRAINT `order1_FK_4`
		FOREIGN KEY (`user_history_id`)
		REFERENCES `user_history` (`id`)
)Type=InnoDB;

#-----------------------------------------------------------------------------
#-- Orders
#-----------------------------------------------------------------------------

ALTER TABLE  `print_job` ADD `order_id` INTEGER AFTER `id`;
ALTER TABLE  `print_job` ADD INDEX `print_job_FI_1bos` (`order_id`);
ALTER TABLE `print_job` ADD CONSTRAINT `print_job_FK_1bis`
		FOREIGN KEY (`order_id`)
		REFERENCES `order1` (`id`)
		ON DELETE SET NULL;

SET FOREIGN_KEY_CHECKS = 1;

