Commit | Line | Data |
046e344c |
1 | use strict; |
05d322c8 |
2 | use warnings; |
3a89a69f |
3 | use Test::More; |
4 | use Test::Exception; |
1ad8e8c3 |
5 | use DBIx::Class::Schema::Loader::Utils 'warnings_exist_silent'; |
c4a69b87 |
6 | use Try::Tiny; |
7 | use File::Path 'rmtree'; |
8 | use DBIx::Class::Schema::Loader 'make_schema_at'; |
1ad8e8c3 |
9 | use namespace::clean; |
5975bbe6 |
10 | use Scope::Guard (); |
05d322c8 |
11 | |
12 | # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else |
13 | BEGIN { |
14 | if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) { |
15 | unshift @INC, $_ for split /:/, $lib_dirs; |
16 | } |
17 | } |
18 | |
c4a69b87 |
19 | use lib qw(t/lib); |
20 | |
21 | use dbixcsl_common_tests (); |
22 | use dbixcsl_test_dir '$tdir'; |
23 | |
24 | use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump"; |
25 | |
5975bbe6 |
26 | # for extra tests cleanup |
27 | my $schema; |
c4a69b87 |
28 | |
8dcf4292 |
29 | my ($dsns, $common_version); |
c4a69b87 |
30 | |
afcd3c32 |
31 | for (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 | |
53 | plan 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 |
56 | my $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 | |
89 | my $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 | ) |
304 | EOF |
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 | ) |
313 | EOF |
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 | ) |
323 | EOF |
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 | ) |
331 | EOF |
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 | ) |
339 | EOF |
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 | ) |
347 | EOF |
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: { |
b826c589 |
500 | # for ADO |
501 | my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; |
502 | local $SIG{__WARN__} = sub { |
503 | $warn_handler->(@_) unless $_[0] =~ /Changed database context/; |
504 | }; |
505 | |
c4a69b87 |
506 | my $dbh = $schema->storage->dbh; |
507 | |
508 | try { |
509 | $dbh->do('USE master'); |
510 | $dbh->do('CREATE DATABASE dbicsl_test1'); |
511 | } |
512 | catch { |
5975bbe6 |
513 | skip "no CREATE DATABASE privileges", 26 * 2; |
c4a69b87 |
514 | }; |
515 | |
516 | $dbh->do('CREATE DATABASE dbicsl_test2'); |
517 | |
518 | $dbh->do('USE dbicsl_test1'); |
519 | |
520 | $dbh->do(<<'EOF'); |
521 | CREATE TABLE mssql_loader_test13 ( |
522 | id INT IDENTITY PRIMARY KEY, |
523 | value VARCHAR(100) |
524 | ) |
525 | EOF |
526 | $dbh->do(<<'EOF'); |
527 | CREATE TABLE mssql_loader_test14 ( |
528 | id INT IDENTITY PRIMARY KEY, |
529 | value VARCHAR(100), |
5975bbe6 |
530 | thirteen_id INTEGER REFERENCES mssql_loader_test13 (id), |
531 | CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id) |
c4a69b87 |
532 | ) |
533 | EOF |
534 | |
c4a69b87 |
535 | $dbh->do('USE dbicsl_test2'); |
536 | |
5975bbe6 |
537 | $dbh->do(<<'EOF'); |
538 | CREATE TABLE mssql_loader_test14 ( |
539 | pk INT IDENTITY PRIMARY KEY, |
540 | value VARCHAR(100), |
541 | thirteen_id INTEGER, |
542 | CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id) |
543 | ) |
544 | EOF |
545 | |
c4a69b87 |
546 | $dbh->do(<<"EOF"); |
547 | CREATE TABLE mssql_loader_test15 ( |
548 | id INT IDENTITY PRIMARY KEY, |
549 | value VARCHAR(100) |
550 | ) |
551 | EOF |
552 | $dbh->do(<<"EOF"); |
553 | CREATE TABLE mssql_loader_test16 ( |
554 | id INT IDENTITY PRIMARY KEY, |
555 | value VARCHAR(100), |
556 | fifteen_id INTEGER UNIQUE REFERENCES mssql_loader_test15 (id) |
557 | ) |
558 | EOF |
559 | |
5975bbe6 |
560 | my $guard = Scope::Guard->new(\&cleanup_databases); |
c4a69b87 |
561 | |
5975bbe6 |
562 | foreach my $db_schema ({ dbicsl_test1 => '%', dbicsl_test2 => '%' }, { '%' => '%' }) { |
563 | lives_and { |
564 | my @warns; |
565 | local $SIG{__WARN__} = sub { |
566 | push @warns, $_[0] unless $_[0] =~ /\bcollides\b/; |
567 | }; |
568 | |
569 | make_schema_at( |
570 | 'MSSQLMultiDatabase', |
571 | { |
572 | naming => 'current', |
573 | db_schema => $db_schema, |
574 | moniker_parts => [qw/database name/], |
575 | dump_directory => EXTRA_DUMP_DIR, |
576 | quiet => 1, |
577 | }, |
578 | $connect_info, |
579 | ); |
c4a69b87 |
580 | |
5975bbe6 |
581 | diag join "\n", @warns if @warns; |
c4a69b87 |
582 | |
5975bbe6 |
583 | is @warns, 0; |
584 | } "dumped schema for databases 'dbicsl_test1' and 'dbicsl_test2' with no warnings"; |
c4a69b87 |
585 | |
5975bbe6 |
586 | my $test_schema; |
c4a69b87 |
587 | |
5975bbe6 |
588 | lives_and { |
589 | ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info); |
590 | } 'connected test schema'; |
c4a69b87 |
591 | |
5975bbe6 |
592 | my ($rsrc, $rs, $row, $rel_info, %uniqs); |
c4a69b87 |
593 | |
5975bbe6 |
594 | lives_and { |
595 | ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest13'); |
596 | } 'got source for table in database one'; |
c4a69b87 |
597 | |
5975bbe6 |
598 | is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, |
599 | 'column in database one'; |
c4a69b87 |
600 | |
5975bbe6 |
601 | is try { $rsrc->column_info('value')->{data_type} }, 'varchar', |
602 | 'column in database one'; |
c4a69b87 |
603 | |
5975bbe6 |
604 | is try { $rsrc->column_info('value')->{size} }, 100, |
605 | 'column in database one'; |
c4a69b87 |
606 | |
5975bbe6 |
607 | lives_and { |
608 | ok $rs = $test_schema->resultset('DbicslTest1MssqlLoaderTest13'); |
609 | } 'got resultset for table in database one'; |
c4a69b87 |
610 | |
5975bbe6 |
611 | lives_and { |
612 | ok $row = $rs->create({ value => 'foo' }); |
613 | } 'executed SQL on table in database one'; |
c4a69b87 |
614 | |
5975bbe6 |
615 | $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') }; |
c4a69b87 |
616 | |
5975bbe6 |
617 | is_deeply $rel_info->{cond}, { |
618 | 'foreign.thirteen_id' => 'self.id' |
619 | }, 'relationship in database one'; |
c4a69b87 |
620 | |
5975bbe6 |
621 | is $rel_info->{attrs}{accessor}, 'single', |
622 | 'relationship in database one'; |
c4a69b87 |
623 | |
5975bbe6 |
624 | is $rel_info->{attrs}{join_type}, 'LEFT', |
625 | 'relationship in database one'; |
c4a69b87 |
626 | |
5975bbe6 |
627 | lives_and { |
628 | ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest14'); |
629 | } 'got source for table in database one'; |
c4a69b87 |
630 | |
5975bbe6 |
631 | %uniqs = try { $rsrc->unique_constraints }; |
c4a69b87 |
632 | |
5975bbe6 |
633 | is keys %uniqs, 2, |
634 | 'got unique and primary constraint in database one'; |
c4a69b87 |
635 | |
5975bbe6 |
636 | delete $uniqs{primary}; |
c4a69b87 |
637 | |
5975bbe6 |
638 | is_deeply ((values %uniqs)[0], ['thirteen_id'], |
639 | 'correct unique constraint in database one'); |
c4a69b87 |
640 | |
5975bbe6 |
641 | lives_and { |
642 | ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest15'); |
643 | } 'got source for table in database two'; |
c4a69b87 |
644 | |
5975bbe6 |
645 | is try { $rsrc->column_info('id')->{is_auto_increment} }, 1, |
646 | 'column in database two introspected correctly'; |
c4a69b87 |
647 | |
5975bbe6 |
648 | is try { $rsrc->column_info('value')->{data_type} }, 'varchar', |
649 | 'column in database two introspected correctly'; |
c4a69b87 |
650 | |
5975bbe6 |
651 | is try { $rsrc->column_info('value')->{size} }, 100, |
652 | 'column in database two introspected correctly'; |
c4a69b87 |
653 | |
5975bbe6 |
654 | lives_and { |
655 | ok $rs = $test_schema->resultset('DbicslTest2MssqlLoaderTest15'); |
656 | } 'got resultset for table in database two'; |
c4a69b87 |
657 | |
5975bbe6 |
658 | lives_and { |
659 | ok $row = $rs->create({ value => 'foo' }); |
660 | } 'executed SQL on table in database two'; |
c4a69b87 |
661 | |
5975bbe6 |
662 | $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') }; |
c4a69b87 |
663 | |
5975bbe6 |
664 | is_deeply $rel_info->{cond}, { |
665 | 'foreign.fifteen_id' => 'self.id' |
666 | }, 'relationship in database two'; |
c4a69b87 |
667 | |
5975bbe6 |
668 | is $rel_info->{attrs}{accessor}, 'single', |
669 | 'relationship in database two'; |
670 | |
671 | is $rel_info->{attrs}{join_type}, 'LEFT', |
672 | 'relationship in database two'; |
673 | |
674 | lives_and { |
675 | ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest16'); |
676 | } 'got source for table in database two'; |
c4a69b87 |
677 | |
5975bbe6 |
678 | %uniqs = try { $rsrc->unique_constraints }; |
679 | |
680 | is keys %uniqs, 2, |
681 | 'got unique and primary constraint in database two'; |
c4a69b87 |
682 | |
5975bbe6 |
683 | delete $uniqs{primary}; |
684 | |
685 | is_deeply ((values %uniqs)[0], ['fifteen_id'], |
686 | 'correct unique constraint in database two'); |
687 | } |
c4a69b87 |
688 | } |
3a89a69f |
689 | }, |
690 | }, |
046e344c |
691 | ); |
692 | |
8dcf4292 |
693 | $tester->run_tests(); |
694 | |
5975bbe6 |
695 | sub cleanup_schemas { |
696 | return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; |
697 | |
698 | # switch back to default database |
699 | $schema->storage->disconnect; |
700 | my $dbh = $schema->storage->dbh; |
701 | |
702 | foreach my $table ('[dbicsl-test].mssql_loader_test12', |
703 | '[dbicsl.test].mssql_loader_test11', |
704 | '[dbicsl.test].mssql_loader_test10', |
705 | '[dbicsl.test].mssql_loader_test9', |
706 | '[dbicsl-test].mssql_loader_test9', |
707 | '[dbicsl-test].mssql_loader_test8') { |
708 | try { |
709 | $dbh->do("DROP TABLE $table"); |
710 | } |
711 | catch { |
712 | diag "Error dropping table: $_"; |
713 | }; |
714 | } |
c4a69b87 |
715 | |
5975bbe6 |
716 | foreach my $db_schema (qw/dbicsl-test dbicsl.test/) { |
717 | try { |
718 | $dbh->do(qq{DROP SCHEMA [$db_schema]}); |
719 | } |
720 | catch { |
721 | diag "Error dropping test schema $db_schema: $_"; |
722 | }; |
723 | } |
c4a69b87 |
724 | |
5975bbe6 |
725 | rmtree EXTRA_DUMP_DIR; |
726 | } |
c4a69b87 |
727 | |
5975bbe6 |
728 | sub cleanup_databases { |
729 | return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; |
c4a69b87 |
730 | |
b826c589 |
731 | # for ADO |
732 | my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; |
733 | local $SIG{__WARN__} = sub { |
734 | $warn_handler->(@_) unless $_[0] =~ /Changed database context/; |
735 | }; |
736 | |
5975bbe6 |
737 | my $dbh = $schema->storage->dbh; |
c4a69b87 |
738 | |
5975bbe6 |
739 | $dbh->do('USE dbicsl_test1'); |
c4a69b87 |
740 | |
5975bbe6 |
741 | foreach my $table ('mssql_loader_test14', |
742 | 'mssql_loader_test13') { |
743 | try { |
744 | $dbh->do("DROP TABLE $table"); |
745 | } |
746 | catch { |
747 | diag "Error dropping table: $_"; |
748 | }; |
749 | } |
c4a69b87 |
750 | |
5975bbe6 |
751 | $dbh->do('USE dbicsl_test2'); |
c4a69b87 |
752 | |
5975bbe6 |
753 | foreach my $table ('mssql_loader_test16', |
754 | 'mssql_loader_test15', |
755 | 'mssql_loader_test14') { |
756 | try { |
757 | $dbh->do("DROP TABLE $table"); |
758 | } |
759 | catch { |
760 | diag "Error dropping table: $_"; |
761 | }; |
762 | } |
c4a69b87 |
763 | |
5975bbe6 |
764 | $dbh->do('USE master'); |
765 | |
766 | foreach my $database (qw/dbicsl_test1 dbicsl_test2/) { |
767 | try { |
768 | $dbh->do(qq{DROP DATABASE $database}); |
c4a69b87 |
769 | } |
5975bbe6 |
770 | catch { |
771 | diag "Error dropping test database '$database': $_"; |
772 | }; |
c4a69b87 |
773 | } |
5975bbe6 |
774 | |
775 | rmtree EXTRA_DUMP_DIR; |
c4a69b87 |
776 | } |
060f5ecd |
777 | # vim:et sts=4 sw=4 tw=0: |