[Jifty-commit] jifty branch, setupwizard-refactor, updated. ae6b0474bb9d6b05d96d508d733820523d90785b

Jifty commits jifty-commit at lists.jifty.org
Thu Jul 8 10:03:23 EDT 2010


The branch, setupwizard-refactor has been updated
       via  ae6b0474bb9d6b05d96d508d733820523d90785b (commit)
      from  01ada000ff224b77eb6c829048de1aa995275771 (commit)

Summary of changes:
 Makefile.PL         |    2 +-
 lib/Jifty/Handle.pm |   16 ++++++++++-
 lib/Jifty/Schema.pm |   74 +++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 73 insertions(+), 19 deletions(-)

- Log -----------------------------------------------------------------
commit ae6b0474bb9d6b05d96d508d733820523d90785b
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Jul 8 10:04:13 2010 -0400

    Refactor manage_database_existence for easier calling from outside

diff --git a/Makefile.PL b/Makefile.PL
index 534d964..b63123c 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -55,7 +55,7 @@ requires('Hash::MultiValue', 0.05);
 requires('IO::Handle::Util');
 requires('IPC::PubSub' => '0.23' );
 requires('IPC::Run3');
-requires('Jifty::DBI' => '0.62' );            # Jifty::DBI::Collection Jifty::DBI::Handle Jifty::DBI::Record::Cachable Jifty::DBI::SchemaGenerator
+requires('Jifty::DBI' => '0.62' );            # Jifty::DBI::Collection Jifty::DBI::Handle Jifty::DBI::Record::Cachable Jifty::DBI::SchemaGenerator Class::ReturnValue
 requires('JSON' => 2.17);
 requires('List::MoreUtils');
 requires('Locale::Maketext::Extract' => '0.35');
diff --git a/lib/Jifty/Handle.pm b/lib/Jifty/Handle.pm
index dbff226..c22ce09 100644
--- a/lib/Jifty/Handle.pm
+++ b/lib/Jifty/Handle.pm
@@ -15,6 +15,7 @@ database
 =cut
 
 use Jifty::Util;
+use Class::ReturnValue;
 our @ISA;
 
 =head1 METHODS
@@ -273,11 +274,24 @@ sub drop_database {
     my $driver   = Jifty->config->framework('Database')->{'Driver'};
     if ( $mode eq 'print' ) {
         print "DROP DATABASE $database;\n";
+        return 1;
     } elsif ( $driver =~ /SQLite/ ) {
 
         # Win32 complains when you try to unlink open DB
         $self->disconnect if $^O eq 'MSWin32';
-        return unlink($database);
+
+        if ( not unlink $database ) {
+            # simple_query returns a RV, so we should too
+            my $ret = Class::ReturnValue->new;
+            $ret->as_error(
+                errno   => $!,
+                message => "Unable to unlink the database '$database': $!",
+                do_backtrace => undef
+            );
+            return ( $ret->return_value );
+        }
+
+        return 1;
     } else {
         local $SIG{__WARN__}
             = sub { warn $_[0] unless $_[0] =~ /exist|couldn't execute/i };
diff --git a/lib/Jifty/Schema.pm b/lib/Jifty/Schema.pm
index 18da178..bfff733 100644
--- a/lib/Jifty/Schema.pm
+++ b/lib/Jifty/Schema.pm
@@ -197,26 +197,66 @@ Dies with an error message if the database drop or create fails.
 sub manage_database_existence {
     my $self = shift;
 
-    my $handle = $self->connect_to_db_for_management();
+    return if not $self->flags->{'drop_database'}
+           or not $self->flags->{'create_database'};
 
-    if ( $self->flags->{'print'} ) {
-        $handle->drop_database('print')   if ( $self->flags->{'drop_database'} );
-        $handle->create_database('print') if ( $self->flags->{'create_database'} );
-    } else {
-        if ( $self->flags->{'drop_database'} ) {
-            my $ret = $handle->drop_database('execute');
-            die "Error dropping database: ". $ret->error_message
-                unless $ret or $ret->error_message =~ /database .*?(?:does not|doesn't) exist|unknown database/i;
-        }
+    my $handle = $self->connect_to_db_for_management();
 
-        if ( $self->flags->{'create_database'} ) {
-            my $ret = $handle->create_database('execute');
-            die "Error creating database: ". $ret->error_message unless $ret;
-        }
+    # Drop the DB if necessary
+    if ( $self->flags->{'drop_database'} ) {
+        my $ret = $self->drop_database($handle);
+        die "Error dropping database: ". $ret->error_message
+            unless $ret or $ret->error_message =~ /database .*?(?:does not|doesn't) exist|unknown database/i;
+    }
 
-        $handle->disconnect;
-        $self->_reinit_handle() if ( $self->flags->{'create_database'} );
+    # Create it too
+    if ( $self->flags->{'create_database'} ) {
+        my $ret = $self->create_database($handle);
+        die "Error creating database: ". $ret->error_message unless $ret;
     }
+
+    $handle->disconnect;
+}
+
+=head2 drop_database [HANDLE]
+
+A thin wrapper around L<Jifty::Handle/drop_database>, optionally using the
+HANDLE provided.  If no handle is provided, one is created using
+L</connect_to_db_for_management>.
+
+Returns undef on failure.
+
+=cut
+
+sub drop_database {
+    my $self   = shift;
+    my $handle = shift || $self->connect_to_db_for_management();
+
+    return $handle->drop_database( $self->flags->{'print'}
+                                        ? 'print' : 'execute' );
+}
+
+=head2 create_database [HANDLE]
+
+A thin wrapper around L<Jifty::Handle/create_database>, optionally using the
+HANDLE provided.  If no handle is provided, one is created using
+L</connect_to_db_for_management>.  This method will reinit Jifty->handle
+on success unless the C<print> flag is set.
+
+Returns undef on failure.
+
+=cut
+
+sub create_database {
+    my $self   = shift;
+    my $handle = shift || $self->connect_to_db_for_management();
+
+    my $good = $handle->create_database( $self->flags->{'print'}
+                                            ? 'print' : 'execute' );
+
+    $self->_reinit_handle() unless not $good or $self->flags->{'print'};
+
+    return $good;
 }
 
 sub _reinit_handle {

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


More information about the Jifty-commit mailing list