use warnings;
use Test::More;
use Test::Exception;
-use DBIx::Class::Schema::Loader::Utils 'warnings_exist_silent';
+use DBIx::Class::Optional::Dependencies;
+use DBIx::Class::Schema::Loader::Utils qw/warnings_exist_silent sigwarn_silencer/;
use Try::Tiny;
use File::Path 'rmtree';
use DBIx::Class::Schema::Loader 'make_schema_at';
use namespace::clean;
+use Scope::Guard ();
# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
BEGIN {
use constant EXTRA_DUMP_DIR => "$tdir/mssql_extra_dump";
-# for cleanup in END for extra tests
-my ($schema, $schemas_created, $databases_created);
+# for extra tests cleanup
+my $schema;
-my ($dsns, $common_version);
+my (%dsns, $common_version);
for (qw/MSSQL MSSQL_ODBC MSSQL_ADO/) {
next unless $ENV{"DBICTEST_${_}_DSN"};
- $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
- $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
- $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
+ (my $dep_group = lc "rdbms_$_") =~ s/mssql$/mssql_sybase/;
+ if (!DBIx::Class::Optional::Dependencies->req_ok_for($dep_group)) {
+ diag 'You need to install ' . DBIx::Class::Optional::Dependencies->req_missing_for($dep_group)
+ . " to test with $_";
+ next;
+ }
+
+ $dsns{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"};
+ $dsns{$_}{user} = $ENV{"DBICTEST_${_}_USER"};
+ $dsns{$_}{password} = $ENV{"DBICTEST_${_}_PASS"};
require DBI;
- my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
+ my $dbh = DBI->connect (@{$dsns{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} );
my $srv_ver = eval {
$dbh->get_info(18)
||
}
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'
- unless $dsns;
+ unless %dsns;
my $mssql_2008_new_data_types = {
date => { data_type => 'date' },
vendor => 'mssql',
auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY',
default_function_def => 'DATETIME DEFAULT getdate()',
- connect_info => [values %$dsns],
+ connect_info => [ map { $dsns{$_} } sort keys %dsns ],
preserve_case_mode_is_exclusive => 1,
quote_char => [ qw/[ ]/ ],
basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME',
+ default_on_clause => 'NO ACTION',
data_types => {
# http://msdn.microsoft.com/en-us/library/ms187752.aspx
[Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id])
)
},
+ # 8 through 12 are used for the multi-schema tests and 13 through 16 are used for multi-db tests
+ q{
+ create table mssql_loader_test17 (
+ id int identity primary key
+ )
+ },
+ q{
+ create table mssql_loader_test18 (
+ id int identity primary key,
+ seventeen_id int,
+ foreign key (seventeen_id) references mssql_loader_test17(id)
+ on delete set default on update set null
+ )
+ },
],
pre_drop_ddl => [
'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)',
'mssql_loader_test3',
'MSSQL_Loader_Test6',
'MSSQL_Loader_Test5',
+ 'mssql_loader_test17',
+ 'mssql_loader_test18',
],
- count => 10 + 28 * 2 + 24,
+ count => 14 + 30 * 2 + 26 * 2, # extra + multi-schema + mutli-db
run => sub {
my ($monikers, $classes, $self);
($schema, $monikers, $classes, $self) = @_;
} qr/Can't find source/,
'no source registered for bad view';
+ # test on delete/update fk clause introspection
+ ok ((my $rel_info = $schema->source('MssqlLoaderTest18')->relationship_info('seventeen')),
+ 'got rel info');
+
+ is $rel_info->{attrs}{on_delete}, 'SET DEFAULT',
+ 'ON DELETE clause introspected correctly';
+
+ is $rel_info->{attrs}{on_update}, 'SET NULL',
+ 'ON UPDATE clause introspected correctly';
+
+ is $rel_info->{attrs}{is_deferrable}, 1,
+ 'is_deferrable defaults to 1';
+
SKIP: {
my $dbh = $schema->storage->dbh;
try {
- $dbh->do('CREATE SCHEMA "dbicsl-test"');
+ $dbh->do('CREATE SCHEMA [dbicsl-test]');
}
catch {
- $schemas_created = 0;
- skip "no CREATE SCHEMA privileges", 28 * 2;
+ skip "no CREATE SCHEMA privileges", 30 * 2;
};
$dbh->do(<<"EOF");
CREATE TABLE [dbicsl-test].mssql_loader_test9 (
id INT IDENTITY PRIMARY KEY,
value VARCHAR(100),
- eight_id INTEGER NOT NULL UNIQUE,
+ eight_id INTEGER NOT NULL,
+ CONSTRAINT loader_test9_uniq UNIQUE (eight_id),
FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
)
EOF
$dbh->do('CREATE SCHEMA [dbicsl.test]');
$dbh->do(<<"EOF");
+ CREATE TABLE [dbicsl.test].mssql_loader_test9 (
+ pk INT IDENTITY PRIMARY KEY,
+ value VARCHAR(100),
+ eight_id INTEGER NOT NULL,
+ CONSTRAINT loader_test9_uniq UNIQUE (eight_id),
+ FOREIGN KEY (eight_id) REFERENCES [dbicsl-test].mssql_loader_test8 (id)
+ )
+EOF
+ $dbh->do(<<"EOF");
CREATE TABLE [dbicsl.test].mssql_loader_test10 (
id INT IDENTITY PRIMARY KEY,
value VARCHAR(100),
)
EOF
- $schemas_created = 1;
+ my $guard = Scope::Guard->new(\&cleanup_schemas);
foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
lives_and {
ok $row = $rs->create({ value => 'foo' });
} 'executed SQL on table in schema name with dash';
- $rel_info = try { $rsrc->relationship_info('mssql_loader_test9') };
+ $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_mssql_loader_test9') };
is_deeply $rel_info->{cond}, {
'foreign.eight_id' => 'self.id'
'relationship in schema name with dash';
lives_and {
- ok $rsrc = $test_schema->source('MssqlLoaderTest9');
+ ok $rsrc = $test_schema->source('DbicslDashTestMssqlLoaderTest9');
} 'got source for table in schema name with dash';
%uniqs = try { $rsrc->unique_constraints };
is keys %uniqs, 2,
'got unique and primary constraint in schema name with dash';
+ delete $uniqs{primary};
+
+ is_deeply ((values %uniqs)[0], ['eight_id'],
+ 'correct unique constraint in schema name with dash');
+
lives_and {
ok $rsrc = $test_schema->source('MssqlLoaderTest10');
} 'got source for table in schema name with dot';
is keys %uniqs, 2,
'got unique and primary constraint in schema name with dot';
+ delete $uniqs{primary};
+
+ is_deeply ((values %uniqs)[0], ['ten_id'],
+ 'correct unique constraint in schema name with dot');
+
lives_and {
ok $test_schema->source('MssqlLoaderTest10')
->has_relationship('mssql_loader_test8');
}
SKIP: {
+ # for ADO
+ local $SIG{__WARN__} = sigwarn_silencer(
+ qr/Changed database context/
+ );
+
my $dbh = $schema->storage->dbh;
try {
$dbh->do('CREATE DATABASE dbicsl_test1');
}
catch {
- skip "no CREATE DATABASE privileges", 24;
+ diag "no CREATE DATABASE privileges: '$_'";
+ skip "no CREATE DATABASE privileges", 26 * 2;
};
$dbh->do('CREATE DATABASE dbicsl_test2');
CREATE TABLE mssql_loader_test14 (
id INT IDENTITY PRIMARY KEY,
value VARCHAR(100),
- thirteen_id INTEGER UNIQUE REFERENCES mssql_loader_test13 (id)
+ thirteen_id INTEGER REFERENCES mssql_loader_test13 (id),
+ CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
)
EOF
- $dbh->do('USE master');
$dbh->do('USE dbicsl_test2');
+ $dbh->do(<<'EOF');
+ CREATE TABLE mssql_loader_test14 (
+ pk INT IDENTITY PRIMARY KEY,
+ value VARCHAR(100),
+ thirteen_id INTEGER,
+ CONSTRAINT loader_test14_uniq UNIQUE (thirteen_id)
+ )
+EOF
+
$dbh->do(<<"EOF");
CREATE TABLE mssql_loader_test15 (
id INT IDENTITY PRIMARY KEY,
)
EOF
- $databases_created = 1;
+ my $guard = Scope::Guard->new(\&cleanup_databases);
- lives_and {
- my @warns;
- local $SIG{__WARN__} = sub {
- push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
- };
-
- make_schema_at(
- 'MSSQLMultiDatabase',
- {
- naming => 'current',
- db_schema => { '%' => '%' },
- dump_directory => EXTRA_DUMP_DIR,
- quiet => 1,
- },
- $connect_info,
- );
+ foreach my $db_schema ({ dbicsl_test1 => '%', dbicsl_test2 => '%' }, { '%' => '%' }) {
+ lives_and {
+ my @warns;
+ local $SIG{__WARN__} = sub {
+ push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
+ };
+
+ make_schema_at(
+ 'MSSQLMultiDatabase',
+ {
+ naming => 'current',
+ db_schema => $db_schema,
+ dump_directory => EXTRA_DUMP_DIR,
+ quiet => 1,
+ },
+ $connect_info,
+ );
- diag join "\n", @warns if @warns;
+ diag join "\n", @warns if @warns;
- is @warns, 0;
- } 'dumped schema for all databases with no warnings';
+ is @warns, 0;
+ } "dumped schema for databases 'dbicsl_test1' and 'dbicsl_test2' with no warnings";
- my $test_schema;
+ my $test_schema;
- lives_and {
- ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info);
- } 'connected test schema';
+ lives_and {
+ ok $test_schema = MSSQLMultiDatabase->connect(@$connect_info);
+ } 'connected test schema';
- my ($rsrc, $rs, $row, $rel_info, %uniqs);
+ my ($rsrc, $rs, $row, $rel_info, %uniqs);
- lives_and {
- ok $rsrc = $test_schema->source('MssqlLoaderTest13');
- } 'got source for table in database one';
+ lives_and {
+ ok $rsrc = $test_schema->source('MssqlLoaderTest13');
+ } 'got source for table in database one';
- is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
- 'column in database one';
+ is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+ 'column in database one';
- is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
- 'column in database one';
+ is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+ 'column in database one';
- is try { $rsrc->column_info('value')->{size} }, 100,
- 'column in database one';
+ is try { $rsrc->column_info('value')->{size} }, 100,
+ 'column in database one';
- lives_and {
- ok $rs = $test_schema->resultset('MssqlLoaderTest13');
- } 'got resultset for table in database one';
+ lives_and {
+ ok $rs = $test_schema->resultset('MssqlLoaderTest13');
+ } 'got resultset for table in database one';
- lives_and {
- ok $row = $rs->create({ value => 'foo' });
- } 'executed SQL on table in database one';
+ lives_and {
+ ok $row = $rs->create({ value => 'foo' });
+ } 'executed SQL on table in database one';
- $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') };
+ $rel_info = try { $rsrc->relationship_info('mssql_loader_test14') };
- is_deeply $rel_info->{cond}, {
- 'foreign.thirteen_id' => 'self.id'
- }, 'relationship in database one';
+ is_deeply $rel_info->{cond}, {
+ 'foreign.thirteen_id' => 'self.id'
+ }, 'relationship in database one';
- is $rel_info->{attrs}{accessor}, 'single',
- 'relationship in database one';
+ is $rel_info->{attrs}{accessor}, 'single',
+ 'relationship in database one';
- is $rel_info->{attrs}{join_type}, 'LEFT',
- 'relationship in database one';
+ is $rel_info->{attrs}{join_type}, 'LEFT',
+ 'relationship in database one';
- lives_and {
- ok $rsrc = $test_schema->source('MssqlLoaderTest14');
- } 'got source for table in database one';
+ lives_and {
+ ok $rsrc = $test_schema->source('DbicslTest1MssqlLoaderTest14');
+ } 'got source for table in database one';
- %uniqs = try { $rsrc->unique_constraints };
+ %uniqs = try { $rsrc->unique_constraints };
- is keys %uniqs, 2,
- 'got unique and primary constraint in database one';
+ is keys %uniqs, 2,
+ 'got unique and primary constraint in database one';
- lives_and {
- ok $rsrc = $test_schema->source('MssqlLoaderTest15');
- } 'got source for table in database two';
+ delete $uniqs{primary};
- is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
- 'column in database two introspected correctly';
+ is_deeply ((values %uniqs)[0], ['thirteen_id'],
+ 'correct unique constraint in database one');
- is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
- 'column in database two introspected correctly';
+ lives_and {
+ ok $rsrc = $test_schema->source('MssqlLoaderTest15');
+ } 'got source for table in database two';
- is try { $rsrc->column_info('value')->{size} }, 100,
- 'column in database two introspected correctly';
+ is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+ 'column in database two introspected correctly';
- lives_and {
- ok $rs = $test_schema->resultset('MssqlLoaderTest15');
- } 'got resultset for table in database two';
+ is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+ 'column in database two introspected correctly';
- lives_and {
- ok $row = $rs->create({ value => 'foo' });
- } 'executed SQL on table in database two';
+ is try { $rsrc->column_info('value')->{size} }, 100,
+ 'column in database two introspected correctly';
- $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') };
+ lives_and {
+ ok $rs = $test_schema->resultset('MssqlLoaderTest15');
+ } 'got resultset for table in database two';
+
+ lives_and {
+ ok $row = $rs->create({ value => 'foo' });
+ } 'executed SQL on table in database two';
+
+ $rel_info = try { $rsrc->relationship_info('mssql_loader_test16') };
+
+ is_deeply $rel_info->{cond}, {
+ 'foreign.fifteen_id' => 'self.id'
+ }, 'relationship in database two';
- is_deeply $rel_info->{cond}, {
- 'foreign.fifteen_id' => 'self.id'
- }, 'relationship in database two';
+ is $rel_info->{attrs}{accessor}, 'single',
+ 'relationship in database two';
- is $rel_info->{attrs}{accessor}, 'single',
- 'relationship in database two';
+ is $rel_info->{attrs}{join_type}, 'LEFT',
+ 'relationship in database two';
- is $rel_info->{attrs}{join_type}, 'LEFT',
- 'relationship in database two';
+ lives_and {
+ ok $rsrc = $test_schema->source('MssqlLoaderTest16');
+ } 'got source for table in database two';
- lives_and {
- ok $rsrc = $test_schema->source('MssqlLoaderTest16');
- } 'got source for table in database two';
+ %uniqs = try { $rsrc->unique_constraints };
- %uniqs = try { $rsrc->unique_constraints };
+ is keys %uniqs, 2,
+ 'got unique and primary constraint in database two';
- is keys %uniqs, 2,
- 'got unique and primary constraint in database two';
+ delete $uniqs{primary};
+
+ is_deeply ((values %uniqs)[0], ['fifteen_id'],
+ 'correct unique constraint in database two');
+ }
}
},
},
$tester->run_tests();
-END {
- if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
- if ($schema) {
- # switch back to default database
- $schema->storage->disconnect;
- my $dbh = $schema->storage->dbh;
+sub cleanup_schemas {
+ return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
+
+ # switch back to default database
+ $schema->storage->disconnect;
+ my $dbh = $schema->storage->dbh;
+
+ foreach my $table ('[dbicsl-test].mssql_loader_test12',
+ '[dbicsl.test].mssql_loader_test11',
+ '[dbicsl.test].mssql_loader_test10',
+ '[dbicsl.test].mssql_loader_test9',
+ '[dbicsl-test].mssql_loader_test9',
+ '[dbicsl-test].mssql_loader_test8') {
+ try {
+ $dbh->do("DROP TABLE $table");
+ }
+ catch {
+ diag "Error dropping table: $_";
+ };
+ }
- if ($schemas_created) {
- foreach my $table ('[dbicsl-test].mssql_loader_test12',
- '[dbicsl.test].mssql_loader_test11',
- '[dbicsl.test].mssql_loader_test10',
- '[dbicsl-test].mssql_loader_test9',
- '[dbicsl-test].mssql_loader_test8') {
- try {
- $dbh->do("DROP TABLE $table");
- }
- catch {
- diag "Error dropping table: $_";
- };
- }
+ foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
+ try {
+ $dbh->do(qq{DROP SCHEMA [$db_schema]});
+ }
+ catch {
+ diag "Error dropping test schema $db_schema: $_";
+ };
+ }
- foreach my $db_schema (qw/dbicsl-test dbicsl.test/) {
- try {
- $dbh->do(qq{DROP SCHEMA [$db_schema]});
- }
- catch {
- diag "Error dropping test schema $db_schema: $_";
- };
- }
- }
+ rmtree EXTRA_DUMP_DIR;
+}
- if ($databases_created) {
- $dbh->do('USE dbicsl_test1');
+sub cleanup_databases {
+ return if $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
- foreach my $table ('mssql_loader_test14',
- 'mssql_loader_test13') {
- try {
- $dbh->do("DROP TABLE $table");
- }
- catch {
- diag "Error dropping table: $_";
- };
- }
+ # for ADO
+ local $SIG{__WARN__} = sigwarn_silencer(
+ qr/Changed database context/
+ );
- $dbh->do('USE dbicsl_test2');
+ my $dbh = $schema->storage->dbh;
- foreach my $table ('mssql_loader_test16',
- 'mssql_loader_test15') {
- try {
- $dbh->do("DROP TABLE $table");
- }
- catch {
- diag "Error dropping table: $_";
- };
- }
+ $dbh->do('USE dbicsl_test1');
- $dbh->do('USE master');
+ foreach my $table ('mssql_loader_test14',
+ 'mssql_loader_test13') {
+ try {
+ $dbh->do("DROP TABLE $table");
+ }
+ catch {
+ diag "Error dropping table: $_";
+ };
+ }
- foreach my $database (qw/dbicsl_test1 dbicsl_test2/) {
- try {
- $dbh->do(qq{DROP DATABASE $database});
- }
- catch {
- diag "Error dropping test database '$database': $_";
- };
- }
- }
+ $dbh->do('USE dbicsl_test2');
+
+ foreach my $table ('mssql_loader_test16',
+ 'mssql_loader_test15',
+ 'mssql_loader_test14') {
+ try {
+ $dbh->do("DROP TABLE $table");
+ }
+ catch {
+ diag "Error dropping table: $_";
+ };
+ }
+
+ $dbh->do('USE master');
- rmtree EXTRA_DUMP_DIR;
+ foreach my $database (qw/dbicsl_test1 dbicsl_test2/) {
+ try {
+ $dbh->do(qq{DROP DATABASE $database});
}
+ catch {
+ diag "Error dropping test database '$database': $_";
+ };
}
+
+ rmtree EXTRA_DUMP_DIR;
}
# vim:et sts=4 sw=4 tw=0: