5 use DBIx::Class::Schema::Loader::Utils 'warnings_exist_silent';
7 use File::Path 'rmtree';
8 use DBIx::Class::Schema::Loader 'make_schema_at';
12 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
14 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
15 unshift @INC, $_ for split /:/, $lib_dirs;
21 use dbixcsl_common_tests ();
22 use dbixcsl_test_dir '$tdir';
24 use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump";
26 # for extra tests cleanup
29 my ($dsns, $common_version);
31 for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
32 next unless $ENV{"DBICTEST_${_}_DSN"};
34 $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
35 $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
36 $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
39 my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
43 $dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion')->{Character_Value}
46 my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/;
48 if (! defined $common_version or $common_version > $maj_srv_ver ) {
49 $common_version = $maj_srv_ver;
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'
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' },
86 hierarchyid => { data_type => 'hierarchyid' },
89 my $tester = dbixcsl_common_tests->new(
91 auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
92 default_function_def => 'DATETIME DEFAULT getdate()',
93 connect_info => [values %$dsns],
94 preserve_case_mode_is_exclusive => 1,
95 quote_char => [ qw/[ ]/ ],
96 basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME',
98 # http://msdn.microsoft.com/en-us/library/ms187752.aspx
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' },
110 real => { data_type => 'real' },
111 'float(14)' => { data_type => 'real' },
112 'float(24)' => { data_type => 'real' },
113 'float(25)' => { data_type => 'double precision' },
114 'float(53)' => { data_type => 'double precision' },
115 float => { data_type => 'double precision' },
117 => { data_type => 'double precision' },
118 'numeric(6)' => { data_type => 'numeric', size => [6,0] },
119 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
120 'decimal(6)' => { data_type => 'decimal', size => [6,0] },
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' },
128 datetime => { data_type => 'datetime' },
129 # test rewriting getdate() to current_timestamp
130 'datetime default getdate()'
131 => { data_type => 'datetime', default_value => \'current_timestamp',
132 original => { default_value => \'getdate()' } },
133 smalldatetime => { data_type => 'smalldatetime' },
135 ($common_version >= 10) ? %$mssql_2008_new_data_types : (),
138 char => { data_type => 'char', size => 1 },
139 'char(2)' => { data_type => 'char', size => 2 },
140 character => { data_type => 'char', size => 1 },
141 'character(2)' => { data_type => 'char', size => 2 },
142 'varchar(2)' => { data_type => 'varchar', size => 2 },
144 nchar => { data_type => 'nchar', size => 1 },
145 'nchar(2)' => { data_type => 'nchar', size => 2 },
146 'nvarchar(2)' => { data_type => 'nvarchar', size => 2 },
149 'binary' => { data_type => 'binary', size => 1 },
150 'binary(2)' => { data_type => 'binary', size => 2 },
151 'varbinary(2)' => { data_type => 'varbinary', size => 2 },
154 'varchar(max)' => { data_type => 'text' },
155 text => { data_type => 'text' },
157 'nvarchar(max)' => { data_type => 'ntext' },
158 ntext => { data_type => 'ntext' },
160 'varbinary(max)' => { data_type => 'image' },
161 image => { data_type => 'image' },
164 timestamp => { data_type => 'timestamp', inflate_datetime => 0 },
165 rowversion => { data_type => 'rowversion' },
166 uniqueidentifier => { data_type => 'uniqueidentifier' },
167 sql_variant => { data_type => 'sql_variant' },
168 xml => { data_type => 'xml' },
173 CREATE TABLE [mssql_loader_test1.dot] (
174 id INT IDENTITY NOT NULL PRIMARY KEY,
179 CREATE TABLE mssql_loader_test3 (
180 id INT IDENTITY NOT NULL PRIMARY KEY
184 CREATE VIEW mssql_loader_test4 AS
185 SELECT * FROM mssql_loader_test3
187 # test capitalization of cols in unique constraints and rels
188 q{ SET QUOTED_IDENTIFIER ON },
189 q{ SET ANSI_NULLS ON },
191 CREATE TABLE [MSSQL_Loader_Test5] (
192 [Id] INT IDENTITY NOT NULL PRIMARY KEY,
193 [FooCol] INT NOT NULL,
194 [BarCol] INT NOT NULL,
195 UNIQUE ([FooCol], [BarCol])
199 CREATE TABLE [MSSQL_Loader_Test6] (
200 [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
205 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
206 'DROP VIEW mssql_loader_test4',
209 '[mssql_loader_test1.dot]',
210 'mssql_loader_test3',
211 'MSSQL_Loader_Test6',
212 'MSSQL_Loader_Test5',
214 count => 10 + 30 * 2 + 26 * 2, # extra + multi-schema + mutli-db
216 my ($monikers, $classes, $self);
217 ($schema, $monikers, $classes, $self) = @_;
219 my $connect_info = [@$self{qw/dsn user password/}];
221 # Test that the table above (with '.' in name) gets loaded correctly.
223 $schema->resultset('MssqlLoaderTest1Dot') }),
224 'got a resultset for table with dot in name');
226 ok((my $from = eval { $rs->result_source->from }),
227 'got an $rsrc->from for table with dot in name');
229 is ref($from), 'SCALAR', '->table with dot in name is a scalar ref';
231 is eval { $$from }, "[mssql_loader_test1.dot]",
232 '->table with dot in name has correct name';
234 # Test capitalization of columns and unique constraints
235 ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
236 'got result_source');
238 if ($schema->loader->preserve_case) {
239 is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/],
240 'column name case is preserved with case-sensitive collation';
242 my %uniqs = $rsrc->unique_constraints;
243 delete $uniqs{primary};
245 is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/],
246 'column name case is preserved in unique constraint with case-sensitive collation');
249 is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
250 'column names are lowercased for case-insensitive collation';
252 my %uniqs = $rsrc->unique_constraints;
253 delete $uniqs{primary};
255 is_deeply ((values %uniqs)[0], [qw/foocol barcol/],
256 'columns in unique constraint lowercased for case-insensitive collation');
260 my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({});
262 if ($schema->loader->preserve_case) {
263 $five_row->foo_col(1);
264 $five_row->bar_col(2);
267 $five_row->foocol(1);
268 $five_row->barcol(2);
272 my $six_row = $five_row->create_related('mssql_loader_test6s', {});
274 is $six_row->five->id, 1;
275 } 'relationships for mixed-case tables/columns detected';
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");
281 warnings_exist_silent { $schema->rescan }
282 qr/^Bad table or view 'mssql_loader_test4'/, 'bad view ignored';
285 $schema->resultset($monikers->{mssql_loader_test4})
286 } qr/Can't find source/,
287 'no source registered for bad view';
290 my $dbh = $schema->storage->dbh;
293 $dbh->do('CREATE SCHEMA [dbicsl-test]');
296 skip "no CREATE SCHEMA privileges", 30 * 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,
310 CONSTRAINT loader_test9_uniq UNIQUE (eight_id),
311 FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
314 $dbh->do('CREATE SCHEMA [dbicsl.test]');
316 CREATE TABLE [dbicsl.test].mssql_loader_test9 (
317 pk INT IDENTITY PRIMARY KEY,
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)
325 CREATE TABLE [dbicsl.test].mssql_loader_test10 (
326 id INT IDENTITY PRIMARY KEY,
328 mssql_loader_test8_id INTEGER,
329 FOREIGN KEY (mssql_loader_test8_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
333 CREATE TABLE [dbicsl.test].mssql_loader_test11 (
334 id INT IDENTITY PRIMARY KEY,
336 ten_id INTEGER NOT NULL UNIQUE,
337 FOREIGN KEY (ten_id) REFERENCES [dbicsl.test].mssql_loader_test10 (id)
341 CREATE TABLE [dbicsl-test].mssql_loader_test12 (
342 id INT IDENTITY PRIMARY KEY,
344 mssql_loader_test11_id INTEGER,
345 FOREIGN KEY (mssql_loader_test11_id) REFERENCES [dbicsl.test].mssql_loader_test11 (id)
349 my $guard = Scope::Guard->new(\&cleanup_schemas);
351 foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
353 rmtree EXTRA_DUMP_DIR;
356 local $SIG{__WARN__} = sub {
357 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
364 db_schema => $db_schema,
365 moniker_parts => [qw/schema name/],
366 dump_directory => EXTRA_DUMP_DIR,
372 diag join "\n", @warns if @warns;
375 } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
377 my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
380 ok $test_schema = MSSQLMultiSchema->connect(@$connect_info);
381 } 'connected test schema';
384 ok $rsrc = $test_schema->source('DbicslDashTestMssqlLoaderTest8');
385 } 'got source for table in schema name with dash';
387 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
388 'column in schema name with dash';
390 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
391 'column in schema name with dash';
393 is try { $rsrc->column_info('value')->{size} }, 100,
394 'column in schema name with dash';
397 ok $rs = $test_schema->resultset('DbicslDashTestMssqlLoaderTest8');
398 } 'got resultset for table in schema name with dash';
401 ok $row = $rs->create({ value => 'foo' });
402 } 'executed SQL on table in schema name with dash';
404 $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_mssql_loader_test9') };
406 is_deeply $rel_info->{cond}, {
407 'foreign.eight_id' => 'self.id'
408 }, 'relationship in schema name with dash';
410 is $rel_info->{attrs}{accessor}, 'single',
411 'relationship in schema name with dash';
413 is $rel_info->{attrs}{join_type}, 'LEFT',
414 'relationship in schema name with dash';
417 ok $rsrc = $test_schema->source('DbicslDashTestMssqlLoaderTest9');
418 } 'got source for table in schema name with dash';
420 %uniqs = try { $rsrc->unique_constraints };
423 'got unique and primary constraint in schema name with dash';
425 delete $uniqs{primary};
427 is_deeply ((values %uniqs)[0], ['eight_id'],
428 'correct unique constraint in schema name with dash');
431 ok $rsrc = $test_schema->source('DbicslDotTestMssqlLoaderTest10');
432 } 'got source for table in schema name with dot';
434 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
435 'column in schema name with dot introspected correctly';
437 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
438 'column in schema name with dot introspected correctly';
440 is try { $rsrc->column_info('value')->{size} }, 100,
441 'column in schema name with dot introspected correctly';
444 ok $rs = $test_schema->resultset('DbicslDotTestMssqlLoaderTest10');
445 } 'got resultset for table in schema name with dot';
448 ok $row = $rs->create({ value => 'foo' });
449 } 'executed SQL on table in schema name with dot';
451 $rel_info = try { $rsrc->relationship_info('mssql_loader_test11') };
453 is_deeply $rel_info->{cond}, {
454 'foreign.ten_id' => 'self.id'
455 }, 'relationship in schema name with dot';
457 is $rel_info->{attrs}{accessor}, 'single',
458 'relationship in schema name with dot';
460 is $rel_info->{attrs}{join_type}, 'LEFT',
461 'relationship in schema name with dot';
464 ok $rsrc = $test_schema->source('DbicslDotTestMssqlLoaderTest11');
465 } 'got source for table in schema name with dot';
467 %uniqs = try { $rsrc->unique_constraints };
470 'got unique and primary constraint in schema name with dot';
472 delete $uniqs{primary};
474 is_deeply ((values %uniqs)[0], ['ten_id'],
475 'correct unique constraint in schema name with dot');
478 ok $test_schema->source('DbicslDotTestMssqlLoaderTest10')
479 ->has_relationship('mssql_loader_test8');
480 } 'cross-schema relationship in multi-db_schema';
483 ok $test_schema->source('DbicslDashTestMssqlLoaderTest8')
484 ->has_relationship('mssql_loader_test10s');
485 } 'cross-schema relationship in multi-db_schema';
488 ok $test_schema->source('DbicslDashTestMssqlLoaderTest12')
489 ->has_relationship('mssql_loader_test11');
490 } 'cross-schema relationship in multi-db_schema';
493 ok $test_schema->source('DbicslDotTestMssqlLoaderTest11')
494 ->has_relationship('mssql_loader_test12s');
495 } 'cross-schema relationship in multi-db_schema';
500 my $dbh = $schema->storage->dbh;
503 $dbh->do('USE master');
504 $dbh->do('CREATE DATABASE dbicsl_test1');
507 skip "no CREATE DATABASE privileges", 26 * 2;
510 $dbh->do('CREATE DATABASE dbicsl_test2');
512 $dbh->do('USE dbicsl_test1');
515 CREATE TABLE mssql_loader_test13 (
516 id INT IDENTITY PRIMARY KEY,
521 CREATE TABLE mssql_loader_test14 (
522 id INT IDENTITY PRIMARY KEY,
524 thirteen_id INTEGER REFERENCES mssql_loader_test13 (id),
525 CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
529 $dbh->do('USE dbicsl_test2');
532 CREATE TABLE mssql_loader_test14 (
533 pk INT IDENTITY PRIMARY KEY,
536 CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
541 CREATE TABLE mssql_loader_test15 (
542 id INT IDENTITY PRIMARY KEY,
547 CREATE TABLE mssql_loader_test16 (
548 id INT IDENTITY PRIMARY KEY,
550 fifteen_id INTEGER UNIQUE REFERENCES mssql_loader_test15 (id)
554 my $guard = Scope::Guard->new(\&cleanup_databases);
556 foreach my $db_schema ({ dbicsl_test1 => '%', dbicsl_test2 => '%' }, { '%' => '%' }) {
559 local $SIG{__WARN__} = sub {
560 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
564 'MSSQLMultiDatabase',
567 db_schema => $db_schema,
568 moniker_parts => [qw/database name/],
569 dump_directory => EXTRA_DUMP_DIR,
575 diag join "\n", @warns if @warns;
578 } "dumped schema for databases 'dbicsl_test1' and 'dbicsl_test2' with no warnings";
583 ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info);
584 } 'connected test schema';
586 my ($rsrc, $rs, $row, $rel_info, %uniqs);
589 ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest13');
590 } 'got source for table in database one';
592 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
593 'column in database one';
595 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
596 'column in database one';
598 is try { $rsrc->column_info('value')->{size} }, 100,
599 'column in database one';
602 ok $rs = $test_schema->resultset('DbicslTest1MssqlLoaderTest13');
603 } 'got resultset for table in database one';
606 ok $row = $rs->create({ value => 'foo' });
607 } 'executed SQL on table in database one';
609 $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') };
611 is_deeply $rel_info->{cond}, {
612 'foreign.thirteen_id' => 'self.id'
613 }, 'relationship in database one';
615 is $rel_info->{attrs}{accessor}, 'single',
616 'relationship in database one';
618 is $rel_info->{attrs}{join_type}, 'LEFT',
619 'relationship in database one';
622 ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest14');
623 } 'got source for table in database one';
625 %uniqs = try { $rsrc->unique_constraints };
628 'got unique and primary constraint in database one';
630 delete $uniqs{primary};
632 is_deeply ((values %uniqs)[0], ['thirteen_id'],
633 'correct unique constraint in database one');
636 ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest15');
637 } 'got source for table in database two';
639 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
640 'column in database two introspected correctly';
642 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
643 'column in database two introspected correctly';
645 is try { $rsrc->column_info('value')->{size} }, 100,
646 'column in database two introspected correctly';
649 ok $rs = $test_schema->resultset('DbicslTest2MssqlLoaderTest15');
650 } 'got resultset for table in database two';
653 ok $row = $rs->create({ value => 'foo' });
654 } 'executed SQL on table in database two';
656 $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') };
658 is_deeply $rel_info->{cond}, {
659 'foreign.fifteen_id' => 'self.id'
660 }, 'relationship in database two';
662 is $rel_info->{attrs}{accessor}, 'single',
663 'relationship in database two';
665 is $rel_info->{attrs}{join_type}, 'LEFT',
666 'relationship in database two';
669 ok $rsrc = $test_schema->source('DbicslTest2MssqlLoaderTest16');
670 } 'got source for table in database two';
672 %uniqs = try { $rsrc->unique_constraints };
675 'got unique and primary constraint in database two';
677 delete $uniqs{primary};
679 is_deeply ((values %uniqs)[0], ['fifteen_id'],
680 'correct unique constraint in database two');
687 $tester->run_tests();
689 sub cleanup_schemas {
690 return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
692 # switch back to default database
693 $schema->storage->disconnect;
694 my $dbh = $schema->storage->dbh;
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') {
703 $dbh->do("DROP TABLE $table");
706 diag "Error dropping table: $_";
710 foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
712 $dbh->do(qq{DROP SCHEMA [$db_schema]});
715 diag "Error dropping test schema $db_schema: $_";
719 rmtree EXTRA_DUMP_DIR;
722 sub cleanup_databases {
723 return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
725 my $dbh = $schema->storage->dbh;
727 $dbh->do('USE dbicsl_test1');
729 foreach my $table ('mssql_loader_test14',
730 'mssql_loader_test13') {
732 $dbh->do("DROP TABLE $table");
735 diag "Error dropping table: $_";
739 $dbh->do('USE dbicsl_test2');
741 foreach my $table ('mssql_loader_test16',
742 'mssql_loader_test15',
743 'mssql_loader_test14') {
745 $dbh->do("DROP TABLE $table");
748 diag "Error dropping table: $_";
752 $dbh->do('USE master');
754 foreach my $database (qw/dbicsl_test1 dbicsl_test2/) {
756 $dbh->do(qq{DROP DATABASE $database});
759 diag "Error dropping test database '$database': $_";
763 rmtree EXTRA_DUMP_DIR;
765 # vim:et sts=4 sw=4 tw=0: