[Dbix-class] Creating tables

Sir Robert Burbridge rburbrid at cisco.com
Fri Mar 12 22:14:59 GMT 2010


Hey all,

Is there a built-in way of doing a class-by-class reversal of 
DBIx::Class::Schema::Loader's dump_to_dir() operation?  In particular, 
I'd like to be able to have tables created automatically if they don't 
exist when access is attempted.

I'd like to be able to do something like this with DBIC, such as:

    ### From the DBIx::Class docs...
    $schema->populate('Cd', [
       [qw/title artist/],
         @cds,
    ]);
       

and have the "popluate" command automatically create the requested 
tables if there's a corresponding ::Schema::Result::Cd class already.  
I'm currently (pre-DBIx::Class) doing that with something like the 
following (not even close to real code, but you get the idea).

    package My::Object;
    ...
    use DB::Helpers qw(__create_table_and_retry_query);

    sub some_method {
        my $sth = $dbh->prepare($sql);
        if ($sth->execute) {
           success($sth);
        } elsif ($sth->errstr has something like "table doesn't exist") {
           if (__create_table_and_retry_query($table_definition, $sql)) {
              success();
           } else {
              failure();
           }
        } else {
           $failure($sth);
        }

        $sth->finish();
        ### ...etc.
    }

    ### Elsewhere...
    package DB::Helpers;

    sub __create_table_and_retry_query {
        my ($table_definition, $sql) = @_;
        ### ... try to create the table, on success re-execute $sql.
        return 1|0;
    }

Thanks =)

-Sir








More information about the DBIx-Class mailing list