alter table `user` add index `user_email_index`(`email`);
alter table `user` add index `user_role_index`(`role`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/User/2017_04_12_170600_update_user_table.php", 63);


alter table `ticket` add `reopened_time` int null after `resolved_time`, add `last_message_time` int not null default '0' after `last_reply_time`, add `last_reply_by` tinyint null after `last_message_id`, add `last_message_by` tinyint null after `last_reply_by`;
alter table `ticket` add index `ticket_last_reply_by_index`(`last_reply_by`);
alter table `ticket` add index `ticket_last_message_by_index`(`last_message_by`);
alter table `ticket` add index `ticket_department_id_brand_id_status_id_index`(`department_id`, `brand_id`, `status_id`);
alter table `ticket` add index `ticket_due_time_index`(`due_time`);
alter table `ticket` add index `ticket_last_reply_time_index`(`last_reply_time`);
alter table `ticket` add index `ticket_created_at_index`(`created_at`);
update `ticket` set `last_message_time` = `last_reply_time`;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_04_12_189203_update_ticket_table.php", 63);


alter table `activity_log` add `brand_id` int unsigned null after `id`;
alter table `activity_log` add constraint `activity_log_brand_id_foreign` foreign key (`brand_id`) references `brand` (`id`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Core/2017_06_08_113000_update_activity_log_table.php", 63);


alter table `article_tag_membership` add unique `article_tag_membership_article_id_tag_id_unique`(`article_id`, `tag_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Selfservice/2017_06_14_132400_update_article_tag_membership_table.php", 63);


alter table `brand_operator_group_membership` drop foreign key `brand_operator_group_membership_brand_id_foreign`;
alter table `brand_operator_group_membership` drop foreign key `brand_operator_group_membership_group_id_foreign`;
ALTER TABLE brand_operator_group_membership CHANGE brand_id brand_id INT UNSIGNED NOT NULL, CHANGE group_id group_id INT UNSIGNED NOT NULL;
alter table `brand_operator_group_membership` add constraint `brand_operator_group_membership_brand_id_foreign` foreign key (`brand_id`) references `brand` (`id`) on delete cascade;
alter table `brand_operator_group_membership` add constraint `brand_operator_group_membership_group_id_foreign` foreign key (`group_id`) references `user_group` (`id`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Core/2017_06_14_133000_update_brand_operator_group_membership_table.php", 63);


alter table `canned_response_group_membership` add unique `canned_response_group_membership_response_id_group_id_unique`(`response_id`, `group_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_133229_update_canned_response_group_membership_table.php", 63);


alter table `canned_response_tag_membership` add unique `canned_response_tag_membership_response_id_tag_id_unique`(`response_id`, `tag_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_133616_update_canned_response_tag_membership_table.php", 63);


alter table `department_priority_membership` add unique `department_priority_membership_department_id_priority_id_unique`(`department_id`, `priority_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_133935_update_department_priority_membership_table.php", 63);


alter table `email_queue_attachments` add unique `email_queue_attachments_queue_id_upload_id_unique`(`queue_id`, `upload_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Core/2017_06_15_134209_update_email_queue_attachments_table.php", 63);


alter table `migrations` add `id` int unsigned not null auto_increment primary key first;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Core/2017_06_15_134943_update_migrations_table.php", 63);


alter table `permission_role` add unique `permission_role_permission_id_role_id_unique`(`permission_id`, `role_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/User/2017_06_15_135251_update_permission_role_table.php", 63);


alter table `ticket_filter_group_membership` add unique `ticket_filter_group_membership_filter_id_group_id_unique`(`filter_id`, `group_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_135309_update_ticket_filter_group_membership_table.php", 63);


alter table `ticket_filter_viewed` drop foreign key `ticket_filter_viewed_filter_id_foreign`;
alter table `ticket_filter_viewed` drop foreign key `ticket_filter_viewed_user_id_foreign`;
ALTER TABLE ticket_filter_viewed CHANGE filter_id filter_id INT UNSIGNED NOT NULL, CHANGE user_id user_id INT UNSIGNED NOT NULL;
alter table `ticket_filter_viewed` add constraint `ticket_filter_viewed_filter_id_foreign` foreign key (`filter_id`) references `ticket_filter` (`id`) on delete cascade;
alter table `ticket_filter_viewed` add constraint `ticket_filter_viewed_user_id_foreign` foreign key (`user_id`) references `user` (`id`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_135326_update_ticket_filter_viewed_table.php", 63);


alter table `ticket_tag_membership` add `id` int unsigned not null auto_increment primary key first;
DELETE t1 FROM `ticket_tag_membership` t1 JOIN (SELECT MAX(`id`) as `max_id`, `ticket_id`, `tag_id` FROM `ticket_tag_membership` GROUP BY `ticket_id`, `tag_id` HAVING COUNT(*) > 1) t2 ON t1.`id` < t2.`max_id` AND t1.`ticket_id` = t2.`ticket_id` AND t1.`tag_id` = t2.`tag_id`;
alter table `ticket_tag_membership` drop `id`;
alter table `ticket_tag_membership` add unique `ticket_tag_membership_ticket_id_tag_id_unique`(`ticket_id`, `tag_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_135342_update_ticket_tag_membership_table.php", 63);


alter table `ticket_viewed` add unique `ticket_viewed_ticket_id_user_id_unique`(`ticket_id`, `user_id`);
alter table `ticket_viewed` add index `ticket_viewed_user_type_updated_at_index`(`user_type`, `updated_at`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_135357_update_ticket_viewed_table.php", 63);


alter table `user_organisation_viewed` drop foreign key `user_organisation_viewed_organisation_id_foreign`;
alter table `user_organisation_viewed` drop foreign key `user_organisation_viewed_user_id_foreign`;
ALTER TABLE user_organisation_viewed CHANGE organisation_id organisation_id INT UNSIGNED NOT NULL, CHANGE user_id user_id INT UNSIGNED NOT NULL;
alter table `user_organisation_viewed` add constraint `user_organisation_viewed_organisation_id_foreign` foreign key (`organisation_id`) references `user_organisation` (`id`) on delete cascade;
alter table `user_organisation_viewed` add constraint `user_organisation_viewed_user_id_foreign` foreign key (`user_id`) references `user` (`id`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/User/2017_06_15_135414_update_user_organisation_viewed_table.php", 63);


alter table `user_viewed` drop foreign key `user_viewed_user_id_foreign`;
alter table `user_viewed` drop foreign key `user_viewed_operator_id_foreign`;
ALTER TABLE user_viewed CHANGE user_id user_id INT UNSIGNED NOT NULL, CHANGE operator_id operator_id INT UNSIGNED NOT NULL;
alter table `user_viewed` add constraint `user_viewed_user_id_foreign` foreign key (`user_id`) references `user` (`id`) on delete cascade;
alter table `user_viewed` add constraint `user_viewed_operator_id_foreign` foreign key (`operator_id`) references `user` (`id`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/User/2017_06_15_135430_update_user_viewed_table.php", 63);


alter table `department_customfield_membership` add unique `department_customfield_membership_field_id_department_id_unique`(`field_id`, `department_id`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_06_15_141923_update_department_customfield_membership_table.php", 63);


alter table `merge_log` add `extra` text null after `tags`;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_07_03_130600_update_merge_log_table.php", 63);


alter table `article_attachment` add `locale` varchar(5) null after `article_id`;
alter table `article_attachment` add constraint `article_attachment_locale_foreign` foreign key (`locale`) references `language` (`code`) on delete cascade;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Selfservice/2017_07_05_144943_update_article_attachment_table.php", 63);


alter table `ticket_message` add `purified_text` longtext not null after `text`;
alter table `ticket_message` add index `ticket_message_created_at_index`(`created_at`);
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Ticket/2017_07_27_111500_update_ticket_message_table.php", 63);


alter table `comment` add `purified_text` mediumtext not null after `text`;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Selfservice/2017_07_28_180800_update_comment_table.php", 63);


alter table `article` drop foreign key `article_author_id_foreign`;
ALTER TABLE article CHANGE author_id author_id INT UNSIGNED DEFAULT NULL;
alter table `article` add constraint `article_author_id_foreign` foreign key (`author_id`) references `user` (`id`) on delete set null;
insert into `migrations` (`migration`, `batch`) values ("database/migrations/2.2.0/Selfservice/2017_08_03_095030_update_article_table.php", 63);
