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 | |
1dac6833 |
8 | |
0e2c6809 |
9 | BEGIN { |
10 | eval "use DBD::mysql; use SQL::Translator 0.09;"; |
1dac6833 |
11 | if ($@) { |
d23160de |
12 | plan skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing'; |
1dac6833 |
13 | } |
0e2c6809 |
14 | } |
15 | |
16 | my $schema = DBICTest->init_schema(); |
1dac6833 |
17 | plan tests => ($schema->sources * 3); |
0e2c6809 |
18 | |
19 | { |
20 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } }); |
21 | |
22 | foreach my $source ($schema->sources) { |
23 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
24 | |
25 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
26 | my @indices = $table->get_indices; |
27 | my $index_count = scalar(@indices); |
2581038c |
28 | $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def |
0e2c6809 |
29 | is($index_count, $fk_count, "correct number of indices for $source with no args"); |
30 | } |
31 | } |
32 | |
33 | { |
34 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } }); |
35 | |
36 | foreach my $source ($schema->sources) { |
37 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
38 | |
39 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
40 | my @indices = $table->get_indices; |
41 | my $index_count = scalar(@indices); |
2581038c |
42 | $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def |
0e2c6809 |
43 | is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1"); |
44 | } |
45 | } |
46 | |
47 | { |
48 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } }); |
49 | |
50 | foreach my $source ($schema->sources) { |
51 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
52 | |
53 | my @indices = $table->get_indices; |
54 | my $index_count = scalar(@indices); |
55 | is($index_count, 0, "correct number of indices for $source with add_fk_index => 0"); |
56 | } |
57 | } |
58 | |
59 | sub create_schema { |
60 | my $args = shift; |
61 | |
62 | my $schema = $args->{schema}; |
63 | my $additional_sqltargs = $args->{args} || {}; |
64 | |
65 | my $sqltargs = { |
66 | add_drop_table => 1, |
67 | ignore_constraint_names => 1, |
68 | ignore_index_names => 1, |
69 | %{$additional_sqltargs} |
70 | }; |
71 | |
72 | my $sqlt = SQL::Translator->new( $sqltargs ); |
73 | |
74 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
75 | return $sqlt->translate({ data => $schema }) or die $sqlt->error; |
76 | } |