Revision history for Perl extension DBIx::Class::Schema::Loader
+ - SQLite: detect is_deferrable for inline FKs
- support coderefs for relationship_attrs
0.07031 2012-09-06 15:07:08
EOF
foreach my $fk (@rels) {
- my $local_cols = '"?' . (join '"? \s* , \s* "?', @{ $fk->{local_columns} }) . '"?';
- my $remote_cols = '"?' . (join '"? \s* , \s* "?', @{ $fk->{remote_columns} || [] }) . '"?';
+ my $local_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{local_columns} }) . '"?';
+ my $remote_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{remote_columns} || [] }) . '"?';
my ($deferrable_clause) = $ddl =~ /
foreign \s+ key \s* \( \s* $local_cols \s* \) \s* references \s* (?:\S+|".+?(?<!")") \s*
(?:\( \s* $remote_cols \s* \) \s*)?
$fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
}
else {
- $fk->{attrs}{is_deferrable} = 0;
+ # check for inline constraint if 1 local column
+ if (@{ $fk->{local_columns} } == 1) {
+ my ($local_col) = @{ $fk->{local_columns} };
+ my ($remote_col) = @{ $fk->{remote_columns} || [] };
+ $remote_col ||= '';
+
+ my ($deferrable_clause) = $ddl =~ /
+ "?\Q$local_col\E"? \s* (?:\w+\s*)* (?: \( \s* \d\+ (?:\s*,\s*\d+)* \s* \) )? \s*
+ references \s* (?:\S+|".+?(?<!")") (?:\s* \( \s* "?\Q$remote_col\E"? \s* \))? \s*
+ (?:(?:
+ on \s+ (?:delete|update) \s+ (?:set \s+ null|set \s+ default|cascade|restrict|no \s+ action)
+ |
+ match \s* (?:\S+|".+?(?<!")")
+ ) \s*)*
+ ((?:not)? \s* deferrable)?
+ /sxi;
+
+ if ($deferrable_clause) {
+ $fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
+ }
+ else {
+ $fk->{attrs}{is_deferrable} = 0;
+ }
+ }
+ else {
+ $fk->{attrs}{is_deferrable} = 0;
+ }
}
}
on delete restrict on update set null deferrable
)
},
+ # test inline constraint
+ q{
+ create table extra_loader_test10 (
+ id integer primary key,
+ eight_id int references extra_loader_test8(id) on delete restrict on update set null deferrable
+ )
+ },
],
pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
extra_loader_test4 extra_loader_test6 extra_loader_test7
- extra_loader_test8 extra_loader_test9 / ],
- count => 15,
+ extra_loader_test8 extra_loader_test9 extra_loader_test10 / ],
+ count => 19,
run => sub {
my ($schema, $monikers, $classes) = @_;
is $rel_info->{attrs}{is_deferrable}, 1,
'DEFERRABLE clause introspected correctly';
+
+ ok (($rel_info = $schema->source('ExtraLoaderTest10')->relationship_info('eight')),
+ 'got rel info');
+
+ is $rel_info->{attrs}{on_delete}, 'RESTRICT',
+ 'ON DELETE clause introspected correctly for inline FK';
+
+ is $rel_info->{attrs}{on_update}, 'SET NULL',
+ 'ON UPDATE clause introspected correctly for inline FK';
+
+ is $rel_info->{attrs}{is_deferrable}, 1,
+ 'DEFERRABLE clause introspected correctly for inline FK';
},
},
);