Add retrieve_on_insert column_info flag, to autoretrieve RDBMS-side defaults
[dbsrgits/DBIx-Class.git] / t / 73oracle.t
CommitLineData
4bea1fe7 1use strict;
2use warnings;
3
4use Test::Exception;
5use Test::More;
6use Sub::Name;
7
8use lib qw(t/lib);
9use DBICTest;
10use DBIC::SqlMakerTest;
11
12plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_oracle')
13 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_oracle');
14
15$ENV{NLS_SORT} = "BINARY";
16$ENV{NLS_COMP} = "BINARY";
17$ENV{NLS_LANG} = "AMERICAN";
18
19my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
20
21# optional:
22my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_ORA_EXTRAUSER_${_}" } qw/DSN USER PASS/};
23
24plan skip_all => 'Set $ENV{DBICTEST_ORA_DSN}, _USER and _PASS to run this test.'
25 unless ($dsn && $user && $pass);
26
cb464582 27{
28 package # hide from PAUSE
29 DBICTest::Schema::ArtistFQN;
30
31 use base 'DBIx::Class::Core';
32
33 __PACKAGE__->table(
a6646e1b 34 $ENV{DBICTEST_ORA_USER}
07cda1c5 35 ? (uc $ENV{DBICTEST_ORA_USER}) . '.artist'
a6646e1b 36 : '??_no_user_??'
cb464582 37 );
38 __PACKAGE__->add_columns(
07cda1c5 39 'artistid' => {
40 data_type => 'integer',
41 is_auto_increment => 1,
42 },
43 'name' => {
44 data_type => 'varchar',
45 size => 100,
46 is_nullable => 1,
47 },
48 'autoinc_col' => {
49 data_type => 'integer',
50 is_auto_increment => 1,
51 },
8b9473f5 52 'default_value_col' => {
53 data_type => 'varchar',
54 size => 100,
55 is_nullable => 0,
56 retrieve_on_insert => 1,
57 }
cb464582 58 );
bf51641f 59 __PACKAGE__->set_primary_key(qw/ artistid autoinc_col /);
cb464582 60
61 1;
62}
63
cb464582 64DBICTest::Schema->load_classes('ArtistFQN');
0567538f 65
07cda1c5 66# This is in Core now, but it's here just to test that it doesn't break
67DBICTest::Schema::Artist->load_components('PK::Auto');
68# These are compat shims for PK::Auto...
69DBICTest::Schema::CD->load_components('PK::Auto::Oracle');
70DBICTest::Schema::Track->load_components('PK::Auto::Oracle');
ba12b23f 71
0567538f 72
bf51641f 73# check if we indeed do support stuff
f116ff4e 74my $v = do {
75 my $v = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_dbh_get_info(18);
bf51641f 76 $v =~ /^(\d+)\.(\d+)/
77 or die "Unparseable Oracle server version: $v\n";
78
f116ff4e 79 sprintf('%d.%03d', $1, $2);
80};
81
538878de 82my $test_server_supports_only_orajoins = $v < 9;
f116ff4e 83
bf51641f 84# TODO find out which version supports the RETURNING syntax
86b23415 85# 8i (8.1) has it and earlier docs are a 404 on oracle.com
f116ff4e 86my $test_server_supports_insert_returning = $v >= 8.001;
87
bf51641f 88is (
89 DBICTest::Schema->connect($dsn, $user, $pass)->storage->_use_insert_returning,
90 $test_server_supports_insert_returning,
91 'insert returning capability guessed correctly'
92);
93
f116ff4e 94##########
538878de 95# the recyclebin (new for 10g) sometimes comes in the way
96my $on_connect_sql = $v >= 10 ? ["ALTER SESSION SET recyclebin = OFF"] : [];
f116ff4e 97
98# iterate all tests on following options
99my @tryopt = (
100 { on_connect_do => $on_connect_sql },
101 { quote_char => '"', on_connect_do => $on_connect_sql },
102);
103
104# keep a database handle open for cleanup
105my ($dbh, $dbh2);
106
bf51641f 107my $schema;
f116ff4e 108for my $use_insert_returning ($test_server_supports_insert_returning ? (1,0) : (0) ) {
109 for my $force_ora_joins ($test_server_supports_only_orajoins ? (0) : (0,1) ) {
110
111 no warnings qw/once/;
112 local *DBICTest::Schema::connection = subname 'DBICTest::Schema::connection' => sub {
113 my $s = shift->next::method (@_);
114 $s->storage->_use_insert_returning ($use_insert_returning);
115 $s->storage->sql_maker_class('DBIx::Class::SQLMaker::OracleJoins') if $force_ora_joins;
116 $s;
117 };
118
119 for my $opt (@tryopt) {
120 # clean all cached sequences from previous run
121 for (map { values %{DBICTest::Schema->source($_)->columns_info} } (qw/Artist CD Track/) ) {
122 delete $_->{sequence};
123 }
bf51641f 124
f116ff4e 125 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
07cda1c5 126
f116ff4e 127 $dbh = $schema->storage->dbh;
128 my $q = $schema->storage->sql_maker->quote_char || '';
bf51641f 129
f116ff4e 130 do_creates($dbh, $q);
bf51641f 131
f116ff4e 132 _run_tests($schema, $opt);
133 }
bf51641f 134 }
135}
07cda1c5 136
bf51641f 137sub _run_tests {
138 my ($schema, $opt) = @_;
07cda1c5 139
bf51641f 140 my $q = $schema->storage->sql_maker->quote_char || '';
ab4f4e4c 141
142# test primary key handling with multiple triggers
07cda1c5 143 my ($new, $seq);
0567538f 144
bf51641f 145 my $new_artist = $schema->resultset('Artist')->create({ name => 'foo' });
146 my $new_cd = $schema->resultset('CD')->create({ artist => 1, title => 'EP C', year => '2003' });
6f5f880d 147
bf51641f 148 SKIP: {
149 skip 'not detecting sequences when using INSERT ... RETURNING', 4
150 if $schema->storage->_use_insert_returning;
151
152 is($new_artist->artistid, 1, "Oracle Auto-PK worked for standard sqlt-like trigger");
153 $seq = $new_artist->result_source->column_info('artistid')->{sequence};
154 $seq = $$seq if ref $seq;
155 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
156
157 is($new_cd->cdid, 1, 'Oracle Auto-PK worked - using scalar ref as table name/custom weird trigger');
158 $seq = $new_cd->result_source->column_info('cdid')->{sequence};
159 $seq = $$seq if ref $seq;
160 like ($seq, qr/\.${q}cd_seq${q}$/, 'Correct PK sequence selected for custom trigger');
161 }
e6dd7b42 162
07cda1c5 163# test PKs again with fully-qualified table name
164 my $artistfqn_rs = $schema->resultset('ArtistFQN');
165 my $artist_rsrc = $artistfqn_rs->result_source;
ab4f4e4c 166
07cda1c5 167 delete $artist_rsrc->column_info('artistid')->{sequence};
168 $new = $artistfqn_rs->create( { name => 'bar' } );
ab4f4e4c 169
bf51641f 170 is_deeply( {map { $_ => $new->$_ } $artist_rsrc->primary_columns},
171 { artistid => 2, autoinc_col => 2},
172 "Oracle Multi-Auto-PK worked with fully-qualified tablename" );
cb464582 173
ab4f4e4c 174
07cda1c5 175 delete $artist_rsrc->column_info('artistid')->{sequence};
176 $new = $artistfqn_rs->create( { name => 'bar', autoinc_col => 1000 } );
177
178 is( $new->artistid, 3, "Oracle Auto-PK worked with fully-qualified tablename" );
179 is( $new->autoinc_col, 1000, "Oracle Auto-Inc overruled with fully-qualified tablename");
bf51641f 180
8b9473f5 181
182 is( $new->default_value_col, 'default_value', $schema->storage->_use_insert_returning
183 ? 'Check retrieve_on_insert on default_value_col with INSERT ... RETURNING'
184 : 'Check retrieve_on_insert on default_value_col without INSERT ... RETURNING'
185 );
186
bf51641f 187 SKIP: {
188 skip 'not detecting sequences when using INSERT ... RETURNING', 1
189 if $schema->storage->_use_insert_returning;
190
191 $seq = $new->result_source->column_info('artistid')->{sequence};
192 $seq = $$seq if ref $seq;
193 like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger');
194 }
ab4f4e4c 195
ab4f4e4c 196
197# test LIMIT support
07cda1c5 198 for (1..6) {
ab4f4e4c 199 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
07cda1c5 200 }
201 my $it = $schema->resultset('Artist')->search( { name => { -like => 'Artist %' } }, {
202 rows => 3,
203 offset => 4,
204 order_by => 'artistid'
205 });
206
207 is( $it->count, 2, "LIMIT count past end of RS ok" );
208 is( $it->next->name, "Artist 5", "iterator->next ok" );
209 is( $it->next->name, "Artist 6", "iterator->next ok" );
210 is( $it->next, undef, "next past end of resultset ok" );
211
07cda1c5 212# test identifiers over the 30 char limit
213 lives_ok {
214 my @results = $schema->resultset('CD')->search(undef, {
215 prefetch => 'very_long_artist_relationship',
216 rows => 3,
217 offset => 0,
218 })->all;
219 ok( scalar @results > 0, 'limit with long identifiers returned something');
220 } 'limit with long identifiers executed successfully';
ab4f4e4c 221
ab4f4e4c 222
62d4dbae 223# test rel names over the 30 char limit
ab4f4e4c 224 my $query = $schema->resultset('Artist')->search({
225 artistid => 1
6c0230de 226 }, {
227 prefetch => 'cds_very_very_very_long_relationship_name'
228 });
229
230 lives_and {
07cda1c5 231 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
ab4f4e4c 232 } 'query with rel name over 30 chars survived and worked';
233
19c4cc62 234# test rel names over the 30 char limit using group_by and join
235 {
236 my @group_cols = ( 'me.name' );
237 my $query = $schema->resultset('Artist')->search({
238 artistid => 1
239 }, {
240 select => \@group_cols,
241 as => [map { /^\w+\.(\w+)$/ } @group_cols],
242 join => [qw( cds_very_very_very_long_relationship_name )],
243 group_by => \@group_cols,
244 });
245
246 lives_and {
247 my @got = $query->get_column('name')->all();
248 is_deeply \@got, [$new_artist->name];
249 } 'query with rel name over 30 chars worked on join, group_by for me col';
250
251 lives_and {
252 is $query->count(), 1
253 } 'query with rel name over 30 chars worked on join, group_by, count for me col';
254 }
255 {
256 my @group_cols = ( 'cds_very_very_very_long_relationship_name.title' );
257 my $query = $schema->resultset('Artist')->search({
258 artistid => 1
259 }, {
260 select => \@group_cols,
261 as => [map { /^\w+\.(\w+)$/ } @group_cols],
262 join => [qw( cds_very_very_very_long_relationship_name )],
263 group_by => \@group_cols,
264 });
265
266 lives_and {
267 my @got = $query->get_column('title')->all();
268 is_deeply \@got, [$new_cd->title];
269 } 'query with rel name over 30 chars worked on join, group_by for long rel col';
270
271 lives_and {
272 is $query->count(), 1
273 } 'query with rel name over 30 chars worked on join, group_by, count for long rel col';
274 }
275
ab4f4e4c 276 # rel name over 30 char limit with user condition
277 # This requires walking the SQLA data structure.
278 {
ab4f4e4c 279 $query = $schema->resultset('Artist')->search({
280 'cds_very_very_very_long_relationship_name.title' => 'EP C'
281 }, {
282 prefetch => 'cds_very_very_very_long_relationship_name'
283 });
284
285 lives_and {
286 is $query->first->cds_very_very_very_long_relationship_name->first->cdid, 1
287 } 'query with rel name over 30 chars and user condition survived and worked';
288 }
07cda1c5 289
6c0230de 290
9900b569 291# test join with row count ambiguity
07cda1c5 292 my $cd = $schema->resultset('CD')->next;
293 my $track = $cd->create_related('tracks', { position => 1, title => 'Track1'} );
294 my $tjoin = $schema->resultset('Track')->search({ 'me.title' => 'Track1'}, {
295 join => 'cd', rows => 2
296 });
d2a3958e 297
07cda1c5 298 ok(my $row = $tjoin->next);
299
300 is($row->title, 'Track1', "ambiguous column ok");
2660b14e 301
d2a3958e 302
2660b14e 303
286f32b3 304# check count distinct with multiple columns
07cda1c5 305 my $other_track = $schema->resultset('Track')->create({ cd => $cd->cdid, position => 1, title => 'Track2' });
11d68671 306
07cda1c5 307 my $tcount = $schema->resultset('Track')->search(
308 {},
309 {
310 select => [ qw/position title/ ],
311 distinct => 1,
312 }
313 );
314 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
11d68671 315
07cda1c5 316 $tcount = $schema->resultset('Track')->search(
317 {},
318 {
319 columns => [ qw/position title/ ],
320 distinct => 1,
321 }
322 );
323 is($tcount->count, 2, 'multiple column COUNT DISTINCT ok');
286f32b3 324
07cda1c5 325 $tcount = $schema->resultset('Track')->search(
326 {},
327 {
328 group_by => [ qw/position title/ ]
329 }
330 );
331 is($tcount->count, 2, 'multiple column COUNT DISTINCT using column syntax ok');
2660b14e 332
e8e971f2 333
07cda1c5 334# check group_by
335 my $g_rs = $schema->resultset('Track')->search( undef, { columns=>[qw/trackid position/], group_by=> [ qw/trackid position/ ] , rows => 2, offset => 1 });
336 is( scalar $g_rs->all, 1, "Group by with limit OK" );
337
bd691933 338
b7b18f32 339# test with_deferred_fk_checks
07cda1c5 340 lives_ok {
341 $schema->storage->with_deferred_fk_checks(sub {
342 $schema->resultset('Track')->create({
343 trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
344 });
345 $schema->resultset('CD')->create({
346 artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
347 });
b7b18f32 348 });
07cda1c5 349 } 'with_deferred_fk_checks code survived';
b7b18f32 350
07cda1c5 351 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
8b9473f5 352 'code in with_deferred_fk_checks worked';
07cda1c5 353
354 throws_ok {
355 $schema->resultset('Track')->create({
356 trackid => 1, cd => 9999, position => 1, title => 'Track1'
357 });
358 } qr/constraint/i, 'with_deferred_fk_checks is off';
b7b18f32 359
b7b18f32 360
ccd6f984 361# test auto increment using sequences WITHOUT triggers
07cda1c5 362 for (1..5) {
39b8d119 363 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
364 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
365 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
366 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
07cda1c5 367 }
368 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
369 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
370
ccd6f984 371
12e05c15 372# test BLOBs
07cda1c5 373 SKIP: {
374 TODO: {
375 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
376 $binstr{'large'} = $binstr{'small'} x 1024;
8068691e 377
07cda1c5 378 my $maxloblen = length $binstr{'large'};
379 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
380 local $dbh->{'LongReadLen'} = $maxloblen;
5db2758d 381
07cda1c5 382 my $rs = $schema->resultset('BindType');
383 my $id = 0;
5db2758d 384
07cda1c5 385 if ($DBD::Oracle::VERSION eq '1.23') {
386 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
387 qr/broken/,
388 'throws on blob insert with DBD::Oracle == 1.23';
5db2758d 389
07cda1c5 390 skip 'buggy BLOB support in DBD::Oracle 1.23', 7;
391 }
5db2758d 392
07cda1c5 393 # disable BLOB mega-output
394 my $orig_debug = $schema->storage->debug;
395 $schema->storage->debug (0);
f3f6c13a 396
07cda1c5 397 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
86b23415 398 . ': https://rt.cpan.org/Ticket/Display.html?id=64206'
07cda1c5 399 if $q;
5db2758d 400
07cda1c5 401 foreach my $type (qw( blob clob )) {
402 foreach my $size (qw( small large )) {
403 $id++;
d7f20fdf 404
07cda1c5 405 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
406 "inserted $size $type without dying";
407 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
408 }
d7f20fdf 409 }
f3f6c13a 410
07cda1c5 411 $schema->storage->debug ($orig_debug);
412 }}
413
a5a27e7a 414# test populate (identity, success and error handling)
415 my $art_rs = $schema->resultset('Artist');
416
417 my $seq_pos = $art_rs->get_column('artistid')->max;
418 ok($seq_pos, 'Starting with something in the artist table');
419
420
421 my $pop_rs = $schema->resultset('Artist')->search(
422 { name => { -like => 'pop_art_%' } },
423 { order_by => 'artistid' }
424 );
425
426 $art_rs->delete;
427 lives_ok {
428 $pop_rs->populate([
429 map { +{ name => "pop_art_$_" } }
430 (1,2,3)
431 ]);
432
433 is_deeply (
434 [ $pop_rs->get_column('artistid')->all ],
435 [ map { $seq_pos + $_ } (1,2,3) ],
436 'Sequence works after empty-table insertion'
437 );
438 } 'Populate without identity does not throw';
439
440 lives_ok {
441 $pop_rs->populate([
442 map { +{ artistid => $_, name => "pop_art_$_" } }
443 (1,2,3)
444 ]);
445
446 is_deeply (
447 [ $pop_rs->get_column('artistid')->all ],
448 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
449 'Explicit id population works'
450 );
451 } 'Populate with identity does not throw';
452
453 throws_ok {
454 $pop_rs->populate([
455 map { +{ artistid => $_, name => "pop_art_$_" } }
456 (200, 1, 300)
457 ]);
458 } qr/unique constraint.+populate slice.+name => "pop_art_1"/s, 'Partially failed populate throws';
459
460 is_deeply (
461 [ $pop_rs->get_column('artistid')->all ],
462 [ 1,2,3, map { $seq_pos + $_ } (1,2,3) ],
463 'Partially failed populate did not alter table contents'
464 );
5db2758d 465
f116ff4e 466# test complex join (exercise orajoins)
467 lives_ok {
468 my @hri = $schema->resultset('CD')->search(
469 { 'artist.name' => 'pop_art_1', 'me.cdid' => { '!=', 999} },
470 { join => 'artist', prefetch => 'tracks', rows => 4, order_by => 'tracks.trackid' }
471 )->hri_dump->all;
472
473 my $expect = [{
474 artist => 1,
475 cdid => 1,
476 genreid => undef,
477 single_track => undef,
478 title => "EP C",
479 tracks => [
480 {
481 cd => 1,
482 last_updated_at => undef,
483 last_updated_on => undef,
484 position => 1,
485 title => "Track1",
486 trackid => 1
487 },
488 {
489 cd => 1,
490 last_updated_at => undef,
491 last_updated_on => undef,
492 position => 1,
493 title => "Track2",
494 trackid => 2
495 },
496 ],
497 year => 2003
498 }];
499
500 is_deeply (
501 \@hri,
502 $expect,
503 'Correct set of data prefetched',
504 );
505
506 } 'complex prefetch ok';
507
df6e3f5c 508# test sequence detection from a different schema
07cda1c5 509 SKIP: {
510 TODO: {
511 skip ((join '',
512 'Set DBICTEST_ORA_EXTRAUSER_DSN, _USER and _PASS to a *DIFFERENT* Oracle user',
bf51641f 513 ' to run the cross-schema sequence detection test.'),
07cda1c5 514 1) unless $dsn2 && $user2 && $user2 ne $user;
9d7d2f00 515
bf51641f 516 skip 'not detecting cross-schema sequence name when using INSERT ... RETURNING', 1
517 if $schema->storage->_use_insert_returning;
518
8b9473f5 519 # Oracle8i Reference Release 2 (8.1.6)
07cda1c5 520 # http://download.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch294.htm#993
521 # Oracle Database Reference 10g Release 2 (10.2)
522 # http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#sthref1297
523 local $TODO = "On Oracle8i all_triggers view is empty, i don't yet know why..."
524 if $schema->storage->_server_info->{normalized_dbms_version} < 9;
606b30c3 525
bf51641f 526 my $schema2 = $schema->connect($dsn2, $user2, $pass2, $opt);
a6646e1b 527 my $dbh2 = $schema2->storage->dbh;
df6e3f5c 528
a6646e1b 529 # create identically named tables/sequences in the other schema
530 do_creates($dbh2, $q);
df6e3f5c 531
a6646e1b 532 # grand select privileges to the 2nd user
533 $dbh->do("GRANT INSERT ON ${q}artist${q} TO " . uc $user2);
8b9473f5 534 $dbh->do("GRANT SELECT ON ${q}artist${q} TO " . uc $user2);
a6646e1b 535 $dbh->do("GRANT SELECT ON ${q}artist_pk_seq${q} TO " . uc $user2);
536 $dbh->do("GRANT SELECT ON ${q}artist_autoinc_seq${q} TO " . uc $user2);
df6e3f5c 537
a6646e1b 538 # test with a fully qualified table (user1/schema prepended)
539 my $rs2 = $schema2->resultset('ArtistFQN');
540 delete $rs2->result_source->column_info('artistid')->{sequence};
df6e3f5c 541
07cda1c5 542 lives_and {
a6646e1b 543 my $row = $rs2->create({ name => 'From Different Schema' });
07cda1c5 544 ok $row->artistid;
545 } 'used autoinc sequence across schemas';
546
547 # now quote the sequence name (do_creates always uses an lc name)
548 my $q_seq = $q
549 ? '"artist_pk_seq"'
550 : '"ARTIST_PK_SEQ"'
551 ;
a6646e1b 552 delete $rs2->result_source->column_info('artistid')->{sequence};
553 $dbh->do(qq{
07cda1c5 554 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
555 BEFORE INSERT ON ${q}artist${q}
556 FOR EACH ROW
557 BEGIN
558 IF :new.${q}artistid${q} IS NULL THEN
559 SELECT $q_seq.nextval
560 INTO :new.${q}artistid${q}
561 FROM DUAL;
562 END IF;
563 END;
564 });
72044892 565
72044892 566
07cda1c5 567 lives_and {
a6646e1b 568 my $row = $rs2->create({ name => 'From Different Schema With Quoted Sequence' });
07cda1c5 569 ok $row->artistid;
570 } 'used quoted autoinc sequence across schemas';
72044892 571
a6646e1b 572 is_deeply $rs2->result_source->column_info('artistid')->{sequence},
573 \( (uc $user) . ".$q_seq"),
07cda1c5 574 'quoted sequence name correctly extracted';
a6646e1b 575
576 # try an insert operation on the default user2 artist
577 my $art1 = $schema->resultset('Artist');
578 my $art2 = $schema2->resultset('Artist');
579 my $art1_count = $art1->count || 0;
580 my $art2_count = $art2->count;
581
582 is( $art2_count, 0, 'No artists created yet in second schema' );
583
584 delete $art2->result_source->column_info('artistid')->{sequence};
585 my $new_art = $art2->create({ name => '2nd best' });
586
587 is ($art1->count, $art1_count, 'No new rows in main schema');
588 is ($art2->count, 1, 'One artist create in 2nd schema');
589
590 is( $new_art->artistid, 1, 'Expected first PK' );
591
592 do_clean ($dbh2);
07cda1c5 593 }}
594
595 do_clean ($dbh);
596}
df6e3f5c 597
86cc4156 598done_testing;
599
df6e3f5c 600sub do_creates {
07cda1c5 601 my ($dbh, $q) = @_;
602
603 do_clean($dbh);
604
605 $dbh->do("CREATE SEQUENCE ${q}artist_autoinc_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
606 $dbh->do("CREATE SEQUENCE ${q}artist_pk_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
607 $dbh->do("CREATE SEQUENCE ${q}cd_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
608 $dbh->do("CREATE SEQUENCE ${q}track_seq${q} START WITH 1 MAXVALUE 999999 MINVALUE 0");
609
610 $dbh->do("CREATE SEQUENCE ${q}nonpkid_seq${q} START WITH 20 MAXVALUE 999999 MINVALUE 0");
611 # this one is always quoted as per manually specified sequence =>
612 $dbh->do('CREATE SEQUENCE "pkid1_seq" START WITH 1 MAXVALUE 999999 MINVALUE 0');
613 # this one is always unquoted as per manually specified sequence =>
df6e3f5c 614 $dbh->do("CREATE SEQUENCE pkid2_seq START WITH 10 MAXVALUE 999999 MINVALUE 0");
df6e3f5c 615
8b9473f5 616 $dbh->do("CREATE TABLE ${q}artist${q} (${q}artistid${q} NUMBER(12), ${q}name${q} VARCHAR(255),${q}default_value_col${q} VARCHAR(255) DEFAULT 'default_value', ${q}autoinc_col${q} NUMBER(12), ${q}rank${q} NUMBER(38), ${q}charfield${q} VARCHAR2(10))");
07cda1c5 617 $dbh->do("ALTER TABLE ${q}artist${q} ADD (CONSTRAINT ${q}artist_pk${q} PRIMARY KEY (${q}artistid${q}))");
df6e3f5c 618
07cda1c5 619 $dbh->do("CREATE TABLE ${q}sequence_test${q} (${q}pkid1${q} NUMBER(12), ${q}pkid2${q} NUMBER(12), ${q}nonpkid${q} NUMBER(12), ${q}name${q} VARCHAR(255))");
620 $dbh->do("ALTER TABLE ${q}sequence_test${q} ADD (CONSTRAINT ${q}sequence_test_constraint${q} PRIMARY KEY (${q}pkid1${q}, ${q}pkid2${q}))");
df6e3f5c 621
07cda1c5 622 # table cd will be unquoted => Oracle will see it as uppercase
623 $dbh->do("CREATE TABLE cd (${q}cdid${q} NUMBER(12), ${q}artist${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}year${q} VARCHAR(4), ${q}genreid${q} NUMBER(12), ${q}single_track${q} NUMBER(12))");
624 $dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
df6e3f5c 625
3d98c75e 626 $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE)");
07cda1c5 627 $dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
df6e3f5c 628
f3a9ea3d 629 $dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}clob${q} clob NULL, ${q}a_memo${q} integer NULL)");
12e05c15 630
df6e3f5c 631 $dbh->do(qq{
07cda1c5 632 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_auto${q}
633 BEFORE INSERT ON ${q}artist${q}
ab4f4e4c 634 FOR EACH ROW
635 BEGIN
07cda1c5 636 IF :new.${q}autoinc_col${q} IS NULL THEN
637 SELECT ${q}artist_autoinc_seq${q}.nextval
638 INTO :new.${q}autoinc_col${q}
ab4f4e4c 639 FROM DUAL;
640 END IF;
641 END;
642 });
07cda1c5 643
ab4f4e4c 644 $dbh->do(qq{
07cda1c5 645 CREATE OR REPLACE TRIGGER ${q}artist_insert_trg_pk${q}
646 BEFORE INSERT ON ${q}artist${q}
df6e3f5c 647 FOR EACH ROW
648 BEGIN
07cda1c5 649 IF :new.${q}artistid${q} IS NULL THEN
650 SELECT ${q}artist_pk_seq${q}.nextval
651 INTO :new.${q}artistid${q}
df6e3f5c 652 FROM DUAL;
653 END IF;
654 END;
655 });
07cda1c5 656
df6e3f5c 657 $dbh->do(qq{
07cda1c5 658 CREATE OR REPLACE TRIGGER ${q}cd_insert_trg${q}
df6e3f5c 659 BEFORE INSERT OR UPDATE ON cd
660 FOR EACH ROW
07cda1c5 661
6f5f880d 662 DECLARE
663 tmpVar NUMBER;
664
df6e3f5c 665 BEGIN
6f5f880d 666 tmpVar := 0;
667
07cda1c5 668 IF :new.${q}cdid${q} IS NULL THEN
669 SELECT ${q}cd_seq${q}.nextval
6f5f880d 670 INTO tmpVar
671 FROM dual;
672
07cda1c5 673 :new.${q}cdid${q} := tmpVar;
df6e3f5c 674 END IF;
675 END;
676 });
07cda1c5 677
df6e3f5c 678 $dbh->do(qq{
07cda1c5 679 CREATE OR REPLACE TRIGGER ${q}track_insert_trg${q}
680 BEFORE INSERT ON ${q}track${q}
df6e3f5c 681 FOR EACH ROW
682 BEGIN
07cda1c5 683 IF :new.${q}trackid${q} IS NULL THEN
684 SELECT ${q}track_seq${q}.nextval
685 INTO :new.${q}trackid${q}
df6e3f5c 686 FROM DUAL;
687 END IF;
688 END;
689 });
690}
691
0567538f 692# clean up our mess
07cda1c5 693sub do_clean {
694
695 my $dbh = shift || return;
696
697 for my $q ('', '"') {
698 my @clean = (
699 "DROP TRIGGER ${q}track_insert_trg${q}",
700 "DROP TRIGGER ${q}cd_insert_trg${q}",
701 "DROP TRIGGER ${q}artist_insert_trg_auto${q}",
702 "DROP TRIGGER ${q}artist_insert_trg_pk${q}",
703 "DROP SEQUENCE ${q}nonpkid_seq${q}",
704 "DROP SEQUENCE ${q}pkid2_seq${q}",
705 "DROP SEQUENCE ${q}pkid1_seq${q}",
706 "DROP SEQUENCE ${q}track_seq${q}",
707 "DROP SEQUENCE ${q}cd_seq${q}",
708 "DROP SEQUENCE ${q}artist_autoinc_seq${q}",
709 "DROP SEQUENCE ${q}artist_pk_seq${q}",
710 "DROP TABLE ${q}bindtype_test${q}",
711 "DROP TABLE ${q}sequence_test${q}",
712 "DROP TABLE ${q}track${q}",
713 "DROP TABLE ${q}cd${q}",
714 "DROP TABLE ${q}artist${q}",
715 );
716 eval { $dbh -> do ($_) } for @clean;
717 }
718}
719
3ff5b740 720END {
a6646e1b 721 for ($dbh, $dbh2) {
722 next unless $_;
07cda1c5 723 local $SIG{__WARN__} = sub {};
a6646e1b 724 do_clean($_);
725 $_->disconnect;
df6e3f5c 726 }
3ff5b740 727}