Use sigwarn_silencer() everywhere appropriate
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_07mssql_common.t
CommitLineData
046e344c 1use strict;
05d322c8 2use warnings;
3a89a69f 3use Test::More;
4use Test::Exception;
ff4b0152 5use DBIx::Class::Schema::Loader::Utils qw/warnings_exist_silent sigwarn_silencer/;
c4a69b87 6use Try::Tiny;
7use File::Path 'rmtree';
8use DBIx::Class::Schema::Loader 'make_schema_at';
1ad8e8c3 9use namespace::clean;
5975bbe6 10use Scope::Guard ();
05d322c8 11
12# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
13BEGIN {
14 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
15 unshift @INC, $_ for split /:/, $lib_dirs;
16 }
17}
18
c4a69b87 19use lib qw(t/lib);
20
21use dbixcsl_common_tests ();
22use dbixcsl_test_dir '$tdir';
23
24use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump";
25
5975bbe6 26# for extra tests cleanup
27my $schema;
c4a69b87 28
8dcf4292 29my ($dsns, $common_version);
c4a69b87 30
afcd3c32 31for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
8dcf4292 32 next unless $ENV{"DBICTEST_${_}_DSN"};
33
34 $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
35 $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
36 $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
37
38 require DBI;
39 my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
40 my $srv_ver = eval {
41 $dbh->get_info(18)
42 ||
43 $dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion')->{Character_Value}
44 } || 0;
45
46 my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/;
47
48 if (! defined $common_version or $common_version > $maj_srv_ver ) {
49 $common_version = $maj_srv_ver;
50 }
51}
52
53plan skip_all => 'You need to set the DBICTEST_MSSQL_DSN, _USER and _PASS and/or the DBICTEST_MSSQL_ODBC_DSN, _USER and _PASS environment variables'
54 unless $dsns;
55
8dcf4292 56my $mssql_2008_new_data_types = {
57 date => { data_type => 'date' },
58 time => { data_type => 'time' },
59 'time(0)'=> { data_type => 'time', size => 0 },
60 'time(1)'=> { data_type => 'time', size => 1 },
61 'time(2)'=> { data_type => 'time', size => 2 },
62 'time(3)'=> { data_type => 'time', size => 3 },
63 'time(4)'=> { data_type => 'time', size => 4 },
64 'time(5)'=> { data_type => 'time', size => 5 },
65 'time(6)'=> { data_type => 'time', size => 6 },
66 'time(7)'=> { data_type => 'time' },
67 datetimeoffset => { data_type => 'datetimeoffset' },
68 'datetimeoffset(0)' => { data_type => 'datetimeoffset', size => 0 },
69 'datetimeoffset(1)' => { data_type => 'datetimeoffset', size => 1 },
70 'datetimeoffset(2)' => { data_type => 'datetimeoffset', size => 2 },
71 'datetimeoffset(3)' => { data_type => 'datetimeoffset', size => 3 },
72 'datetimeoffset(4)' => { data_type => 'datetimeoffset', size => 4 },
73 'datetimeoffset(5)' => { data_type => 'datetimeoffset', size => 5 },
74 'datetimeoffset(6)' => { data_type => 'datetimeoffset', size => 6 },
75 'datetimeoffset(7)' => { data_type => 'datetimeoffset' },
76 datetime2 => { data_type => 'datetime2' },
77 'datetime2(0)' => { data_type => 'datetime2', size => 0 },
78 'datetime2(1)' => { data_type => 'datetime2', size => 1 },
79 'datetime2(2)' => { data_type => 'datetime2', size => 2 },
80 'datetime2(3)' => { data_type => 'datetime2', size => 3 },
81 'datetime2(4)' => { data_type => 'datetime2', size => 4 },
82 'datetime2(5)' => { data_type => 'datetime2', size => 5 },
83 'datetime2(6)' => { data_type => 'datetime2', size => 6 },
84 'datetime2(7)' => { data_type => 'datetime2' },
3a89a69f 85
8dcf4292 86 hierarchyid => { data_type => 'hierarchyid' },
87};
046e344c 88
89my $tester = dbixcsl_common_tests->new(
b1e43108 90 vendor => 'mssql',
046e344c 91 auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
41968729 92 default_function_def => 'DATETIME DEFAULT getdate()',
8dcf4292 93 connect_info => [values %$dsns],
b1d11550 94 preserve_case_mode_is_exclusive => 1,
95 quote_char => [ qw/[ ]/ ],
8dcf4292 96 basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME',
f8640ecc 97 default_on_clause => 'NO ACTION',
deedd576 98 data_types => {
81ade4d9 99 # http://msdn.microsoft.com/en-us/library/ms187752.aspx
100
101 # numeric types
102 'int identity' => { data_type => 'integer', is_auto_increment => 1 },
103 bigint => { data_type => 'bigint' },
104 int => { data_type => 'integer' },
105 integer => { data_type => 'integer' },
106 smallint => { data_type => 'smallint' },
107 tinyint => { data_type => 'tinyint' },
108 money => { data_type => 'money' },
109 smallmoney => { data_type => 'smallmoney' },
110 bit => { data_type => 'bit' },
8c41c3ce 111 real => { data_type => 'real' },
112 'float(14)' => { data_type => 'real' },
113 'float(24)' => { data_type => 'real' },
81ade4d9 114 'float(25)' => { data_type => 'double precision' },
115 'float(53)' => { data_type => 'double precision' },
116 float => { data_type => 'double precision' },
117 'double precision'
118 => { data_type => 'double precision' },
afcd3c32 119 'numeric(6)' => { data_type => 'numeric', size => [6,0] },
81ade4d9 120 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
afcd3c32 121 'decimal(6)' => { data_type => 'decimal', size => [6,0] },
81ade4d9 122 'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
123 'dec(6,3)' => { data_type => 'decimal', size => [6,3] },
124 numeric => { data_type => 'numeric' },
125 decimal => { data_type => 'decimal' },
126 dec => { data_type => 'decimal' },
127
128 # datetime types
81ade4d9 129 datetime => { data_type => 'datetime' },
6e566cc4 130 # test rewriting getdate() to current_timestamp
131 'datetime default getdate()'
701cd3e3 132 => { data_type => 'datetime', default_value => \'current_timestamp',
133 original => { default_value => \'getdate()' } },
81ade4d9 134 smalldatetime => { data_type => 'smalldatetime' },
8dcf4292 135
136 ($common_version >= 10) ? %$mssql_2008_new_data_types : (),
81ade4d9 137
138 # string types
139 char => { data_type => 'char', size => 1 },
140 'char(2)' => { data_type => 'char', size => 2 },
c7e6dc1f 141 character => { data_type => 'char', size => 1 },
142 'character(2)' => { data_type => 'char', size => 2 },
81ade4d9 143 'varchar(2)' => { data_type => 'varchar', size => 2 },
b48f3f80 144
81ade4d9 145 nchar => { data_type => 'nchar', size => 1 },
146 'nchar(2)' => { data_type => 'nchar', size => 2 },
147 'nvarchar(2)' => { data_type => 'nvarchar', size => 2 },
148
149 # binary types
8c41c3ce 150 'binary' => { data_type => 'binary', size => 1 },
151 'binary(2)' => { data_type => 'binary', size => 2 },
152 'varbinary(2)' => { data_type => 'varbinary', size => 2 },
81ade4d9 153
154 # blob types
155 'varchar(max)' => { data_type => 'text' },
156 text => { data_type => 'text' },
b48f3f80 157
81ade4d9 158 'nvarchar(max)' => { data_type => 'ntext' },
159 ntext => { data_type => 'ntext' },
b48f3f80 160
81ade4d9 161 'varbinary(max)' => { data_type => 'image' },
162 image => { data_type => 'image' },
163
164 # other types
165 timestamp => { data_type => 'timestamp', inflate_datetime => 0 },
16773d6d 166 rowversion => { data_type => 'rowversion' },
825c42a3 167 uniqueidentifier => { data_type => 'uniqueidentifier' },
81ade4d9 168 sql_variant => { data_type => 'sql_variant' },
169 xml => { data_type => 'xml' },
deedd576 170 },
3a89a69f 171 extra => {
172 create => [
173 q{
174 CREATE TABLE [mssql_loader_test1.dot] (
175 id INT IDENTITY NOT NULL PRIMARY KEY,
176 dat VARCHAR(8)
177 )
178 },
179 q{
180 CREATE TABLE mssql_loader_test3 (
181 id INT IDENTITY NOT NULL PRIMARY KEY
182 )
183 },
184 q{
185 CREATE VIEW mssql_loader_test4 AS
186 SELECT * FROM mssql_loader_test3
187 },
020f3c3a 188 # test capitalization of cols in unique constraints and rels
ccce0e82 189 q{ SET QUOTED_IDENTIFIER ON },
190 q{ SET ANSI_NULLS ON },
deedd576 191 q{
020f3c3a 192 CREATE TABLE [MSSQL_Loader_Test5] (
193 [Id] INT IDENTITY NOT NULL PRIMARY KEY,
ccce0e82 194 [FooCol] INT NOT NULL,
195 [BarCol] INT NOT NULL,
196 UNIQUE ([FooCol], [BarCol])
deedd576 197 )
198 },
020f3c3a 199 q{
200 CREATE TABLE [MSSQL_Loader_Test6] (
201 [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
202 )
203 },
f8640ecc 204 # 8 through 12 are used for the multi-schema tests and 13 through 16 are used for multi-db tests
205 q{
206 create table mssql_loader_test17 (
207 id int identity primary key
208 )
209 },
210 q{
211 create table mssql_loader_test18 (
212 id int identity primary key,
213 seventeen_id int,
214 foreign key (seventeen_id) references mssql_loader_test17(id)
215 on delete set default on update set null
216 )
217 },
3a89a69f 218 ],
219 pre_drop_ddl => [
220 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
221 'DROP VIEW mssql_loader_test4',
222 ],
223 drop => [
224 '[mssql_loader_test1.dot]',
deedd576 225 'mssql_loader_test3',
060f5ecd 226 'MSSQL_Loader_Test6',
227 'MSSQL_Loader_Test5',
f8640ecc 228 'mssql_loader_test17',
229 'mssql_loader_test18',
3a89a69f 230 ],
f8640ecc 231 count => 14 + 30 * 2 + 26 * 2, # extra + multi-schema + mutli-db
3a89a69f 232 run => sub {
c4a69b87 233 my ($monikers, $classes, $self);
234 ($schema, $monikers, $classes, $self) = @_;
235
236 my $connect_info = [@$self{qw/dsn user password/}];
3a89a69f 237
238# Test that the table above (with '.' in name) gets loaded correctly.
239 ok((my $rs = eval {
c4a69b87 240 $schema->resultset('MssqlLoaderTest1Dot') }),
3a89a69f 241 'got a resultset for table with dot in name');
242
243 ok((my $from = eval { $rs->result_source->from }),
244 'got an $rsrc->from for table with dot in name');
245
246 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
247
248 is eval { $$from }, "[mssql_loader_test1.dot]",
249 '->table with dot in name has correct name';
250
deedd576 251# Test capitalization of columns and unique constraints
252 ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
253 'got result_source');
254
c4a69b87 255 if ($schema->loader->preserve_case) {
060f5ecd 256 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
257 'column name case is preserved with case-sensitive collation';
020f3c3a 258
060f5ecd 259 my %uniqs = $rsrc->unique_constraints;
260 delete $uniqs{primary};
3a89a69f 261
060f5ecd 262 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
263 'column name case is preserved in unique constraint with case-sensitive collation');
264 }
265 else {
266 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
267 'column names are lowercased for case-insensitive collation';
268
269 my %uniqs = $rsrc->unique_constraints;
270 delete $uniqs{primary};
3a89a69f 271
060f5ecd 272 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
273 'columns in unique constraint lowercased for case-insensitive collation');
274 }
3a89a69f 275
020f3c3a 276 lives_and {
060f5ecd 277 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
16773d6d 278
c4a69b87 279 if ($schema->loader->preserve_case) {
16773d6d 280 $five_row->foo_col(1);
281 $five_row->bar_col(2);
282 }
283 else {
284 $five_row->foocol(1);
285 $five_row->barcol(2);
286 }
060f5ecd 287 $five_row->insert;
288
16773d6d 289 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
020f3c3a 290
291 is $six_row->five->id, 1;
292 } 'relationships for mixed-case tables/columns detected';
293
3a89a69f 294# Test that a bad view (where underlying table is gone) is ignored.
295 my $dbh = $schema->storage->dbh;
296 $dbh->do("DROP TABLE mssql_loader_test3");
297
1ad8e8c3 298 warnings_exist_silent { $schema->rescan }
c38ec663 299 qr/^Bad table or view 'mssql_loader_test4'/, 'bad view ignored';
3a89a69f 300
301 throws_ok {
302 $schema->resultset($monikers->{mssql_loader_test4})
303 } qr/Can't find source/,
304 'no source registered for bad view';
c4a69b87 305
f8640ecc 306 # test on delete/update fk clause introspection
307 ok ((my $rel_info = $schema->source('MssqlLoaderTest18')->relationship_info('seventeen')),
308 'got rel info');
309
310 is $rel_info->{attrs}{on_delete}, 'SET DEFAULT',
311 'ON DELETE clause introspected correctly';
312
313 is $rel_info->{attrs}{on_update}, 'SET NULL',
314 'ON UPDATE clause introspected correctly';
315
316 is $rel_info->{attrs}{is_deferrable}, 1,
317 'is_deferrable defaults to 1';
318
c4a69b87 319 SKIP: {
320 my $dbh = $schema->storage->dbh;
321
322 try {
5975bbe6 323 $dbh->do('CREATE SCHEMA [dbicsl-test]');
c4a69b87 324 }
325 catch {
5975bbe6 326 skip "no CREATE SCHEMA privileges", 30 * 2;
c4a69b87 327 };
328
329 $dbh->do(<<"EOF");
330 CREATE TABLE [dbicsl-test].mssql_loader_test8 (
331 id INT IDENTITY PRIMARY KEY,
332 value VARCHAR(100)
333 )
334EOF
335 $dbh->do(<<"EOF");
336 CREATE TABLE [dbicsl-test].mssql_loader_test9 (
337 id INT IDENTITY PRIMARY KEY,
338 value VARCHAR(100),
5975bbe6 339 eight_id INTEGER NOT NULL,
340 CONSTRAINT loader_test9_uniq UNIQUE (eight_id),
c4a69b87 341 FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
342 )
343EOF
344 $dbh->do('CREATE SCHEMA [dbicsl.test]');
345 $dbh->do(<<"EOF");
5975bbe6 346 CREATE TABLE [dbicsl.test].mssql_loader_test9 (
347 pk INT IDENTITY PRIMARY KEY,
348 value VARCHAR(100),
349 eight_id INTEGER NOT NULL,
350 CONSTRAINT loader_test9_uniq UNIQUE (eight_id),
351 FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
352 )
353EOF
354 $dbh->do(<<"EOF");
c4a69b87 355 CREATE TABLE [dbicsl.test].mssql_loader_test10 (
356 id INT IDENTITY PRIMARY KEY,
357 value VARCHAR(100),
358 mssql_loader_test8_id INTEGER,
359 FOREIGN KEY (mssql_loader_test8_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
360 )
361EOF
362 $dbh->do(<<"EOF");
363 CREATE TABLE [dbicsl.test].mssql_loader_test11 (
364 id INT IDENTITY PRIMARY KEY,
365 value VARCHAR(100),
366 ten_id INTEGER NOT NULL UNIQUE,
367 FOREIGN KEY (ten_id) REFERENCES [dbicsl.test].mssql_loader_test10 (id)
368 )
369EOF
370 $dbh->do(<<"EOF");
371 CREATE TABLE [dbicsl-test].mssql_loader_test12 (
372 id INT IDENTITY PRIMARY KEY,
373 value VARCHAR(100),
374 mssql_loader_test11_id INTEGER,
375 FOREIGN KEY (mssql_loader_test11_id) REFERENCES [dbicsl.test].mssql_loader_test11 (id)
376 )
377EOF
378
5975bbe6 379 my $guard = Scope::Guard->new(\&cleanup_schemas);
c4a69b87 380
381 foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
382 lives_and {
383 rmtree EXTRA_DUMP_DIR;
384
385 my @warns;
386 local $SIG{__WARN__} = sub {
387 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
388 };
389
390 make_schema_at(
391 'MSSQLMultiSchema',
392 {
393 naming => 'current',
394 db_schema => $db_schema,
395 dump_directory => EXTRA_DUMP_DIR,
396 quiet => 1,
397 },
398 $connect_info,
399 );
400
401 diag join "\n", @warns if @warns;
402
403 is @warns, 0;
404 } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
405
406 my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
407
408 lives_and {
409 ok $test_schema = MSSQLMultiSchema->connect(@$connect_info);
410 } 'connected test schema';
411
412 lives_and {
4c2e2ce9 413 ok $rsrc = $test_schema->source('MssqlLoaderTest8');
c4a69b87 414 } 'got source for table in schema name with dash';
415
416 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
417 'column in schema name with dash';
418
419 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
420 'column in schema name with dash';
421
422 is try { $rsrc->column_info('value')->{size} }, 100,
423 'column in schema name with dash';
424
425 lives_and {
4c2e2ce9 426 ok $rs = $test_schema->resultset('MssqlLoaderTest8');
c4a69b87 427 } 'got resultset for table in schema name with dash';
428
429 lives_and {
430 ok $row = $rs->create({ value => 'foo' });
431 } 'executed SQL on table in schema name with dash';
432
5975bbe6 433 $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_mssql_loader_test9') };
c4a69b87 434
435 is_deeply $rel_info->{cond}, {
436 'foreign.eight_id' => 'self.id'
437 }, 'relationship in schema name with dash';
438
439 is $rel_info->{attrs}{accessor}, 'single',
440 'relationship in schema name with dash';
441
442 is $rel_info->{attrs}{join_type}, 'LEFT',
443 'relationship in schema name with dash';
444
445 lives_and {
5975bbe6 446 ok $rsrc = $test_schema->source('DbicslDashTestMssqlLoaderTest9');
c4a69b87 447 } 'got source for table in schema name with dash';
448
449 %uniqs = try { $rsrc->unique_constraints };
450
451 is keys %uniqs, 2,
452 'got unique and primary constraint in schema name with dash';
453
5975bbe6 454 delete $uniqs{primary};
455
456 is_deeply ((values %uniqs)[0], ['eight_id'],
457 'correct unique constraint in schema name with dash');
458
c4a69b87 459 lives_and {
4c2e2ce9 460 ok $rsrc = $test_schema->source('MssqlLoaderTest10');
c4a69b87 461 } 'got source for table in schema name with dot';
462
463 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
464 'column in schema name with dot introspected correctly';
465
466 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
467 'column in schema name with dot introspected correctly';
468
469 is try { $rsrc->column_info('value')->{size} }, 100,
470 'column in schema name with dot introspected correctly';
471
472 lives_and {
4c2e2ce9 473 ok $rs = $test_schema->resultset('MssqlLoaderTest10');
c4a69b87 474 } 'got resultset for table in schema name with dot';
475
476 lives_and {
477 ok $row = $rs->create({ value => 'foo' });
478 } 'executed SQL on table in schema name with dot';
479
480 $rel_info = try { $rsrc->relationship_info('mssql_loader_test11') };
481
482 is_deeply $rel_info->{cond}, {
483 'foreign.ten_id' => 'self.id'
484 }, 'relationship in schema name with dot';
485
486 is $rel_info->{attrs}{accessor}, 'single',
487 'relationship in schema name with dot';
488
489 is $rel_info->{attrs}{join_type}, 'LEFT',
490 'relationship in schema name with dot';
491
492 lives_and {
4c2e2ce9 493 ok $rsrc = $test_schema->source('MssqlLoaderTest11');
c4a69b87 494 } 'got source for table in schema name with dot';
495
496 %uniqs = try { $rsrc->unique_constraints };
497
498 is keys %uniqs, 2,
499 'got unique and primary constraint in schema name with dot';
500
5975bbe6 501 delete $uniqs{primary};
502
503 is_deeply ((values %uniqs)[0], ['ten_id'],
504 'correct unique constraint in schema name with dot');
505
c4a69b87 506 lives_and {
4c2e2ce9 507 ok $test_schema->source('MssqlLoaderTest10')
c4a69b87 508 ->has_relationship('mssql_loader_test8');
509 } 'cross-schema relationship in multi-db_schema';
510
511 lives_and {
4c2e2ce9 512 ok $test_schema->source('MssqlLoaderTest8')
c4a69b87 513 ->has_relationship('mssql_loader_test10s');
514 } 'cross-schema relationship in multi-db_schema';
515
516 lives_and {
4c2e2ce9 517 ok $test_schema->source('MssqlLoaderTest12')
c4a69b87 518 ->has_relationship('mssql_loader_test11');
519 } 'cross-schema relationship in multi-db_schema';
520
521 lives_and {
4c2e2ce9 522 ok $test_schema->source('MssqlLoaderTest11')
c4a69b87 523 ->has_relationship('mssql_loader_test12s');
524 } 'cross-schema relationship in multi-db_schema';
525 }
526 }
527
528 SKIP: {
b826c589 529 # for ADO
ff4b0152 530 local $SIG{__WARN__} = sigwarn_silencer(
531 qr/Changed database context/
532 );
b826c589 533
c4a69b87 534 my $dbh = $schema->storage->dbh;
535
536 try {
537 $dbh->do('USE master');
538 $dbh->do('CREATE DATABASE dbicsl_test1');
539 }
540 catch {
4c2e2ce9 541 diag "no CREATE DATABASE privileges: '$_'";
5975bbe6 542 skip "no CREATE DATABASE privileges", 26 * 2;
c4a69b87 543 };
544
545 $dbh->do('CREATE DATABASE dbicsl_test2');
546
547 $dbh->do('USE dbicsl_test1');
548
549 $dbh->do(<<'EOF');
550 CREATE TABLE mssql_loader_test13 (
551 id INT IDENTITY PRIMARY KEY,
552 value VARCHAR(100)
553 )
554EOF
555 $dbh->do(<<'EOF');
556 CREATE TABLE mssql_loader_test14 (
557 id INT IDENTITY PRIMARY KEY,
558 value VARCHAR(100),
5975bbe6 559 thirteen_id INTEGER REFERENCES mssql_loader_test13 (id),
560 CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
c4a69b87 561 )
562EOF
563
c4a69b87 564 $dbh->do('USE dbicsl_test2');
565
5975bbe6 566 $dbh->do(<<'EOF');
567 CREATE TABLE mssql_loader_test14 (
568 pk INT IDENTITY PRIMARY KEY,
569 value VARCHAR(100),
570 thirteen_id INTEGER,
571 CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
572 )
573EOF
574
c4a69b87 575 $dbh->do(<<"EOF");
576 CREATE TABLE mssql_loader_test15 (
577 id INT IDENTITY PRIMARY KEY,
578 value VARCHAR(100)
579 )
580EOF
581 $dbh->do(<<"EOF");
582 CREATE TABLE mssql_loader_test16 (
583 id INT IDENTITY PRIMARY KEY,
584 value VARCHAR(100),
585 fifteen_id INTEGER UNIQUE REFERENCES mssql_loader_test15 (id)
586 )
587EOF
588
5975bbe6 589 my $guard = Scope::Guard->new(\&cleanup_databases);
c4a69b87 590
5975bbe6 591 foreach my $db_schema ({ dbicsl_test1 => '%', dbicsl_test2 => '%' }, { '%' => '%' }) {
592 lives_and {
593 my @warns;
594 local $SIG{__WARN__} = sub {
595 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
596 };
597
598 make_schema_at(
599 'MSSQLMultiDatabase',
600 {
601 naming => 'current',
602 db_schema => $db_schema,
5975bbe6 603 dump_directory => EXTRA_DUMP_DIR,
604 quiet => 1,
605 },
606 $connect_info,
607 );
c4a69b87 608
5975bbe6 609 diag join "\n", @warns if @warns;
c4a69b87 610
5975bbe6 611 is @warns, 0;
612 } "dumped schema for databases 'dbicsl_test1' and 'dbicsl_test2' with no warnings";
c4a69b87 613
5975bbe6 614 my $test_schema;
c4a69b87 615
5975bbe6 616 lives_and {
617 ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info);
618 } 'connected test schema';
c4a69b87 619
5975bbe6 620 my ($rsrc, $rs, $row, $rel_info, %uniqs);
c4a69b87 621
5975bbe6 622 lives_and {
4c2e2ce9 623 ok $rsrc = $test_schema->source('MssqlLoaderTest13');
5975bbe6 624 } 'got source for table in database one';
c4a69b87 625
5975bbe6 626 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
627 'column in database one';
c4a69b87 628
5975bbe6 629 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
630 'column in database one';
c4a69b87 631
5975bbe6 632 is try { $rsrc->column_info('value')->{size} }, 100,
633 'column in database one';
c4a69b87 634
5975bbe6 635 lives_and {
4c2e2ce9 636 ok $rs = $test_schema->resultset('MssqlLoaderTest13');
5975bbe6 637 } 'got resultset for table in database one';
c4a69b87 638
5975bbe6 639 lives_and {
640 ok $row = $rs->create({ value => 'foo' });
641 } 'executed SQL on table in database one';
c4a69b87 642
5975bbe6 643 $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') };
c4a69b87 644
5975bbe6 645 is_deeply $rel_info->{cond}, {
646 'foreign.thirteen_id' => 'self.id'
647 }, 'relationship in database one';
c4a69b87 648
5975bbe6 649 is $rel_info->{attrs}{accessor}, 'single',
650 'relationship in database one';
c4a69b87 651
5975bbe6 652 is $rel_info->{attrs}{join_type}, 'LEFT',
653 'relationship in database one';
c4a69b87 654
5975bbe6 655 lives_and {
656 ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest14');
657 } 'got source for table in database one';
c4a69b87 658
5975bbe6 659 %uniqs = try { $rsrc->unique_constraints };
c4a69b87 660
5975bbe6 661 is keys %uniqs, 2,
662 'got unique and primary constraint in database one';
c4a69b87 663
5975bbe6 664 delete $uniqs{primary};
c4a69b87 665
5975bbe6 666 is_deeply ((values %uniqs)[0], ['thirteen_id'],
667 'correct unique constraint in database one');
c4a69b87 668
5975bbe6 669 lives_and {
4c2e2ce9 670 ok $rsrc = $test_schema->source('MssqlLoaderTest15');
5975bbe6 671 } 'got source for table in database two';
c4a69b87 672
5975bbe6 673 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
674 'column in database two introspected correctly';
c4a69b87 675
5975bbe6 676 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
677 'column in database two introspected correctly';
c4a69b87 678
5975bbe6 679 is try { $rsrc->column_info('value')->{size} }, 100,
680 'column in database two introspected correctly';
c4a69b87 681
5975bbe6 682 lives_and {
4c2e2ce9 683 ok $rs = $test_schema->resultset('MssqlLoaderTest15');
5975bbe6 684 } 'got resultset for table in database two';
c4a69b87 685
5975bbe6 686 lives_and {
687 ok $row = $rs->create({ value => 'foo' });
688 } 'executed SQL on table in database two';
c4a69b87 689
5975bbe6 690 $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') };
c4a69b87 691
5975bbe6 692 is_deeply $rel_info->{cond}, {
693 'foreign.fifteen_id' => 'self.id'
694 }, 'relationship in database two';
c4a69b87 695
5975bbe6 696 is $rel_info->{attrs}{accessor}, 'single',
697 'relationship in database two';
698
699 is $rel_info->{attrs}{join_type}, 'LEFT',
700 'relationship in database two';
701
702 lives_and {
4c2e2ce9 703 ok $rsrc = $test_schema->source('MssqlLoaderTest16');
5975bbe6 704 } 'got source for table in database two';
c4a69b87 705
5975bbe6 706 %uniqs = try { $rsrc->unique_constraints };
707
708 is keys %uniqs, 2,
709 'got unique and primary constraint in database two';
c4a69b87 710
5975bbe6 711 delete $uniqs{primary};
712
713 is_deeply ((values %uniqs)[0], ['fifteen_id'],
714 'correct unique constraint in database two');
715 }
c4a69b87 716 }
3a89a69f 717 },
718 },
046e344c 719);
720
8dcf4292 721$tester->run_tests();
722
5975bbe6 723sub cleanup_schemas {
724 return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
725
726 # switch back to default database
727 $schema->storage->disconnect;
728 my $dbh = $schema->storage->dbh;
729
730 foreach my $table ('[dbicsl-test].mssql_loader_test12',
731 '[dbicsl.test].mssql_loader_test11',
732 '[dbicsl.test].mssql_loader_test10',
733 '[dbicsl.test].mssql_loader_test9',
734 '[dbicsl-test].mssql_loader_test9',
735 '[dbicsl-test].mssql_loader_test8') {
736 try {
737 $dbh->do("DROP TABLE $table");
738 }
739 catch {
740 diag "Error dropping table: $_";
741 };
742 }
c4a69b87 743
5975bbe6 744 foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
745 try {
746 $dbh->do(qq{DROP SCHEMA [$db_schema]});
747 }
748 catch {
749 diag "Error dropping test schema $db_schema: $_";
750 };
751 }
c4a69b87 752
5975bbe6 753 rmtree EXTRA_DUMP_DIR;
754}
c4a69b87 755
5975bbe6 756sub cleanup_databases {
757 return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
c4a69b87 758
b826c589 759 # for ADO
ff4b0152 760 local $SIG{__WARN__} = sigwarn_silencer(
761 qr/Changed database context/
762 );
b826c589 763
5975bbe6 764 my $dbh = $schema->storage->dbh;
c4a69b87 765
5975bbe6 766 $dbh->do('USE dbicsl_test1');
c4a69b87 767
5975bbe6 768 foreach my $table ('mssql_loader_test14',
769 'mssql_loader_test13') {
770 try {
771 $dbh->do("DROP TABLE $table");
772 }
773 catch {
774 diag "Error dropping table: $_";
775 };
776 }
c4a69b87 777
5975bbe6 778 $dbh->do('USE dbicsl_test2');
c4a69b87 779
5975bbe6 780 foreach my $table ('mssql_loader_test16',
781 'mssql_loader_test15',
782 'mssql_loader_test14') {
783 try {
784 $dbh->do("DROP TABLE $table");
785 }
786 catch {
787 diag "Error dropping table: $_";
788 };
789 }
c4a69b87 790
5975bbe6 791 $dbh->do('USE master');
792
793 foreach my $database (qw/dbicsl_test1 dbicsl_test2/) {
794 try {
795 $dbh->do(qq{DROP DATABASE $database});
c4a69b87 796 }
5975bbe6 797 catch {
798 diag "Error dropping test database '$database': $_";
799 };
c4a69b87 800 }
5975bbe6 801
802 rmtree EXTRA_DUMP_DIR;
c4a69b87 803}
060f5ecd 804# vim:et sts=4 sw=4 tw=0: