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