[Rt-commit] rt branch, 4.0/set-articles-seq-nextval, created. rt-4.0.23-19-g1f6a8c6

? sunnavy sunnavy at bestpractical.com
Sat Apr 11 08:15:57 EDT 2015


The branch, 4.0/set-articles-seq-nextval has been created
        at  1f6a8c6a9962c361b60bb200f6813bd378a76266 (commit)

- Log -----------------------------------------------------------------
commit 1f6a8c6a9962c361b60bb200f6813bd378a76266
Author: Todd Wade <todd at bestpractical.com>
Date:   Thu Mar 26 21:44:02 2015 -0400

    properly migrate Articles sequence objects
    
    Set the articles sequence value to the max article id so that RT doesn't try
    to give new articles an id of 1 after upgrading.
    
    FIXES: I#18248

diff --git a/etc/upgrade/4.0.24/content b/etc/upgrade/4.0.24/content
new file mode 100644
index 0000000..9a81c0c
--- /dev/null
+++ b/etc/upgrade/4.0.24/content
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+our @Initial = (
+   sub {
+       if ( RT->Config->Get('DatabaseType') eq 'Oracle' ) {
+           my $dbh = RT->DatabaseHandle->dbh;
+           my $rows = $dbh->selectall_arrayref('SELECT id FROM Articles WHERE rownum = 1 ORDER BY id DESC');
+
+           if ( @$rows ) {
+               my $last_id = $rows->[0][0];
+               my $next_value = $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL')->[0][0];
+               return if $last_id == $next_value; # next seq value will be correct
+               if ( $last_id > $next_value ) {
+                   while ( $last_id > $next_value ) {
+                       $next_value = $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL')->[0][0];
+                   }
+               }
+               else {
+                   # everything was fine, get back to the original seq value
+                   $dbh->do('ALTER SEQUENCE Articles_seq INCREMENT BY -1');
+                   # ->do doesn't really work here, Oracle--
+                   $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL');
+                   $dbh->do('ALTER SEQUENCE Articles_seq INCREMENT BY 1'),
+               }
+           }
+       }
+   },
+);
diff --git a/etc/upgrade/4.0.24/schema.Pg b/etc/upgrade/4.0.24/schema.Pg
new file mode 100644
index 0000000..7af25eb
--- /dev/null
+++ b/etc/upgrade/4.0.24/schema.Pg
@@ -0,0 +1 @@
+SELECT SETVAL('articles_id_seq', COALESCE( MAX(id), 1) ) FROM Articles;
diff --git a/etc/upgrade/upgrade-articles.in b/etc/upgrade/upgrade-articles.in
index 21cba4b..f5f8cb6 100644
--- a/etc/upgrade/upgrade-articles.in
+++ b/etc/upgrade/upgrade-articles.in
@@ -262,3 +262,32 @@ sub copy_tables {
         warn "Updating Attribute ".$a->Name." to point to RT::Class";
     }
 }
+
+{ # update sequence
+    if ( $db_type eq 'Pg' ) {
+        my $sql = "SELECT SETVAL('articles_id_seq', COALESCE( MAX(id), 1) ) FROM Articles";
+        $RT::Logger->debug($sql);
+        $dbh->do($sql);
+    }
+    elsif ( $db_type eq 'Oracle' ) {
+        my $rows = $dbh->selectall_arrayref('SELECT id FROM Articles WHERE rownum = 1 ORDER BY id DESC');
+
+        if ( @$rows ) {
+            my $last_id = $rows->[0][0];
+            my $next_value = $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL')->[0][0];
+            return if $last_id == $next_value; # next seq value will be correct
+            if ( $last_id > $next_value ) {
+                while ( $last_id > $next_value ) {
+                    $next_value = $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL')->[0][0];
+                }
+            }
+            else {
+                # everything was fine, get back to the original seq value
+                $dbh->do('ALTER SEQUENCE Articles_seq INCREMENT BY -1');
+                # ->do doesn't really work here, Oracle--
+                $dbh->selectall_arrayref('SELECT Articles_seq.nextval FROM DUAL');
+                $dbh->do('ALTER SEQUENCE Articles_seq INCREMENT BY 1'),
+            }
+        }
+    }
+}

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


More information about the rt-commit mailing list