tests are a mess, but Versioned.pm should work now
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema / Versioned.pm
index 6966e5b..aa76fff 100644 (file)
@@ -48,6 +48,7 @@ use Data::Dumper;
 __PACKAGE__->mk_classdata('_filedata');
 __PACKAGE__->mk_classdata('upgrade_directory');
 __PACKAGE__->mk_classdata('backup_directory');
+__PACKAGE__->mk_classdata('do_backup');
 
 sub schema_version {
   my ($self) = @_;
@@ -70,28 +71,20 @@ sub connection {
 sub _on_connect
 {
     my ($self) = @_;
-    my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
-    my $vtable = $vschema->resultset('Table');
+    $self->{vschema} = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
+    my $vtable = $self->{vschema}->resultset('Table');
     my $pversion;
 
     if(!$self->_source_exists($vtable))
     {
-#        $vschema->storage->debug(1);
-        $vschema->storage->ensure_connected();
-        $vschema->deploy();
+#        $self->{vschema}->storage->debug(1);
+        $self->{vschema}->storage->ensure_connected();
+        $self->{vschema}->deploy();
         $pversion = 0;
     }
     else
     {
-        my $psearch = $vtable->search(undef, 
-                                      { select => [
-                                                   { 'max' => 'Installed' },
-                                                   ],
-                                            as => ['maxinstall'],
-                                        })->first;
-        $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
-                                  })->first;
-        $pversion = $pversion->Version if($pversion);
+      $pversion = $self->get_db_version();
     }
 #    warn("Previous version: $pversion\n");
     if($pversion eq $self->schema_version)
@@ -139,7 +132,7 @@ sub _on_connect
 
     my $fh;
     open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)");
-    my @data = split(/;\n/, join('', <$fh>));
+    my @data = split(/[;\n]/, join('', <$fh>));
     close($fh);
     @data = grep { $_ && $_ !~ /^-- / } @data;
     @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data;
@@ -153,6 +146,23 @@ sub _on_connect
 #    $self->upgrade($pversion, $self->schema_version);
 }
 
+sub get_db_version
+{
+    my ($self, $rs) = @_;
+
+    my $vtable = $self->{vschema}->resultset('Table');
+    my $psearch = $vtable->search(undef, 
+                                    { select => [
+                                                 { 'max' => 'Installed' },
+                                                 ],
+                                          as => ['maxinstall'],
+                                      })->first;
+    my $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
+                                })->first;
+    $pversion = $pversion->Version if($pversion);
+    return $pversion;
+}
+
 sub _source_exists
 {
     my ($self, $rs) = @_;
@@ -176,25 +186,31 @@ sub upgrade
 {
     my ($self) = @_;
 
-    ## overridable sub, per default just run all the commands.
+    $self->backup() if($self->do_backup);
+
+    $self->txn_do(sub {
+      $self->do_upgrade();
+    });
+
+    my $vtable = $self->{vschema}->resultset('Table');
+    $vtable->create({ Version => $self->schema_version,
+                      Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
+                      });
+
+}
 
-    $self->backup();
+sub do_upgrade
+{
+    my ($self) = @_;
 
+    ## overridable sub, per default just run all the commands.
     $self->run_upgrade(qr/create/i);
     $self->run_upgrade(qr/alter table .*? add/i);
     $self->run_upgrade(qr/alter table .*? (?!drop)/i);
     $self->run_upgrade(qr/alter table .*? drop/i);
     $self->run_upgrade(qr/drop/i);
-#    $self->run_upgrade(qr//i);
-
-    my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
-    my $vtable = $vschema->resultset('Table');
-    $vtable->create({ Version => $self->schema_version,
-                      Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
-                      });
 }
 
-
 sub run_upgrade
 {
     my ($self, $stm) = @_;
@@ -205,9 +221,9 @@ sub run_upgrade
 
     for (@statements)
     {
-        $self->storage->debugfh->print("$_\n") if $self->storage->debug;
-#        print "Running \n>>$_<<\n";
+        $self->storage->debugobj->query_start($_) if $self->storage->debug;
         $self->storage->dbh->do($_) or warn "SQL was:\n $_";
+        $self->storage->debugobj->query_end($_) if $self->storage->debug;
     }
 
     return 1;
@@ -217,7 +233,7 @@ sub run_upgrade
 
 =head1 NAME
 
-DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades
+DBIx::Class::Schema::Versioned - DBIx::Class::Schema plugin for Schema upgrades
 
 =head1 SYNOPSIS
 
@@ -285,13 +301,15 @@ allow you to make a backup of the database. Per default this method attempts
 to call C<< $self->storage->backup >>, to run the standard backup on each
 database type. 
 
-This method should return the name of the backup file, if appropriate.
-
-C<backup> is called from C<upgrade>, make sure you call it, if you write your
-own <upgrade> method.
+This method should return the name of the backup file, if appropriate..
 
 =head2 upgrade
 
+This is the main upgrade method which calls the overridable do_upgrade and
+also handles the backups and updating of the SchemaVersion table.
+
+=head2 do_upgrade
+
 This is an overwritable method used to run your upgrade. The freeform method
 allows you to run your upgrade any way you please, you can call C<run_upgrade>
 any number of times to run the actual SQL commands, and in between you can