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)
- 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
# 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
use File::Temp 'tempfile';
use namespace::clean;
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
__PACKAGE__->mk_group_ro_accessors('simple', qw/
schema
db_schema
qualify_objects
moniker_parts
+ moniker_part_separator
/);
my $CURRENT_V = 'v7';
The L</moniker_parts> 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</moniker_part_separator>.
The C<'name'> entry B<must> be present.
=back
+=head2 moniker_part_separator
+
+String used to join L</moniker_parts> 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<qr//>
+form), or as an arrayref of arrayrefs. Regexes are matched against
+the (unqualified) table name, while arrayrefs are matched according to
+L</moniker_parts>.
+
+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<foo> and C<bar> in C<some_schema> and
+C<baz> in C<other_schema> 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<table object|DBIx::Class::Schema::Loader::Table> 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</moniker_parts>
+
+For example:
+
+ moniker_parts => [qw(schema name)],
+ moniker_map => {
+ foo => {
+ bar => "FooishBar",
+ },
+ },
+
+In which case the table C<bar> in the C<foo> schema would get the moniker
+C<FooishBar>.
+
+=item *
+
+a hashref of unqualified table name keys and moniker values
+
+=item *
+
+a coderef for a translator function taking a L<table
+object|DBIx::Class::Schema::Loader::Table> 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
}
}
+ if (not defined $self->moniker_part_separator) {
+ $self->moniker_part_separator('');
+ }
+
return $self;
}
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 );
@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 {
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
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) = @_;
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 {
use base 'DBIx::Class::Schema::Loader::DBI';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use Try::Tiny;
use namespace::clean;
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
/;
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base 'DBIx::Class::Schema::Loader::DBI';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use DBIx::Class::Schema::Loader::Table ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base qw/DBIx::Class::Schema::Loader::DBI::InterBase/;
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use namespace::clean;
use DBIx::Class::Schema::Loader::Table::Informix ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use namespace::clean;
use DBIx::Class::Schema::Loader::Table ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
sub _supports_db_schema { 0 }
use DBIx::Class::Schema::Loader::Table::Sybase ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base 'DBIx::Class::Schema::Loader::DBI';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
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
/;
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
/;
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
/;
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use Try::Tiny;
use namespace::clean;
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use namespace::clean;
use DBIx::Class::Schema::Loader::Table ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use mro 'c3';
use DBIx::Class::Schema::Loader::Table ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use DBIx::Class::Schema::Loader::Table::Sybase ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base 'DBIx::Class::Schema::Loader::DBI';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use base 'DBIx::Class::Schema::Loader::DBI::MSSQL';
use mro 'c3';
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
package DBIx::Class::Schema::Loader::DBI::Writing;
use strict;
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
# Empty. POD only.
use namespace::clean;
use DBIx::Class::Schema::Loader::Table ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
=head1 NAME
use overload
'""' => sub { $_[0]->name },
+ '@{}' => sub { $_[0]->name_parts },
fallback => 1;
=head2 new
return $self->name;
}
+=head2 name_parts
+
+Returns an arrayref of the values returned by the methods specified in
+the L<moniker_parts|DBIx::Class::Scheam::Loader::Base/moniker_parts> of
+the L</loader> 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<DBIx::Class::Schema::Loader::Table>, L<DBIx::Class::Schema::Loader>,
use Class::Unload ();
use Class::Inspector ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
# Glossary:
#
$_->{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];
$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,
$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,
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 ) = @_;
use namespace::clean;
use Lingua::EN::Inflect::Number ();
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
sub _to_PL {
my ($self, $name) = @_;
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) = @_;
=cut
-our $VERSION = '0.07036';
+our $VERSION = '0.07036_02';
sub _strip_id_postfix {
my ($self, $name) = @_;
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,
],
},
$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];
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 {
) : (),
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,
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} || [] };
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" );
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;
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;
) $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 = (
loader_test28
loader_test29
loader_test27
+ loader_test37
loader_test32
loader_test31
loader_test34