upgrade will only produce a diff between the DB and the DBIC schema if explicitly...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema / Versioned.pm
index 7eeb110..9b923a6 100644 (file)
@@ -145,69 +145,74 @@ sub backup
 
 sub upgrade
 {
-    my ($self) = @_;
+    my ($self, $params) = @_;
+    $params ||= {};
     my $db_version = $self->get_db_version();
 
+    my %driver_to_db_map = (
+                            'mysql' => 'MySQL'
+                           );
     if (!$db_version) {
-      my %driver_to_db_map = (
-        'mysql' => 'MySQL',
-        'Pg' => 'PostgreSQL',
-        'Oracle' => 'Oracle'
-      );
       my $db = $driver_to_db_map{$self->storage->dbh->{Driver}->{Name}};
       unless ($db) {
         print "Sorry, this is an unsupported DB\n";
         return;
       }
 
-      require SQL::Translator;
-      require SQL::Translator::Diff;
-      my $db_tr = SQL::Translator->new({ 
-        add_drop_table => 1, 
-        parser => 'DBI',
-        parser_args => { dbh => $self->storage->dbh }
-      });
-
-      $db_tr->producer($db);      
-      my $dbic_tr = SQL::Translator->new;
-      $dbic_tr->parser('SQL::Translator::Parser::DBIx::Class');
-      $dbic_tr = $self->storage->configure_sqlt($dbic_tr, $db);
-      $dbic_tr->data($self);
-      $dbic_tr->producer($db);
-
-      $db_tr->schema->name('db_schema');
-      $dbic_tr->schema->name('dbic_schema');
-
-      # is this really necessary?
-      foreach my $tr ($db_tr, $dbic_tr) {
-        my $data = $tr->data;
-        $tr->parser->($tr, $$data);
-      }
-      
-      my $diff = SQL::Translator::Diff::schema_diff($db_tr->schema, $db, 
-                                                    $dbic_tr->schema, $db,
-                                                    { ignore_constraint_names => 1, ignore_index_names => 1, caseopt => 1 });
-
-      my $filename = $self->ddl_filename(
-                                 $self->storage->sqlt_type,
-                                 $self->upgrade_directory,
-                                 $self->schema_version,
-                                 'PRE',
-                                 );
-      my $file;
-      if(!open($file, ">$filename"))
-      {
+      if ($params->{create_diff}) {
+        require SQL::Translator;
+        require SQL::Translator::Diff;
+        my $db_tr = SQL::Translator->new({ 
+          add_drop_table => 1, 
+          parser => 'DBI',
+          parser_args => { dbh => $self->storage->dbh }
+        });
+        
+        $db_tr->producer($db);
+        my $dbic_tr = SQL::Translator->new;
+        $dbic_tr->parser('SQL::Translator::Parser::DBIx::Class');
+        $dbic_tr = $self->storage->configure_sqlt($dbic_tr, $db);
+        $dbic_tr->data($self);
+        $dbic_tr->producer($db);
+        
+        $db_tr->schema->name('db_schema');
+        $dbic_tr->schema->name('dbic_schema');
+        
+        # is this really necessary?
+        foreach my $tr ($db_tr, $dbic_tr) {
+          my $data = $tr->data;
+          $tr->parser->($tr, $$data);
+        }
+        
+        my $diff = SQL::Translator::Diff::schema_diff($db_tr->schema, $db, 
+                                                      $dbic_tr->schema, $db,
+                                                      { caseopt => 1 });
+        
+        my $filename = $self->ddl_filename(
+                                           $db,
+                                           $self->upgrade_directory,
+                                           $self->schema_version,
+                                           'PRE',
+                                           );
+        my $file;
+        if(!open($file, ">$filename")) {
           $self->throw_exception("Can't open $filename for writing ($!)");
           next;
+        }
+        print $file $diff;
+        close($file);
+        
+        print "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.\n";
       }
-      print $file $diff;
-      close($file);
 
       # create versions table
       $self->{vschema}->deploy;
-
-      print "WARNING: There may be differences between your DB and your DBIC schema. Please review and if necessary run the SQL in $filename to sync your DB.\n";
     } else {
+      if ($db_version eq $self->schema_version) {
+        print "Upgrade not necessary\n";
+        return;
+      }
+
       my $file = $self->ddl_filename(
                                  $self->storage->sqlt_type,
                                  $self->upgrade_directory,
@@ -223,7 +228,9 @@ sub upgrade
 
       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>));
+      @data = grep(!/^--/, @data);
+      @data = split(/;/, join('', @data));
       close($fh);
       @data = grep { $_ && $_ !~ /^-- / } @data;
       @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data;
@@ -264,7 +271,7 @@ sub run_upgrade
     $self->_filedata([ grep { $_ !~ /$stm/i } @{$self->_filedata} ]);
 
     for (@statements)
-    {
+    {      
         $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;