Efficiently Create Multiple Related Rows in DBIx::Class

This article will talk about something seemingly simple and boring -
inserting new rows into your database. If you frequent
irc.perl.org#dbix-class you probably have heard of the mysterious
MultiCreate, yet you are not entirely sure if it can bring you fame and money.
Are you watching closely?

The Turn

Say some XML describing a modest CD collection needs to be parsed and stuffed
in the above tables, while preserving relational integrity. A seasoned
programmer will sigh, drink some coffee, and write the following code:

which was properly taken apart and inserted in the appropriate tables using
the appropriate foreign keys as one would expect. In addition, since
create() is a single call, DBIx::Class
takes care of making it atomic by dutifully wrapping the entire operation in
a transaction.

The returned object does not always have the proper related objects inserted
at the expected slots (similar to the effect of
prefetch). We need a good amount of tests
before we attempt to make this work as expected - patches welcome!

The find_or_create() use mentioned
above can result in some strange behavior. Consider:

If the vague genre genre already exists, DBIx::Class will not
descend to check if the oddball cd is in fact created. This has not yet
caused anyone grief, thus there is no motivation for the (non-trivial) fix.

What The Future Holds

While the new row creation is mostly done and over with (in fact it delayed
the release of the 0.081xx series by several months), there are more ways to
expand the existing codebase.

Port DBIx::Class::ResultSet::RecursiveUpdate to leverage the (substantial)
infrastructure behind MultiCreate. While some conceptual differences
exist between both modules, they can certainly be resolved during the
test-writing phase.

Merge create and update into the logical multi_update_or_create !

If you have tuits or simply ideas - you are always welcome to hang out on
IRC, and start a discussion (or snatch a commit-bit and start hacking).