[Rt-commit] rt branch, 4.2/upgrading-indexes-and-review, updated. rt-4.1.19-43-g40cd350

Ruslan Zakirov ruz at bestpractical.com
Thu Aug 29 06:39:13 EDT 2013


The branch, 4.2/upgrading-indexes-and-review has been updated
       via  40cd35095ded1644dffe426aa2f4bfa4340a5f7d (commit)
      from  3a8ba5c288fff90852b5a44e146b616fc58ec16e (commit)

Summary of changes:
 etc/upgrade/4.1.10/indexes | 55 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

- Log -----------------------------------------------------------------
commit 40cd35095ded1644dffe426aa2f4bfa4340a5f7d
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Aug 29 14:32:49 2013 +0400

    de-duplicate Name in Users/Queues
    
    It's possible that an old setup has non unique values
    in the columns, so we have to go over and get rid of
    duplicates by changing Name to 'xxx-dup-#'.
    
    I've tried to use RT's API, but not all objects are
    available during 'indexes' upgrade action.

diff --git a/etc/upgrade/4.1.10/indexes b/etc/upgrade/4.1.10/indexes
index 546a3bd..53bcd0b 100644
--- a/etc/upgrade/4.1.10/indexes
+++ b/etc/upgrade/4.1.10/indexes
@@ -31,6 +31,58 @@ use warnings;
     RT->Logger->info($msg);
 }
 
+my $dedup = sub {
+    my ($table, $column) = (@_);
+
+    my $collection_class = "RT::$table";
+    my $record_class = $collection_class;
+    $record_class =~ s/s$//;
+
+    my $sql;
+
+    my $cs = $RT::Handle->CaseSensitive;
+    if ($cs) {
+        $sql = "SELECT DISTINCT LOWER(t1.$column) FROM $table t1, $table t2"
+        ." WHERE LOWER(t1.$column) = LOWER(t2.$column)"
+        .' AND t1.id != t2.id';
+    } else {
+        $sql = "SELECT DISTINCT t1.$column FROM $table t1, $table t2"
+        ." WHERE t1.$column = t2.$column"
+        .' AND t1.id != t2.id';
+    }
+
+    my $dbh = $RT::Handle->dbh;
+    my $sth = $dbh->prepare($sql);
+    $sth->execute;
+
+    my $found = 0;
+    while ( my ($value) = $sth->fetchrow_array ) {
+        $found = 1;
+
+        my $ids = $dbh->selectcol_arrayref(
+            "SELECT id FROM $table WHERE ". ($cs? "LOWER($column)" : $column) ." = LOWER(?)",
+            undef,
+            $value
+        );
+
+        # skip first
+        shift @$ids;
+
+        my $i = 0;
+        foreach my $id ( @$ids ) {
+            RT->Logger->debug("Changing $column of $record_class #". $id );
+            $dbh->do("UPDATE $table SET $column = ? WHERE id = ?", undef,  $value . '-dup-'.$$.++$i, $id);
+        }
+    }
+
+    if ( $found ) {
+        RT->Logger->warning(
+            "Records in $table table had not unique values in $column column."
+            ." $column has been changed for such records, search LIKE '%-dup-$$%'"
+        );
+    }
+};
+
 # a few case insensitive and unique indexes
 {
     my @list = (
@@ -50,6 +102,9 @@ use warnings;
             RT->Logger->info("Required index exists. Skipping.");
             next;
         }
+
+        $dedup->( $e->{'Table'}, $e->{'Column'} );
+
         if ( $index ) {
             my ($status, $msg) = $RT::Handle->DropIndex(
                 Table => $e->{'Table'}, Name => $index->{'Name'},

-----------------------------------------------------------------------


More information about the Rt-commit mailing list