From: Dagfinn Ilmari Mannsåker Date: Sun, 20 Oct 2013 14:25:06 +0000 (+0100) Subject: Merge branch 'topic/better-multi-schema' into master X-Git-Tag: 0.07036_03~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c11cfe74e511b3ff0b2dd31480d3a2cdc3909df7;hp=764b262a2915729db4d18100254f9389e6b12dd5;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Merge branch 'topic/better-multi-schema' into master --- diff --git a/Changes b/Changes index 6215d25..122d573 100644 --- a/Changes +++ b/Changes @@ -1,8 +1,19 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - Restore support for PostgreSQL 8.3 (RT#87291) + +0.07036_02 2013-09-25 + - Skip many_to_many bridges involving might_have relationships + +0.07036_01 2013-08-11 - Fix typos in POD and comments (RT#87644) - Don't ship MYMETA.* files (RT#87713) - - Restore support for PostgreSQL 8.3 (RT#87291) + - Fix many_to_many bridges involving might_have relationships + - Allow specifying custom attributes for many_to_many bridges + - Allow specifying the separator when joining database, schema + and table names to form a moniker + - Allow using all the moniker parts in hashref moniker_map + - Allow matching all the moniker parts in constraint/exclude 0.07036 2013-07-08 - Fix stray comma in Pg on_delete/on_update => CASCADE (RT#84706) diff --git a/TODO b/TODO index 646d484..6895711 100644 --- a/TODO +++ b/TODO @@ -6,7 +6,6 @@ - encode loader options in Schema.pm - introspect on_update/on_delete/is_deferrable - Low Priority - - support multiple/all schemas, instead of just one - support pk/uk/fk info on views, possibly (materialized views?) - remove deprecated ResultSetManager stuff, rewrite using current features - Refactor RelBuilder so that it doesn't require a live mostly-built diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index 3bb3308..1da4ea0 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -16,7 +16,7 @@ use namespace::clean; # Always remember to do all digits for the version even if they're 0 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports # brain damage and presumably various other packaging systems too -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; __PACKAGE__->mk_group_accessors('inherited', qw/ _loader_args diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index b37223c..87c62a4 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -29,7 +29,7 @@ use List::MoreUtils qw/all any firstidx uniq/; use File::Temp 'tempfile'; use namespace::clean; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; __PACKAGE__->mk_group_ro_accessors('simple', qw/ schema @@ -109,6 +109,7 @@ __PACKAGE__->mk_group_accessors('simple', qw/ db_schema qualify_objects moniker_parts + moniker_part_separator /); my $CURRENT_V = 'v7'; @@ -520,6 +521,7 @@ the table. The L option is an arrayref of methods on the table class corresponding to parts of the fully qualified table name, defaulting to C<['name']>, in the order those parts are used to create the moniker name. +The parts are joined together using L. The C<'name'> entry B be present. @@ -537,21 +539,72 @@ C, C, C =back +=head2 moniker_part_separator + +String used to join L when creating the moniker. +Defaults to the empty string. Use C<::> to get a separate namespace per +database and/or schema. + =head2 constraint -Only load tables matching regex. Best specified as a qr// regex. +Only load matching tables. =head2 exclude -Exclude tables matching regex. Best specified as a qr// regex. +Exclude matching tables. + +These can be specified either as a regex (preferrably on the C +form), or as an arrayref of arrayrefs. Regexes are matched against +the (unqualified) table name, while arrayrefs are matched according to +L. + +For example: + + db_schema => [qw(some_schema other_schema)], + moniker_parts => [qw(schema name)], + constraint => [ + [ qr/\Asome_schema\z/ => qr/\A(?:foo|bar)\z/ ], + [ qr/\Aother_schema\z/ => qr/\Abaz\z/ ], + ], + +In this case only the tables C and C in C and +C in C will be dumped. =head2 moniker_map -Overrides the default table name to moniker translation. Can be either a -hashref of table keys and moniker values, or a coderef for a translator -function taking a L argument -(which stringifies to the unqualified table name) and returning a scalar -moniker. If the hash entry does not exist, or the function returns a false +Overrides the default table name to moniker translation. Either + +=over + +=item * + +a nested hashref, which will be traversed according to L + +For example: + + moniker_parts => [qw(schema name)], + moniker_map => { + foo => { + bar => "FooishBar", + }, + }, + +In which case the table C in the C schema would get the moniker +C. + +=item * + +a hashref of unqualified table name keys and moniker values + +=item * + +a coderef for a translator function taking a L
argument (which stringifies to the +unqualified table name) and returning a scalar moniker + +=back + +If the hash entry does not exist, or the function returns a false value, the code falls back to default behavior for that table name. The default behavior is to split on case transition and non-alphanumeric @@ -1173,6 +1226,10 @@ sub new { } } + if (not defined $self->moniker_part_separator) { + $self->moniker_part_separator(''); + } + return $self; } @@ -2343,7 +2400,23 @@ sub _run_user_map { my $default_ident = $default_code->( $ident, @extra ); my $new_ident; if( $map && ref $map eq 'HASH' ) { - $new_ident = $map->{ $ident }; + if (my @parts = try{ @{ $ident } }) { + my $part_map = $map; + while (@parts) { + my $part = shift @parts; + last unless exists $part_map->{ $part }; + if ( !ref $part_map->{ $part } && !@parts ) { + $new_ident = $part_map->{ $part }; + last; + } + elsif ( ref $part_map->{ $part } eq 'HASH' ) { + $part_map = $part_map->{ $part }; + } + } + } + if( !$new_ident && !ref $map->{ $ident } ) { + $new_ident = $map->{ $ident }; + } } elsif( $map && ref $map eq 'CODE' ) { $new_ident = $map->( $ident, $default_ident, @extra ); @@ -2596,10 +2669,10 @@ sub _default_table2moniker { @part_parts = split /\s+/, $inflected; } - push @all_parts, map ucfirst, @part_parts; + push @all_parts, join '', map ucfirst, @part_parts; } - return join '', @all_parts; + return join $self->moniker_part_separator, @all_parts; } sub _table2moniker { diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index d3ce9b7..97c6901 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -10,7 +10,7 @@ use Carp::Clan qw/^DBIx::Class/; use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; __PACKAGE__->mk_group_accessors('simple', qw/ _disable_pk_detection @@ -194,6 +194,40 @@ sub _tables_list { return $self->_filter_tables(\@tables, $opts); } +sub _recurse_constraint { + my ($constraint, @parts) = @_; + + my $name = shift @parts; + + # If there are any parts left, the constraint must be an arrayref + croak "depth of constraint/exclude array does not match length of moniker_parts" + unless !!@parts == !!(ref $constraint eq 'ARRAY'); + + # if ths is the last part, use the constraint directly + return $name =~ $constraint unless @parts; + + # recurse into the first matching subconstraint + foreach (@{$constraint}) { + my ($re, $sub) = @{$_}; + return _recurse_constraint($sub, @parts) + if $name =~ $re; + } + return 0; +} + +sub _check_constraint { + my ($include, $constraint, @tables) = @_; + + return @tables unless defined $constraint; + + return grep { !$include xor _recurse_constraint($constraint, @{$_}) } @tables + if ref $constraint eq 'ARRAY'; + + return grep { !$include xor /$constraint/ } @tables; +} + + + # apply constraint/exclude and ignore bad tables and views sub _filter_tables { my ($self, $tables, $opts) = @_; @@ -202,11 +236,8 @@ sub _filter_tables { my @filtered_tables; $opts ||= {}; - my $constraint = $opts->{constraint}; - my $exclude = $opts->{exclude}; - - @tables = grep { /$constraint/ } @tables if defined $constraint; - @tables = grep { ! /$exclude/ } @tables if defined $exclude; + @tables = _check_constraint(1, $opts->{constraint}, @tables); + @tables = _check_constraint(0, $opts->{exclude}, @tables); TABLE: for my $table (@tables) { try { diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm b/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm index 2ee1728..097d341 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ADO/MS_Jet.pm b/lib/DBIx/Class/Schema/Loader/DBI/ADO/MS_Jet.pm index 3c66544..2071009 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ADO/MS_Jet.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ADO/MS_Jet.pm @@ -10,7 +10,7 @@ use mro 'c3'; use Try::Tiny; use namespace::clean; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ADO/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Schema/Loader/DBI/ADO/Microsoft_SQL_Server.pm index aaf7d60..0348a25 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ADO/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ADO/Microsoft_SQL_Server.pm @@ -8,7 +8,7 @@ use base qw/ /; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Component/QuotedDefault.pm b/lib/DBIx/Class/Schema/Loader/DBI/Component/QuotedDefault.pm index 501aa9a..293ba20 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Component/QuotedDefault.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Component/QuotedDefault.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm b/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm index a083f7a..b954a48 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm @@ -13,7 +13,7 @@ use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Firebird.pm b/lib/DBIx/Class/Schema/Loader/DBI/Firebird.pm index eeb4c87..7471138 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Firebird.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Firebird.pm @@ -5,7 +5,7 @@ use warnings; use base qw/DBIx::Class::Schema::Loader::DBI::InterBase/; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Informix.pm b/lib/DBIx/Class/Schema/Loader/DBI/Informix.pm index 67a27a3..82edd10 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Informix.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Informix.pm @@ -10,7 +10,7 @@ use Try::Tiny; use namespace::clean; use DBIx::Class::Schema::Loader::Table::Informix (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/InterBase.pm b/lib/DBIx/Class/Schema/Loader/DBI/InterBase.pm index a18f9c1..2a1fe4c 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/InterBase.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/InterBase.pm @@ -9,7 +9,7 @@ use List::Util 'first'; use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; sub _supports_db_schema { 0 } diff --git a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm index 8460194..d6c94d6 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm @@ -10,7 +10,7 @@ use namespace::clean; use DBIx::Class::Schema::Loader::Table::Sybase (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ODBC.pm b/lib/DBIx/Class/Schema/Loader/DBI/ODBC.pm index 2e6f076..b3ac4f2 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ODBC.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ODBC.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/ACCESS.pm b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/ACCESS.pm index dbac81e..2b03bf3 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/ACCESS.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/ACCESS.pm @@ -8,7 +8,7 @@ use Try::Tiny; use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; __PACKAGE__->mk_group_accessors('simple', qw/ __ado_connection diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Firebird.pm b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Firebird.pm index b318cbf..3d40078 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Firebird.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Firebird.pm @@ -8,7 +8,7 @@ use base qw/ /; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Microsoft_SQL_Server.pm index b8e2e1c..c45053a 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/Microsoft_SQL_Server.pm @@ -8,7 +8,7 @@ use base qw/ /; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/SQL_Anywhere.pm b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/SQL_Anywhere.pm index 9a6920a..d4cb102 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/ODBC/SQL_Anywhere.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/ODBC/SQL_Anywhere.pm @@ -8,7 +8,7 @@ use base qw/ /; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index ff3356e..29b791e 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -7,7 +7,7 @@ use mro 'c3'; use Try::Tiny; use namespace::clean; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm index c2cd75e..09d7854 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm b/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm index 52b1f6c..d0c1f67 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/SQLAnywhere.pm @@ -8,7 +8,7 @@ use List::MoreUtils 'any'; use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm b/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm index 2a64224..9ddcbc7 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm @@ -6,7 +6,7 @@ use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault'; use mro 'c3'; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm b/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm index d660d37..c64790c 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Sybase.pm @@ -9,7 +9,7 @@ use namespace::clean; use DBIx::Class::Schema::Loader::Table::Sybase (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Common.pm b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Common.pm index 5293131..2eeb8f5 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Common.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Common.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm index b5e4a0b..4df9049 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI::MSSQL'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Writing.pm b/lib/DBIx/Class/Schema/Loader/DBI/Writing.pm index 0bb20f9..a506f86 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Writing.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Writing.pm @@ -1,7 +1,7 @@ package DBIx::Class::Schema::Loader::DBI::Writing; use strict; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; # Empty. POD only. diff --git a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm index d493b88..adc2fba 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm @@ -12,7 +12,7 @@ use Scalar::Util 'blessed'; use namespace::clean; use DBIx::Class::Schema::Loader::Table (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; =head1 NAME diff --git a/lib/DBIx/Class/Schema/Loader/DBObject.pm b/lib/DBIx/Class/Schema/Loader/DBObject.pm index 1e6df30..a4a9e2c 100644 --- a/lib/DBIx/Class/Schema/Loader/DBObject.pm +++ b/lib/DBIx/Class/Schema/Loader/DBObject.pm @@ -35,6 +35,7 @@ __PACKAGE__->mk_group_accessors(simple => qw/ use overload '""' => sub { $_[0]->name }, + '@{}' => sub { $_[0]->name_parts }, fallback => 1; =head2 new @@ -153,6 +154,20 @@ sub dbic_name { return $self->name; } +=head2 name_parts + +Returns an arrayref of the values returned by the methods specified in +the L of +the L object. The object arrayrefifies to this value. + +=cut + +sub name_parts { + my ($self) = shift; + return [ map { $self->$_ } @{$self->loader->moniker_parts} ]; +} + + =head1 SEE ALSO L, L, diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index c5f213a..a4f3f0a 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -18,7 +18,7 @@ use String::ToIdentifier::EN::Unicode (); use Class::Unload (); use Class::Inspector (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; # Glossary: # @@ -517,12 +517,16 @@ sub _generate_m2ms { $_->{method} eq 'has_many' && $_->{args}[1] eq $class } @{ $all_code->{$class1} }; + next unless $class1_to_link_table_rel; + my $class1_to_link_table_rel_name = $class1_to_link_table_rel->{args}[0]; my $class2_to_link_table_rel = first { $_->{method} eq 'has_many' && $_->{args}[1] eq $class } @{ $all_code->{$class2} }; + next unless $class2_to_link_table_rel; + my $class2_to_link_table_rel_name = $class2_to_link_table_rel->{args}[0]; my $class1_link_rel = $rels->[1]{args}[0]; @@ -605,6 +609,16 @@ sub _generate_m2ms { $class1_to_class2_relname, $class1_to_link_table_rel_name, $class1_link_rel, + $self->_relationship_attrs('many_to_many', {}, { + rel_type => 'many_to_many', + rel_name => $class1_to_class2_relname, + local_source => $self->schema->source($class1_local_moniker), + remote_source => $self->schema->source($class1_remote_moniker), + local_table => $self->loader->class_to_table->{$class1}, + local_cols => \@class1_from_cols, + remote_table => $self->loader->class_to_table->{$class2}, + remote_cols => \@class2_from_cols, + }) || (), ], extra => { local_class => $class1, @@ -620,6 +634,16 @@ sub _generate_m2ms { $class2_to_class1_relname, $class2_to_link_table_rel_name, $class2_link_rel, + $self->_relationship_attrs('many_to_many', {}, { + rel_type => 'many_to_many', + rel_name => $class2_to_class1_relname, + local_source => $self->schema->source($class2_local_moniker), + remote_source => $self->schema->source($class2_remote_moniker), + local_table => $self->loader->class_to_table->{$class2}, + local_cols => \@class2_from_cols, + remote_table => $self->loader->class_to_table->{$class1}, + remote_cols => \@class1_from_cols, + }) || (), ], extra => { local_class => $class2, diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm index 19c01f2..68351dd 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; sub _relnames_and_method { my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm index da6abb2..1bc3b91 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm @@ -8,7 +8,7 @@ use DBIx::Class::Schema::Loader::Utils 'array_eq'; use namespace::clean; use Lingua::EN::Inflect::Number (); -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; sub _to_PL { my ($self, $name) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm index ea06243..b541125 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_07'; use mro 'c3'; -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; sub _normalize_name { my ($self, $name) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_07.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_07.pm index d7a3912..5166723 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_07.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_07.pm @@ -17,7 +17,7 @@ L. =cut -our $VERSION = '0.07036'; +our $VERSION = '0.07036_02'; sub _strip_id_postfix { my ($self, $name) = @_; diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 0ca9461..07b1bc1 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -343,7 +343,62 @@ $t->dump_test( regexes => { 'Result/MySchemaFoo' => [ qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, - # the has_many relname should not have the schema in it! + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchemaBar"/m, + ], + }, +); + +# test moniker_part_separator +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + moniker_part_separator => '::', + qualify_objects => 1, + use_namespaces => 1, + }, + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchema/Foo' => [ + qr/^package DBICTest::DumpMore::1::Result::MySchema::Foo;$/m, + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + # the has_many relname should not have the schema in it, but the class should + qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchema::Bar"/m, + ], + }, +); + +# test moniker_part_separator + moniker_map + recursive constraints +$t->dump_test( + classname => 'DBICTest::DumpMore::1', + options => { + db_schema => 'my_schema', + moniker_parts => ['_schema', 'name'], + moniker_part_separator => '::', + qualify_objects => 1, + use_namespaces => 1, + moniker_map => { + my_schema => { foo => "MySchema::Floop" }, + }, + constraint => [ [ qr/my_schema/ => qr/foo|bar/ ] ], + exclude => [ [ qr/my_schema/ => qr/bar/ ] ], + }, + warnings => [ + qr/^db_schema is not supported on SQLite/, + ], + regexes => { + 'Result/MySchema/Floop' => [ + qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;$/m, + qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m, + ], + }, + neg_regexes => { + 'Result/MySchema/Floop' => [ + # the bar table should not be loaded, so no relationship should exist qr/^__PACKAGE__->has_many\(\n "bars"/m, ], }, diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 8b174d0..9c92e07 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -119,7 +119,7 @@ sub run_tests { $num_rescans++ if $self->{vendor} eq 'Firebird'; plan tests => @connect_info * - (221 + $num_rescans * $col_accessor_map_tests + $extra_count + ($self->{data_type_tests}{test_count} || 0)); + (228 + $num_rescans * $col_accessor_map_tests + $extra_count + ($self->{data_type_tests}{test_count} || 0)); foreach my $info_idx (0..$#connect_info) { my $info = $connect_info[$info_idx]; @@ -212,7 +212,7 @@ my (@statements, @statements_reltests, @statements_advanced, sub CONSTRAINT { my $self = shift; -return qr/^(?:\S+\.)?(?:(?:$self->{vendor}|extra)[_-]?)?loader[_-]?test[0-9]+(?!.*_)/i; +return qr/^(?:(?:$self->{vendor}|extra)[_-]?)?loader[_-]?test[0-9]+(?!.*_)/i; } sub setup_schema { @@ -254,6 +254,7 @@ sub setup_schema { ) : (), col_collision_map => { '^(can)\z' => 'caught_collision_%s' }, rel_collision_map => { '^(set_primary_key)\z' => 'caught_rel_collision_%s' }, + relationship_attrs => { many_to_many => { order_by => 'me.id' } }, col_accessor_map => \&test_col_accessor_map, result_components_map => { LoaderTest2X => 'TestComponentForMap', LoaderTest1 => '+TestComponentForMapFQN' }, uniq_to_primary => 1, @@ -283,7 +284,7 @@ sub setup_schema { my $standard_sources = not defined $expected_count; if ($standard_sources) { - $expected_count = 37; + $expected_count = 38; if (not ($self->{vendor} eq 'mssql' && $connect_info->[0] =~ /Sybase/)) { $expected_count++ for @{ $self->{data_type_tests}{table_names} || [] }; @@ -722,6 +723,10 @@ qr/\n__PACKAGE__->load_components\("TestSchemaComponent", "\+TestSchemaComponent my $class36 = $classes->{loader_test36}; my $rsobj36 = $conn->resultset($moniker36); + my $moniker37 = $monikers->{loader_test37}; + my $class37 = $classes->{loader_test37}; + my $rsobj37 = $conn->resultset($moniker37); + isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); @@ -746,6 +751,7 @@ qr/\n__PACKAGE__->load_components\("TestSchemaComponent", "\+TestSchemaComponent isa_ok( $rsobj33, "DBIx::Class::ResultSet" ); isa_ok( $rsobj34, "DBIx::Class::ResultSet" ); isa_ok( $rsobj36, "DBIx::Class::ResultSet" ); + isa_ok( $rsobj37, "DBIx::Class::ResultSet" ); # basic rel test my $obj4 = try { $rsobj4->find(123) } || $rsobj4->search({ id => 123 })->single; @@ -929,11 +935,19 @@ qr/\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\( is $m2m->{relation}, 'loader_test20s', 'm2m near rel'; is $m2m->{foreign_relation}, 'child', 'm2m far rel'; + is $m2m->{attrs}->{order_by}, 'me.id', 'm2m bridge attrs'; ok($m2m = (try { $class19->_m2m_metadata->{parents} }), 'many_to_many created'); is $m2m->{relation}, 'loader_test20s', 'm2m near rel'; is $m2m->{foreign_relation}, 'parent', 'm2m far rel'; + is $m2m->{attrs}->{order_by}, 'me.id', 'm2m bridge attrs'; + + ok( $class37->relationship_info('parent'), 'parents rel created' ); + ok( $class37->relationship_info('child'), 'child rel created' ); + + is_deeply($class32->_m2m_metadata, {}, 'many_to_many not created for might_have'); + is_deeply($class34->_m2m_metadata, {}, 'many_to_many not created for might_have'); # test double multi-col fk 26 -> 25 my $obj26 = try { $rsobj26->find(33) } || $rsobj26->search({ id => 33 })->single; @@ -1851,6 +1865,17 @@ sub create { ) $self->{innodb} }, q{ INSERT INTO loader_test34 (id,rel1,rel2) VALUES (1,2,2) }, + + qq{ + CREATE TABLE loader_test37 ( + parent INTEGER NOT NULL, + child INTEGER NOT NULL UNIQUE, + PRIMARY KEY (parent, child), + FOREIGN KEY (parent) REFERENCES loader_test32 (id), + FOREIGN KEY (child) REFERENCES loader_test34 (id) + ) $self->{innodb} + }, + q{ INSERT INTO loader_test37 (parent, child) VALUES (1,1) }, ); @statements_advanced = ( @@ -2043,6 +2068,7 @@ sub drop_tables { loader_test28 loader_test29 loader_test27 + loader_test37 loader_test32 loader_test31 loader_test34