[Dbix-class] copying between databases

Alan Humphrey alan.humphrey at comcast.net
Fri Feb 10 00:23:09 CET 2006


OK, I'm stumped again.  I tried your technique, modified to allow more
freedom for updates (as opposed to inserts).  That quickly led to the
realization that many-to-many relationships meant we'd be spidering the bulk
of the database on each "publish".  No good.

So I decided to take the 80% solution and just update directly related
records.  The result is this:

sub _migrate_obj_rels {
     my ( $self, $obj ) = @_;

     foreach my $rel ( $obj->relationships ) {

		 my $dest = $production->resultset( $self->_get_class_name(
obj->relationship_info($rel)->{source} ) );
         my $rs = $obj->search_related($rel);
         while (my $rel_obj = $rs->next) {
             my $cols = { $rel_obj->get_columns };
			 my $dest_rel_obj = $dest->find_or_create(
$self->_get_keys( $rel_obj ) );
			 $dest_rel_obj->update( $cols );
         }
     }
}

This works right up to the final update. At that point we go to
DBIx::Class::Relationship::CascadeActions::update, which ultimately does
nothing because there are no relationships defined:

  DB<24> x ref $source
0  'DBIx::Class::ResultSource::Table'
  DB<25> l
29        my %rels = map { $_ => $source->relationship_info($_) }
$source->relationships;
30:       my @cascade = grep { $rels{$_}{attrs}{cascade_update} } keys
%rels;
31:==>    foreach my $rel (@cascade) {
32:         $_->update for $self->$rel;
33        }
34:       return $ret;
35      }
36 
37:     1;
  DB<25> x @cascade
  empty array

Any ideas why I'm in CascadeActions?

Thanks!

- Alan

-----Original Message-----
From: dbix-class-bounces at lists.rawmode.org
[mailto:dbix-class-bounces at lists.rawmode.org] On Behalf Of David Kamholz
Sent: Wednesday, February 08, 2006 7:32 PM
To: dbix-class at lists.rawmode.org
Subject: Re: [Dbix-class] copying between databases

This should do it, but it may be a bit more insert-happy than you  
want. It might be good to be able to tell it more precisely where to  
stop. It shouldn't end up in an infinite loop though, at least.

Dave

sub migrate_obj {
     my ($obj,$dest_schema) = @_;
     my $dest_rs = $dest_schema->resultset(ref $obj);
     my $cols = { $obj->get_columns };
     unless ($dest_rs->find($cols)) {
         my $dest_obj = $dest_rs->create($cols);
         _migrate_obj_rels($obj,$dest_obj);
     }
}

sub _migrate_obj_rels {
     my ($obj,$dest_obj) = @_;
     foreach my $rel ($obj->relationships) {
         my $rs = $obj->search_related($rel);
         while (my $rel_obj = $rs->next) {
             my $cols = { $rel_obj->get_columns };
             unless ($dest_obj->find_related($rel, $cols)) {
                 my $dest_rel_obj = $dest_obj->create_related($rel,  
$cols);
                 _migrate_obj_rels($rel_obj,$dest_rel_obj);
             }
         }
     }
}

_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/




More information about the Dbix-class mailing list