Fix skip count for Oracle multi-schema tests
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_05ora_common.t
CommitLineData
e7262300 1use strict;
fcf328c7 2use warnings;
6b0e47fc 3use Test::More;
4use Test::Exception;
c4a69b87 5use DBIx::Class::Schema::Loader 'make_schema_at';
5975bbe6 6use DBIx::Class::Schema::Loader::Utils qw/slurp_file split_name/;
c4a69b87 7use Try::Tiny;
8use File::Path 'rmtree';
5975bbe6 9use String::ToIdentifier::EN::Unicode 'to_identifier';
fcf328c7 10use namespace::clean;
c4a69b87 11
fcf328c7 12use lib qw(t/lib);
c4a69b87 13use dbixcsl_common_tests ();
14use dbixcsl_test_dir '$tdir';
15
16use constant EXTRA_DUMP_DIR => "$tdir/ora_extra_dump";
e7262300 17
18my $dsn = $ENV{DBICTEST_ORA_DSN} || '';
19my $user = $ENV{DBICTEST_ORA_USER} || '';
20my $password = $ENV{DBICTEST_ORA_PASS} || '';
21
c4a69b87 22my ($schema, $extra_schema); # for cleanup in END for extra tests
23
5975bbe6 24my $auto_inc_cb = sub {
25 my ($table, $col) = @_;
26 return (
27 qq{ CREATE SEQUENCE ${table}_${col}_seq START WITH 1 INCREMENT BY 1},
a40434df 28 qq{
5975bbe6 29 CREATE OR REPLACE TRIGGER ${table}_${col}_trigger
30 BEFORE INSERT ON ${table}
31 FOR EACH ROW
32 BEGIN
33 SELECT ${table}_${col}_seq.nextval INTO :NEW.${col} FROM dual;
34 END;
35 }
36 );
37};
38
39my $auto_inc_drop_cb = sub {
40 my ($table, $col) = @_;
41 return qq{ DROP SEQUENCE ${table}_${col}_seq };
42};
43
e7262300 44my $tester = dbixcsl_common_tests->new(
45 vendor => 'Oracle',
c1ac681d 46 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
5975bbe6 47 auto_inc_cb => $auto_inc_cb,
a40434df 48 auto_inc_drop_cb => $auto_inc_drop_cb,
b511f36e 49 preserve_case_mode_is_exclusive => 1,
50 quote_char => '"',
a40434df 51 default_is_deferrable => 0,
52 default_on_delete_clause => 'NO ACTION',
53 default_on_update_clause => 'NO ACTION',
e7262300 54 dsn => $dsn,
55 user => $user,
56 password => $password,
760fd65c 57 data_types => {
58 # From:
59 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements001.htm#i54330
60 #
61 # These tests require at least Oracle 9.2, because of the VARCHAR to
62 # VARCHAR2 casting.
63 #
64 # Character Types
65 'char' => { data_type => 'char', size => 1 },
66 'char(11)' => { data_type => 'char', size => 11 },
67 'nchar' => { data_type => 'nchar', size => 1 },
bc319c63 68 'national character'
69 => { data_type => 'nchar', size => 1 },
760fd65c 70 'nchar(11)' => { data_type => 'nchar', size => 11 },
bc319c63 71 'national character(11)'
72 => { data_type => 'nchar', size => 11 },
760fd65c 73 'varchar(20)' => { data_type => 'varchar2', size => 20 },
74 'varchar2(20)' => { data_type => 'varchar2', size => 20 },
75 'nvarchar2(20)'=> { data_type => 'nvarchar2', size => 20 },
bc319c63 76 'national character varying(20)'
77 => { data_type => 'nvarchar2', size => 20 },
760fd65c 78
79 # Numeric Types
80 #
1c22571b 81 # integer/decimal/numeric is alised to NUMBER
760fd65c 82 #
4ea15dfe 83 'integer' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
84 'int' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
85 'smallint' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
760fd65c 86
1af21646 87 # very long DEFAULT throws an ORA-24345
88 "number(15) DEFAULT to_number(decode(substrb(userenv('CLIENT_INFO'),1,1),' ',null,substrb(userenv('CLIENT_INFO'),1,10)))" => {
89 data_type => 'numeric', size => [15,0], original => { data_type => 'number' },
90 default_value => \"to_number(decode(substrb(userenv('CLIENT_INFO'),1,1),' ',null,substrb(userenv('CLIENT_INFO'),1,10)))"
91 },
92
4ea15dfe 93 'decimal' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
94 'dec' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
95 'numeric' => { data_type => 'integer', original => { data_type => 'number', size => [38,0] } },
760fd65c 96
4ea15dfe 97 'decimal(3)' => { data_type => 'numeric', size => [3,0], original => { data_type => 'number' } },
98 'dec(3)' => { data_type => 'numeric', size => [3,0], original => { data_type => 'number' } },
99 'numeric(3)' => { data_type => 'numeric', size => [3,0], original => { data_type => 'number' } },
760fd65c 100
4ea15dfe 101 'decimal(3,3)' => { data_type => 'numeric', size => [3,3], original => { data_type => 'number' } },
102 'dec(3,3)' => { data_type => 'numeric', size => [3,3], original => { data_type => 'number' } },
103 'numeric(3,3)' => { data_type => 'numeric', size => [3,3], original => { data_type => 'number' } },
760fd65c 104
1c22571b 105 'binary_float' => { data_type => 'real', original => { data_type => 'binary_float' } },
106 'binary_double' => { data_type => 'double precision', original => { data_type => 'binary_double' } },
107
108 # these are not mentioned in the summary chart, must be aliased
ceb009d3 109 real => { data_type => 'real', original => { data_type => 'float', size => 63 } },
1c22571b 110 'float(63)' => { data_type => 'real', original => { data_type => 'float', size => 63 } },
111 'float(64)' => { data_type => 'double precision', original => { data_type => 'float', size => 64 } },
112 'float(126)' => { data_type => 'double precision', original => { data_type => 'float', size => 126 } },
113 float => { data_type => 'double precision', original => { data_type => 'float', size => 126 } },
760fd65c 114
115 # Blob Types
116 'raw(50)' => { data_type => 'raw', size => 50 },
117 'clob' => { data_type => 'clob' },
118 'nclob' => { data_type => 'nclob' },
119 'blob' => { data_type => 'blob' },
120 'bfile' => { data_type => 'bfile' },
3b29d0ac 121 'long' => { data_type => 'long' },
760fd65c 122 'long raw' => { data_type => 'long raw' },
123
124 # Datetime Types
3785bc2e 125 'date' => { data_type => 'datetime', original => { data_type => 'date' } },
760fd65c 126 'date default sysdate'
3785bc2e 127 => { data_type => 'datetime', default_value => \'current_timestamp',
128 original => { data_type => 'date', default_value => \'sysdate' } },
760fd65c 129 'timestamp' => { data_type => 'timestamp' },
130 'timestamp default current_timestamp'
131 => { data_type => 'timestamp', default_value => \'current_timestamp' },
132 'timestamp(3)' => { data_type => 'timestamp', size => 3 },
133 'timestamp with time zone'
134 => { data_type => 'timestamp with time zone' },
135 'timestamp(3) with time zone'
136 => { data_type => 'timestamp with time zone', size => 3 },
137 'timestamp with local time zone'
138 => { data_type => 'timestamp with local time zone' },
139 'timestamp(3) with local time zone'
140 => { data_type => 'timestamp with local time zone', size => 3 },
141 'interval year to month'
142 => { data_type => 'interval year to month' },
143 'interval year(3) to month'
144 => { data_type => 'interval year to month', size => 3 },
145 'interval day to second'
146 => { data_type => 'interval day to second' },
147 'interval day(3) to second'
148 => { data_type => 'interval day to second', size => [3,6] },
149 'interval day to second(3)'
150 => { data_type => 'interval day to second', size => [2,3] },
151 'interval day(3) to second(3)'
152 => { data_type => 'interval day to second', size => [3,3] },
153
154 # Other Types
155 'rowid' => { data_type => 'rowid' },
156 'urowid' => { data_type => 'urowid' },
157 'urowid(3333)' => { data_type => 'urowid', size => 3333 },
158 },
5cd600fa 159 extra => {
4cd5155b 160 create => [
a40434df 161 q{
4cd5155b 162 CREATE TABLE oracle_loader_test1 (
163 id NUMBER(11),
164 value VARCHAR2(100)
165 )
166 },
167 q{ COMMENT ON TABLE oracle_loader_test1 IS 'oracle_loader_test1 table comment' },
168 q{ COMMENT ON COLUMN oracle_loader_test1.value IS 'oracle_loader_test1.value column comment' },
a40434df 169 # 4 through 8 are used for the multi-schema tests
170 q{
171 create table oracle_loader_test9 (
172 id int primary key
173 )
174 },
175 q{
176 create table oracle_loader_test10 (
177 id int primary key,
178 nine_id int,
179 foreign key (nine_id) references oracle_loader_test9(id)
180 on delete set null deferrable
181 )
182 },
4cd5155b 183 ],
a40434df 184 drop => [qw/oracle_loader_test1 oracle_loader_test9 oracle_loader_test10/],
185 count => 7 + 30 * 2,
5cd600fa 186 run => sub {
c4a69b87 187 my ($monikers, $classes);
188 ($schema, $monikers, $classes) = @_;
5cd600fa 189
190 SKIP: {
191 if (my $source = $monikers->{loader_test1s}) {
192 is $schema->source($source)->column_info('id')->{sequence},
193 'loader_test1s_id_seq',
194 'Oracle sequence detection';
195 }
196 else {
7640ef4b 197 skip 'not running common tests', 1;
5cd600fa 198 }
199 }
4cd5155b 200
61d1cca1 201 my $class = $classes->{oracle_loader_test1};
c4a69b87 202
203 my $filename = $schema->loader->get_dump_filename($class);
fcf328c7 204 my $code = slurp_file $filename;
4cd5155b 205
61d1cca1 206 like $code, qr/^=head1 NAME\n\n^$class - oracle_loader_test1 table comment\n\n^=cut\n/m,
207 'table comment';
4cd5155b 208
61d1cca1 209 like $code, qr/^=head2 value\n\n(.+:.+\n)+\noracle_loader_test1\.value column comment\n\n/m,
210 'column comment and attrs';
c4a69b87 211
a40434df 212 # test on delete/update fk clause introspection
213 ok ((my $rel_info = $schema->source('OracleLoaderTest10')->relationship_info('nine')),
214 'got rel info');
215
216 is $rel_info->{attrs}{on_delete}, 'SET NULL',
217 'ON DELETE clause introspected correctly';
218
219 is $rel_info->{attrs}{on_update}, 'NO ACTION',
220 'ON UPDATE clause set to NO ACTION by default';
221
222 is $rel_info->{attrs}{is_deferrable}, 1,
223 'DEFERRABLE clause introspected correctly';
224
c4a69b87 225 SKIP: {
17ecc734 226 skip 'Set the DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS environment variables to run the cross-schema relationship tests', 31 * 2
c4a69b87 227 unless $ENV{DBICTEST_ORA_EXTRAUSER_DSN};
228
229 $extra_schema = $schema->clone;
230 $extra_schema->connection(@ENV{map "DBICTEST_ORA_EXTRAUSER_$_",
231 qw/DSN USER PASS/
232 });
233
234 my $dbh1 = $schema->storage->dbh;
235 my $dbh2 = $extra_schema->storage->dbh;
236
237 my ($schema1) = $dbh1->selectrow_array('SELECT USER FROM DUAL');
238 my ($schema2) = $dbh2->selectrow_array('SELECT USER FROM DUAL');
239
240 $dbh1->do(<<'EOF');
241 CREATE TABLE oracle_loader_test4 (
242 id INT NOT NULL PRIMARY KEY,
243 value VARCHAR(100)
244 )
245EOF
5975bbe6 246
247 $dbh1->do($_) for $auto_inc_cb->('oracle_loader_test4', 'id');
248
c4a69b87 249 $dbh1->do("GRANT ALL ON oracle_loader_test4 TO $schema2");
5975bbe6 250 $dbh1->do("GRANT ALL ON oracle_loader_test4_id_seq TO $schema2");
251
252 $dbh1->do(<<"EOF");
253 CREATE TABLE oracle_loader_test5 (
254 id INT NOT NULL PRIMARY KEY,
255 value VARCHAR(100),
256 four_id INT REFERENCES ${schema1}.oracle_loader_test4 (id),
257 CONSTRAINT ora_loader5_uniq UNIQUE (four_id)
258 )
259EOF
260 $dbh1->do($_) for $auto_inc_cb->('oracle_loader_test5', 'id');
261 $dbh1->do("GRANT ALL ON oracle_loader_test5 TO $schema2");
262 $dbh1->do("GRANT ALL ON oracle_loader_test5_id_seq TO $schema2");
263
264 $dbh2->do(<<"EOF");
265 CREATE TABLE oracle_loader_test5 (
266 pk INT NOT NULL PRIMARY KEY,
267 value VARCHAR(100),
268 four_id INT REFERENCES ${schema1}.oracle_loader_test4 (id),
269 CONSTRAINT ora_loader5_uniq UNIQUE (four_id)
270 )
271EOF
272 $dbh2->do($_) for $auto_inc_cb->('oracle_loader_test5', 'pk');
273 $dbh2->do("GRANT ALL ON oracle_loader_test5 TO $schema1");
274 $dbh2->do("GRANT ALL ON oracle_loader_test5_pk_seq TO $schema1");
275
c4a69b87 276 $dbh2->do(<<"EOF");
277 CREATE TABLE oracle_loader_test6 (
278 id INT NOT NULL PRIMARY KEY,
279 value VARCHAR(100),
280 oracle_loader_test4_id INT REFERENCES ${schema1}.oracle_loader_test4 (id)
281 )
282EOF
5975bbe6 283 $dbh2->do($_) for $auto_inc_cb->('oracle_loader_test6', 'id');
c4a69b87 284 $dbh2->do("GRANT ALL ON oracle_loader_test6 to $schema1");
5975bbe6 285 $dbh2->do("GRANT ALL ON oracle_loader_test6_id_seq TO $schema1");
286
c4a69b87 287 $dbh2->do(<<"EOF");
288 CREATE TABLE oracle_loader_test7 (
289 id INT NOT NULL PRIMARY KEY,
5975bbe6 290 value VARCHAR(100),
291 six_id INT UNIQUE REFERENCES ${schema2}.oracle_loader_test6 (id)
c4a69b87 292 )
293EOF
5975bbe6 294 $dbh2->do($_) for $auto_inc_cb->('oracle_loader_test7', 'id');
c4a69b87 295 $dbh2->do("GRANT ALL ON oracle_loader_test7 to $schema1");
5975bbe6 296 $dbh2->do("GRANT ALL ON oracle_loader_test7_id_seq TO $schema1");
297
c4a69b87 298 $dbh1->do(<<"EOF");
299 CREATE TABLE oracle_loader_test8 (
300 id INT NOT NULL PRIMARY KEY,
301 value VARCHAR(100),
302 oracle_loader_test7_id INT REFERENCES ${schema2}.oracle_loader_test7 (id)
303 )
304EOF
5975bbe6 305 $dbh1->do($_) for $auto_inc_cb->('oracle_loader_test8', 'id');
306 $dbh1->do("GRANT ALL ON oracle_loader_test8 to $schema2");
307 $dbh1->do("GRANT ALL ON oracle_loader_test8_id_seq TO $schema2");
308
309 # We add schema to moniker_parts, so make a monikers hash for
310 # the tests, of the form schemanum.tablenum
311 my $schema1_moniker = join '', map ucfirst lc, split_name to_identifier $schema1;
312 my $schema2_moniker = join '', map ucfirst lc, split_name to_identifier $schema2;
313
314 my %monikers;
5975bbe6 315 $monikers{'1.5'} = $schema1_moniker . 'OracleLoaderTest5';
316 $monikers{'2.5'} = $schema2_moniker . 'OracleLoaderTest5';
c4a69b87 317
318 foreach my $db_schema ([$schema1, $schema2], '%') {
319 lives_and {
320 rmtree EXTRA_DUMP_DIR;
321
322 my @warns;
323 local $SIG{__WARN__} = sub {
324 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
325 };
326
327 make_schema_at(
328 'OracleMultiSchema',
329 {
330 naming => 'current',
331 db_schema => $db_schema,
c4a69b87 332 dump_directory => EXTRA_DUMP_DIR,
333 quiet => 1,
334 },
335 [ $dsn, $user, $password ],
336 );
337
338 diag join "\n", @warns if @warns;
339
340 is @warns, 0;
341 } qq{dumped schema for "$schema1" and "$schema2" schemas with no warnings};
342
5975bbe6 343 my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
c4a69b87 344
345 lives_and {
346 ok $test_schema = OracleMultiSchema->connect($dsn, $user, $password);
347 } 'connected test schema';
348
349 lives_and {
4c2e2ce9 350 ok $rsrc = $test_schema->source('OracleLoaderTest4');
5975bbe6 351 } 'got source for table in schema1';
352
353 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
354 'column in schema1';
355
356 is try { $rsrc->column_info('value')->{data_type} }, 'varchar2',
357 'column in schema1';
358
359 is try { $rsrc->column_info('value')->{size} }, 100,
360 'column in schema1';
361
362 lives_and {
4c2e2ce9 363 ok $rs = $test_schema->resultset('OracleLoaderTest4');
5975bbe6 364 } 'got resultset for table in schema1';
365
366 lives_and {
367 ok $row = $rs->create({ value => 'foo' });
368 } 'executed SQL on table in schema1';
369
370 my $schema1_identifier = join '_', map lc, split_name to_identifier $schema1;
371
372 $rel_info = try { $rsrc->relationship_info(
373 $schema1_identifier . '_oracle_loader_test5'
374 ) };
375
376 is_deeply $rel_info->{cond}, {
377 'foreign.four_id' => 'self.id'
378 }, 'relationship in schema1';
379
380 is $rel_info->{attrs}{accessor}, 'single',
381 'relationship in schema1';
382
383 is $rel_info->{attrs}{join_type}, 'LEFT',
384 'relationship in schema1';
385
386 lives_and {
387 ok $rsrc = $test_schema->source($monikers{'1.5'});
388 } 'got source for table in schema1';
389
390 %uniqs = try { $rsrc->unique_constraints };
391
392 is keys %uniqs, 2,
393 'got unique and primary constraint in schema1';
394
395 delete $uniqs{primary};
396
397 is_deeply ((values %uniqs)[0], ['four_id'],
398 'correct unique constraint in schema1');
399
400 lives_and {
4c2e2ce9 401 ok $rsrc = $test_schema->source('OracleLoaderTest6');
5975bbe6 402 } 'got source for table in schema2';
403
404 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
405 'column in schema2 introspected correctly';
406
407 is try { $rsrc->column_info('value')->{data_type} }, 'varchar2',
408 'column in schema2 introspected correctly';
409
410 is try { $rsrc->column_info('value')->{size} }, 100,
411 'column in schema2 introspected correctly';
412
413 lives_and {
4c2e2ce9 414 ok $rs = $test_schema->resultset('OracleLoaderTest6');
5975bbe6 415 } 'got resultset for table in schema2';
416
417 lives_and {
418 ok $row = $rs->create({ value => 'foo' });
419 } 'executed SQL on table in schema2';
420
421 $rel_info = try { $rsrc->relationship_info('oracle_loader_test7') };
422
423 is_deeply $rel_info->{cond}, {
424 'foreign.six_id' => 'self.id'
425 }, 'relationship in schema2';
426
427 is $rel_info->{attrs}{accessor}, 'single',
428 'relationship in schema2';
429
430 is $rel_info->{attrs}{join_type}, 'LEFT',
431 'relationship in schema2';
432
433 lives_and {
4c2e2ce9 434 ok $rsrc = $test_schema->source('OracleLoaderTest7');
5975bbe6 435 } 'got source for table in schema2';
436
437 %uniqs = try { $rsrc->unique_constraints };
438
439 is keys %uniqs, 2,
440 'got unique and primary constraint in schema2';
441
442 delete $uniqs{primary};
443
444 is_deeply ((values %uniqs)[0], ['six_id'],
445 'correct unique constraint in schema2');
446
447 lives_and {
4c2e2ce9 448 ok $test_schema->source('OracleLoaderTest6')
c4a69b87 449 ->has_relationship('oracle_loader_test4');
450 } 'cross-schema relationship in multi-db_schema';
451
452 lives_and {
4c2e2ce9 453 ok $test_schema->source('OracleLoaderTest4')
c4a69b87 454 ->has_relationship('oracle_loader_test6s');
455 } 'cross-schema relationship in multi-db_schema';
456
457 lives_and {
4c2e2ce9 458 ok $test_schema->source('OracleLoaderTest8')
c4a69b87 459 ->has_relationship('oracle_loader_test7');
460 } 'cross-schema relationship in multi-db_schema';
461
462 lives_and {
4c2e2ce9 463 ok $test_schema->source('OracleLoaderTest7')
c4a69b87 464 ->has_relationship('oracle_loader_test8s');
465 } 'cross-schema relationship in multi-db_schema';
466 }
467 }
5cd600fa 468 },
469 },
e7262300 470);
471
472if( !$dsn || !$user ) {
473 $tester->skip_tests('You need to set the DBICTEST_ORA_DSN, _USER, and _PASS environment variables');
474}
475else {
476 $tester->run_tests();
477}
c4a69b87 478
479END {
480 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
481 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
482 my $dbh1 = $schema->storage->dbh;
483
484 try {
5975bbe6 485 $dbh1->do($_) for $auto_inc_drop_cb->('oracle_loader_test8', 'id');
486 $dbh2->do($_) for $auto_inc_drop_cb->('oracle_loader_test7', 'id');
487 $dbh2->do($_) for $auto_inc_drop_cb->('oracle_loader_test6', 'id');
488 $dbh2->do($_) for $auto_inc_drop_cb->('oracle_loader_test5', 'pk');
489 $dbh1->do($_) for $auto_inc_drop_cb->('oracle_loader_test5', 'id');
490 $dbh1->do($_) for $auto_inc_drop_cb->('oracle_loader_test4', 'id');
491 }
492 catch {
493 die "Error dropping sequences for cross-schema test tables: $_";
494 };
495
496 try {
c4a69b87 497 $dbh1->do('DROP TABLE oracle_loader_test8');
498 $dbh2->do('DROP TABLE oracle_loader_test7');
5975bbe6 499 $dbh2->do('DROP TABLE oracle_loader_test6');
500 $dbh2->do('DROP TABLE oracle_loader_test5');
501 $dbh1->do('DROP TABLE oracle_loader_test5');
502 $dbh1->do('DROP TABLE oracle_loader_test4');
c4a69b87 503 }
504 catch {
505 die "Error dropping cross-schema test tables: $_";
506 };
507 }
508
509 rmtree EXTRA_DUMP_DIR;
510 }
511}
760fd65c 512# vim:et sw=4 sts=4 tw=0: