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