DBICx::Deploy - deploy a DBIx::Class schema


DBICx-Deploy documentation Contained in the DBICx-Deploy distribution.

Index


Code Index:

NAME

Top

DBICx::Deploy - deploy a DBIx::Class schema

SYNOPSIS

Top

   use DBICx::Deploy;
   DBICx::Deploy->deploy('My::Schema' => 'DBI:SQLite:root/database');

or

   $ dbicdeploy -Ilib My::Schema DBI:SQLite:root/database

METHODS

Top

deploy($schema, $dsn, @args)

Loads the DBIC schema $schema, connects to $dsn (with extra args @args like username, password, and options), and deploys the schema. Dies on failure.

If $dsn doesn't start with "DBI", deploy assumes that you want to write the SQL to generate the schema to a directory called $dsn. If $dsn doesn't exist, it (and its parents) will be created for you.

When deploying to SQL files, @args is a list of database engines you want to generate SQL for. It defauts to "MySQL", "SQLite", and "PostgreSQL". See SQL::Translator for a list of possible engines.

SEE ALSO

Top

dbicdeploy (dbicdeploy), included with this distribution.

AUTHOR

Top

Jonathan Rockway <jrockway@cpan.org>

CONTRIBUTORS

Top

The following people have contributed code or bug reports:

Brian Cassidy
Andreas Marienborg
Pedro Melo

Thanks!

LICENSE

Top

This program is free software. You may redistribute it under the same terms as Perl itself.


DBICx-Deploy documentation Contained in the DBICx-Deploy distribution.

package DBICx::Deploy;
use strict;
use warnings;
use Carp;
use File::Spec;

our $VERSION = '0.02';

sub deploy {
    my ($class, $schema_class, $dsn, @args) = @_;
    croak 'need schema' unless $schema_class;
    croak 'need dsn' unless $dsn;

    eval "require $schema_class" or die "Failed to use $schema_class: $@";

    if($dsn =~ /^DBI:/i){
        my $schema = $schema_class->connect($dsn, @args);
        $schema->deploy;
    }
    else {
        # $dsn is a directory
        my $schema = $schema_class->connect;
        _mkdir($dsn);
        @args = qw/MySQL SQLite PostgreSQL/ if !@args;
        $schema->create_ddl_dir(\@args, undef, $dsn);
    }
}

# wtf.  why?
sub _mkdir {
    my $dir = shift;
    my @dirs = File::Spec->splitdir($dir);
    
    my $base = shift @dirs;
    mkdir $base;
    foreach my $d (@dirs){
        $base = File::Spec->catdir($base, $d);
        mkdir $base;
    }
}

1;

__END__