From 14b1ca6ecfb38f0ec36160711e580f187a0f6369 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Mon, 11 Apr 2011 16:58:04 +0200
Subject: [PATCH] Issue #1122188 by Berdir: Fixed uid column not found errors during 6.x-1.x to 6.x-2.x upgrade.

---
 privatemsg_filter/privatemsg_filter.install |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/privatemsg_filter/privatemsg_filter.install b/privatemsg_filter/privatemsg_filter.install
index 9517161..b664828 100644
--- a/privatemsg_filter/privatemsg_filter.install
+++ b/privatemsg_filter/privatemsg_filter.install
@@ -167,6 +167,13 @@ function privatemsg_filter_update_6201(&$sandbox) {
     $sandbox['max'] = db_result(db_query('SELECT MAX(thread_id) FROM {pm_index}'));
   }
 
+  // There is no way to know if this update is run before or after
+  // privatemsg_update_update_6201() which renames uid to recipient.
+  $uid_column = 'uid';
+  if (db_column_exists('pm_index', 'recipient')) {
+    $uid_column = 'recipient';
+  }
+
   // Fetch the 10 next thread_ids.
   $result = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > %d ORDER BY thread_id ASC', $sandbox['current_thread_id'], 0, 20);
   $threads = array();
@@ -179,7 +186,7 @@ function privatemsg_filter_update_6201(&$sandbox) {
     // on a specific set of threads, this allows us to process the slow having
     // part on a relatively small subset of pm_index that can be selected based on
     // the thread_id index.
-    $sql = 'SELECT pmi.thread_id, pmi.uid FROM {pm_index} pmi WHERE thread_id IN (' . db_placeholders($threads) . ')  GROUP BY pmi.thread_id, pmi.uid HAVING ((SELECT pmf.author FROM pm_message pmf WHERE pmf.mid = pmi.thread_id) = pmi.uid AND COUNT(pmi.thread_id) > 1) OR (SELECT COUNT(*) FROM pm_message pmf INNER JOIN pm_index pmif ON (pmf.mid = pmif.mid) WHERE pmif.thread_id = pmi.thread_id AND pmf.author <> pmi.uid) > 0';
+    $sql = 'SELECT pmi.thread_id, pmi.' . $uid_column . ' AS uid FROM {pm_index} pmi WHERE thread_id IN (' . db_placeholders($threads) . ')  GROUP BY pmi.thread_id, pmi.' . $uid_column . ' HAVING ((SELECT pmf.author FROM pm_message pmf WHERE pmf.mid = pmi.thread_id) = pmi.' . $uid_column . ' AND COUNT(pmi.thread_id) > 1) OR (SELECT COUNT(*) FROM pm_message pmf INNER JOIN pm_index pmif ON (pmf.mid = pmif.mid) WHERE pmif.thread_id = pmi.thread_id AND pmf.author <> pmi.' . $uid_column . ') > 0';
     $result = db_query($sql, $threads);
     while ($row = db_fetch_object($result)) {
       // Make sure that we don't add a tag to a thread twice,
-- 
1.7.4.1

