From 1c50ff4e62dcbe384c6daefb8680f8c3441d68c1 Mon Sep 17 00:00:00 2001
From: Zed Pobre <zed@resonant.org>
Date: Tue, 19 Apr 2011 15:25:22 -0400
Subject: [PATCH] Fix data corruption in PostgreSQL databases when multiple comments are made.

This is an update of the patch provided in Comment #1 at

http://drupal.org/node/97327

to apply against Drupal 7.  It was tested locally as stopping the data
corruption.

There may be additional issues with thread sorting that remain,
related to discrepancies between the collating mechanisms of MySQL and
PostgreSQL.  See the rest of the comments in that thread for details.
---
 modules/comment/comment.module |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 5526beb..08e11d1 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -1508,8 +1508,11 @@ function comment_save($comment) {
         $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid))->fetchField();
         // Strip the "/" from the end of the thread.
         $max = rtrim($max, '/');
+        // We need to get the value at the correct depth.
+        $parts = explode('.', $max);
+        $firstsegment = $parts[0];
         // Finally, build the thread field for this new comment.
-        $thread = int2vancode(vancode2int($max) + 1) . '/';
+        $thread = int2vancode(vancode2int($firstsegment) + 1) .'/';
       }
       else {
         // This is a comment with a parent comment, so increase the part of the
-- 
1.7.2.5

