multi db_schema support
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_04db2_common.t
index e5d5a0e..03b4b54 100644 (file)
@@ -1,6 +1,17 @@
 use strict;
-
+use warnings;
 use Test::More;
+use Test::Exception;
+use Try::Tiny;
+use File::Path 'rmtree';
+use DBIx::Class::Schema::Loader 'make_schema_at';
+
+use lib qw(t/lib);
+
+use dbixcsl_common_tests ();
+use dbixcsl_test_dir '$tdir';
+
+use constant EXTRA_DUMP_DIR => "$tdir/db2_extra_dump";
 
 my $dsn      = $ENV{DBICTEST_DB2_DSN} || '';
 my $user     = $ENV{DBICTEST_DB2_USER} || '';
@@ -9,6 +20,8 @@ my $password = $ENV{DBICTEST_DB2_PASS} || '';
 plan skip_all => 'You need to set the DBICTEST_DB2_DSN, _USER, and _PASS environment variables'
     unless ($dsn && $user);
 
+my ($schema, $schemas_created); # for cleanup in END for extra tests
+
 my $srv_ver = do {
     require DBI;
     my $dbh = DBI->connect ($dsn, $user, $password, { RaiseError => 1, PrintError => 0} );
@@ -16,9 +29,6 @@ my $srv_ver = do {
 };
 my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/;
 
-use lib qw(t/lib);
-use dbixcsl_common_tests;
-
 my $extra_graphics_data_types = {
     graphic            => { data_type => 'graphic', size => 1 },
     'graphic(3)'       => { data_type => 'graphic', size => 3 },
@@ -93,8 +103,233 @@ my $tester = dbixcsl_common_tests->new(
         # XXX I don't know how to make these
 #        datalink           => { data_type => 'datalink' },
     },
+    extra => {
+        count => 28 * 2,
+        run => sub {
+            SKIP: {
+                $schema = shift;
+
+                my $dbh = $schema->storage->dbh;
+
+                try {
+                    $dbh->do('CREATE SCHEMA "dbicsl-test"');
+                }
+                catch {
+                    $schemas_created = 0;
+                    skip "no CREATE SCHEMA privileges", 28 * 2;
+                };
+
+                $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl-test".db2_loader_test4 (
+                        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100)
+                    )
+EOF
+                $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl-test".db2_loader_test5 (
+                        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100),
+                        four_id INTEGER NOT NULL UNIQUE,
+                        FOREIGN KEY (four_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
+                    )
+EOF
+                $dbh->do('CREATE SCHEMA "dbicsl.test"');
+                $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl.test".db2_loader_test6 (
+                        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100),
+                        db2_loader_test4_id INTEGER,
+                        FOREIGN KEY (db2_loader_test4_id) REFERENCES "dbicsl-test".db2_loader_test4 (id)
+                    )
+EOF
+                $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl.test".db2_loader_test7 (
+                        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100),
+                        six_id INTEGER NOT NULL UNIQUE,
+                        FOREIGN KEY (six_id) REFERENCES "dbicsl.test".db2_loader_test6 (id)
+                    )
+EOF
+                $dbh->do(<<"EOF");
+                    CREATE TABLE "dbicsl-test".db2_loader_test8 (
+                        id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
+                        value VARCHAR(100),
+                        db2_loader_test7_id INTEGER,
+                        FOREIGN KEY (db2_loader_test7_id) REFERENCES "dbicsl.test".db2_loader_test7 (id)
+                    )
+EOF
+
+                $schemas_created = 1;
+
+                foreach my $db_schema (['dbicsl-test', 'dbicsl.test'], '%') {
+                    lives_and {
+                        rmtree EXTRA_DUMP_DIR;
+
+                        my @warns;
+                        local $SIG{__WARN__} = sub {
+                            push @warns, $_[0] unless $_[0] =~ /\bcollides\b/;
+                        };
+
+                        make_schema_at(
+                            'DB2MultiSchema',
+                            {
+                                naming => 'current',
+                                db_schema => $db_schema,
+                                dump_directory => EXTRA_DUMP_DIR,
+                                quiet => 1,
+                            },
+                            [ $dsn, $user, $password ],
+                        );
+
+                        diag join "\n", @warns if @warns;
+
+                        is @warns, 0;
+                    } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
+
+                    my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
+
+                    lives_and {
+                        ok $test_schema = DB2MultiSchema->connect($dsn, $user, $password);
+                    } 'connected test schema';
+
+                    lives_and {
+                        ok $rsrc = $test_schema->source('Db2LoaderTest4');
+                    } 'got source for table in schema name with dash';
+
+                    is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+                        'column in schema name with dash';
+
+                    is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+                        'column in schema name with dash';
+
+                    is try { $rsrc->column_info('value')->{size} }, 100,
+                        'column in schema name with dash';
+
+                    lives_and {
+                        ok $rs = $test_schema->resultset('Db2LoaderTest4');
+                    } 'got resultset for table in schema name with dash';
+
+                    lives_and {
+                        ok $row = $rs->create({ value => 'foo' });
+                    } 'executed SQL on table in schema name with dash';
+
+                    $rel_info = try { $rsrc->relationship_info('db2_loader_test5') };
+
+                    is_deeply $rel_info->{cond}, {
+                        'foreign.four_id' => 'self.id'
+                    }, 'relationship in schema name with dash';
+
+                    is $rel_info->{attrs}{accessor}, 'single',
+                        'relationship in schema name with dash';
+
+                    is $rel_info->{attrs}{join_type}, 'LEFT',
+                        'relationship in schema name with dash';
+
+                    lives_and {
+                        ok $rsrc = $test_schema->source('Db2LoaderTest5');
+                    } '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';
+
+                    lives_and {
+                        ok $rsrc = $test_schema->source('Db2LoaderTest6');
+                    } 'got source for table in schema name with dot';
+
+                    is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+                        'column in schema name with dot introspected correctly';
+
+                    is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+                        'column in schema name with dot introspected correctly';
+
+                    is try { $rsrc->column_info('value')->{size} }, 100,
+                        'column in schema name with dot introspected correctly';
+
+                    lives_and {
+                        ok $rs = $test_schema->resultset('Db2LoaderTest6');
+                    } 'got resultset for table in schema name with dot';
+
+                    lives_and {
+                        ok $row = $rs->create({ value => 'foo' });
+                    } 'executed SQL on table in schema name with dot';
+
+                    $rel_info = try { $rsrc->relationship_info('db2_loader_test7') };
+
+                    is_deeply $rel_info->{cond}, {
+                        'foreign.six_id' => 'self.id'
+                    }, 'relationship in schema name with dot';
+
+                    is $rel_info->{attrs}{accessor}, 'single',
+                        'relationship in schema name with dot';
+
+                    is $rel_info->{attrs}{join_type}, 'LEFT',
+                        'relationship in schema name with dot';
+
+                    lives_and {
+                        ok $rsrc = $test_schema->source('Db2LoaderTest7');
+                    } 'got source for table in schema name with dot';
+
+                    %uniqs = try { $rsrc->unique_constraints };
+
+                    is keys %uniqs, 2,
+                        'got unique and primary constraint in schema name with dot';
+
+                    lives_and {
+                        ok $test_schema->source('Db2LoaderTest6')
+                            ->has_relationship('db2_loader_test4');
+                    } 'cross-schema relationship in multi-db_schema';
+
+                    lives_and {
+                        ok $test_schema->source('Db2LoaderTest4')
+                            ->has_relationship('db2_loader_test6s');
+                    } 'cross-schema relationship in multi-db_schema';
+
+                    lives_and {
+                        ok $test_schema->source('Db2LoaderTest8')
+                            ->has_relationship('db2_loader_test7');
+                    } 'cross-schema relationship in multi-db_schema';
+
+                    lives_and {
+                        ok $test_schema->source('Db2LoaderTest7')
+                            ->has_relationship('db2_loader_test8s');
+                    } 'cross-schema relationship in multi-db_schema';
+                }
+            }
+
+        },
+    },
 );
 
 $tester->run_tests();
 
+END {
+    if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
+        if ($schemas_created && (my $dbh = try { $schema->storage->dbh })) {
+            foreach my $table ('"dbicsl-test".db2_loader_test8',
+                               '"dbicsl.test".db2_loader_test7',
+                               '"dbicsl.test".db2_loader_test6',
+                               '"dbicsl-test".db2_loader_test5',
+                               '"dbicsl-test".db2_loader_test4') {
+                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" RESTRICT});
+                }
+                catch {
+                    diag "Error dropping test schema $db_schema: $_";
+                };
+            }
+        }
+        rmtree EXTRA_DUMP_DIR;
+    }
+}
 # vim:et sts=4 sw=4 tw=0: