Commit | Line | Data |
cb551b07 |
1 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy'; |
2 | |
0e2c6809 |
3 | use strict; |
4 | use warnings; |
206d1995 |
5 | |
0e2c6809 |
6 | use Test::More; |
a07ae2aa |
7 | use Test::Warn; |
8fd10683 |
8 | use Test::Exception; |
06e15b8e |
9 | use Scalar::Util (); |
0e2c6809 |
10 | |
4bea1fe7 |
11 | use lib qw(t/lib); |
12 | use DBICTest; |
052a832c |
13 | use DBIx::Class::_Util 'sigwarn_silencer'; |
4bea1fe7 |
14 | |
06e15b8e |
15 | # Test for SQLT-related leaks |
16 | { |
17 | my $s = DBICTest::Schema->clone; |
48775dd1 |
18 | |
19 | my @schemas = ( |
20 | create_schema ({ schema => $s }), |
a07ae2aa |
21 | create_schema ({ args => { parser_args => { dbic_schema => $s } } }), |
48775dd1 |
22 | ); |
23 | |
a07ae2aa |
24 | for my $parser_args_key (qw( |
25 | DBIx::Class::Schema |
26 | DBIx::Schema |
27 | package |
28 | )) { |
29 | warnings_exist { |
30 | push @schemas, create_schema({ |
31 | args => { parser_args => { $parser_args_key => $s } } |
32 | }); |
569b96bb |
33 | } qr/\Qparser_args => {\E.+?is deprecated.+\Q@{[__FILE__]}/, |
a07ae2aa |
34 | "deprecated crazy parser_arg '$parser_args_key' warned"; |
35 | } |
36 | |
06e15b8e |
37 | Scalar::Util::weaken ($s); |
38 | |
39 | ok (!$s, 'Schema not leaked'); |
02730621 |
40 | |
48775dd1 |
41 | isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced") |
42 | for @schemas; |
06e15b8e |
43 | } |
44 | |
02730621 |
45 | # make sure classname-style works |
46 | lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') }; |
47 | |
31399b48 |
48 | # make sure a connected instance passed via $args does not get the $dbh improperly serialized |
49 | SKIP: { |
50 | |
51 | # YAML is a build_requires dep of SQLT - it may or may not be here |
52 | eval { require YAML } or skip "Test requires YAML.pm", 1; |
53 | |
54 | lives_ok { |
55 | |
56 | my $s = DBICTest->init_schema(no_populate => 1); |
57 | ok ($s->storage->connected, '$schema instance connected'); |
58 | |
59 | # roundtrip through YAML |
60 | my $yaml_rt_schema = SQL::Translator->new( |
61 | parser => 'SQL::Translator::Parser::YAML' |
62 | )->translate( |
63 | data => SQL::Translator->new( |
569b96bb |
64 | parser_args => { dbic_schema => $s }, |
31399b48 |
65 | parser => 'SQL::Translator::Parser::DBIx::Class', |
66 | producer => 'SQL::Translator::Producer::YAML', |
67 | )->translate |
68 | ); |
69 | |
70 | isa_ok ( $yaml_rt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced after YAML roundtrip'); |
71 | |
72 | ok ($s->storage->connected, '$schema instance still connected'); |
73 | } |
74 | |
75 | eval <<'EOE' or die $@; |
76 | END { |
052a832c |
77 | # we are in END - everything remains global |
78 | # |
31399b48 |
79 | $^W = 1; # important, otherwise DBI won't trip the next fail() |
80 | $SIG{__WARN__} = sub { |
81 | fail "Unexpected global destruction warning" |
82 | if $_[0] =~ /is not a DBI/; |
83 | warn @_; |
84 | }; |
85 | } |
86 | EOE |
87 | |
88 | } |
06e15b8e |
89 | |
6ddb4ac0 |
90 | my $schema = DBICTest->init_schema( no_deploy => 1 ); |
91 | |
0fc7cd47 |
92 | # Dummy was yanked out by the sqlt hook test |
8e8a8d91 |
93 | # CustomSql tests the horrific/deprecated ->name(\$sql) hack |
e0cd97a4 |
94 | # YearXXXXCDs are views |
8e8a8d91 |
95 | # |
96 | my @sources = grep |
d6c322f8 |
97 | { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x } |
8e8a8d91 |
98 | $schema->sources |
99 | ; |
0fc7cd47 |
100 | |
f0ac764e |
101 | my $idx_exceptions = { |
102 | 'Artwork' => -1, |
103 | 'ForceForeign' => -1, |
104 | 'LinerNotes' => -1, |
827a808f |
105 | 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def |
f0ac764e |
106 | }; |
107 | |
c49ff507 |
108 | { |
206d1995 |
109 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } }); |
0e2c6809 |
110 | |
620df7e8 |
111 | foreach my $source_name (@sources) { |
112 | my $table = get_table($sqlt_schema, $schema, $source_name); |
0e2c6809 |
113 | |
206d1995 |
114 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
f0ac764e |
115 | $fk_count += $idx_exceptions->{$source_name} || 0; |
206d1995 |
116 | my @indices = $table->get_indices; |
620df7e8 |
117 | |
206d1995 |
118 | my $index_count = scalar(@indices); |
620df7e8 |
119 | is($index_count, $fk_count, "correct number of indices for $source_name with no args"); |
c49ff507 |
120 | |
620df7e8 |
121 | for my $index (@indices) { |
122 | my $source = $schema->source($source_name); |
3eaae0f2 |
123 | my $pk_test = join("\x00", $source->primary_columns); |
124 | my $idx_test = join("\x00", $index->fields); |
c1092055 |
125 | isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name"); |
620df7e8 |
126 | } |
206d1995 |
127 | } |
0e2c6809 |
128 | } |
129 | |
c49ff507 |
130 | { |
206d1995 |
131 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } }); |
0e2c6809 |
132 | |
f0ac764e |
133 | foreach my $source_name (@sources) { |
134 | my $table = get_table($sqlt_schema, $schema, $source_name); |
0e2c6809 |
135 | |
206d1995 |
136 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
f0ac764e |
137 | $fk_count += $idx_exceptions->{$source_name} || 0; |
206d1995 |
138 | my @indices = $table->get_indices; |
139 | my $index_count = scalar(@indices); |
f0ac764e |
140 | is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1"); |
206d1995 |
141 | } |
0e2c6809 |
142 | } |
143 | |
c49ff507 |
144 | { |
206d1995 |
145 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } }); |
0e2c6809 |
146 | |
206d1995 |
147 | foreach my $source (@sources) { |
148 | my $table = get_table($sqlt_schema, $schema, $source); |
0e2c6809 |
149 | |
206d1995 |
150 | my @indices = $table->get_indices; |
151 | my $index_count = scalar(@indices); |
152 | is($index_count, 0, "correct number of indices for $source with add_fk_index => 0"); |
153 | } |
0e2c6809 |
154 | } |
155 | |
c49ff507 |
156 | { |
ab7e74aa |
157 | { |
158 | package # hide from PAUSE |
159 | DBICTest::Schema::NoViewDefinition; |
160 | |
161 | use base qw/DBICTest::BaseResult/; |
8f1617e2 |
162 | |
ab7e74aa |
163 | __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); |
164 | __PACKAGE__->table('noviewdefinition'); |
8f1617e2 |
165 | |
ab7e74aa |
166 | 1; |
8f1617e2 |
167 | } |
168 | |
ab7e74aa |
169 | my $schema_invalid_view = $schema->clone; |
170 | $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition'); |
8f1617e2 |
171 | |
8fd10683 |
172 | throws_ok { create_schema({ schema => $schema_invalid_view }) } |
173 | qr/view noviewdefinition is missing a view_definition/, |
174 | 'parser detects views with a view_definition'; |
8f1617e2 |
175 | } |
176 | |
a7f4b74c |
177 | lives_ok (sub { |
178 | my $sqlt_schema = create_schema ({ |
179 | schema => $schema, |
180 | args => { |
181 | parser_args => { |
182 | sources => ['CD'] |
183 | }, |
184 | }, |
185 | }); |
186 | |
187 | is_deeply ( |
188 | [$sqlt_schema->get_tables ], |
189 | ['cd'], |
190 | 'sources limitng with relationships works', |
191 | ); |
192 | |
193 | }); |
194 | |
5b9ecfcc |
195 | { |
196 | package DBICTest::PartialSchema; |
197 | |
198 | use base qw/DBIx::Class::Schema/; |
199 | |
200 | __PACKAGE__->load_classes( |
201 | { 'DBICTest::Schema' => [qw/ |
202 | CD |
203 | Track |
204 | Tag |
205 | Producer |
206 | CD_to_Producer |
207 | /]} |
208 | ); |
209 | } |
210 | |
211 | { |
212 | my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database); |
213 | |
214 | lives_ok (sub { |
215 | my $sqlt_schema = do { |
216 | |
052a832c |
217 | local $SIG{__WARN__} = sigwarn_silencer( |
218 | qr/Ignoring relationship .+ related resultsource .+ is not registered with this schema/ |
219 | ); |
5b9ecfcc |
220 | |
221 | create_schema({ schema => $partial_schema }); |
222 | }; |
223 | |
224 | my @tables = $sqlt_schema->get_tables; |
225 | |
226 | is_deeply ( |
227 | [sort map { $_->name } @tables], |
228 | [qw/cd cd_to_producer producer tags track/], |
229 | 'partial dbic schema parsing ok', |
230 | ); |
231 | |
232 | # the primary key is currently unnamed in sqlt - adding below |
233 | my %constraints_for_table = ( |
234 | producer => [qw/prod_name /], |
235 | tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /], |
236 | track => [qw/track_cd_position track_cd_title track_fk_cd /], |
237 | cd => [qw/cd_artist_title cd_fk_single_track /], |
238 | cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /], |
239 | ); |
240 | |
241 | for my $table (@tables) { |
242 | my $tablename = $table->name; |
243 | my @constraints = $table->get_constraints; |
244 | is_deeply ( |
245 | [ sort map { $_->name } @constraints ], |
246 | |
247 | # the primary key (present on all loaded tables) is currently named '' in sqlt |
248 | # subject to future changes |
249 | [ '', @{$constraints_for_table{$tablename}} ], |
250 | |
251 | "constraints of table '$tablename' ok", |
252 | ); |
253 | } |
254 | }, 'partial schema tests successful'); |
255 | } |
256 | |
0e14d918 |
257 | { |
258 | my $cd_rsrc = $schema->source('CD'); |
259 | $cd_rsrc->name(\'main.cd'); |
260 | |
261 | my $sqlt_schema = create_schema( |
262 | { schema => $schema }, |
263 | args => { ignore_constraint_names => 0, ignore_index_names => 0 } |
264 | ); |
265 | |
266 | foreach my $source_name (qw(CD)) { |
267 | my $table = get_table($sqlt_schema, $schema, $source_name); |
268 | ok( |
269 | !(grep {$_->name =~ m/main\./} $table->get_indices), |
270 | 'indices have periods stripped out' |
271 | ); |
272 | ok( |
273 | !(grep {$_->name =~ m/main\./} $table->get_constraints), |
274 | 'constraints have periods stripped out' |
275 | ); |
276 | } |
277 | } |
278 | |
7f6f5b69 |
279 | done_testing; |
280 | |
0e2c6809 |
281 | sub create_schema { |
206d1995 |
282 | my $args = shift; |
0e2c6809 |
283 | |
206d1995 |
284 | my $additional_sqltargs = $args->{args} || {}; |
0e2c6809 |
285 | |
206d1995 |
286 | my $sqltargs = { |
8273e845 |
287 | add_drop_table => 1, |
206d1995 |
288 | ignore_constraint_names => 1, |
289 | ignore_index_names => 1, |
290 | %{$additional_sqltargs} |
291 | }; |
0e2c6809 |
292 | |
206d1995 |
293 | my $sqlt = SQL::Translator->new( $sqltargs ); |
0e2c6809 |
294 | |
206d1995 |
295 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
a07ae2aa |
296 | return $sqlt->translate( |
297 | $args->{schema} ? ( data => $args->{schema} ) : () |
298 | ) || die $sqlt->error; |
0e2c6809 |
299 | } |
c2b7c5dc |
300 | |
301 | sub get_table { |
302 | my ($sqlt_schema, $schema, $source) = @_; |
303 | |
304 | my $table_name = $schema->source($source)->from; |
305 | $table_name = $$table_name if ref $table_name; |
306 | |
307 | return $sqlt_schema->get_table($table_name); |
308 | } |