[Rt-commit] r5212 - in rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES: .

ruz at bestpractical.com ruz at bestpractical.com
Fri May 12 16:24:40 EDT 2006


Author: ruz
Date: Fri May 12 16:24:37 2006
New Revision: 5212

Modified:
   rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES/   (props changed)
   rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES/sbin/rt-setup-database.in

Log:
 r2875 at cubic-pc:  cubic | 2006-05-12 19:17:03 +0400
 * reshufle functions around
 * drop noisy comments # {{{|}}}.*\n


Modified: rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES/sbin/rt-setup-database.in
==============================================================================
--- rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES/sbin/rt-setup-database.in	(original)
+++ rt/branches/3.5-EXPERIMENTAL-MYSQL-UPDATES/sbin/rt-setup-database.in	Fri May 12 16:24:37 2006
@@ -175,100 +175,29 @@
     exit(-1);
 }
 
-# {{{ sub insert_schema
-sub insert_schema {
-    my $base_path = (shift || $RT::EtcPath);
-
-    my $file = get_version_file( $base_path . "/schema." . $RT::DatabaseType );
-    unless ( $file ) {
-        die "Couldn't find schema file in '$base_path' dir";
-    }
-    unless ( -f $file || -r $file ) {
-        die "File '$file' doesn't exist or couldn't be read";
+sub create_db {
+    print "Creating $RT::DatabaseType database $RT::DatabaseName.\n";
+    if ( $RT::DatabaseType eq 'SQLite' ) {
+        return;
     }
-
-    my (@schema);
-    print "Creating database schema.\n";
-
-    no warnings 'unopened';
-
-    open my $fh_schema, "<$file";
-    open my $fh_schema_local, "<" . get_version_file( $RT::LocalEtcPath . "/schema." . $RT::DatabaseType );
-
-    my $statement = "";
-    foreach my $line (<$fh_schema>, ($_ = ';;'), <$fh_schema_local>) {
-        $line =~ s/\#.*//g;
-        $line =~ s/--.*//g;
-        $statement .= $line;
-        if ( $line =~ /;(\s*)$/ ) {
-            $statement =~ s/;(\s*)$//g;
-            push @schema, $statement;
-            $statement = "";
+    elsif ( $RT::DatabaseType eq 'Pg' ) {
+        $dbh->do("CREATE DATABASE $RT::DatabaseName WITH ENCODING='UNICODE'");
+        if ( $DBI::errstr ) {
+            $dbh->do("CREATE DATABASE $RT::DatabaseName") || die $DBI::errstr;
         }
     }
-    close $fh_schema; close $fh_schema_local;
-
-    local $SIG{__WARN__} = sub {};
-    my $is_local = 0; # local/etc/schema needs to be nonfatal.
-    $dbh->begin_work or die $dbh->errstr;
-    foreach my $statement (@schema) {
-        if ( $statement =~ /^\s*;$/ ) { $is_local = 1; next; }
-
-        print "Executing SQL:\n$statement\n" if defined $args{'debug'};
-        my $sth = $dbh->prepare($statement) or die $dbh->errstr;
-        unless ( $sth->execute or $is_local ) {
-            die "Problem with statement:\n$statement\n" . $sth->errstr;
-        }
+    elsif ( $RT::DatabaseType eq 'Oracle' ) {
+        insert_acl();
     }
-    $dbh->commit or die $dbh->errstr;
-
-    print "Done setting up database schema.\n";
-}
-
-=head1 get_version_file
-
-Takes base name of the file as argument, scans for <base name>-<version> named
-files and returns file name with closest version to the version of the RT DB.
-
-=cut
-
-sub get_version_file {
-    my $base_name = shift;
-
-    require File::Glob;
-    my @files = File::Glob::bsd_glob("$base_name*");
-    return '' unless @files;
-
-    my %version = map { $_ =~ /\.\w+-([-\w\.]+)$/; ($1||0) => $_ } @files;
-    my $db_version = connect_rt_handle()->DatabaseVersion;
-    print "Server version $db_version\n";
-    my $version;
-    foreach ( reverse sort cmp_version keys %version ) {
-        print "version $_\n"; print "comparision ".  cmp_version( $db_version, $_ ) ."\n";
-        if ( cmp_version( $db_version, $_ ) >= 0 ) {
-            $version = $_;
-            last;
-        }
+    elsif ( $RT::DatabaseType eq 'Informix' ) {
+        $ENV{DB_LOCALE} = 'en_us.utf8';
+        $dbh->do("CREATE DATABASE $RT::DatabaseName WITH BUFFERED LOG");
     }
-
-    return defined $version? $version{ $version } : undef;
-}
-
-sub cmp_version($$) {
-    my ($a, $b) = (@_);
-    my @a = split /[^0-9]+/, $a;
-    my @b = split /[^0-9]+/, $b;
-    for ( my $i = 0; $i < @a; $i++ ) {
-        return 1 unless defined $b[$i];
-        return $a[$i] <=> $b[$i] if $a[$i] <=> $b[$i];
+    else {
+        $dbh->do("CREATE DATABASE $RT::DatabaseName") or die $DBI::errstr;
     }
-    return 0 if @a == @b;
-    return -1;
 }
 
-# }}}
-
-# {{{ sub drop_db
 sub drop_db {
     if ( $RT::DatabaseType eq 'Oracle' ) {
         print <<END;
@@ -300,61 +229,8 @@
     $dbh->do("DROP DATABASE $RT::DatabaseName") or warn $DBI::errstr;
 }
 
-# }}}
 
-# {{{ sub create_db
-sub create_db {
-    print "Creating $RT::DatabaseType database $RT::DatabaseName.\n";
-    if ( $RT::DatabaseType eq 'SQLite' ) {
-        return;
-    }
-    elsif ( $RT::DatabaseType eq 'Pg' ) {
-        $dbh->do("CREATE DATABASE $RT::DatabaseName WITH ENCODING='UNICODE'");
-        if ( $DBI::errstr ) {
-            $dbh->do("CREATE DATABASE $RT::DatabaseName") || die $DBI::errstr;
-        }
-    }
-    elsif ( $RT::DatabaseType eq 'Oracle' ) {
-        insert_acl();
-    }
-    elsif ( $RT::DatabaseType eq 'Informix' ) {
-        $ENV{DB_LOCALE} = 'en_us.utf8';
-        $dbh->do("CREATE DATABASE $RT::DatabaseName WITH BUFFERED LOG");
-    }
-    else {
-        $dbh->do("CREATE DATABASE $RT::DatabaseName") or die $DBI::errstr;
-    }
-}
 
-# }}}
-
-sub get_dba_password {
-    print "In order to create or update your RT database,";
-    print "this script needs to connect to your "
-      . $RT::DatabaseType
-      . " instance on "
-      . $RT::DatabaseHost . " as "
-      . $args{'dba'} . ".\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 _yesno
-sub _yesno {
-    print "Proceed [y/N]:";
-    my $x = scalar(<STDIN>);
-    $x =~ /^y/i;
-}
-
-# }}}
-
-# {{{ insert_acls
 sub insert_acl {
     my $base_path = (shift || $RT::EtcPath);
 
@@ -395,53 +271,56 @@
     print "Done setting up database ACLs.\n";
 }
 
-# }}}
-
-=head2 get_system_dsn
-
-Returns a dsn suitable for database creates and drops
-and user creates and drops
-
-=cut
-
-sub get_system_dsn {
-
-    my $dsn = $Handle->DSN;
-
-    #with mysql, you want to connect sans database to funge things
-    if ( $RT::DatabaseType eq 'mysql' ) {
-        $dsn =~ s/dbname=$RT::DatabaseName//;
+sub insert_schema {
+    my $base_path = (shift || $RT::EtcPath);
 
-        # with postgres, you want to connect to database1
-    }
-    elsif ( $RT::DatabaseType eq 'Pg' ) {
-        $dsn =~ s/dbname=$RT::DatabaseName/dbname=template1/;
+    my $file = get_version_file( $base_path . "/schema." . $RT::DatabaseType );
+    unless ( $file ) {
+        die "Couldn't find schema file in '$base_path' dir";
     }
-    elsif ( $RT::DatabaseType eq 'Informix' ) {
-        # with Informix, you want to connect sans database:
-        $dsn =~ s/Informix:$RT::DatabaseName/Informix:/;
+    unless ( -f $file || -r $file ) {
+        die "File '$file' doesn't exist or couldn't be read";
     }
-    return $dsn;
-}
 
-=head2 connect_rt_handle
+    my (@schema);
+    print "Creating database schema.\n";
 
-Returns connected C<$RT::Handle>, connect with credentials from RT config.
+    no warnings 'unopened';
 
-=cut
+    open my $fh_schema, "<$file";
+    open my $fh_schema_local, "<" . get_version_file( $RT::LocalEtcPath . "/schema." . $RT::DatabaseType );
 
-sub connect_rt_handle {
+    my $statement = "";
+    foreach my $line (<$fh_schema>, ($_ = ';;'), <$fh_schema_local>) {
+        $line =~ s/\#.*//g;
+        $line =~ s/--.*//g;
+        $statement .= $line;
+        if ( $line =~ /;(\s*)$/ ) {
+            $statement =~ s/;(\s*)$//g;
+            push @schema, $statement;
+            $statement = "";
+        }
+    }
+    close $fh_schema; close $fh_schema_local;
 
-    require RT::Handle;
+    local $SIG{__WARN__} = sub {};
+    my $is_local = 0; # local/etc/schema needs to be nonfatal.
+    $dbh->begin_work or die $dbh->errstr;
+    foreach my $statement (@schema) {
+        if ( $statement =~ /^\s*;$/ ) { $is_local = 1; next; }
 
-    unless ( $RT::Handle ) {
-        $RT::Handle = RT::Handle->new;
-        $RT::Handle->Connect;
+        print "Executing SQL:\n$statement\n" if defined $args{'debug'};
+        my $sth = $dbh->prepare($statement) or die $dbh->errstr;
+        unless ( $sth->execute or $is_local ) {
+            die "Problem with statement:\n$statement\n" . $sth->errstr;
+        }
     }
+    $dbh->commit or die $dbh->errstr;
 
-    return $RT::Handle;
+    print "Done setting up database schema.\n";
 }
 
+
 sub insert_initial_data {
 
     RT::InitLogging();
@@ -706,6 +585,115 @@
     print "Done setting up database content.\n";
 }
 
+sub get_dba_password {
+    print "In order to create or update your RT database,";
+    print "this script needs to connect to your "
+      . $RT::DatabaseType
+      . " instance on "
+      . $RT::DatabaseHost . " as "
+      . $args{'dba'} . ".\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 _yesno {
+    print "Proceed [y/N]:";
+    my $x = scalar(<STDIN>);
+    $x =~ /^y/i;
+}
+
+=head2 get_system_dsn
+
+Returns a dsn suitable for database creates and drops
+and user creates and drops
+
+=cut
+
+sub get_system_dsn {
+
+    my $dsn = $Handle->DSN;
+
+    #with mysql, you want to connect sans database to funge things
+    if ( $RT::DatabaseType eq 'mysql' ) {
+        $dsn =~ s/dbname=$RT::DatabaseName//;
+
+        # with postgres, you want to connect to database1
+    }
+    elsif ( $RT::DatabaseType eq 'Pg' ) {
+        $dsn =~ s/dbname=$RT::DatabaseName/dbname=template1/;
+    }
+    elsif ( $RT::DatabaseType eq 'Informix' ) {
+        # with Informix, you want to connect sans database:
+        $dsn =~ s/Informix:$RT::DatabaseName/Informix:/;
+    }
+    return $dsn;
+}
+
+=head2 connect_rt_handle
+
+Returns connected C<$RT::Handle>, connect with credentials from RT config.
+
+=cut
+
+sub connect_rt_handle {
+
+    require RT::Handle;
+
+    unless ( $RT::Handle ) {
+        $RT::Handle = RT::Handle->new;
+        $RT::Handle->Connect;
+    }
+
+    return $RT::Handle;
+}
+
+=head1 get_version_file
+
+Takes base name of the file as argument, scans for <base name>-<version> named
+files and returns file name with closest version to the version of the RT DB.
+
+=cut
+
+sub get_version_file {
+    my $base_name = shift;
+
+    require File::Glob;
+    my @files = File::Glob::bsd_glob("$base_name*");
+    return '' unless @files;
+
+    my %version = map { $_ =~ /\.\w+-([-\w\.]+)$/; ($1||0) => $_ } @files;
+    my $db_version = connect_rt_handle()->DatabaseVersion;
+    print "Server version $db_version\n";
+    my $version;
+    foreach ( reverse sort cmp_version keys %version ) {
+        print "version $_\n"; print "comparision ".  cmp_version( $db_version, $_ ) ."\n";
+        if ( cmp_version( $db_version, $_ ) >= 0 ) {
+            $version = $_;
+            last;
+        }
+    }
+
+    return defined $version? $version{ $version } : undef;
+}
+
+sub cmp_version($$) {
+    my ($a, $b) = (@_);
+    my @a = split /[^0-9]+/, $a;
+    my @b = split /[^0-9]+/, $b;
+    for ( my $i = 0; $i < @a; $i++ ) {
+        return 1 unless defined $b[$i];
+        return $a[$i] <=> $b[$i] if $a[$i] <=> $b[$i];
+    }
+    return 0 if @a == @b;
+    return -1;
+}
+
 =head2 ACLEquivGroupId
 
 Given a userid, return that user's acl equivalence group


More information about the Rt-commit mailing list