[Bast-commits] r8214 - in branches/DBIx-Class-Schema-Loader/current: . lib/DBIx/Class/Schema lib/DBIx/Class/Schema/Loader t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Fri Jan 1 11:00:36 GMT 2010


Author: caelum
Date: 2010-01-01 11:00:35 +0000 (Fri, 01 Jan 2010)
New Revision: 8214

Added:
   branches/DBIx-Class-Schema-Loader/current/t/45relationships.t
Modified:
   branches/DBIx-Class-Schema-Loader/current/
   branches/DBIx-Class-Schema-Loader/current/Changes
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm
   branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm
Log:
 r22337 at hlagh (orig r8210):  rbuels | 2009-12-31 23:44:13 -0500
 making branch to add relationship_attrs option
 r22338 at hlagh (orig r8211):  rbuels | 2009-12-31 23:45:18 -0500
 added relationship_attrs option, plus tests for it
 r22339 at hlagh (orig r8212):  rbuels | 2009-12-31 23:47:11 -0500
 added self to contributors, added note to changelog



Property changes on: branches/DBIx-Class-Schema-Loader/current
___________________________________________________________________
Name: svk:merge
   - 46bc3436-8211-0410-8564-d96f7a728040:/local/DBIx-Class-Schema-Loader/branches/common-dump:37173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/back-compat:8172
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/deprecate_CDA:8193
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/mssql_tweaks:7407
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/odbc-mssql:6439
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/skip_load_external:8198
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class-Schema-Loader:5726
   + 46bc3436-8211-0410-8564-d96f7a728040:/local/DBIx-Class-Schema-Loader/branches/common-dump:37173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/back-compat:8172
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/deprecate_CDA:8193
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/mssql_tweaks:7407
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/odbc-mssql:6439
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/relationship_attrs:8212
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-Schema-Loader/skip_load_external:8198
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class-Schema-Loader:5726

Modified: branches/DBIx-Class-Schema-Loader/current/Changes
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/Changes	2010-01-01 10:55:01 UTC (rev 8213)
+++ branches/DBIx-Class-Schema-Loader/current/Changes	2010-01-01 11:00:35 UTC (rev 8214)
@@ -1,5 +1,7 @@
 Revision history for Perl extension DBIx::Class::Schema::Loader
 
+	- added relationship_attrs option for setting attributes in
+          generated relationships
         - added overwrite_modifications option that ignores md5sums on
           generated code
         - better handling of db_schema for Oracle, based on (RT#35732)

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm	2010-01-01 10:55:01 UTC (rev 8213)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/Base.pm	2010-01-01 11:00:35 UTC (rev 8214)
@@ -46,6 +46,8 @@
                                 result_base_class
 				overwrite_modifications
 
+                                relationship_attrs
+
                                 db_schema
                                 _tables
                                 classes
@@ -153,6 +155,26 @@
 
     __PACKAGE__->naming('v5');
 
+=head2 relationship_attrs
+
+Hashref of attributes to pass to each generated relationship, listed
+by type.  Also supports relationship type 'all', containing options to
+pass to all generated relationships.  Attributes set for more specific
+relationship types override those set in 'all'.
+
+For example:
+
+  relationship_attrs => {
+    all      => { cascade_delete => 0 },
+    has_many => { cascade_delete => 1 },
+  },
+
+will set the C<cascade_delete> option to 0 for all generated relationships,
+except for C<has_many>, which will have cascade_delete as 1.
+
+NOTE: this option is not supported if v4 backward-compatible naming is
+set either globally (naming => 'v4') or just for relationships.
+
 =head2 debug
 
 If set to true, each constructive L<DBIx::Class> statement the loader
@@ -656,8 +678,11 @@
             );
     }
 
-    $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new(
-        $self->schema, $self->inflect_plural, $self->inflect_singular
+    $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new (
+	 $self->schema,
+	 $self->inflect_plural,
+	 $self->inflect_singular,
+	 $self->relationship_attrs,
     );
 }
 

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm	2010-01-01 10:55:01 UTC (rev 8213)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader/RelBuilder.pm	2010-01-01 11:00:35 UTC (rev 8214)
@@ -76,17 +76,23 @@
 =cut
 
 sub new {
-    my ( $class, $schema, $inflect_pl, $inflect_singular ) = @_;
 
+    my ( $class, $schema, $inflect_pl, $inflect_singular, $rel_attrs ) = @_;
+
     my $self = {
         schema => $schema,
         inflect_plural => $inflect_pl,
         inflect_singular => $inflect_singular,
+        relationship_attrs => $rel_attrs,
     };
 
-    bless $self => $class;
+    # validate the relationship_attrs arg
+    if( defined $self->{relationship_attrs} ) {
+	ref($self->{relationship_attrs}) eq 'HASH'
+	    or croak "relationship_attrs must be a hashref";
+    }
 
-    $self;
+    return bless $self => $class;
 }
 
 
@@ -122,6 +128,23 @@
     return Lingua::EN::Inflect::Number::to_S($relname);
 }
 
+# accessor for options to be passed to each generated relationship
+# type.  take single argument, the relationship type name, and returns
+# either a hashref (if some options are set), or nothing
+sub _relationship_attrs {
+    my ( $self, $reltype ) = @_;
+    my $r = $self->{relationship_attrs};
+    return unless $r && ( $r->{all} || $r->{$reltype} );
+
+    my %composite = %{ $r->{all} || {} };
+    if( my $specific = $r->{$reltype} ) {
+	while( my ($k,$v) = each %$specific ) {
+	    $composite{$k} = $v;
+	}
+    }
+    return \%composite;
+}
+
 sub _array_eq {
     my ($a, $b) = @_;
 
@@ -152,11 +175,17 @@
 sub _remote_attrs {
 	my ($self, $local_moniker, $local_cols) = @_;
 
-	# If the referring column is nullable, make 'belongs_to' an outer join:
+	# get our base set of attrs from _relationship_attrs, if present
+	my $attrs = $self->_relationship_attrs('belongs_to') || {};
+
+	# If the referring column is nullable, make 'belongs_to' an
+	# outer join, unless explicitly set by relationship_attrs
 	my $nullable = grep { $self->{schema}->source($local_moniker)->column_info($_)->{is_nullable} }
 		@$local_cols;
+	$attrs->{join_type} = 'LEFT'
+	    if $nullable && !defined $attrs->{join_type};
 
-	return $nullable ? { join_type => 'LEFT' } : ();
+	return $attrs;
 }
 
 sub _remote_relname {
@@ -252,6 +281,7 @@
               args => [ $local_relname,
                         $local_class,
                         \%rev_cond,
+			$self->_relationship_attrs($remote_method),
               ],
             }
         );

Modified: branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm	2010-01-01 10:55:01 UTC (rev 8213)
+++ branches/DBIx-Class-Schema-Loader/current/lib/DBIx/Class/Schema/Loader.pm	2010-01-01 11:00:35 UTC (rev 8214)
@@ -455,6 +455,8 @@
 
 jhannah: Jay Hannah <jay at jays.net>
 
+rbuels: Robert Buels <rmb32 at cornell.edu>
+
 ... and lots of other folks. If we forgot you, please write the current
 maintainer or RT.
 

Added: branches/DBIx-Class-Schema-Loader/current/t/45relationships.t
===================================================================
--- branches/DBIx-Class-Schema-Loader/current/t/45relationships.t	                        (rev 0)
+++ branches/DBIx-Class-Schema-Loader/current/t/45relationships.t	2010-01-01 11:00:35 UTC (rev 8214)
@@ -0,0 +1,60 @@
+use strict;
+use Test::More tests => 6;
+use Test::Exception;
+use lib qw(t/lib);
+use make_dbictest_db;
+
+use DBIx::Class::Schema::Loader;
+
+# test skip_relationships
+my $regular = schema_with();
+is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH',
+    'regularly-made schema has fooref rel',
+  );
+my $skip_rel = schema_with( skip_relationships => 1 );
+is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef,
+    'skip_relationships blocks generation of fooref rel',
+  );
+
+
+# test relationship_attrs
+throws_ok {
+    schema_with( relationship_attrs => 'laughably invalid!!!' );
+} qr/relationship_attrs/, 'throws error for invalid relationship_attrs';
+
+
+{
+    my $nodelete = schema_with( relationship_attrs =>
+				{
+				 all        => { cascade_delete => 0 },
+				 belongs_to => { cascade_delete => 1 },
+				},
+			      );
+
+    my $bars_info   = $nodelete->source('Foo')->relationship_info('bars');
+    #use Data::Dumper;
+    #die Dumper([ $nodelete->source('Foo')->relationships() ]);
+    my $fooref_info = $nodelete->source('Bar')->relationship_info('fooref');
+    is( ref($fooref_info), 'HASH',
+	'fooref rel is present',
+      );
+    is( $bars_info->{attrs}->{cascade_delete}, 0,
+	'relationship_attrs settings seem to be getting through to the generated rels',
+      );
+    is( $fooref_info->{attrs}->{cascade_delete}, 1,
+	'belongs_to in relationship_attrs overrides all def',
+      );
+}
+
+
+#### generates a new schema with the given opts every time it's called
+my $schema_counter = 0;
+sub schema_with {
+    $schema_counter++;
+    DBIx::Class::Schema::Loader::make_schema_at(
+            'DBICTest::Schema::'.$schema_counter,
+            { naming => 'current', @_ },
+            [ $make_dbictest_db::dsn ],
+    );
+    "DBICTest::Schema::$schema_counter"->clone;
+}




More information about the Bast-commits mailing list