From: Luke Saunders Date: Wed, 20 Feb 2008 11:27:00 +0000 (+0000) Subject: added ability to dump everything in db without config X-Git-Tag: v1.001002~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2ef30e958797d4ee6c4b918b4d08ec8bbb3b189a;p=dbsrgits%2FDBIx-Class-Fixtures.git added ability to dump everything in db without config --- diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 0b783c5..e2f5161 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -373,13 +373,21 @@ sub new { directory => '/home/me/app/fixtures' # output directory }); + or + + $fixtures->dump({ + all => 1, # just dump everything that's in the schema + schema => $source_dbic_schema, + directory => '/home/me/app/fixtures' # output directory + }); + In this case objects will be dumped to subdirectories in the specified directory. For example: /home/me/app/fixtures/artist/1.fix /home/me/app/fixtures/artist/3.fix /home/me/app/fixtures/producer/5.fix -config, schema and directory are all required attributes. +schema and directory are required attributes. also, one of config or all must be specified. =cut @@ -391,30 +399,42 @@ sub dump { return DBIx::Class::Exception->throw('first arg to dump must be hash ref'); } - foreach my $param (qw/config schema directory/) { + foreach my $param (qw/schema directory/) { unless ($params->{$param}) { return DBIx::Class::Exception->throw($param . ' param not specified'); } } - my $config_file = file($self->config_dir, $params->{config}); - unless (-e $config_file) { - return DBIx::Class::Exception->throw('config does not exist at ' . $config_file); - } + my $schema = $params->{schema}; + my $config_file; + my $config; + if ($params->{config}) { + $config_file = file($self->config_dir, $params->{config}); + unless (-e $config_file) { + return DBIx::Class::Exception->throw('config does not exist at ' . $config_file); + } + + $config = Config::Any::JSON->load($config_file); + unless ($config && $config->{sets} && ref $config->{sets} eq 'ARRAY' && scalar(@{$config->{sets}})) { + return DBIx::Class::Exception->throw('config has no sets'); + } - my $config = Config::Any::JSON->load($config_file); - unless ($config && $config->{sets} && ref $config->{sets} eq 'ARRAY' && scalar(@{$config->{sets}})) { - return DBIx::Class::Exception->throw('config has no sets'); + $config->{might_have} = { fetch => 0 } unless (exists $config->{might_have}); + $config->{has_many} = { fetch => 0 } unless (exists $config->{has_many}); + $config->{belongs_to} = { fetch => 1 } unless (exists $config->{belongs_to}); + } elsif ($params->{all}) { + $config = { might_have => { fetch => 0 }, has_many => { fetch => 0 }, belongs_to => { fetch => 0 }, sets => [map {{ class => $_, quantity => 'all' }} $schema->sources] }; + print Dumper($config); + } else { + return DBIx::Class::Exception->throw('must pass config or set all'); } my $output_dir = dir($params->{directory}); unless (-e $output_dir) { $output_dir->mkpath || - return DBIx::Class::Exception->throw('output directory does not exist at ' . $output_dir); + return DBIx::Class::Exception->throw('output directory does not exist at ' . $output_dir); } - my $schema = $params->{schema}; - $self->msg("generating fixtures"); my $tmp_output_dir = dir($output_dir, '-~dump~-' . $<); @@ -438,8 +458,8 @@ sub dump { $source = merge( $source, $rule ) if ($rule); # fetch objects - my $rs = $schema->resultset($source->{class}); - $rs = $rs->search($source->{cond}, { join => $source->{join} }) if ($source->{cond}); + my $rs = $schema->resultset($source->{class}); + $rs = $rs->search($source->{cond}, { join => $source->{join} }) if ($source->{cond}); $self->msg("- dumping $source->{class}"); my @objects; my %source_options = ( set => { %{$config}, %{$source} } ); @@ -538,6 +558,9 @@ sub dump_object { my $mode = 0777; chmod $mode, $file->stringify; } + # don't bother looking at rels unless we are actually planning to dump at least one type + return unless ($set->{might_have}->{fetch} || $set->{belongs_to}->{fetch} || $set->{has_many}->{fetch} || $set->{fetch}); + # dump rels of object my $s = $object->result_source; unless ($exists) { diff --git a/t/07-dump-all.t b/t/07-dump-all.t new file mode 100644 index 0000000..18c8bc0 --- /dev/null +++ b/t/07-dump-all.t @@ -0,0 +1,28 @@ +#!perl + +use DBIx::Class::Fixtures; +use Test::More; +use lib qw(t/lib); +use DBICTest; +use Path::Class; +use Data::Dumper; + +plan tests => 9; + +# set up and populate schema +ok(my $schema = DBICTest->init_schema( ), 'got schema'); + +my $config_dir = 't/var/configs'; +my $fixture_dir = 't/var/fixtures'; + +# do dump +ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir'); +ok($fixtures->dump({ all => 1, schema => $schema, directory => 't/var/fixtures' }), 'fetch dump executed okay'); + + +foreach my $source ($schema->sources) { + my $rs = $schema->resultset($source); + my $dir = dir($fixture_dir, $rs->result_source->from); + my @children = $dir->children; + is (scalar(@children), $rs->count, 'all objects from $source dumped'); +}