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