5 use DBIx::Class::Schema::Loader::Utils 'warnings_exist_silent';
7 use File::Path 'rmtree';
8 use DBIx::Class::Schema::Loader 'make_schema_at';
11 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
13 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
14 unshift @INC, $_ for split /:/, $lib_dirs;
20 use dbixcsl_common_tests ();
21 use dbixcsl_test_dir '$tdir';
23 use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump";
25 # for cleanup in END for extra tests
26 my ($schema, $schemas_created, $databases_created);
28 my ($dsns, $common_version);
30 for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
31 next unless $ENV{"DBICTEST_${_}_DSN"};
33 $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
34 $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
35 $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
38 my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
42 $dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion')->{Character_Value}
45 my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/;
47 if (! defined $common_version or $common_version > $maj_srv_ver ) {
48 $common_version = $maj_srv_ver;
52 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'
55 my $mssql_2008_new_data_types = {
56 date => { data_type => 'date' },
57 time => { data_type => 'time' },
58 'time(0)'=> { data_type => 'time', size => 0 },
59 'time(1)'=> { data_type => 'time', size => 1 },
60 'time(2)'=> { data_type => 'time', size => 2 },
61 'time(3)'=> { data_type => 'time', size => 3 },
62 'time(4)'=> { data_type => 'time', size => 4 },
63 'time(5)'=> { data_type => 'time', size => 5 },
64 'time(6)'=> { data_type => 'time', size => 6 },
65 'time(7)'=> { data_type => 'time' },
66 datetimeoffset => { data_type => 'datetimeoffset' },
67 'datetimeoffset(0)' => { data_type => 'datetimeoffset', size => 0 },
68 'datetimeoffset(1)' => { data_type => 'datetimeoffset', size => 1 },
69 'datetimeoffset(2)' => { data_type => 'datetimeoffset', size => 2 },
70 'datetimeoffset(3)' => { data_type => 'datetimeoffset', size => 3 },
71 'datetimeoffset(4)' => { data_type => 'datetimeoffset', size => 4 },
72 'datetimeoffset(5)' => { data_type => 'datetimeoffset', size => 5 },
73 'datetimeoffset(6)' => { data_type => 'datetimeoffset', size => 6 },
74 'datetimeoffset(7)' => { data_type => 'datetimeoffset' },
75 datetime2 => { data_type => 'datetime2' },
76 'datetime2(0)' => { data_type => 'datetime2', size => 0 },
77 'datetime2(1)' => { data_type => 'datetime2', size => 1 },
78 'datetime2(2)' => { data_type => 'datetime2', size => 2 },
79 'datetime2(3)' => { data_type => 'datetime2', size => 3 },
80 'datetime2(4)' => { data_type => 'datetime2', size => 4 },
81 'datetime2(5)' => { data_type => 'datetime2', size => 5 },
82 'datetime2(6)' => { data_type => 'datetime2', size => 6 },
83 'datetime2(7)' => { data_type => 'datetime2' },
85 hierarchyid => { data_type => 'hierarchyid' },
88 my $tester = dbixcsl_common_tests->new(
90 auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
91 default_function_def => 'DATETIME DEFAULT getdate()',
92 connect_info => [values %$dsns],
93 preserve_case_mode_is_exclusive => 1,
94 quote_char => [ qw/[ ]/ ],
95 basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME',
97 # http://msdn.microsoft.com/en-us/library/ms187752.aspx
100 'int identity' => { data_type => 'integer', is_auto_increment => 1 },
101 bigint => { data_type => 'bigint' },
102 int => { data_type => 'integer' },
103 integer => { data_type => 'integer' },
104 smallint => { data_type => 'smallint' },
105 tinyint => { data_type => 'tinyint' },
106 money => { data_type => 'money' },
107 smallmoney => { data_type => 'smallmoney' },
108 bit => { data_type => 'bit' },
109 real => { data_type => 'real' },
110 'float(14)' => { data_type => 'real' },
111 'float(24)' => { data_type => 'real' },
112 'float(25)' => { data_type => 'double precision' },
113 'float(53)' => { data_type => 'double precision' },
114 float => { data_type => 'double precision' },
116 => { data_type => 'double precision' },
117 'numeric(6)' => { data_type => 'numeric', size => [6,0] },
118 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
119 'decimal(6)' => { data_type => 'decimal', size => [6,0] },
120 'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
121 'dec(6,3)' => { data_type => 'decimal', size => [6,3] },
122 numeric => { data_type => 'numeric' },
123 decimal => { data_type => 'decimal' },
124 dec => { data_type => 'decimal' },
127 datetime => { data_type => 'datetime' },
128 # test rewriting getdate() to current_timestamp
129 'datetime default getdate()'
130 => { data_type => 'datetime', default_value => \'current_timestamp',
131 original => { default_value => \'getdate()' } },
132 smalldatetime => { data_type => 'smalldatetime' },
134 ($common_version >= 10) ? %$mssql_2008_new_data_types : (),
137 char => { data_type => 'char', size => 1 },
138 'char(2)' => { data_type => 'char', size => 2 },
139 character => { data_type => 'char', size => 1 },
140 'character(2)' => { data_type => 'char', size => 2 },
141 'varchar(2)' => { data_type => 'varchar', size => 2 },
143 nchar => { data_type => 'nchar', size => 1 },
144 'nchar(2)' => { data_type => 'nchar', size => 2 },
145 'nvarchar(2)' => { data_type => 'nvarchar', size => 2 },
148 'binary' => { data_type => 'binary', size => 1 },
149 'binary(2)' => { data_type => 'binary', size => 2 },
150 'varbinary(2)' => { data_type => 'varbinary', size => 2 },
153 'varchar(max)' => { data_type => 'text' },
154 text => { data_type => 'text' },
156 'nvarchar(max)' => { data_type => 'ntext' },
157 ntext => { data_type => 'ntext' },
159 'varbinary(max)' => { data_type => 'image' },
160 image => { data_type => 'image' },
163 timestamp => { data_type => 'timestamp', inflate_datetime => 0 },
164 rowversion => { data_type => 'rowversion' },
165 uniqueidentifier => { data_type => 'uniqueidentifier' },
166 sql_variant => { data_type => 'sql_variant' },
167 xml => { data_type => 'xml' },
172 CREATE TABLE [mssql_loader_test1.dot] (
173 id INT IDENTITY NOT NULL PRIMARY KEY,
178 CREATE TABLE mssql_loader_test3 (
179 id INT IDENTITY NOT NULL PRIMARY KEY
183 CREATE VIEW mssql_loader_test4 AS
184 SELECT * FROM mssql_loader_test3
186 # test capitalization of cols in unique constraints and rels
187 q{ SET QUOTED_IDENTIFIER ON },
188 q{ SET ANSI_NULLS ON },
190 CREATE TABLE [MSSQL_Loader_Test5] (
191 [Id] INT IDENTITY NOT NULL PRIMARY KEY,
192 [FooCol] INT NOT NULL,
193 [BarCol] INT NOT NULL,
194 UNIQUE ([FooCol], [BarCol])
198 CREATE TABLE [MSSQL_Loader_Test6] (
199 [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
204 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
205 'DROP VIEW mssql_loader_test4',
208 '[mssql_loader_test1.dot]',
209 'mssql_loader_test3',
210 'MSSQL_Loader_Test6',
211 'MSSQL_Loader_Test5',
213 count => 10 + 28 * 2 + 24,
215 my ($monikers, $classes, $self);
216 ($schema, $monikers, $classes, $self) = @_;
218 my $connect_info = [@$self{qw/dsn user password/}];
220 # Test that the table above (with '.' in name) gets loaded correctly.
222 $schema->resultset('MssqlLoaderTest1Dot') }),
223 'got a resultset for table with dot in name');
225 ok((my $from = eval { $rs->result_source->from }),
226 'got an $rsrc->from for table with dot in name');
228 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
230 is eval { $$from }, "[mssql_loader_test1.dot]",
231 '->table with dot in name has correct name';
233 # Test capitalization of columns and unique constraints
234 ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
235 'got result_source');
237 if ($schema->loader->preserve_case) {
238 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
239 'column name case is preserved with case-sensitive collation';
241 my %uniqs = $rsrc->unique_constraints;
242 delete $uniqs{primary};
244 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
245 'column name case is preserved in unique constraint with case-sensitive collation');
248 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
249 'column names are lowercased for case-insensitive collation';
251 my %uniqs = $rsrc->unique_constraints;
252 delete $uniqs{primary};
254 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
255 'columns in unique constraint lowercased for case-insensitive collation');
259 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
261 if ($schema->loader->preserve_case) {
262 $five_row->foo_col(1);
263 $five_row->bar_col(2);
266 $five_row->foocol(1);
267 $five_row->barcol(2);
271 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
273 is $six_row->five->id, 1;
274 } 'relationships for mixed-case tables/columns detected';
276 # Test that a bad view (where underlying table is gone) is ignored.
277 my $dbh = $schema->storage->dbh;
278 $dbh->do("DROP TABLE mssql_loader_test3");
280 warnings_exist_silent { $schema->rescan }
281 qr/^Bad table or view 'mssql_loader_test4'/, 'bad view ignored';
284 $schema->resultset($monikers->{mssql_loader_test4})
285 } qr/Can't find source/,
286 'no source registered for bad view';
289 my $dbh = $schema->storage->dbh;
292 $dbh->do('CREATE SCHEMA "dbicsl-test"');
295 $schemas_created = 0;
296 skip "no CREATE SCHEMA privileges", 28 * 2;
300 CREATE TABLE [dbicsl-test].mssql_loader_test8 (
301 id INT IDENTITY PRIMARY KEY,
306 CREATE TABLE [dbicsl-test].mssql_loader_test9 (
307 id INT IDENTITY PRIMARY KEY,
309 eight_id INTEGER NOT NULL UNIQUE,
310 FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
313 $dbh->do('CREATE SCHEMA [dbicsl.test]');
315 CREATE TABLE [dbicsl.test].mssql_loader_test10 (
316 id INT IDENTITY PRIMARY KEY,
318 mssql_loader_test8_id INTEGER,
319 FOREIGN KEY (mssql_loader_test8_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
323 CREATE TABLE [dbicsl.test].mssql_loader_test11 (
324 id INT IDENTITY PRIMARY KEY,
326 ten_id INTEGER NOT NULL UNIQUE,
327 FOREIGN KEY (ten_id) REFERENCES [dbicsl.test].mssql_loader_test10 (id)
331 CREATE TABLE [dbicsl-test].mssql_loader_test12 (
332 id INT IDENTITY PRIMARY KEY,
334 mssql_loader_test11_id INTEGER,
335 FOREIGN KEY (mssql_loader_test11_id) REFERENCES [dbicsl.test].mssql_loader_test11 (id)
339 $schemas_created = 1;
341 foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
343 rmtree EXTRA_DUMP_DIR;
346 local $SIG{__WARN__} = sub {
347 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
354 db_schema => $db_schema,
355 dump_directory => EXTRA_DUMP_DIR,
361 diag join "\n", @warns if @warns;
364 } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
366 my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
369 ok $test_schema = MSSQLMultiSchema->connect(@$connect_info);
370 } 'connected test schema';
373 ok $rsrc = $test_schema->source('MssqlLoaderTest8');
374 } 'got source for table in schema name with dash';
376 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
377 'column in schema name with dash';
379 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
380 'column in schema name with dash';
382 is try { $rsrc->column_info('value')->{size} }, 100,
383 'column in schema name with dash';
386 ok $rs = $test_schema->resultset('MssqlLoaderTest8');
387 } 'got resultset for table in schema name with dash';
390 ok $row = $rs->create({ value => 'foo' });
391 } 'executed SQL on table in schema name with dash';
393 $rel_info = try { $rsrc->relationship_info('mssql_loader_test9') };
395 is_deeply $rel_info->{cond}, {
396 'foreign.eight_id' => 'self.id'
397 }, 'relationship in schema name with dash';
399 is $rel_info->{attrs}{accessor}, 'single',
400 'relationship in schema name with dash';
402 is $rel_info->{attrs}{join_type}, 'LEFT',
403 'relationship in schema name with dash';
406 ok $rsrc = $test_schema->source('MssqlLoaderTest9');
407 } 'got source for table in schema name with dash';
409 %uniqs = try { $rsrc->unique_constraints };
412 'got unique and primary constraint in schema name with dash';
415 ok $rsrc = $test_schema->source('MssqlLoaderTest10');
416 } 'got source for table in schema name with dot';
418 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
419 'column in schema name with dot introspected correctly';
421 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
422 'column in schema name with dot introspected correctly';
424 is try { $rsrc->column_info('value')->{size} }, 100,
425 'column in schema name with dot introspected correctly';
428 ok $rs = $test_schema->resultset('MssqlLoaderTest10');
429 } 'got resultset for table in schema name with dot';
432 ok $row = $rs->create({ value => 'foo' });
433 } 'executed SQL on table in schema name with dot';
435 $rel_info = try { $rsrc->relationship_info('mssql_loader_test11') };
437 is_deeply $rel_info->{cond}, {
438 'foreign.ten_id' => 'self.id'
439 }, 'relationship in schema name with dot';
441 is $rel_info->{attrs}{accessor}, 'single',
442 'relationship in schema name with dot';
444 is $rel_info->{attrs}{join_type}, 'LEFT',
445 'relationship in schema name with dot';
448 ok $rsrc = $test_schema->source('MssqlLoaderTest11');
449 } 'got source for table in schema name with dot';
451 %uniqs = try { $rsrc->unique_constraints };
454 'got unique and primary constraint in schema name with dot';
457 ok $test_schema->source('MssqlLoaderTest10')
458 ->has_relationship('mssql_loader_test8');
459 } 'cross-schema relationship in multi-db_schema';
462 ok $test_schema->source('MssqlLoaderTest8')
463 ->has_relationship('mssql_loader_test10s');
464 } 'cross-schema relationship in multi-db_schema';
467 ok $test_schema->source('MssqlLoaderTest12')
468 ->has_relationship('mssql_loader_test11');
469 } 'cross-schema relationship in multi-db_schema';
472 ok $test_schema->source('MssqlLoaderTest11')
473 ->has_relationship('mssql_loader_test12s');
474 } 'cross-schema relationship in multi-db_schema';
479 my $dbh = $schema->storage->dbh;
482 $dbh->do('USE master');
483 $dbh->do('CREATE DATABASE dbicsl_test1');
486 skip "no CREATE DATABASE privileges", 24;
489 $dbh->do('CREATE DATABASE dbicsl_test2');
491 $dbh->do('USE dbicsl_test1');
494 CREATE TABLE mssql_loader_test13 (
495 id INT IDENTITY PRIMARY KEY,
500 CREATE TABLE mssql_loader_test14 (
501 id INT IDENTITY PRIMARY KEY,
503 thirteen_id INTEGER UNIQUE REFERENCES mssql_loader_test13 (id)
507 $dbh->do('USE master');
508 $dbh->do('USE dbicsl_test2');
511 CREATE TABLE mssql_loader_test15 (
512 id INT IDENTITY PRIMARY KEY,
517 CREATE TABLE mssql_loader_test16 (
518 id INT IDENTITY PRIMARY KEY,
520 fifteen_id INTEGER UNIQUE REFERENCES mssql_loader_test15 (id)
524 $databases_created = 1;
528 local $SIG{__WARN__} = sub {
529 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
533 'MSSQLMultiDatabase',
536 db_schema => { '%' => '%' },
537 dump_directory => EXTRA_DUMP_DIR,
543 diag join "\n", @warns if @warns;
546 } 'dumped schema for all databases with no warnings';
551 ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info);
552 } 'connected test schema';
554 my ($rsrc, $rs, $row, $rel_info, %uniqs);
557 ok $rsrc = $test_schema->source('MssqlLoaderTest13');
558 } 'got source for table in database one';
560 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
561 'column in database one';
563 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
564 'column in database one';
566 is try { $rsrc->column_info('value')->{size} }, 100,
567 'column in database one';
570 ok $rs = $test_schema->resultset('MssqlLoaderTest13');
571 } 'got resultset for table in database one';
574 ok $row = $rs->create({ value => 'foo' });
575 } 'executed SQL on table in database one';
577 $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') };
579 is_deeply $rel_info->{cond}, {
580 'foreign.thirteen_id' => 'self.id'
581 }, 'relationship in database one';
583 is $rel_info->{attrs}{accessor}, 'single',
584 'relationship in database one';
586 is $rel_info->{attrs}{join_type}, 'LEFT',
587 'relationship in database one';
590 ok $rsrc = $test_schema->source('MssqlLoaderTest14');
591 } 'got source for table in database one';
593 %uniqs = try { $rsrc->unique_constraints };
596 'got unique and primary constraint in database one';
599 ok $rsrc = $test_schema->source('MssqlLoaderTest15');
600 } 'got source for table in database two';
602 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
603 'column in database two introspected correctly';
605 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
606 'column in database two introspected correctly';
608 is try { $rsrc->column_info('value')->{size} }, 100,
609 'column in database two introspected correctly';
612 ok $rs = $test_schema->resultset('MssqlLoaderTest15');
613 } 'got resultset for table in database two';
616 ok $row = $rs->create({ value => 'foo' });
617 } 'executed SQL on table in database two';
619 $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') };
621 is_deeply $rel_info->{cond}, {
622 'foreign.fifteen_id' => 'self.id'
623 }, 'relationship in database two';
625 is $rel_info->{attrs}{accessor}, 'single',
626 'relationship in database two';
628 is $rel_info->{attrs}{join_type}, 'LEFT',
629 'relationship in database two';
632 ok $rsrc = $test_schema->source('MssqlLoaderTest16');
633 } 'got source for table in database two';
635 %uniqs = try { $rsrc->unique_constraints };
638 'got unique and primary constraint in database two';
644 $tester->run_tests();
647 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
649 # switch back to default database
650 $schema->storage->disconnect;
651 my $dbh = $schema->storage->dbh;
653 if ($schemas_created) {
654 foreach my $table ('[dbicsl-test].mssql_loader_test12',
655 '[dbicsl.test].mssql_loader_test11',
656 '[dbicsl.test].mssql_loader_test10',
657 '[dbicsl-test].mssql_loader_test9',
658 '[dbicsl-test].mssql_loader_test8') {
660 $dbh->do("DROP TABLE $table");
663 diag "Error dropping table: $_";
667 foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
669 $dbh->do(qq{DROP SCHEMA [$db_schema]});
672 diag "Error dropping test schema $db_schema: $_";
677 if ($databases_created) {
678 $dbh->do('USE dbicsl_test1');
680 foreach my $table ('mssql_loader_test14',
681 'mssql_loader_test13') {
683 $dbh->do("DROP TABLE $table");
686 diag "Error dropping table: $_";
690 $dbh->do('USE dbicsl_test2');
692 foreach my $table ('mssql_loader_test16',
693 'mssql_loader_test15') {
695 $dbh->do("DROP TABLE $table");
698 diag "Error dropping table: $_";
702 $dbh->do('USE master');
704 foreach my $database (qw/dbicsl_test1 dbicsl_test2/) {
706 $dbh->do(qq{DROP DATABASE $database});
709 diag "Error dropping test database '$database': $_";
714 rmtree EXTRA_DUMP_DIR;
718 # vim:et sts=4 sw=4 tw=0: