[Rt-commit] rt branch, 4.2/set-articles-seq-nextval, created. rt-4.2.10-223-g5ea69f1

? sunnavy sunnavy at bestpractical.com
Sat Apr 11 05:34:35 EDT 2015


The branch, 4.2/set-articles-seq-nextval has been created
        at  5ea69f11b6afdfb8911dcab583bf35fb37e4e051 (commit)

- Log -----------------------------------------------------------------
commit 5ea69f11b6afdfb8911dcab583bf35fb37e4e051
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.2.11/content b/etc/upgrade/4.2.11/content
index 883b519..723873e 100644
--- a/etc/upgrade/4.2.11/content
+++ b/etc/upgrade/4.2.11/content
@@ -56,4 +56,28 @@ EOSQL
             }
         }
     },
+    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.2.11/schema.Pg b/etc/upgrade/4.2.11/schema.Pg
new file mode 100644
index 0000000..7af25eb
--- /dev/null
+++ b/etc/upgrade/4.2.11/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 742fd17..0335150 100644
--- a/etc/upgrade/upgrade-articles.in
+++ b/etc/upgrade/upgrade-articles.in
@@ -259,3 +259,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