[Dbix-class] copying between databases

Alan Humphrey alan.humphrey at comcast.net
Thu Feb 9 17:04:44 CET 2006


This is great!  It's really nice to be able to see the API in use.

I'm going to use this as a starting point.  Changes I'll need to make:

1) related objects need to be migrated before the new object is created in
order to avoid foreign key constraint problems in the db.

2) you're right, this is a little insert-happy.  Most of my changes will be
modifications of existing records.  I'll modify the code to use
find_or_create followed by update.  (Hmm.  Just saw update_or_create in the
docs.  I'll have to look at that...)

Thanks for the help!

- 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