From: Frank Switalski Date: Thu, 27 Jan 2011 18:21:04 +0000 (-0500) Subject: addition of use_create option for populate X-Git-Tag: 1.001011~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=65a80d4e20ce1b47fd20e21e243ee4ea10ec11a6;p=dbsrgits%2FDBIx-Class-Fixtures.git addition of use_create option for populate --- diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 0fa5963..0f0b6de 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -425,7 +425,8 @@ sub new { _inherited_attributes => [qw/datetime_relative might_have rules belongs_to/], debug => $params->{debug} || 0, ignore_sql_errors => $params->{ignore_sql_errors}, - dumped_objects => {} + dumped_objects => {}, + use_create => $params->{use_create} || 0 }; bless $self, $class; @@ -932,6 +933,11 @@ sub _read_sql { # optional, set to 1 to run ddl but not populate no_populate => 0, + # optional, set to 1 to run each fixture through ->create rather than have + # each $rs populated using $rs->populate. Useful if you have overridden new() logic + # that effects the value of column(s). + use_create => 0, + # Dont try to clean the database, just populate over whats there. Requires # schema option. Use this if you want to handle removing old data yourself # no_deploy => 1 @@ -1059,7 +1065,11 @@ sub populate { my $HASH1; eval($contents); $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor; - push(@rows, $HASH1); + if ( $params->{use_create} ) { + $rs->create( $HASH1 ); + } else { + push(@rows, $HASH1); + } } $rs->populate(\@rows) if scalar(@rows); } diff --git a/t/12-populate-basic.t b/t/12-populate-basic.t index 63177ac..e37f490 100644 --- a/t/12-populate-basic.t +++ b/t/12-populate-basic.t @@ -34,7 +34,7 @@ foreach my $set ('simple', 'quantity', 'fetch', 'rules') { directory => 't/var/fixtures' }); - $schema = DBICTest->init_schema(no_deploy => 1); + $schema = DBICTest->init_schema( no_deploy => 1); my $fixture_dir = dir('t/var/fixtures'); foreach my $class ($schema->sources) { @@ -58,3 +58,26 @@ foreach my $set ('simple', 'quantity', 'fetch', 'rules') { } } } + +# use_create => 1 +$schema = DBICTest->init_schema(); +$fixtures = DBIx::Class::Fixtures->new({ + config_dir => $config_dir, + debug => 0 +}); +ok( $fixtures->dump({ + config => "use_create.json", + schema => $schema, + directory => 't/var/fixtures' + }), "use_create dump executed okay" +); +$schema = DBICTest->init_schema( no_populate => 1 ); +$fixtures->populate({ + directory => 't/var/fixtures', + connection_details => ['dbi:SQLite:t/var/DBIxClass.db', '', ''], + schema => $schema, + no_deploy => 1, + use_create => 1 +}); +$schema = DBICTest->init_schema( no_deploy => 1, no_populate => 1 ); +is( $schema->resultset( "Artist" )->find({ artistid => 4 })->name, "Test Name", "use_create => 1 ok" ); diff --git a/t/13populate-two-dbs.t b/t/13-populate-two-dbs.t similarity index 100% rename from t/13populate-two-dbs.t rename to t/13-populate-two-dbs.t diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm index a0414cf..bf36d1b 100755 --- a/t/lib/DBICTest.pm +++ b/t/lib/DBICTest.pm @@ -139,6 +139,7 @@ sub populate_schema { [ 1, 'Caterwauler McCrae' ], [ 2, 'Random Boy Band' ], [ 3, 'We Are Goth' ], + [ 4, '' ] # Test overridden new will default name to "Test Name" using use_create => 1. ]); $schema->populate('CD', [ diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 594a76d..f6d8180 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -22,4 +22,12 @@ __PACKAGE__->has_many( { order_by => 'year' }, ); +sub new { + my ( $class, $args ) = @_; + + $args->{name} = "Test Name" unless $args->{name}; + + return $class->next::method( $args ); +} + 1; diff --git a/t/var/DBIxClassDifferent.db b/t/var/DBIxClassDifferent.db deleted file mode 100644 index b0faeed..0000000 Binary files a/t/var/DBIxClassDifferent.db and /dev/null differ diff --git a/t/var/configs/use_create.json b/t/var/configs/use_create.json new file mode 100644 index 0000000..02d1be8 --- /dev/null +++ b/t/var/configs/use_create.json @@ -0,0 +1,6 @@ +{ + "sets": [{ + "class": "Artist", + "ids": ["4"], + }] +}