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