Commit | Line | Data |
0e2c6809 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
8 | BEGIN { |
9 | eval "use DBD::mysql; use SQL::Translator 0.09;"; |
10 | plan $@ |
11 | ? ( skip_all => 'needs SQL::Translator 0.09 for testing' ) |
12 | : ( tests => 99 ); |
13 | } |
14 | |
15 | my $schema = DBICTest->init_schema(); |
16 | |
17 | { |
18 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } }); |
19 | |
20 | foreach my $source ($schema->sources) { |
21 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
22 | |
23 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
24 | my @indices = $table->get_indices; |
25 | my $index_count = scalar(@indices); |
26 | is($index_count, $fk_count, "correct number of indices for $source with no args"); |
27 | } |
28 | } |
29 | |
30 | { |
31 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } }); |
32 | |
33 | foreach my $source ($schema->sources) { |
34 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
35 | |
36 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
37 | my @indices = $table->get_indices; |
38 | my $index_count = scalar(@indices); |
39 | is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1"); |
40 | } |
41 | } |
42 | |
43 | { |
44 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } }); |
45 | |
46 | foreach my $source ($schema->sources) { |
47 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
48 | |
49 | my @indices = $table->get_indices; |
50 | my $index_count = scalar(@indices); |
51 | is($index_count, 0, "correct number of indices for $source with add_fk_index => 0"); |
52 | } |
53 | } |
54 | |
55 | sub create_schema { |
56 | my $args = shift; |
57 | |
58 | my $schema = $args->{schema}; |
59 | my $additional_sqltargs = $args->{args} || {}; |
60 | |
61 | my $sqltargs = { |
62 | add_drop_table => 1, |
63 | ignore_constraint_names => 1, |
64 | ignore_index_names => 1, |
65 | %{$additional_sqltargs} |
66 | }; |
67 | |
68 | my $sqlt = SQL::Translator->new( $sqltargs ); |
69 | |
70 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
71 | return $sqlt->translate({ data => $schema }) or die $sqlt->error; |
72 | } |