Skip to content
Permalink
Browse files

Ticket Merge Upgrade Patch

This patch modifies the upgrader for ticket merges to add a new table, thread_entry_merge that will store data for thread entries related to ticket merges. This modification improves the upgrade process.
  • Loading branch information...
aydreeihn committed Nov 6, 2019
1 parent b733480 commit 38fada0fb8a88494558e9fe65fcb14ed3ecc147c
@@ -92,6 +92,7 @@ static function defineTables($prefix) {
define('THREAD_TABLE', $prefix.'thread');
define('THREAD_ENTRY_TABLE', $prefix.'thread_entry');
define('THREAD_ENTRY_EMAIL_TABLE', $prefix.'thread_entry_email');
define('THREAD_ENTRY_MERGE_TABLE', $prefix.'thread_entry_merge');
define('LOCK_TABLE',$prefix.'lock');
@@ -768,8 +768,9 @@ class DatabaseExporter {
FAQ_TOPIC_TABLE, FAQ_CATEGORY_TABLE, DRAFT_TABLE,
CANNED_TABLE, TICKET_TABLE, ATTACHMENT_TABLE,
THREAD_TABLE, THREAD_ENTRY_TABLE, THREAD_ENTRY_EMAIL_TABLE,
LOCK_TABLE, THREAD_EVENT_TABLE, TICKET_PRIORITY_TABLE,
EMAIL_TABLE, EMAIL_TEMPLATE_TABLE, EMAIL_TEMPLATE_GRP_TABLE,
THREAD_ENTRY_MERGE_TABLE, LOCK_TABLE, THREAD_EVENT_TABLE,
TICKET_PRIORITY_TABLE, EMAIL_TABLE, EMAIL_TEMPLATE_TABLE,
EMAIL_TEMPLATE_GRP_TABLE,
FILTER_TABLE, FILTER_RULE_TABLE, SLA_TABLE, API_KEY_TABLE,
TIMEZONE_TABLE, SESSION_TABLE, PAGE_TABLE,
FORM_SEC_TABLE, FORM_FIELD_TABLE, LIST_TABLE, LIST_ITEM_TABLE,
@@ -769,6 +769,18 @@ class ThreadEntryEmailInfo extends VerySimpleModel {
);
}
class ThreadEntryMergeInfo extends VerySimpleModel {
static $meta = array(
'table' => THREAD_ENTRY_MERGE_TABLE,
'pk' => array('id'),
'joins' => array(
'thread_entry' => array(
'constraint' => array('thread_entry_id' => 'ThreadEntry.id'),
),
),
);
}
class ThreadEntry extends VerySimpleModel
implements TemplateVariable {
static $meta = array(
@@ -791,6 +803,10 @@ class ThreadEntry extends VerySimpleModel
'reverse' => 'ThreadEntryEmailInfo.thread_entry',
'list' => false,
),
'merge_info' => array(
'reverse' => 'ThreadEntryMergeInfo.thread_entry',
'list' => false,
),
'attachments' => array(
'reverse' => 'Attachment.thread_entry',
'null' => true,
@@ -1539,36 +1555,48 @@ function lookupByRefMessageId($mid, $from) {
}
function setExtra($entries, $info=NULL, $thread_id=NULL) {
foreach ($entries as $entry)
foreach ($entries as $entry) {
$mergeInfo = new ThreadEntryMergeInfo(array(
'thread_entry_id' => $entry->getId(),
'data' => json_encode($info),
));
$mergeInfo->save();
$entry->saveExtra($info, $thread_id);
}
}
function saveExtra($info=NULL, $thread_id=NULL) {
if (!$this->extra) {
$this->extra = !is_null($info) ? json_encode($info) : NULL;
$this->setFlag(ThreadEntry::FLAG_CHILD, true);
}
$this->thread_id = $thread_id ?: $thread_id;
$this->setFlag(ThreadEntry::FLAG_CHILD, true);
$this->thread_id = $thread_id;
$this->save();
}
function getMergeData() {
return $this->merge_info ? $this->merge_info->data : null;
}
function sortEntries($entries, $ticket) {
$buckets = array();
$childEntries = array();
foreach ($entries as $i=>$E) {
if ($ticket) {
$E->extra = json_decode($E->extra, true);
$extra = json_decode($E->getMergeData(), true);
//separated entries
if ($ticket->getMergeType() == 'separate') {
if ($E->extra['thread']) {
if ($extra['thread']) {
$childEntries[$E->getId()] = $E;
if ($childEntries) {
uasort($childEntries, function ($a, $b) { //sort by child ticket
if ($a->extra["thread"] != $b->extra["thread"])
return $b->extra["thread"] - $a->extra["thread"];
$aExtra = json_decode($a->getMergeData(), true);
$bExtra = json_decode($b->getMergeData(), true);
if ($aExtra['thread'] != $bExtra["thread"])
return $bExtra["thread"] - $aExtra['thread'];
});
uasort($childEntries, function($a, $b) { //sort by child created date
if ($a->extra["thread"] == $b->extra["thread"])
$aExtra = json_decode($a->getMergeData(), true);
$bExtra = json_decode($b->getMergeData(), true);
if ($aExtra['thread'] == $bExtra["thread"])
return strtotime($a->created) - strtotime($b->created);
});
}
@@ -24,10 +24,11 @@
if (count($entries)) {
$buckets = ThreadEntry::sortEntries($entries, $ticket);
foreach ($buckets as $entry) {
if ($entry->hasFlag(ThreadEntry::FLAG_CHILD) && $entry->extra) {
if (!is_array($entry->extra))
$entry->extra = json_decode($entry->extra, true);
if (!$thread = Thread::objects()->filter(array('id'=>$entry->extra['thread']))->values_flat('extra'))
$extra = $entry->getMergeData();
if ($entry->hasFlag(ThreadEntry::FLAG_CHILD) && $extra) {
if (!is_array($extra))
$extra = json_decode($extra, true);
if (!$thread = Thread::objects()->filter(array('id'=>$extra['thread']))->values_flat('extra'))
continue;
foreach ($thread as $t)
$threadExtra = $t[0];
@@ -34,10 +34,12 @@
// TODO: Consider adding a date boundary to indicate significant
// changes in dates between thread items.
foreach ($buckets as $entry) {
if ($entry->hasFlag(ThreadEntry::FLAG_CHILD) && $entry->extra) {
if (!is_array($entry->extra))
$entry->extra = json_decode($entry->extra, true);
if (!$thread = Thread::objects()->filter(array('id'=>$entry->extra['thread']))->values_flat('extra'))
$entry = ThreadEntry::lookup($entry->id);
$extra = $entry->getMergeData();
if ($entry->hasFlag(ThreadEntry::FLAG_CHILD) && $extra) {
if (!is_array($extra))
$extra = json_decode($extra, true);
if (!$thread = Thread::objects()->filter(array('id'=>$extra['thread']))->values_flat('extra'))
continue;
foreach ($thread as $t)
$threadExtra = $t[0];
@@ -60,7 +60,7 @@
if ($entry->flags & ThreadEntry::FLAG_REPLY_USER) { ?>
<span class="label label-bare"><i class="icon-user"></i></span>
<?php }
if ($ticket && (get_class($this) != 'TaskThread' && $entry->thread_id != $ticket->getThreadId()) || $entry->extra) {
if ($ticket && (get_class($this) != 'TaskThread' && $entry->thread_id != $ticket->getThreadId()) || $entry->getMergeData()) {
if ($number) { ?>
<span data-toggle="tooltip" title="<?php echo sprintf(__('Ticket #%s'), $number); ?>" class="label label-bare"><i class="icon-code-fork"></i></span>
<?php }
@@ -1 +1 @@
87d4a3233469728d83b86d3ee2f066e1
4bd47d94b10bd8a6bab35c119dadf41f
@@ -0,0 +1,22 @@
/**
* @signature 4bd47d94b10bd8a6bab35c119dadf41f
* @version v1.14.0
* @title Ticket Merge Patch
*
* This patch adds a new table, thread_entry_merge, to helpdesks if they
* have the field 'extra' in their thread_entry table
*
*/
-- Create a new table for merge data
CREATE TABLE IF NOT EXISTS `%TABLE_PREFIX%thread_entry_merge` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_entry_id` int(11) unsigned NOT NULL,
`data` text,
PRIMARY KEY (`id`),
KEY `thread_entry_id` (`thread_entry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Finished with patch
UPDATE `%TABLE_PREFIX%config`
SET `value` = '4bd47d94b10bd8a6bab35c119dadf41f', `updated` = NOW()
WHERE `key` = 'schema_signature' AND `namespace` = 'core';
@@ -0,0 +1,32 @@
<?php
/*
* Populates thread_entry_merge for helpdesks if they
* have the field 'extra' in their thread_entry table
*/
class PopulateThreadEntryMerge extends MigrationTask {
var $description = "Populates thread_entry_merge";
function run($max_time) {
$sql = sprintf('SHOW COLUMNS FROM %s LIKE %s;', THREAD_ENTRY_TABLE, '\'extra\'');
$res = db_query($sql);
if ($res && $res->num_rows > 0) {
$extras = ThreadEntry::objects()
->filter(array('extra__isnull' => false))
->values_flat('id', 'extra');
foreach ($extras as $row) {
list($id, $extra) = $row;
$mergeInfo = new ThreadEntryMergeInfo(array(
'thread_entry_id' => $id,
'data' => $extra,
));
$mergeInfo->save();
}
}
}
}
return 'PopulateThreadEntryMerge';
?>
@@ -6,7 +6,6 @@
* This patch adds new fields to the database to allow us to merge Tickets
*
*/

-- At some point, a flags field was added to the Ticket table in the
-- installer, but it was never added to a patch, so for some people
-- that try to upgrade, they get an error that says it is unable to add
@@ -26,14 +25,19 @@ SET @s = (SELECT IF(
PREPARE stmt FROM @s;
EXECUTE stmt;

-- Add sort and ticket_pid column to tickets
-- Add ticket_pid and sort column to tickets
ALTER TABLE `%TABLE_PREFIX%ticket`
ADD `sort` int(11) unsigned NOT NULL DEFAULT '0' AFTER `flags`,
ADD `ticket_pid` int(11) unsigned DEFAULT NULL AFTER `ticket_id`;
ADD `ticket_pid` int(11) unsigned DEFAULT NULL AFTER `ticket_id`,
ADD `sort` int(11) unsigned NOT NULL DEFAULT '0' AFTER `flags`;

-- Add extra column to thread entries
ALTER TABLE `%TABLE_PREFIX%thread_entry`
ADD `extra` text AFTER `ip_address`;
-- Create a new table for merge data
CREATE TABLE `%TABLE_PREFIX%thread_entry_merge` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_entry_id` int(11) unsigned NOT NULL,
`data` text,
PRIMARY KEY (`id`),
KEY `thread_entry_id` (`thread_entry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Insert new events
INSERT INTO `%TABLE_PREFIX%event` (`id`, `name`, `description`)
@@ -702,6 +702,15 @@ CREATE TABLE `%TABLE_PREFIX%thread_entry_email` (
KEY `mid` (`mid`)
) DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `%TABLE_PREFIX%thread_entry_merge`;
CREATE TABLE `%TABLE_PREFIX%thread_entry_merge` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_entry_id` int(11) unsigned NOT NULL,
`data` text,
PRIMARY KEY (`id`),
KEY `thread_entry_id` (`thread_entry_id`)
) DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `%TABLE_PREFIX%ticket`;
CREATE TABLE `%TABLE_PREFIX%ticket` (
`ticket_id` int(11) unsigned NOT NULL auto_increment,

0 comments on commit 38fada0

Please sign in to comment.
You can’t perform that action at this time.