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