1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy';
6 BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 }
15 use DBIx::Class::_Util 'sigwarn_silencer';
17 # Test for SQLT-related leaks
19 my $s = DBICTest::Schema->clone;
22 create_schema ({ schema => $s }),
23 create_schema ({ args => { parser_args => { dbic_schema => $s } } }),
26 for my $parser_args_key (qw(
32 push @schemas, create_schema({
33 args => { parser_args => { $parser_args_key => $s } }
35 } qr/\Qparser_args => {\E.+?is deprecated.+\Q@{[__FILE__]}/,
36 "deprecated crazy parser_arg '$parser_args_key' warned";
39 Scalar::Util::weaken ($s);
41 ok (!$s, 'Schema not leaked');
43 isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
47 # make sure classname-style works
48 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
50 # make sure a connected instance passed via $args does not get the $dbh improperly serialized
53 DBIx::Class::Optional::Dependencies->skip_without( 'YAML>=0' );
57 my $s = DBICTest->init_schema(no_populate => 1);
58 ok ($s->storage->connected, '$schema instance connected');
60 # roundtrip through YAML
61 my $yaml_rt_schema = SQL::Translator->new(
62 parser => 'SQL::Translator::Parser::YAML'
64 data => SQL::Translator->new(
65 parser_args => { dbic_schema => $s },
66 parser => 'SQL::Translator::Parser::DBIx::Class',
67 producer => 'SQL::Translator::Producer::YAML',
71 isa_ok ( $yaml_rt_schema, 'SQL::Translator::Schema', 'SQLT schema object produced after YAML roundtrip');
73 ok ($s->storage->connected, '$schema instance still connected');
76 eval <<'EOE' or die $@;
78 # we are in END - everything remains global
80 $^W = 1; # important, otherwise DBI won't trip the next fail()
81 $SIG{__WARN__} = sub {
82 fail "Unexpected global destruction warning"
83 if $_[0] =~ /is not a DBI/;
91 my $schema = DBICTest->init_schema( no_deploy => 1 );
93 # Dummy was yanked out by the sqlt hook test
94 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
95 # YearXXXXCDs are views
98 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
102 my $idx_exceptions = {
104 'ForceForeign' => -1,
106 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
110 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
112 foreach my $source_name (@sources) {
113 my $table = get_table($sqlt_schema, $schema, $source_name);
115 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
116 $fk_count += $idx_exceptions->{$source_name} || 0;
117 my @indices = $table->get_indices;
119 my $index_count = scalar(@indices);
120 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
122 for my $index (@indices) {
123 my $source = $schema->source($source_name);
124 my $pk_test = join("\x00", $source->primary_columns);
125 my $idx_test = join("\x00", $index->fields);
126 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
132 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
134 foreach my $source_name (@sources) {
135 my $table = get_table($sqlt_schema, $schema, $source_name);
137 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
138 $fk_count += $idx_exceptions->{$source_name} || 0;
139 my @indices = $table->get_indices;
140 my $index_count = scalar(@indices);
141 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
146 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
148 foreach my $source (@sources) {
149 my $table = get_table($sqlt_schema, $schema, $source);
151 my @indices = $table->get_indices;
152 my $index_count = scalar(@indices);
153 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
159 package # hide from PAUSE
160 DBICTest::Schema::NoViewDefinition;
162 use base qw/DBICTest::BaseResult/;
164 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
165 __PACKAGE__->table('noviewdefinition');
170 my $schema_invalid_view = $schema->clone;
171 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
173 throws_ok { create_schema({ schema => $schema_invalid_view }) }
174 qr/view noviewdefinition is missing a view_definition/,
175 'parser detects views with a view_definition';
179 my $sqlt_schema = create_schema ({
189 [$sqlt_schema->get_tables ],
191 'sources limitng with relationships works',
197 package DBICTest::PartialSchema;
199 use base qw/DBIx::Class::Schema/;
201 __PACKAGE__->load_classes(
202 { 'DBICTest::Schema' => [qw/
213 my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
216 my $sqlt_schema = do {
218 local $SIG{__WARN__} = sigwarn_silencer(
219 qr/Ignoring relationship .+ related resultsource .+ is not registered with this schema/
222 create_schema({ schema => $partial_schema });
225 my @tables = $sqlt_schema->get_tables;
228 [sort map { $_->name } @tables],
229 [qw/cd cd_to_producer producer tags track/],
230 'partial dbic schema parsing ok',
233 # the primary key is currently unnamed in sqlt - adding below
234 my %constraints_for_table = (
235 producer => [qw/prod_name /],
236 tags => [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
237 track => [qw/track_cd_position track_cd_title track_fk_cd /],
238 cd => [qw/cd_artist_title cd_fk_single_track /],
239 cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer /],
242 for my $table (@tables) {
243 my $tablename = $table->name;
244 my @constraints = $table->get_constraints;
246 [ sort map { $_->name } @constraints ],
248 # the primary key (present on all loaded tables) is currently named '' in sqlt
249 # subject to future changes
250 [ '', @{$constraints_for_table{$tablename}} ],
252 "constraints of table '$tablename' ok",
255 }, 'partial schema tests successful');
259 my $cd_rsrc = $schema->source('CD');
260 $cd_rsrc->name(\'main.cd');
262 my $sqlt_schema = create_schema(
263 { schema => $schema },
264 args => { ignore_constraint_names => 0, ignore_index_names => 0 }
267 foreach my $source_name (qw(CD)) {
268 my $table = get_table($sqlt_schema, $schema, $source_name);
270 !(grep {$_->name =~ m/main\./} $table->get_indices),
271 'indices have periods stripped out'
274 !(grep {$_->name =~ m/main\./} $table->get_constraints),
275 'constraints have periods stripped out'
285 my $additional_sqltargs = $args->{args} || {};
289 ignore_constraint_names => 1,
290 ignore_index_names => 1,
291 %{$additional_sqltargs}
294 my $sqlt = SQL::Translator->new( $sqltargs );
296 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
297 return $sqlt->translate(
298 $args->{schema} ? ( data => $args->{schema} ) : ()
299 ) || die $sqlt->error;
303 my ($sqlt_schema, $schema, $source) = @_;
305 my $table_name = $schema->source($source)->from;
306 $table_name = $$table_name if ref $table_name;
308 return $sqlt_schema->get_table($table_name);