[Bast-commits] r3998 - in DBIx-Class-Fixtures/1.000/trunk: lib/DBIx/Class lib/DBIx/Class/Fixtures t/var/configs

captainL at dev.catalyst.perl.org captainL at dev.catalyst.perl.org
Thu Jan 31 16:46:48 GMT 2008


Author: captainL
Date: 2008-01-31 16:46:48 +0000 (Thu, 31 Jan 2008)
New Revision: 3998

Added:
   DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/
   DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/Schema.pm
   DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/SchemaVersioned.pm
   DBIx-Class-Fixtures/1.000/trunk/t/var/configs/fetch.json
   DBIx-Class-Fixtures/1.000/trunk/t/var/configs/quantity.json
   DBIx-Class-Fixtures/1.000/trunk/t/var/configs/rules.json
Modified:
   DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm
Log:
populate code added

Added: DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/Schema.pm
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/Schema.pm	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/Schema.pm	2008-01-31 16:46:48 UTC (rev 3998)
@@ -0,0 +1,10 @@
+package DBIx::Class::Fixtures::Schema;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Schema::Loader';
+
+__PACKAGE__->loader_options( );
+
+1;

Added: DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/SchemaVersioned.pm
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/SchemaVersioned.pm	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures/SchemaVersioned.pm	2008-01-31 16:46:48 UTC (rev 3998)
@@ -0,0 +1,15 @@
+package DBIx::Class::Fixtures::SchemaVersioned;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Schema::Loader';
+
+our $VERSION = 'set-when-loading';
+
+__PACKAGE__->load_components('Schema::Versioned');
+__PACKAGE__->loader_options(
+                            # debug                 => 1,
+                           );
+
+1;

Modified: DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm	2008-01-31 00:28:19 UTC (rev 3997)
+++ DBIx-Class-Fixtures/1.000/trunk/lib/DBIx/Class/Fixtures.pm	2008-01-31 16:46:48 UTC (rev 3998)
@@ -301,6 +301,142 @@
   }
 }
 
+sub _generate_schema {
+  my $self = shift;
+  my $params = shift || {};
+
+  require DBI;
+  $self->msg("\ncreating schema");
+  #   die 'must pass version param to generate_schema_from_ddl' unless $params->{version};
+
+  my $dbh;
+  unless ($dbh = DBI->connect(@{$params->{connection_details}})) {
+    return DBIx::Class::Exception->throw('connection details not valid');
+  }
+  my $connection_details = $params->{connection_details};
+
+  # clear existing db
+  $self->msg("- clearing DB of existing tables");
+  $dbh->do('SET foreign_key_checks=0');
+  my $sth = $dbh->prepare('SHOW TABLES');
+  $sth->execute;
+  my $rows = $sth->fetchall_arrayref;
+  $dbh->do('drop table ' . $_->[0]) for (@{$rows});
+
+  # import new ddl file to db
+  my $ddl_file = $params->{ddl};
+  $self->msg("- deploying schema using $ddl_file");
+  my $fh;
+  open $fh, "<$ddl_file" or die ("Can't open DDL file, $ddl_file ($!)");
+  my @data = split(/\n/, join('', <$fh>));
+  @data = grep(!/^--/, @data);
+  @data = split(/;/, join('', @data));
+  close($fh);
+  @data = grep { $_ && $_ !~ /^-- / } @data;
+  for (@data) {
+      eval { $dbh->do($_) or warn "SQL was:\n $_"};
+	  if ($@) { die "SQL was:\n $_\n$@"; }
+  }
+  $dbh->do('SET foreign_key_checks=1');
+  $self->msg("- finished importing DDL into DB");
+
+  # load schema object from our new DB
+  $self->msg("- loading fresh DBIC object from DB");
+  my $schema = DBIx::Class::Fixtures::Schema->connect(@{$connection_details});
+
+  # manually set the version then set DB version to it (upgrade)
+#   $Takkle::SchemaPopulate::VERSION = $params->{version};
+#   $schema->upgrade(); # set version number  
+  return $schema;
+}
+
+sub populate {
+  my $self = shift;
+  my ($params) = @_;
+  unless (ref $params eq 'HASH') {
+    return DBIx::Class::Exception->throw('first arg to populate must be hash ref');
+  }
+
+  foreach my $param (qw/directory/) {
+    unless ($params->{$param}) {
+      return DBIx::Class::Exception->throw($param . ' param not specified');
+    }
+  }
+  my $fixture_dir = dir($params->{directory});
+  unless (-e $fixture_dir) {
+    return DBIx::Class::Exception->throw('fixture directory does not exist at ' . $fixture_dir);
+  }
+
+  my $ddl_file;
+  my $dbh;  
+  if ($params->{ddl} && $params->{connection_details}) {
+    my $ddl_file = file($params->{ddl});
+    unless (-e $ddl_file) {
+      return DBIx::Class::Exception->throw('DDL does not exist at ' . $ddl_file);
+    }
+    unless (ref $params->{connection_details} eq 'ARRAY') {
+      return DBIx::Class::Exception->throw('connection details must be an arrayref');
+    }
+  } elsif ($params->{schema}) {
+    return DBIx::Class::Exception->throw('passing a schema is not supported at the moment');
+  } else {
+    return DBIx::Class::Exception->throw('you must set the ddl and connection_details params');
+  }
+
+  my $schema = $self->_generate_schema({ ddl => $ddl_file, connection_details => $params->{connection_details} });
+  $self->msg("importing fixtures");
+
+  my $tmp_fixture_dir = dir($fixture_dir, "-~populate~-" . $<);
+
+  my $version_file = file($fixture_dir, '_dumper_version');
+  unless (-e $version_file) {
+#     return DBIx::Class::Exception->throw('no version file found');
+  }
+
+  if (-e $tmp_fixture_dir) {
+    $self->msg("- deleting existing temp directory $tmp_fixture_dir");
+    system("rm -rf $tmp_fixture_dir");
+  }
+  $self->msg("- creating temp dir");
+  system("cp -r $fixture_dir $tmp_fixture_dir");
+
+  $schema->storage->dbh->do('SET foreign_key_checks=0');
+  my $fixup_visitor;
+  my %callbacks;
+  if ($params->{datetime_relative_to}) {
+    $callbacks{'DateTime::Duration'} = sub {
+      $params->{datetime_relative_to}->clone->add_duration($_);
+    };
+  } else {
+    $callbacks{'DateTime::Duration'} = sub {
+      DateTime->today->add_duration($_)
+    };
+  }
+  $callbacks{object} ||= "visit_ref";	
+  $fixup_visitor = new Data::Visitor::Callback(%callbacks);
+
+  foreach my $source (sort $schema->sources) {
+    $self->msg("- adding " . $source);
+    my $rs = $schema->resultset($source);
+    my $source_dir = dir($tmp_fixture_dir, lc($rs->result_source->from));
+    next unless (-e $source_dir);
+    while (my $file = $source_dir->next) {
+      next unless ($file =~ /\.fix$/);
+      next if $file->is_dir;
+      my $contents = $file->slurp;
+      my $HASH1;
+      eval($contents);
+      $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor;
+      $rs->find_or_create($HASH1);
+    }
+  }
+
+  $self->msg("- fixtures imported");
+  $self->msg("- cleaning up");
+  $tmp_fixture_dir->rmtree;
+  $schema->storage->dbh->do('SET foreign_key_checks=1');
+}
+
 sub msg {
   my $self = shift;
   my $subject = shift || return;

Added: DBIx-Class-Fixtures/1.000/trunk/t/var/configs/fetch.json
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/t/var/configs/fetch.json	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/t/var/configs/fetch.json	2008-01-31 16:46:48 UTC (rev 3998)
@@ -0,0 +1,23 @@
+{
+        might_have: {
+            fetch: 0
+        },
+        has_many: {
+            fetch: 0
+        },
+        sets: [{
+            class: 'Artist',
+            ids: ['1'],
+            fetch: [{
+                rel: cds,
+                quantity: all
+            }]
+        }, {
+            class: 'Artist',
+            ids: ['2'],
+            fetch: [{
+                rel: cds,
+                cond: { year: {'>': 2002} }
+            }]
+        }]
+}
\ No newline at end of file

Added: DBIx-Class-Fixtures/1.000/trunk/t/var/configs/quantity.json
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/t/var/configs/quantity.json	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/t/var/configs/quantity.json	2008-01-31 16:46:48 UTC (rev 3998)
@@ -0,0 +1,12 @@
+{
+        might_have: {
+            fetch: 0
+        },
+        has_many: {
+            fetch: 0
+        },
+        sets: [{
+            class: 'CD',
+            quantity: 3
+        }]
+}
\ No newline at end of file

Added: DBIx-Class-Fixtures/1.000/trunk/t/var/configs/rules.json
===================================================================
--- DBIx-Class-Fixtures/1.000/trunk/t/var/configs/rules.json	                        (rev 0)
+++ DBIx-Class-Fixtures/1.000/trunk/t/var/configs/rules.json	2008-01-31 16:46:48 UTC (rev 3998)
@@ -0,0 +1,29 @@
+{
+        might_have: {
+            fetch: 0
+        },
+        has_many: {
+            fetch: 0
+        },
+        sets: [{
+            class: 'CD',
+            ids: ['5']
+        }, {
+            class: 'Artist',
+            ids: ['1'],
+            fetch: [{
+                rel: cds,
+                quantity: all
+            }]
+        }],
+        rules: {
+            'CD': {
+                cond: [{ 'tags.tag': 'Cheesy' }],
+                join: ['tags'],
+                fetch: [{
+                    rel: tracks,
+                    cond: { position: 2 }
+                }]
+            }                    
+        }
+}
\ No newline at end of file




More information about the Bast-commits mailing list