From 832ea943fe4d2b083f21592284a511aae23e9066 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 11 Aug 2015 15:36:26 -0500
Subject: [PATCH] upgrade: Consolidate updates to %thread_entry

The previous code resulted in rebuilding the %thread_entry table several
times during the upgrade process. For systems will several hundred or
thousand megabytes of content in their system, this could result in a very
slow or crashed system.

This patch avoids multiple rebuilds of the table by simply creating a new
one and copying the correct values into the new table. It also avoids
possible duplicates when changing collaborators to the new thread system
considering that collaborators were not properly removed with tickets in
previous versions.
---
 .../streams/core/0d6099a6-98ad7d55.patch.sql  |  3 -
 .../streams/core/2d590ffa-9143a511.patch.sql  | 47 +++++++--------
 .../core/36f6b328-5cd0a25a.cleanup.sql        |  1 -
 .../streams/core/36f6b328-5cd0a25a.patch.sql  | 57 +++++++++++++++----
 .../streams/core/9143a511-0d6099a6.patch.sql  |  4 --
 5 files changed, 65 insertions(+), 47 deletions(-)

diff --git a/include/upgrader/streams/core/0d6099a6-98ad7d55.patch.sql b/include/upgrader/streams/core/0d6099a6-98ad7d55.patch.sql
index be6707ec6..e6a054535 100644
--- a/include/upgrader/streams/core/0d6099a6-98ad7d55.patch.sql
+++ b/include/upgrader/streams/core/0d6099a6-98ad7d55.patch.sql
@@ -33,9 +33,6 @@ ALTER TABLE `%TABLE_PREFIX%staff`
 ALTER TABLE `%TABLE_PREFIX%team_member`
   ADD `flags` int(10) unsigned NOT NULL DEFAULT 1 AFTER `staff_id`;
 
-ALTER TABLE `%TABLE_PREFIX%thread_collaborator`
-  ADD KEY `user_id` (`user_id`);
-
 ALTER TABLE `%TABLE_PREFIX%task`
   ADD `closed` datetime DEFAULT NULL AFTER `duedate`;
 
diff --git a/include/upgrader/streams/core/2d590ffa-9143a511.patch.sql b/include/upgrader/streams/core/2d590ffa-9143a511.patch.sql
index 4244e3bd5..5ffa73385 100644
--- a/include/upgrader/streams/core/2d590ffa-9143a511.patch.sql
+++ b/include/upgrader/streams/core/2d590ffa-9143a511.patch.sql
@@ -23,40 +23,33 @@ ALTER TABLE `%TABLE_PREFIX%lock`
 -- Drop all the current locks as they do not point to anything now
 TRUNCATE TABLE `%TABLE_PREFIX%lock`;
 
-RENAME TABLE `%TABLE_PREFIX%ticket_collaborator` TO `%TABLE_PREFIX%thread_collaborator`;
-ALTER TABLE `%TABLE_PREFIX%thread_collaborator`
-  CHANGE `ticket_id` `thread_id` int(11) unsigned NOT NULL DEFAULT '0';
-
-UPDATE `%TABLE_PREFIX%thread_collaborator` t1
-    LEFT JOIN  `%TABLE_PREFIX%thread` t2 ON (t2.object_id = t1.thread_id  and t2.object_type = 'T')
-    SET t1.thread_id = t2.id, t1.created = t2.created;
+CREATE TABLE `%TABLE_PREFIX%thread_collaborator` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `isactive` tinyint(1) NOT NULL DEFAULT '1',
+  `thread_id` int(11) unsigned NOT NULL DEFAULT '0',
+  `user_id` int(11) unsigned NOT NULL DEFAULT '0',
+  -- M => (message) clients, N => (note) 3rd-Party, R => (reply) external authority
+  `role` char(1) NOT NULL DEFAULT 'M',
+  `created` datetime NOT NULL,
+  `updated` datetime NOT NULL,
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `collab` (`thread_id`,`user_id`),
+  KEY `user_id` (`user_id`)
+) DEFAULT CHARSET=utf8;
 
 -- Drop zombie collaborators from tickets which were deleted and had
 -- collaborators and the collaborators were not removed
-DELETE A1.*
-    FROM `%TABLE_PREFIX%thread_collaborator` A1
-    LEFT JOIN `%TABLE_PREFIX%thread` A2 ON (A2.id = A1.thread_id)
-    WHERE A2.id IS NULL;
+INSERT INTO `%TABLE_PREFIX%thread_collaborator`
+  (`id`, `isactive`, `thread_id`, `user_id`, `role`, `created`, `updated`)
+  SELECT t1.`id`, t1.`isactive`, t2.`id`, t1.`user_id`, t1.`role`, t2.`created`, t1.`updated`
+  FROM `%TABLE_PREFIX%ticket_collaborator` t1
+  JOIN `%TABLE_PREFIX%thread` t2 ON (t2.`object_id` = t1.`ticket_id`  and t2.`object_type` = 'T');
+
+DROP TABLE `%TABLE_PREFIX%ticket_collaborator`;
 
 ALTER TABLE `%TABLE_PREFIX%task`
   ADD `lock_id` int(11) unsigned NOT NULL DEFAULT '0' AFTER `team_id`;
 
-ALTER TABLE `%TABLE_PREFIX%thread_entry`
-  ADD `flags` int(11) unsigned NOT NULL default '0' AFTER `type`;
-
--- Set the ORIGINAL_MESSAGE flag to all the first messages of each thread
-CREATE TABLE `%TABLE_PREFIX%_orig_msg_ids`
-  (id INT NOT NULL, PRIMARY KEY (id))
-  SELECT min(id) as id FROM `%TABLE_PREFIX%thread_entry`
-  WHERE type = 'M'
-  GROUP BY thread_id;
-
-UPDATE `%TABLE_PREFIX%thread_entry` A1
-  JOIN `%TABLE_PREFIX%_orig_msg_ids` A2 ON (A1.id = A2.id)
-  SET A1.`flags` = 1 ;
-
-DROP TABLE `%TABLE_PREFIX%_orig_msg_ids`;
-
 -- Finished with patch
 UPDATE `%TABLE_PREFIX%config`
     SET `value` = '9143a511719555e8f8f09b49523bd022'
diff --git a/include/upgrader/streams/core/36f6b328-5cd0a25a.cleanup.sql b/include/upgrader/streams/core/36f6b328-5cd0a25a.cleanup.sql
index 533991fb4..3c93133c1 100644
--- a/include/upgrader/streams/core/36f6b328-5cd0a25a.cleanup.sql
+++ b/include/upgrader/streams/core/36f6b328-5cd0a25a.cleanup.sql
@@ -19,4 +19,3 @@ DROP TABLE `%TABLE_PREFIX%ticket_attachment`;
 
 OPTIMIZE TABLE `%TABLE_PREFIX%ticket`;
 OPTIMIZE TABLE `%TABLE_PREFIX%thread`;
-OPTIMIZE TABLE `%TABLE_PREFIX%thread_entry`;
diff --git a/include/upgrader/streams/core/36f6b328-5cd0a25a.patch.sql b/include/upgrader/streams/core/36f6b328-5cd0a25a.patch.sql
index e20f60e8b..47a74b05e 100644
--- a/include/upgrader/streams/core/36f6b328-5cd0a25a.patch.sql
+++ b/include/upgrader/streams/core/36f6b328-5cd0a25a.patch.sql
@@ -34,20 +34,53 @@ INSERT INTO `%TABLE_PREFIX%thread`
     ON (t1.ticket_id=t2.ticket_id and t1.id=t2.id)
     ORDER BY t1.created;
 
-ALTER TABLE  `%TABLE_PREFIX%ticket_thread`
-    ADD  `thread_id` INT( 11 ) UNSIGNED NOT NULL DEFAULT  '0' AFTER  `pid` ,
-    ADD INDEX (  `thread_id` );
-
-UPDATE  `%TABLE_PREFIX%ticket_thread` t1
-    LEFT JOIN  `%TABLE_PREFIX%thread` t2 ON ( t2.object_id = t1.ticket_id )
-    SET t1.thread_id = t2.id;
-
 -- convert ticket_thread to thread_entry
-ALTER TABLE  `%TABLE_PREFIX%ticket_thread`
-    CHANGE  `thread_type`  `type` CHAR( 1 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
-    ADD INDEX (  `type` );
+CREATE TABLE `%TABLE_PREFIX%thread_entry` (
+  `id` int(11) unsigned NOT NULL auto_increment,
+  `pid` int(11) unsigned NOT NULL default '0',
+  `thread_id` int(11) unsigned NOT NULL default '0',
+  `staff_id` int(11) unsigned NOT NULL default '0',
+  `user_id` int(11) unsigned not null default 0,
+  `type` char(1) NOT NULL default '',
+  `flags` int(11) unsigned NOT NULL default '0',
+  `poster` varchar(128) NOT NULL default '',
+  `editor` int(10) unsigned NULL,
+  `editor_type` char(1) NULL,
+  `source` varchar(32) NOT NULL default '',
+  `title` varchar(255),
+  `body` text NOT NULL,
+  `format` varchar(16) NOT NULL default 'html',
+  `ip_address` varchar(64) NOT NULL default '',
+  `created` datetime NOT NULL,
+  `updated` datetime NOT NULL,
+  PRIMARY KEY  (`id`),
+  KEY `pid` (`pid`),
+  KEY `thread_id` (`thread_id`),
+  KEY `staff_id` (`staff_id`),
+  KEY `type` (`type`)
+) DEFAULT CHARSET=utf8;
 
-RENAME TABLE `%TABLE_PREFIX%ticket_thread` TO  `%TABLE_PREFIX%thread_entry` ;
+-- Set the ORIGINAL_MESSAGE flag to all the first messages of each thread
+CREATE TABLE `%TABLE_PREFIX%_orig_msg_ids`
+  (`id` INT NOT NULL, PRIMARY KEY (id))
+  SELECT MIN(id) AS `id` FROM `%TABLE_PREFIX%ticket_thread`
+  WHERE `thread_type` = 'M'
+  GROUP BY `ticket_id`;
+
+INSERT INTO `%TABLE_PREFIX%thread_entry`
+  (`id`, `pid`, `thread_id`, `staff_id`, `user_id`, `type`, `flags`,
+    `poster`, `source`, `title`, `body`, `format`, `ip_address`, `created`,
+    `updated`)
+  SELECT t1.`id`, t1.`pid`, t2.`id`, t1.`staff_id`, t1.`user_id`, t1.`thread_type`,
+    CASE WHEN t3.`id` IS NULL THEN 0 ELSE 1 END,
+    t1.`poster`, t1.`source`, t1.`title`, t1.`body`, t1.`format`, t1.`ip_address`,
+    t1.`created`, t1.`updated`
+  FROM `%TABLE_PREFIX%ticket_thread` t1
+  LEFT JOIN `%TABLE_PREFIX%thread` t2 ON (t2.object_id = t1.ticket_id AND t2.object_type = 'T')
+  LEFT JOIN `%TABLE_PREFIX%_orig_msg_ids` t3 ON (t1.id = t3.id);
+
+DROP TABLE `%TABLE_PREFIX%ticket_thread`;
+DROP TABLE `%TABLE_PREFIX%_orig_msg_ids`;
 
 -- add thread id to ticket table
 ALTER TABLE  `%TABLE_PREFIX%ticket`
diff --git a/include/upgrader/streams/core/9143a511-0d6099a6.patch.sql b/include/upgrader/streams/core/9143a511-0d6099a6.patch.sql
index c6bf19b42..d5a151c9e 100644
--- a/include/upgrader/streams/core/9143a511-0d6099a6.patch.sql
+++ b/include/upgrader/streams/core/9143a511-0d6099a6.patch.sql
@@ -36,10 +36,6 @@ UPDATE `%TABLE_PREFIX%thread_event` A1
 ALTER TABLE `%TABLE_PREFIX%user_email`
   ADD `flags` int(10) unsigned NOT NULL DEFAULT 0 AFTER `user_id`;
 
-ALTER TABLE `%TABLE_PREFIX%thread_entry`
-  ADD `editor` int(10) unsigned NULL AFTER `poster`,
-  ADD `editor_type` char(1) NULL AFTER `editor`;
-
 ALTER TABLE `%TABLE_PREFIX%form`
   CHANGE `deletable` `flags` int(10) unsigned NOT NULL DEFAULT 1;
 
-- 
GitLab