[Rt-commit] rt branch, 4.0/database-init-in-tests, created. rt-3.9.7-1201-g49c7b26

Alex Vandiver alexmv at bestpractical.com
Thu Feb 17 00:31:46 EST 2011


The branch, 4.0/database-init-in-tests has been created
        at  49c7b26300aab43b869b63ef31783f61be3b5879 (commit)

- Log -----------------------------------------------------------------
commit 70d0b47622d0d5851c4319364ea89e808e87cd4a
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Jan 18 10:14:06 2011 +0300

    make it possible to override user/pass in ::Handle->Connect

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 49b5140..6f1118f 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -103,6 +103,7 @@ Takes nothing.
 
 sub Connect {
     my $self = shift;
+    my %args = (@_);
 
     my $db_type = RT->Config->Get('DatabaseType');
     if ( $db_type eq 'Oracle' ) {
@@ -113,6 +114,7 @@ sub Connect {
     $self->SUPER::Connect(
         User => RT->Config->Get('DatabaseUser'),
         Password => RT->Config->Get('DatabasePassword'),
+        %args,
     );
 
     if ( $db_type eq 'mysql' ) {

commit 7e8c06de31d1c50ff28c794d8854e253956c7801
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Jan 18 10:17:02 2011 +0300

    don't connect using system DSN when we don't need it

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index d0d465e..db0dd23 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -353,10 +353,6 @@ sub bootstrap_db {
     }
 
     require RT::Handle;
-    # bootstrap with dba cred
-    my $dbh = _get_dbh(RT::Handle->SystemDSN,
-               $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD});
-
     if (my $forceopt = $ENV{RT_TEST_FORCE_OPT}) {
         Test::More::diag "forcing $forceopt";
         $args{$forceopt}=1;
@@ -371,6 +367,12 @@ sub bootstrap_db {
     }
 
     unless ($args{nodb}) {
+        # bootstrap with dba cred
+        my $dbh = _get_dbh(
+            RT::Handle->SystemDSN,
+            $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD}
+        );
+
         unless ( $ENV{RT_TEST_PARALLEL} ) {
             # already dropped db in parallel tests, need to do so for other cases.
             RT::Handle->DropDatabase( $dbh, Force => 1 )

commit a150639b63733c99637319232a8ab81dbc6cfc64
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Jan 18 10:18:03 2011 +0300

    __reconnect_rt and __disconnect_rt functions

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index db0dd23..606cd1f 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -510,6 +510,33 @@ sub _get_dbh {
     return $dbh;
 }
 
+sub __reconnect_rt {
+    my $as_dba = shift;
+    __disconnect_rt();
+
+    # look at %DBIHandle and $PrevHandle in DBIx::SB::Handle for explanation
+    $RT::Handle = RT::Handle->new;
+    $RT::Handle->dbh( undef );
+    $RT::Handle->Connect(
+        $as_dba
+        ? (User => $ENV{RT_DBA_USER}, Password => $ENV{RT_DBA_PASSWORD})
+        : ()
+    );
+    $RT::Handle->PrintError;
+    $RT::Handle->dbh->{PrintError} = 1;
+    return $RT::Handle->dbh;
+}
+
+sub __disconnect_rt {
+    # look at %DBIHandle and $PrevHandle in DBIx::SB::Handle for explanation
+    return unless $RT::Handle;
+    my $dbh = $RT::Handle->dbh;
+    $RT::Handle->dbh(undef);
+    $dbh->disconnect if $dbh;
+    $RT::Handle = undef;
+}
+
+
 =head1 UTILITIES
 
 =head2 load_or_create_user

commit 6b0288d8cdb9094e8e876bf289e8970eaa0d405a
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Jan 18 10:20:20 2011 +0300

    use reconnect and disconnect functions

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 606cd1f..aaa2cdb 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -143,7 +143,7 @@ sub import {
 
     RT::InitPluginPaths();
 
-    RT::ConnectToDatabase()
+    __reconnect_rt()
         unless $args{nodb};
 
     RT::InitClasses();
@@ -381,34 +381,23 @@ sub bootstrap_db {
         $dbh->disconnect;
         $created_new_db++;
 
-        $dbh = _get_dbh(RT::Handle->DSN,
-                        $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD});
-
-        $RT::Handle = RT::Handle->new;
-        $RT::Handle->dbh( $dbh );
-        $RT::Handle->InsertSchema( $dbh );
+        __reconnect_rt('dba');
+        $RT::Handle->InsertSchema;
 
         my $db_type = RT->Config->Get('DatabaseType');
-        $RT::Handle->InsertACL( $dbh ) unless $db_type eq 'Oracle';
+        $RT::Handle->InsertACL unless $db_type eq 'Oracle';
 
-        $RT::Handle = RT::Handle->new;
-        $RT::Handle->dbh( undef );
-        RT->ConnectToDatabase;
         RT->InitLogging;
 
         unless ($args{noinitialdata}) {
+            __reconnect_rt();
             $RT::Handle->InsertInitialData;
 
             DBIx::SearchBuilder::Record::Cachable->FlushCache;
         }
 
-        $RT::Handle = RT::Handle->new;
-        $RT::Handle->dbh( undef );
-        RT->ConnectToDatabase();
-        $RT::Handle->PrintError;
-        $RT::Handle->dbh->{PrintError} = 1;
-
         unless ( $args{'nodata'} ) {
+            __reconnect_rt();
             $RT::Handle->InsertData( $RT::EtcPath . "/initialdata" );
             DBIx::SearchBuilder::Record::Cachable->FlushCache;
         }
@@ -1252,12 +1241,10 @@ sub start_plack_server {
         my $Tester = Test::Builder->new;
         $Tester->ok(1, @_);
 
-        $RT::Handle = RT::Handle->new;
-        $RT::Handle->dbh( undef );
-        RT->ConnectToDatabase;
         # the attribute cache holds on to a stale dbh
         delete $RT::System->{attributes};
 
+        __reconnect_rt();
         return ("http://localhost:$port", RT::Test::Web->new);
     }
 
@@ -1405,10 +1392,9 @@ END {
     if ( $ENV{RT_TEST_PARALLEL} && $created_new_db ) {
 
         # Pg doesn't like if you issue a DROP DATABASE while still connected
-        my $dbh = $RT::Handle->dbh;
-        $dbh->disconnect if $dbh;
+        __disconnect_rt();
 
-        $dbh = _get_dbh( RT::Handle->SystemDSN, $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD} );
+        my $dbh = _get_dbh( RT::Handle->SystemDSN, $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD} );
         RT::Handle->DropDatabase( $dbh, Force => 1 );
         $dbh->disconnect;
     }

commit 86724f50eda221da619a0db2525ea6a418d175c5
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Jan 18 10:46:07 2011 +0300

    Ensure that database handles are not left open across calls to fork()
    
    This solves a problem wherein the previous live database handle was
    still connected during cleanup in the parent, causing Postgres to be
    unable to drop the database.

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index aaa2cdb..edb0700 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -1226,6 +1226,7 @@ sub start_plack_server {
              kill 'USR1' => getppid();
          });
 
+    __disconnect_rt();
     my $pid = fork();
     die "failed to fork" unless defined $pid;
 

commit 4523baff8adedc5e1aaac2d524340189716578f7
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Feb 16 22:35:35 2011 -0500

    Give Apache testing the same disconnect/reconnect across the fork treatment

diff --git a/lib/RT/Test/Apache.pm b/lib/RT/Test/Apache.pm
index ae40e4c..a822741 100644
--- a/lib/RT/Test/Apache.pm
+++ b/lib/RT/Test/Apache.pm
@@ -158,6 +158,7 @@ sub find_apache_server {
 sub fork_exec {
     my $self = shift;
 
+    RT::Test::__disconnect_rt();
     my $pid = fork;
     unless ( defined $pid ) {
         die "cannot fork: $!";
@@ -165,6 +166,7 @@ sub fork_exec {
         exec @_;
         die "can't exec `". join(' ', @_) ."` program: $!";
     } else {
+        RT::Test::__reconnect_rt();
         return $pid;
     }
 }

commit 17f20f4d7702c8995d6e254c6bb4ea5d9d3ad509
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Feb 10 01:43:10 2011 +0300

    extract create and drop database private helpers

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index edb0700..a7bef23 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -367,19 +367,7 @@ sub bootstrap_db {
     }
 
     unless ($args{nodb}) {
-        # bootstrap with dba cred
-        my $dbh = _get_dbh(
-            RT::Handle->SystemDSN,
-            $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD}
-        );
-
-        unless ( $ENV{RT_TEST_PARALLEL} ) {
-            # already dropped db in parallel tests, need to do so for other cases.
-            RT::Handle->DropDatabase( $dbh, Force => 1 )
-        }
-        RT::Handle->CreateDatabase( $dbh );
-        $dbh->disconnect;
-        $created_new_db++;
+        __create_database();
 
         __reconnect_rt('dba');
         $RT::Handle->InsertSchema;
@@ -499,6 +487,39 @@ sub _get_dbh {
     return $dbh;
 }
 
+sub __create_database {
+    # bootstrap with dba cred
+    my $dbh = _get_dbh(
+        RT::Handle->SystemDSN,
+        $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD}
+    );
+
+    unless ( $ENV{RT_TEST_PARALLEL} ) {
+        # already dropped db in parallel tests, need to do so for other cases.
+        __drop_database( $dbh );
+
+    }
+    RT::Handle->CreateDatabase( $dbh );
+    $dbh->disconnect;
+    $created_new_db++;
+}
+
+sub __drop_database {
+    my $dbh = shift;
+
+    # Pg doesn't like if you issue a DROP DATABASE while still connected
+    # it's still may fail if web-server is out there and holding a connection
+    __disconnect_rt();
+
+    my $my_dbh = $dbh? 0 : 1;
+    $dbh ||= _get_dbh(
+        RT::Handle->SystemDSN,
+        $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD}
+    );
+    RT::Handle->DropDatabase( $dbh, Force => 1 );
+    $dbh->disconnect if $my_dbh;
+}
+
 sub __reconnect_rt {
     my $as_dba = shift;
     __disconnect_rt();
@@ -1391,13 +1412,7 @@ END {
     }
 
     if ( $ENV{RT_TEST_PARALLEL} && $created_new_db ) {
-
-        # Pg doesn't like if you issue a DROP DATABASE while still connected
-        __disconnect_rt();
-
-        my $dbh = _get_dbh( RT::Handle->SystemDSN, $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD} );
-        RT::Handle->DropDatabase( $dbh, Force => 1 );
-        $dbh->disconnect;
+        __drop_database();
     }
 }
 

commit 34450f9a075110d0168059f4e1545441838af72b
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Feb 10 01:44:29 2011 +0300

    disconnect and clean every possible handle for sure

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index a7bef23..4867a0e 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -541,8 +541,11 @@ sub __disconnect_rt {
     # look at %DBIHandle and $PrevHandle in DBIx::SB::Handle for explanation
     return unless $RT::Handle;
     my $dbh = $RT::Handle->dbh;
-    $RT::Handle->dbh(undef);
     $dbh->disconnect if $dbh;
+
+    %DBIx::SearchBuilder::Handle::DBIHandle = ();
+    $DBIx::SearchBuilder::Handle::PrevHandle = undef;
+
     $RT::Handle = undef;
 }
 

commit 2552e8a4b54dba236c5aed38004818132b4ba467
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Sun Jan 30 20:06:10 2011 -0500

    Remove references to "Force" argument to DropDatabase, removed back in 141625a

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 6f1118f..d55519e 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -350,15 +350,15 @@ sub CreateDatabase {
     return ($status, $DBI::errstr);
 }
 
-=head3 DropDatabase $DBH [Force => 0]
+=head3 DropDatabase $DBH
 
 Drops RT's database. This method can be used as class method.
 
 Takes DBI handle as first argument. Many database systems require
-special handle to allow you to create a new database, so you have
-to use L<SystemDSN> method during connection.
+a special handle to allow you to drop a database, so you may have
+to use L<SystemDSN> when acquiring the DBI handle.
 
-Fetches type and name of the DB from the config.
+Fetches the type and name of the database from the config.
 
 =cut
 
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 4867a0e..fd7fee8 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -516,7 +516,7 @@ sub __drop_database {
         RT::Handle->SystemDSN,
         $ENV{RT_DBA_USER}, $ENV{RT_DBA_PASSWORD}
     );
-    RT::Handle->DropDatabase( $dbh, Force => 1 );
+    RT::Handle->DropDatabase( $dbh );
     $dbh->disconnect if $my_dbh;
 }
 

commit e7ee68b10d52111c8658e3edb443e02764b78200
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Mon Jan 24 22:48:19 2011 -0500

    Only CREATE DATABASE in postgres once
    
    The second CREATE DATABASE line dates from RT 3.0.0; not only have all
    versions of Postgres since 7.1 had support for "WITH ENCODING" (and we
    require 8.1), but lack of Unicode at the database level would likely
    cause other, subtler, errors.

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index d55519e..89da3d7 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -341,8 +341,7 @@ sub CreateDatabase {
     elsif ( $db_type eq 'Pg' ) {
         # XXX: as we get external DBH we don't know if RaiseError or PrintError
         # are enabled, so we have to setup it here and restore them back
-        $status = $dbh->do("CREATE DATABASE $db_name WITH ENCODING='UNICODE' TEMPLATE template0")
-            || $dbh->do("CREATE DATABASE $db_name TEMPLATE template0");
+        $status = $dbh->do("CREATE DATABASE $db_name WITH ENCODING='UNICODE' TEMPLATE template0");
     }
     else {
         $status = $dbh->do("CREATE DATABASE $db_name");

commit 1df624dffe0c3ad75a882530094da2d05908919a
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Mon Jan 24 22:50:46 2011 -0500

    Remove an incorrect comment
    
    Not only is there no reason that this comment would apply only to
    Postgres, but all places which call CreateDatabase do so with a dbh
    explicitly set RaiseError => 0.

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 89da3d7..0711d9c 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -339,8 +339,6 @@ sub CreateDatabase {
         return (1, "Created user $db_user. All RT's objects should be in his schema.");
     }
     elsif ( $db_type eq 'Pg' ) {
-        # XXX: as we get external DBH we don't know if RaiseError or PrintError
-        # are enabled, so we have to setup it here and restore them back
         $status = $dbh->do("CREATE DATABASE $db_name WITH ENCODING='UNICODE' TEMPLATE template0");
     }
     else {

commit f1d13b4b77b3d51362d50877f254e1313a558276
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Feb 16 22:29:37 2011 -0500

    Ensure that FlushCache is called on every disconnect

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index fd7fee8..d0a7a44 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -380,14 +380,11 @@ sub bootstrap_db {
         unless ($args{noinitialdata}) {
             __reconnect_rt();
             $RT::Handle->InsertInitialData;
-
-            DBIx::SearchBuilder::Record::Cachable->FlushCache;
         }
 
         unless ( $args{'nodata'} ) {
             __reconnect_rt();
             $RT::Handle->InsertData( $RT::EtcPath . "/initialdata" );
-            DBIx::SearchBuilder::Record::Cachable->FlushCache;
         }
     }
 }
@@ -547,6 +544,9 @@ sub __disconnect_rt {
     $DBIx::SearchBuilder::Handle::PrevHandle = undef;
 
     $RT::Handle = undef;
+
+    DBIx::SearchBuilder::Record::Cachable->FlushCache
+          if DBIx::SearchBuilder::Record::Cachable->can("FlushCache");
 }
 
 

commit 1477965de50dce7dba4c4a004cff0bcbb6aa6d40
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Feb 16 22:31:48 2011 -0500

    Ensure that the system attribute cache, which holds a DB handle, is cleared on every disconnect

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index d0a7a44..5685196 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -545,6 +545,8 @@ sub __disconnect_rt {
 
     $RT::Handle = undef;
 
+    delete $RT::System->{attributes};
+
     DBIx::SearchBuilder::Record::Cachable->FlushCache
           if DBIx::SearchBuilder::Record::Cachable->can("FlushCache");
 }
@@ -1266,9 +1268,6 @@ sub start_plack_server {
         my $Tester = Test::Builder->new;
         $Tester->ok(1, @_);
 
-        # the attribute cache holds on to a stale dbh
-        delete $RT::System->{attributes};
-
         __reconnect_rt();
         return ("http://localhost:$port", RT::Test::Web->new);
     }

commit 49c7b26300aab43b869b63ef31783f61be3b5879
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Feb 16 22:33:55 2011 -0500

    Don't bail early from __disconnect_rt, to ensure that we clear all handles

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 5685196..6b9419c 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -536,9 +536,7 @@ sub __reconnect_rt {
 
 sub __disconnect_rt {
     # look at %DBIHandle and $PrevHandle in DBIx::SB::Handle for explanation
-    return unless $RT::Handle;
-    my $dbh = $RT::Handle->dbh;
-    $dbh->disconnect if $dbh;
+    $RT::Handle->dbh->disconnect if $RT::Handle and $RT::Handle->dbh;
 
     %DBIx::SearchBuilder::Handle::DBIHandle = ();
     $DBIx::SearchBuilder::Handle::PrevHandle = undef;

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


More information about the Rt-commit mailing list