minor optimization for Pg FK attrs, better test
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_03pg_common.t
index 02f4683..ed1ed93 100644 (file)
@@ -6,9 +6,14 @@ use DBIx::Class::Schema::Loader::Utils qw/no_warnings slurp_file/;
 use Test::More;
 use Test::Exception;
 use Try::Tiny;
+use File::Path 'rmtree';
 use namespace::clean;
+
 use lib qw(t/lib);
 use dbixcsl_common_tests ();
+use dbixcsl_test_dir '$tdir';
+
+use constant EXTRA_DUMP_DIR => "$tdir/pg_extra_dump";
 
 my $dsn      = $ENV{DBICTEST_PG_DSN} || '';
 my $user     = $ENV{DBICTEST_PG_USER} || '';
@@ -26,6 +31,8 @@ my $tester = dbixcsl_common_tests->new(
         on_connect_do  => [ 'SET client_min_messages=WARNING' ],
     },
     quote_char  => '"',
+    default_is_deferrable => 0,
+    default_on_clause => 'NO ACTION',
     data_types  => {
         # http://www.postgresql.org/docs/7.4/interactive/datatype.html
         #
@@ -34,6 +41,12 @@ my $tester = dbixcsl_common_tests->new(
        bool        => { data_type => 'boolean' },
         'bool default false'
                     => { data_type => 'boolean', default_value => \'false' },
+        'bool default true'
+                    => { data_type => 'boolean', default_value => \'true' },
+        'bool default 0::bool'
+                    => { data_type => 'boolean', default_value => \'false' },
+        'bool default 1::bool'
+                    => { data_type => 'boolean', default_value => \'true' },
 
        bigint      => { data_type => 'bigint' },
        int8        => { data_type => 'bigint' },
@@ -89,12 +102,18 @@ my $tester = dbixcsl_common_tests->new(
        'varchar(2)'                     => { data_type => 'varchar', size => 2 },
        'character(2)'                   => { data_type => 'char', size => 2 },
        'char(2)'                        => { data_type => 'char', size => 2 },
+        # check that default null is correctly rewritten
+        'char(3) default null'           => { data_type => 'char', size => 3,
+                                              default_value => \'null' },
        'character'                      => { data_type => 'char', size => 1 },
        'char'                           => { data_type => 'char', size => 1 },
        text                             => { data_type => 'text' },
         # varchar with no size has unlimited size, we rewrite to 'text'
        varchar                          => { data_type => 'text',
                                               original => { data_type => 'varchar' } },
+        # check default null again (to make sure ref is safe)
+        'varchar(3) default null'        => { data_type => 'varchar', size => 3,
+                                              default_value => \'null' },
 
         # Datetime Types
        date                             => { data_type => 'date' },
@@ -164,16 +183,17 @@ my $tester = dbixcsl_common_tests->new(
                 CREATE SCHEMA "dbicsl-test"
             },
             q{
-                CREATE TABLE "dbicsl-test".pg_loader_test3 (
+                CREATE TABLE "dbicsl-test".pg_loader_test4 (
                     id SERIAL PRIMARY KEY,
                     value VARCHAR(100)
                 )
             },
             q{
-                CREATE TABLE "dbicsl-test".pg_loader_test4 (
+                CREATE TABLE "dbicsl-test".pg_loader_test5 (
                     id SERIAL PRIMARY KEY,
                     value VARCHAR(100),
-                    three_id INTEGER UNIQUE REFERENCES "dbicsl-test".pg_loader_test3 (id)
+                    four_id INTEGER REFERENCES "dbicsl-test".pg_loader_test4 (id),
+                    CONSTRAINT loader_test5_uniq UNIQUE (four_id)
                 )
             },
             q{
@@ -181,17 +201,48 @@ my $tester = dbixcsl_common_tests->new(
             },
             q{
                 CREATE TABLE "dbicsl.test".pg_loader_test5 (
-                    id SERIAL PRIMARY KEY,
-                    value VARCHAR(100)
+                    pk SERIAL PRIMARY KEY,
+                    value VARCHAR(100),
+                    four_id INTEGER REFERENCES "dbicsl-test".pg_loader_test4 (id),
+                    CONSTRAINT loader_test5_uniq UNIQUE (four_id)
                 )
             },
             q{
                 CREATE TABLE "dbicsl.test".pg_loader_test6 (
                     id SERIAL PRIMARY KEY,
                     value VARCHAR(100),
-                    five_id INTEGER UNIQUE REFERENCES "dbicsl.test".pg_loader_test5 (id)
+                    pg_loader_test4_id INTEGER REFERENCES "dbicsl-test".pg_loader_test4 (id)
+                )
+            },
+            q{
+                CREATE TABLE "dbicsl.test".pg_loader_test7 (
+                    id SERIAL PRIMARY KEY,
+                    value VARCHAR(100),
+                    six_id INTEGER UNIQUE REFERENCES "dbicsl.test".pg_loader_test6 (id)
+                )
+            },
+            q{
+                CREATE TABLE "dbicsl-test".pg_loader_test8 (
+                    id SERIAL PRIMARY KEY,
+                    value VARCHAR(100),
+                    pg_loader_test7_id INTEGER REFERENCES "dbicsl.test".pg_loader_test7 (id)
+                )
+            },
+            # 4 through 8 are used for the multi-schema tests
+            q{
+                create table pg_loader_test9 (
+                    id bigserial primary key
                 )
             },
+            q{
+                create table pg_loader_test10 (
+                    id bigserial primary key,
+                    eleven_id int,
+                    foreign key (eleven_id) references pg_loader_test9(id)
+                        on delete restrict on update set null deferrable
+                )
+            },
+
         ],
         pre_drop_ddl => [
             'DROP SCHEMA dbicsl_test CASCADE',
@@ -199,8 +250,8 @@ my $tester = dbixcsl_common_tests->new(
             'DROP SCHEMA "dbicsl.test" CASCADE',
             'DROP TYPE pg_loader_test_enum',
         ],
-        drop  => [ qw/ pg_loader_test1 pg_loader_test2 / ],
-        count => 24,
+        drop  => [ qw/pg_loader_test1 pg_loader_test2 pg_loader_test9 pg_loader_test10/ ],
+        count => 8 + 30 * 2,
         run   => sub {
             my ($schema, $monikers, $classes) = @_;
 
@@ -209,7 +260,7 @@ my $tester = dbixcsl_common_tests->new(
                 'qualified sequence detected';
 
             my $class    = $classes->{pg_loader_test1};
-            my $filename = $schema->_loader->get_dump_filename($class);
+            my $filename = $schema->loader->get_dump_filename($class);
 
             my $code = slurp_file $filename;
 
@@ -220,115 +271,176 @@ my $tester = dbixcsl_common_tests->new(
                 'column comment and attrs';
 
             $class    = $classes->{pg_loader_test2};
-            $filename = $schema->_loader->get_dump_filename($class);
+            $filename = $schema->loader->get_dump_filename($class);
 
             $code = slurp_file $filename;
 
             like $code, qr/^=head1 NAME\n\n^$class\n\n=head1 DESCRIPTION\n\n^very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long comment\n\n^=cut\n/m,
                 'long table comment is in DESCRIPTION';
 
-            lives_and {
-                no_warnings {
+            # test on delete/update fk clause introspection
+            ok ((my $rel_info = $schema->source('PgLoaderTest10')->relationship_info('eleven')),
+                'got rel info');
+
+            is $rel_info->{attrs}{on_delete}, 'RESTRICT',
+                '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,
+                'DEFERRABLE clause introspected correctly';
+
+            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(
-                        'PGSchemaWithDash',
+                        'PGMultiSchema',
                         {
                             naming => 'current',
+                            db_schema => $db_schema,
                             preserve_case => 1,
-                            db_schema => 'dbicsl-test'
+                            dump_directory => EXTRA_DUMP_DIR,
+                            quiet => 1,
                         },
                         [ $dsn, $user, $password, {
                             on_connect_do  => [ 'SET client_min_messages=WARNING' ],
                         } ],
                     );
-                };
-            } 'created dynamic schema for "dbicsl-test" with no warnings';
 
-            my ($rsrc, %uniqs, $rel_info);
+                    diag join "\n", @warns if @warns;
 
-            lives_and {
-                ok $rsrc = PGSchemaWithDash->source('PgLoaderTest3');
-            } 'got source for table in schema name with dash';
+                    is @warns, 0;
+                } 'dumped schema for "dbicsl-test" and "dbicsl.test" schemas with no warnings';
 
-            is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
-                'column in schema name with dash';
+                my ($test_schema, $rsrc, $rs, $row, %uniqs, $rel_info);
 
-            is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
-                'column in schema name with dash';
+                lives_and {
+                    ok $test_schema = PGMultiSchema->connect($dsn, $user, $password, {
+                        on_connect_do  => [ 'SET client_min_messages=WARNING' ],
+                    });
+                } 'connected test schema';
 
-            is try { $rsrc->column_info('value')->{size} }, 100,
-                'column in schema name with dash';
+                lives_and {
+                    ok $rsrc = $test_schema->source('PgLoaderTest4');
+                } 'got source for table in schema name with dash';
 
-            $rel_info = try { $rsrc->relationship_info('pg_loader_test4') };
+                is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+                    'column in schema name with dash';
 
-            is_deeply $rel_info->{cond}, {
-                'foreign.three_id' => 'self.id'
-            }, 'relationship in schema name with dash';
+                is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+                    'column in schema name with dash';
 
-            is $rel_info->{attrs}{accessor}, 'single',
-                'relationship in schema name with dash';
+                is try { $rsrc->column_info('value')->{size} }, 100,
+                    'column in schema name with dash';
 
-            is $rel_info->{attrs}{join_type}, 'LEFT',
-                'relationship in schema name with dash';
+                lives_and {
+                    ok $rs = $test_schema->resultset('PgLoaderTest4');
+                } 'got resultset for table in schema name with dash';
 
-            lives_and {
-                ok $rsrc = PGSchemaWithDash->source('PgLoaderTest4');
-            } 'got source for table in schema name with dash';
+                lives_and {
+                    ok $row = $rs->create({ value => 'foo' });
+                } 'executed SQL on table in schema name with dash';
 
-            %uniqs = try { $rsrc->unique_constraints };
+                $rel_info = try { $rsrc->relationship_info('dbicsl_dash_test_pg_loader_test5') };
 
-            is keys %uniqs, 2,
-                'got unique and primary constraint in schema name with dash';
+                is_deeply $rel_info->{cond}, {
+                    'foreign.four_id' => 'self.id'
+                }, 'relationship in schema name with dash';
 
-            lives_and {
-                no_warnings {
-                    make_schema_at(
-                        'PGSchemaWithDot',
-                        {
-                            naming => 'current',
-                            preserve_case => 1,
-                            db_schema => 'dbicsl.test'
-                        },
-                        [ $dsn, $user, $password, {
-                            on_connect_do  => [ 'SET client_min_messages=WARNING' ],
-                        } ],
-                    );
-                };
-            } 'created dynamic schema for "dbicsl.test" with no warnings';
+                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('DbicslDashTestPgLoaderTest5');
+                } 'got source for table in schema name with dash';
 
-            lives_and {
-                ok $rsrc = PGSchemaWithDot->source('PgLoaderTest5');
-            } 'got source for table in schema name with dot';
+                %uniqs = try { $rsrc->unique_constraints };
 
-            is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
-                'column in schema name with dot introspected correctly';
+                is keys %uniqs, 2,
+                    'got unique and primary constraint in schema name with dash';
 
-            is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
-                'column in schema name with dash introspected correctly';
+                delete $uniqs{primary};
 
-            is try { $rsrc->column_info('value')->{size} }, 100,
-                'column in schema name with dash introspected correctly';
+                is_deeply ((values %uniqs)[0], ['four_id'],
+                    'unique constraint is correct in schema name with dash');
 
-            $rel_info = try { $rsrc->relationship_info('pg_loader_test6') };
+                lives_and {
+                    ok $rsrc = $test_schema->source('PgLoaderTest6');
+                } 'got source for table in schema name with dot';
 
-            is_deeply $rel_info->{cond}, {
-                'foreign.five_id' => 'self.id'
-            }, 'relationship in schema name with dot';
+                is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
+                    'column in schema name with dot introspected correctly';
 
-            is $rel_info->{attrs}{accessor}, 'single',
-                'relationship in schema name with dot';
+                is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
+                    'column in schema name with dot introspected correctly';
 
-            is $rel_info->{attrs}{join_type}, 'LEFT',
-                'relationship in schema name with dot';
+                is try { $rsrc->column_info('value')->{size} }, 100,
+                    'column in schema name with dot introspected correctly';
 
-            lives_and {
-                ok $rsrc = PGSchemaWithDot->source('PgLoaderTest6');
-            } 'got source for table in schema name with dot';
+                lives_and {
+                    ok $rs = $test_schema->resultset('PgLoaderTest6');
+                } 'got resultset for table in schema name with dot';
 
-            %uniqs = try { $rsrc->unique_constraints };
+                lives_and {
+                    ok $row = $rs->create({ value => 'foo' });
+                } 'executed SQL on table in schema name with dot';
 
-            is keys %uniqs, 2,
-                'got unique and primary constraint in schema name with dot';
+                $rel_info = try { $rsrc->relationship_info('pg_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('PgLoaderTest7');
+                } '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';
+
+                delete $uniqs{primary};
+
+                is_deeply ((values %uniqs)[0], ['six_id'],
+                    'unique constraint is correct in schema name with dot');
+
+                lives_and {
+                    ok $test_schema->source('PgLoaderTest6')
+                        ->has_relationship('pg_loader_test4');
+                } 'cross-schema relationship in multi-db_schema';
+
+                lives_and {
+                    ok $test_schema->source('PgLoaderTest4')
+                        ->has_relationship('pg_loader_test6s');
+                } 'cross-schema relationship in multi-db_schema';
+
+                lives_and {
+                    ok $test_schema->source('PgLoaderTest8')
+                        ->has_relationship('pg_loader_test7');
+                } 'cross-schema relationship in multi-db_schema';
+
+                lives_and {
+                    ok $test_schema->source('PgLoaderTest7')
+                        ->has_relationship('pg_loader_test8s');
+                } 'cross-schema relationship in multi-db_schema';
+            }
         },
     },
 );
@@ -339,4 +451,8 @@ if( !$dsn || !$user ) {
 else {
     $tester->run_tests();
 }
+
+END {
+    rmtree EXTRA_DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
+}
 # vim:et sw=4 sts=4 tw=0: