[Rt-commit] rt branch, 5.0/upgrade-configurations, created. rt-5.0.0beta2-26-g2427e416a0

? sunnavy sunnavy at bestpractical.com
Mon Jul 6 16:01:40 EDT 2020


The branch, 5.0/upgrade-configurations has been created
        at  2427e416a0cc195cc96d95bb0f6de3f2741dc944 (commit)

- Log -----------------------------------------------------------------
commit 30d08e13875275de471452bb8d5766df96e1d131
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jul 7 03:15:07 2020 +0800

    Add script to upgrade old RTxDatabaseSettings table from extension

diff --git a/.gitignore b/.gitignore
index cb19dbf1e7..d69cfd9672 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,6 +16,7 @@
 /etc/upgrade/upgrade-sla
 /etc/upgrade/upgrade-assets
 /etc/upgrade/upgrade-authtokens
+/etc/upgrade/upgrade-configurations
 /etc/upgrade/vulnerable-passwords
 /lib/RT/Generated.pm
 /Makefile
diff --git a/configure.ac b/configure.ac
index cf7a2e28e5..70a9c05a24 100755
--- a/configure.ac
+++ b/configure.ac
@@ -463,6 +463,7 @@ AC_CONFIG_FILES([
                  etc/upgrade/upgrade-articles
                  etc/upgrade/upgrade-assets
                  etc/upgrade/upgrade-authtokens
+                 etc/upgrade/upgrade-configurations
                  etc/upgrade/vulnerable-passwords
                  etc/upgrade/upgrade-sla
                  sbin/rt-ldapimport
diff --git a/etc/upgrade/upgrade-configurations.in b/etc/upgrade/upgrade-configurations.in
new file mode 100644
index 0000000000..df1bbceeb8
--- /dev/null
+++ b/etc/upgrade/upgrade-configurations.in
@@ -0,0 +1,212 @@
+#!@PERL@
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2020 Best Practical Solutions, LLC
+#                                          <sales at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+use 5.10.1;
+use strict;
+use warnings;
+
+use lib "@LOCAL_LIB_PATH@";
+use lib "@RT_LIB_PATH@";
+
+use Term::ReadKey;
+use Getopt::Long;
+
+$| = 1; # unbuffer all output.
+
+my %args;
+GetOptions(
+    \%args,
+    'dba=s', 'dba-password=s', 'prompt-for-dba-password',
+);
+
+no warnings 'once';
+use RT::Interface::CLI qw(Init);
+Init();
+
+my $db_type = RT->Config->Get('DatabaseType') || '';
+my $db_host = RT->Config->Get('DatabaseHost') || '';
+my $db_port = RT->Config->Get('DatabasePort') || '';
+my $db_name = RT->Config->Get('DatabaseName') || '';
+my $db_user = RT->Config->Get('DatabaseUser') || '';
+my $db_pass = RT->Config->Get('DatabasePassword') || '';
+
+my $dba_user = $args{'dba'} || $ENV{'RT_DBA_USER'} || RT->Config->Get('DatabaseAdmin') || '';
+my $dba_pass = $args{'dba-password'} || $ENV{'RT_DBA_PASSWORD'};
+
+if ( !$args{force} && ( !defined $dba_pass || $args{'prompt-for-dba-password'} ) ) {
+    $dba_pass = get_dba_password();
+    chomp $dba_pass if defined($dba_pass);
+}
+
+
+my $dbh = $RT::Handle->dbh;
+
+unless ( grep { lc $_ eq 'rtxdatabasesettings' } $RT::Handle->_TableNames ) {
+    warn "Could not find RT::Extension::ConfigInDatabase data to migrate";
+    exit;
+}
+
+print "Working with:\n"
+    ."Type:\t$db_type\nHost:\t$db_host\nPort:\t$db_port\nName:\t$db_name\n"
+    ."User:\t$db_user\nDBA:\t$dba_user" . ($args{'skip-create'} ? ' (No DBA)' : '') . "\n\n";
+
+print "Upgrading Configurations table...\n";
+
+my @columns = qw(id Name Content ContentType Disabled Creator Created LastUpdatedBy LastUpdated);
+copy_tables('RTxDatabaseSettings','Configurations',\@columns);
+
+fix_id_sequence('Configurations', {
+    Pg     => 'configurations_id_seq',
+    Oracle => 'Configurations_seq',
+});
+
+my $configs = RT::Configurations->new( RT->SystemUser );
+$configs->Limit( FIELD => 'ContentType', VALUE => 'storable' );
+use Storable;
+use MIME::Base64;
+while ( my $config = $configs->Next ) {
+    my $thawed = eval { Storable::thaw( decode_base64( $config->_Value('Content') ) ) };
+    if ($@) {
+        $RT::Logger->error( "Storable deserialization of database setting " . $config->Name . " failed: $@" );
+    }
+    else {
+        $RT::Handle->BeginTransaction();
+        my ( $ret, $msg ) = $config->__Set( Field => 'ContentType', Value => 'perl' );
+        if ($ret) {
+            ( $ret, $msg ) = $config->__Set( Field => 'Content', Value => $config->_SerializeContent($thawed) );
+        }
+
+        if ($ret) {
+            $RT::Handle->Commit();
+        }
+        else {
+            $RT::Logger->error( "Couldn't upgrade database setting " . $config->Name . ": $msg" );
+            $RT::Handle->Rollback();
+        }
+    }
+}
+
+print "Configurations table upgrades complete.\n";
+
+sub copy_tables {
+    my ($source, $dest, $columns) = @_;
+    my $column_list = join(', ',@$columns);
+    my $sql;
+    # SQLite: http://www.sqlite.org/lang_insert.html
+    if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
+        $sql = "insert into $dest ($column_list) select $column_list from $source";
+    }
+    # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
+    elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
+        $sql = "insert into $dest ($column_list) (select $column_list from $source)";
+    }
+    $RT::Logger->debug($sql);
+    $dbh->do($sql);
+}
+
+sub fix_id_sequence {
+    my ($table, $sequence_per_db) = @_;
+    my $sequence = $sequence_per_db->{$db_type} or return;
+
+    my $admin_dbh = get_admin_dbh();
+
+    my ($max) = $admin_dbh->selectrow_array("SELECT MAX(id) FROM $table;");
+    my $next_id = ($max || 0) + 1;
+    RT->Logger->info("Resetting $sequence to $next_id\n");
+
+    my @sql;
+    if ($db_type eq 'Pg') {
+        @sql = "ALTER SEQUENCE $sequence RESTART WITH $next_id;";
+    }
+    elsif ($db_type eq 'Oracle') {
+        @sql = (
+            "ALTER SEQUENCE $sequence INCREMENT BY " . ($next_id - 1) . ";",
+            "SELECT $sequence.nextval FROM dual;",
+            "ALTER SEQUENCE $sequence INCREMENT BY 1;",
+        );
+    }
+
+    $RT::Logger->debug($_) for @sql;
+    $admin_dbh->do($_) for @sql;
+}
+
+sub get_dba_password {
+    return "" if $db_type eq 'SQLite';
+    print "In order to create or update your RT database,"
+        . " this script needs to connect to your "
+        . " $db_type instance on $db_host (port '$db_port') as $dba_user\n";
+    print "Please specify that user's database password below. If the user has no database\n";
+    print "password, just press return.\n\n";
+    print "Password: ";
+    ReadMode('noecho');
+    my $password = ReadLine(0);
+    ReadMode('normal');
+    print "\n";
+    return ($password);
+}
+
+sub get_admin_dbh {
+    return _get_dbh( RT::Handle->DSN, $dba_user, $dba_pass );
+}
+
+sub _get_dbh {
+    my ($dsn, $user, $pass) = @_;
+    my $dbh = DBI->connect(
+        $dsn, $user, $pass,
+        { RaiseError => 0, PrintError => 0 },
+    );
+    unless ( $dbh ) {
+        my $msg = "Failed to connect to $dsn as user '$user': ". $DBI::errstr;
+        if ( $args{'debug'} ) {
+            require Carp; Carp::confess( $msg );
+        } else {
+            print STDERR $msg; exit -1;
+        }
+    }
+    return $dbh;
+}

commit b10507b8d5ef906367eb5fe481b526b06f71dd71
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jul 7 03:15:40 2020 +0800

    Add upgrade note for extension RT-Extension-ConfigInDatabase

diff --git a/docs/UPGRADING-5.0 b/docs/UPGRADING-5.0
index 70d17cb263..416d799707 100644
--- a/docs/UPGRADING-5.0
+++ b/docs/UPGRADING-5.0
@@ -193,6 +193,15 @@ F<etc/upgrade/upgrade-authtokens> utility after completing all the other
 upgrade steps from the F<README>. Please also remove
 F<local/plugins/RT-Authen-Token> from your RT installation.
 
+=item *
+
+Users who are currently using the L<RT::Extension::ConfigInDatabase>
+extension should remove C<RT::Extension::ConfigInDatabase> from the plugin
+list and run the F<etc/upgrade/upgrade-configurations> utility after
+completing all the other upgrade steps from the F<README>. Please also
+remove F<local/plugins/RT-Extension-ConfigInDatabase> from your RT
+installation.
+
 =back
 
 =cut

commit 2427e416a0cc195cc96d95bb0f6de3f2741dc944
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jul 7 03:15:54 2020 +0800

    Clean up obsolete code imported from ConfigInDatabase extension

diff --git a/lib/RT/Configuration.pm b/lib/RT/Configuration.pm
index 9a1875cec7..5fa1a320d4 100644
--- a/lib/RT/Configuration.pm
+++ b/lib/RT/Configuration.pm
@@ -53,8 +53,6 @@ use 5.10.1;
 package RT::Configuration;
 use base 'RT::Record';
 
-use Storable ();
-use MIME::Base64;
 use JSON ();
 
 =head1 NAME
@@ -79,7 +77,7 @@ Must be unique.
 =item Content
 
 If you provide a reference, we will automatically serialize the data structure
-using L<Storable>. Otherwise any string is passed through as-is.
+using L<Data::Dumper>. Otherwise any string is passed through as-is.
 
 =item ContentType
 

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


More information about the rt-commit mailing list