[Bast-commits] r4089 - in DBIx-Class-Fixtures/1.000/trunk: lib/DBIx/Class t

captainL at dev.catalyst.perl.org captainL at dev.catalyst.perl.org
Wed Feb 20 11:27:00 GMT 2008


Author: captainL
Date: 2008-02-20 11:27:00 +0000 (Wed, 20 Feb 2008)
New Revision: 4089

Added:
   DBIx-Class-Fixtures/1.000/trunk/t/07-dump-all.t
Modified:
   DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm
Log:
added ability to dump everything in db without config

Modified: DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm	2008-02-19 13:43:58 UTC (rev 4088)
+++ DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm	2008-02-20 11:27:00 UTC (rev 4089)
@@ -373,13 +373,21 @@
     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 @@
     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);
+    }
 
-  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 = 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 @@
     $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 @@
     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) {

Added: DBIx-Class-Fixtures/1.000/trunk/t/07-dump-all.t
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/t/07-dump-all.t	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/t/07-dump-all.t	2008-02-20 11:27:00 UTC (rev 4089)
@@ -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');
+}




More information about the Bast-commits mailing list