From 7f9415ffa84233c3bd97dfad61586df9649723d7 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 27 Jan 2014 13:38:24 -0600
Subject: [PATCH] Add PK to %ticket_email_info.`id`

This patch partially reverts the previous database migration patch which
added a primary key to the table on the `thread_id` column. This column
cannot be used, because there may be multiple entries for thread_id = 0.

This also allows ticket_collaborator.isactive to be negative, as well as
adds the database portion of regex support for ticket filters.
---
 include/upgrader/streams/core.sig             |  2 +-
 .../streams/core/ed60ba20-934954de.patch.sql  |  3 --
 .../core/f1ccd3bb-f5692e24.cleanup.sql        | 18 ++++++++++++
 .../streams/core/f1ccd3bb-f5692e24.patch.sql  | 28 +++++++++++++++++++
 .../streams/core/f1ccd3bb-f5692e24.task.php   | 27 ++++++++++++++++++
 setup/inc/streams/core/install-mysql.sql      |  7 +++--
 6 files changed, 78 insertions(+), 7 deletions(-)
 create mode 100644 include/upgrader/streams/core/f1ccd3bb-f5692e24.cleanup.sql
 create mode 100644 include/upgrader/streams/core/f1ccd3bb-f5692e24.patch.sql
 create mode 100644 include/upgrader/streams/core/f1ccd3bb-f5692e24.task.php

diff --git a/include/upgrader/streams/core.sig b/include/upgrader/streams/core.sig
index 6dad7bd6c..3ac67bf82 100644
--- a/include/upgrader/streams/core.sig
+++ b/include/upgrader/streams/core.sig
@@ -1 +1 @@
-f1ccd3bb620e314b0ae1dbd0a1a99177
+f5692e24c7afba7ab6168dde0b3bb3c8
diff --git a/include/upgrader/streams/core/ed60ba20-934954de.patch.sql b/include/upgrader/streams/core/ed60ba20-934954de.patch.sql
index a9b2ec364..39c210f4d 100644
--- a/include/upgrader/streams/core/ed60ba20-934954de.patch.sql
+++ b/include/upgrader/streams/core/ed60ba20-934954de.patch.sql
@@ -17,11 +17,8 @@ UPDATE `%TABLE_PREFIX%filter_rule`
 -- [#331](https://github.com/osTicket/osTicket-1.8/issues/331)
 -- Previously there was no primary key on the %ticket_email_info table, so
 -- clean up any junk records before adding one
-DELETE FROM `%TABLE_PREFIX%ticket_email_info` WHERE
-    `message_id` = 0 OR `message_id` IS NULL;
 ALTER TABLE `%TABLE_PREFIX%ticket_email_info`
     CHANGE `message_id` `thread_id` int(11) unsigned NOT NULL,
-    ADD PRIMARY KEY (`thread_id`),
     DROP INDEX  `message_id`,
     ADD INDEX  `email_mid` (`email_mid`);
 
diff --git a/include/upgrader/streams/core/f1ccd3bb-f5692e24.cleanup.sql b/include/upgrader/streams/core/f1ccd3bb-f5692e24.cleanup.sql
new file mode 100644
index 000000000..109cf6c02
--- /dev/null
+++ b/include/upgrader/streams/core/f1ccd3bb-f5692e24.cleanup.sql
@@ -0,0 +1,18 @@
+/**
+ * @version v1.8.1
+ * @signature f5692e24c7afba7ab6168dde0b3bb3c8
+ * @title Add regex field to ticket filters
+ *
+ * This fixes a glitch introduced @934954de8914d9bd2bb8343e805340ae where
+ * a primary key was added to the %ticket_email_info table so that deleting
+ * can be supported in a clustered environment. The patch added the
+ * `thread_id` column as the primary key, which was incorrect, because the
+ * `thread_id` may be null when rejected emails are recorded so they are
+ * never considered again if found in the inbox.
+ */
+
+-- Add the primary key. The PK on `thread_id` would have been removed in the
+-- task if it existed
+ALTER TABLE `%TABLE_PREFIX%ticket_email_info`
+    ADD `id` int(11) unsigned not null auto_increment FIRST,
+    ADD PRIMARY KEY (`id`);
diff --git a/include/upgrader/streams/core/f1ccd3bb-f5692e24.patch.sql b/include/upgrader/streams/core/f1ccd3bb-f5692e24.patch.sql
new file mode 100644
index 000000000..ddf764696
--- /dev/null
+++ b/include/upgrader/streams/core/f1ccd3bb-f5692e24.patch.sql
@@ -0,0 +1,28 @@
+/**
+ * @version v1.8.1
+ * @signature f5692e24c7afba7ab6168dde0b3bb3c8
+ * @title Add regex field to ticket filters
+ *
+ * This fixes a glitch introduced @934954de8914d9bd2bb8343e805340ae where
+ * a primary key was added to the %ticket_email_info table so that deleting
+ * can be supported in a clustered environment. The patch added the
+ * `thread_id` column as the primary key, which was incorrect, because the
+ * `thread_id` may be null when rejected emails are recorded so they are
+ * never considered again if found in the inbox.
+ */
+
+-- [#479](https://github.com/osTicket/osTicket-1.8/issues/479)
+-- Add (not)_match to the filter_rule `how`
+ALTER TABLE `%TABLE_PREFIX%filter_rule`
+    CHANGE `how` `how` enum('equal','not_equal','contains','dn_contain','starts','ends','match','not_match')
+    NOT NULL;
+
+-- Allow `isactive` to be `-1` for collaborators, which might indicate
+-- something like 'unsubscribed'
+ALTER TABLE `%TABLE_PREFIX%ticket_collaborator`
+    CHANGE `isactive` `isactive` tinyint(1) NOT NULL DEFAULT '1';
+
+-- Finished with patch
+UPDATE `%TABLE_PREFIX%config`
+    SET `value` = 'f5692e24c7afba7ab6168dde0b3bb3c8'
+    WHERE `key` = 'schema_signature' AND `namespace` = 'core';
diff --git a/include/upgrader/streams/core/f1ccd3bb-f5692e24.task.php b/include/upgrader/streams/core/f1ccd3bb-f5692e24.task.php
new file mode 100644
index 000000000..fd58fe770
--- /dev/null
+++ b/include/upgrader/streams/core/f1ccd3bb-f5692e24.task.php
@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * Drops the `thread_id` primary key on the ticket_email_info table if it
+ * exists
+ */
+
+class DropTicketEmailInfoPk extends MigrationTask {
+    var $description = "Reticulating splines";
+
+    function run($max_time) {
+        $sql = 'SELECT `INDEX_NAME` FROM information_schema.statistics
+          WHERE table_schema = '.db_input(DBNAME)
+           .' AND table_name = '.db_input(TICKET_EMAIL_INFO_TABLE)
+           .' AND column_name = '.db_input('thread_id');
+        if ($name = db_result(db_query($sql))) {
+            if ($name == 'PRIMARY') {
+                db_query('ALTER TABLE `'.TICKET_EMAIL_INFO_TABLE
+                    .'` DROP PRIMARY KEY');
+            }
+        }
+    }
+}
+
+return 'DropTicketEmailInfoPk';
+
+?>
diff --git a/setup/inc/streams/core/install-mysql.sql b/setup/inc/streams/core/install-mysql.sql
index 4b51a63c9..85f069996 100644
--- a/setup/inc/streams/core/install-mysql.sql
+++ b/setup/inc/streams/core/install-mysql.sql
@@ -276,7 +276,7 @@ CREATE TABLE `%TABLE_PREFIX%filter_rule` (
   `id` int(11) unsigned NOT NULL auto_increment,
   `filter_id` int(10) unsigned NOT NULL default '0',
   `what` varchar(32) NOT NULL,
-  `how` enum('equal','not_equal','contains','dn_contain','starts','ends') NOT NULL,
+  `how` enum('equal','not_equal','contains','dn_contain','starts','ends','match','not_match') NOT NULL,
   `val` varchar(255) NOT NULL,
   `isactive` tinyint(1) unsigned NOT NULL DEFAULT '1',
   `notes` tinytext NOT NULL,
@@ -572,10 +572,11 @@ CREATE TABLE `%TABLE_PREFIX%ticket_lock` (
 
 DROP TABLE IF EXISTS `%TABLE_PREFIX%ticket_email_info`;
 CREATE TABLE `%TABLE_PREFIX%ticket_email_info` (
+  `id` int(11) unsigned NOT NULL auto_increment,
   `thread_id` int(11) unsigned NOT NULL,
   `email_mid` varchar(255) NOT NULL,
   `headers` text,
-  PRIMARY KEY (`thread_id`),
+  PRIMARY KEY (`id`),
   KEY `email_mid` (`email_mid`)
 ) DEFAULT CHARSET=utf8;
 
@@ -631,7 +632,7 @@ CREATE TABLE `%TABLE_PREFIX%ticket_thread` (
 
 CREATE TABLE `%TABLE_PREFIX%ticket_collaborator` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `isactive` tinyint(1) unsigned NOT NULL DEFAULT '1',
+  `isactive` tinyint(1) NOT NULL DEFAULT '1',
   `ticket_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
-- 
GitLab