[Rt-commit] r11110 - in rt/branches/3.8-TESTING: lib/RT

ruz at bestpractical.com ruz at bestpractical.com
Tue Mar 18 22:44:43 EDT 2008


Author: ruz
Date: Tue Mar 18 22:44:43 2008
New Revision: 11110

Modified:
   rt/branches/3.8-TESTING/lib/RT/Handle.pm
   rt/branches/3.8-TESTING/sbin/rt-setup-database.in

Log:
* replace 'die' with error reporting in RT::Handle

Modified: rt/branches/3.8-TESTING/lib/RT/Handle.pm
==============================================================================
--- rt/branches/3.8-TESTING/lib/RT/Handle.pm	(original)
+++ rt/branches/3.8-TESTING/lib/RT/Handle.pm	Tue Mar 18 22:44:43 2008
@@ -205,7 +205,7 @@
 
 sub CreateDatabase {
     my $self = shift;
-    my $dbh  = shift or return (0, die "No DBI handle provided");
+    my $dbh  = shift or return (0, "No DBI handle provided");
     my $db_type = RT->Config->Get('DatabaseType');
     my $db_name = RT->Config->Get('DatabaseName');
 
@@ -278,30 +278,30 @@
     my $db_type = RT->Config->Get('DatabaseType');
     return if $db_type eq 'SQLite';
 
-    die "'$base_path' doesn't exist" unless -e $base_path;
+    return (0, "'$base_path' doesn't exist") unless -e $base_path;
 
     my $path;
     if ( -d $base_path ) {
         $path = File::Spec->catfile( $base_path, "acl.$db_type");
         $path = File::Spec->catfile( $base_path, "acl")
             unless -e $path;
-        die "Couldn't find ACLs for $db_type"
+        return (0, "Couldn't find ACLs for $db_type")
             unless -e $path;
     } else {
         $path = $base_path;
     }
 
     local *acl;
-    do $path || die "Couldn't load ACLs: " . $@;
+    do $path || return (0, "Couldn't load ACLs: " . $@);
     my @acl = acl($dbh);
     foreach my $statement (@acl) {
-#        print STDERR $statement if $args{'debug'};
-        my $sth = $dbh->prepare($statement) or die $dbh->errstr;
+        my $sth = $dbh->prepare($statement)
+            or return (0, "Couldn't prepare SQL query:\n $statement\n\nERROR: ". $dbh->errstr);
         unless ( $sth->execute ) {
-            die "Problem with statement:\n $statement\n" . $sth->errstr;
+            return (0, "Couldn't run SQL query:\n $statement\n\nERROR: ". $sth->errstr);
         }
     }
-    print "Done setting up database ACLs.\n";
+    return (1);
 }
 
 =head2 InsertSchema
@@ -312,18 +312,24 @@
     my $self = shift;
     my $dbh  = shift || $self->dbh;
     my $base_path = (shift || $RT::EtcPath);
-    my $db_type = RT->Config->Get('DatabaseType');
 
-    my $file = get_version_file( $base_path . "/schema." . $db_type );
+    my $file;
+    if ( -d $base_path ) {
+        my $db_type = RT->Config->Get('DatabaseType');
+        $file = $base_path . "/schema." . $db_type;
+    } else {
+        $file = $base_path;
+    }
+
+    $file = get_version_file( $file );
     unless ( $file ) {
-        die "Couldn't find schema file in '$base_path' dir";
+        return (0, "Couldn't find schema file(s) '$file*'");
     }
     unless ( -f $file || -r $file ) {
-        die "File '$file' doesn't exist or couldn't be read";
+        return (0, "File '$file' doesn't exist or couldn't be read");
     }
 
     my (@schema);
-    print "Creating database schema.\n";
 
     open my $fh_schema, "<$file";
 
@@ -345,20 +351,21 @@
     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;
+    my $is_local = 0;
+    $dbh->begin_work or return (0, "Couldn't begin transaction: ". $dbh->errstr);
     foreach my $statement (@schema) {
-        if ( $statement =~ /^\s*;$/ ) { $is_local = 1; next; }
+        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;
+        my $sth = $dbh->prepare($statement)
+            or return (0, "Couldn't prepare SQL query:\n$statement\n\nERROR: ". $dbh->errstr);
         unless ( $sth->execute or $is_local ) {
-            die "Problem with statement:\n$statement\n" . $sth->errstr;
+            return (0, "Couldn't run SQL query:\n$statement\n\nERROR: ". $sth->errstr);
         }
     }
-    $dbh->commit or die $dbh->errstr;
-
-    print "Done setting up database schema.\n";
+    $dbh->commit or return (0, "Couldn't commit transaction: ". $dbh->errstr);
+    return (1);
 }
 
 =head1 get_version_file
@@ -484,8 +491,9 @@
     local (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
            @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
 
-    require $datafile
-      || die "Couldn't find initial data for import\n" . $@;
+    local $@;
+    eval { require $datafile }
+      or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:". $@);
 
     if ( @Initial ) {
         print "Running initial actions...\n";

Modified: rt/branches/3.8-TESTING/sbin/rt-setup-database.in
==============================================================================
--- rt/branches/3.8-TESTING/sbin/rt-setup-database.in	(original)
+++ rt/branches/3.8-TESTING/sbin/rt-setup-database.in	Tue Mar 18 22:44:43 2008
@@ -140,16 +140,13 @@
     ."User:\t$db_user\nDBA:\t$dba_user\n";
 
 foreach my $action ( @actions ) {
+    my ($status, $msg) = (1, '');
     if ( $action eq 'create' ) {
         my $dbh = get_system_dbh();
         print "Now creating a $db_type database $db_name for RT.\n";
-        my ($status, $msg) = RT::Handle->CreateDatabase( $dbh );
-        die "Couldn't create a DB: $msg" unless $status;
-        print "Done.\n";
+        ($status, $msg) = RT::Handle->CreateDatabase( $dbh );
     }
     elsif ( $action eq 'drop' ) {
-        my $dbh = get_system_dbh();
-
         if ( $db_type eq 'Oracle' ) {
             print <<END;
 
@@ -172,12 +169,8 @@
             exit(-2) unless _yesno();
         }
 
-        my ($status, $msg) = RT::Handle->DropDatabase( $dbh );
-        unless ( $status ) {
-            print STDERR "Couldn't delete database: $msg\n";
-            exit(-1);
-        }
-        print "Done.\n";
+        my $dbh = get_system_dbh();
+        ($status, $msg) = RT::Handle->DropDatabase( $dbh );
     }
     elsif ( $action eq 'schema' ) {
         my $dbh = get_admin_dbh();
@@ -187,7 +180,7 @@
         $RT::Handle->dbh( $dbh );
 
         print "Now populating database schema.\n";
-        $RT::Handle->InsertSchema( $dbh, $args{'datafile'} || $args{'datadir'} );
+        ($status, $msg) = $RT::Handle->InsertSchema( $dbh, $args{'datafile'} || $args{'datadir'} );
     }
     elsif ( $action eq 'acl' ) {
         my $dbh = get_admin_dbh();
@@ -197,7 +190,7 @@
         $RT::Handle->dbh( $dbh );
 
         print "Now inserting database ACLs\n";
-        $RT::Handle->InsertACL( $dbh, $args{'datafile'} || $args{'datadir'} );
+        ($status, $msg) = $RT::Handle->InsertACL( $dbh, $args{'datafile'} || $args{'datadir'} );
     }
     elsif ( $action eq 'coredata' ) {
         $RT::Handle = new RT::Handle;
@@ -208,7 +201,7 @@
         check_db_compatibility( $RT::Handle->dbh );
 
         print "Now inserting RT core system objects\n";
-        $RT::Handle->InsertInitialData();
+        ($status, $msg) = $RT::Handle->InsertInitialData;
     }
     elsif ( $action eq 'insert' ) {
         $RT::Handle = new RT::Handle;
@@ -219,8 +212,14 @@
         my $file = $args{'datafile'};
         $file = $RT::EtcPath . "/initialdata" if $init && !$file;
         $file ||= $args{'datadir'}."/content";
-        $RT::Handle->InsertData( $file );
+        ($status, $msg) = $RT::Handle->InsertData( $file );
+    }
+    unless ( $status ) {
+        print STDERR "Couldn't finish '$action' step.\n\n";
+        print STDERR "ERROR: $msg\n\n";
+        exit(-1);
     }
+    print "Done.\n";
 }
 
 sub check_db_compatibility {


More information about the Rt-commit mailing list