pick up views in SQLite
Rafael Kitover [Wed, 17 Mar 2010 23:14:34 +0000 (19:14 -0400)]
Changes
lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm
t/10sqlite_common.t
t/lib/dbixcsl_common_tests.pm

diff --git a/Changes b/Changes
index 0512855..a93df57 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Revision history for Perl extension DBIx::Class::Schema::Loader
 
-        - better inflection using Lingua::EN::Inflect::Phrase
+        - pick up views in SQLite too
+        - better rel inflection using Lingua::EN::Inflect::Phrase
         - cascade_delete and cascade_copy are turned off for has_many/might_have
           by default, and belongs_to has on_delete => 'CASCADE', on_update =>
           'CASCADE' and is_deferrable => 1 by default, overridable via
index b771681..24a2ba1 100644 (file)
@@ -139,7 +139,7 @@ sub _tables_list {
     $sth->execute;
     my @tables;
     while ( my $row = $sth->fetchrow_hashref ) {
-        next unless lc( $row->{type} ) eq 'table';
+        next unless $row->{type} =~ /^(?:table|view)\z/i;
         next if $row->{tbl_name} =~ /^sqlite_/;
         push @tables, $row->{tbl_name};
     }
index 7feba6e..865835e 100644 (file)
@@ -31,7 +31,7 @@ my $tester = dbixcsl_common_tests->new(
                     person_id INTEGER PRIMARY KEY
                 )
             },
-            # Wordy, newline-heavy SQL to stress the regexes
+            # Wordy, newline-heavy SQL
             q{
                 CREATE TABLE extra_loader_test4 (
                     event_id INTEGER NOT NULL
@@ -43,7 +43,12 @@ my $tester = dbixcsl_common_tests->new(
                     PRIMARY KEY (event_id, person_id)
                 )
             },
+            # make sure views are picked up
+            q{
+                CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
+            }
         ],
+        pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
         drop  => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3 extra_loader_test4 / ],
         count => 8,
         run   => sub {
index 9b1177e..b197ff4 100644 (file)
@@ -135,46 +135,48 @@ sub setup_schema {
     Class::Unload->unload($schema_class);
 
     my $file_count;
-    my $expected_count = 36 + ($self->{data_type_tests} ? 1 : 0);
+    my $expected_count = 36 + ($self->{data_type_tests}{test_count} ? 1 : 0);
     {
-       my @loader_warnings;
-       local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
-        eval qq{
-            package $schema_class;
-            use base qw/DBIx::Class::Schema::Loader/;
-    
-            __PACKAGE__->loader_options(\%loader_opts);
-            __PACKAGE__->connection(\@connect_info);
-        };
-
-       ok(!$@, "Loader initialization") or diag $@;
-
-       find sub { return if -d; $file_count++ }, $DUMP_DIR;
-
-       $expected_count += grep /CREATE (?:TABLE|VIEW)/i,
-           @{ $self->{extra}{create} || [] };
-
-       $expected_count -= grep /CREATE TABLE/, @statements_inline_rels
-           if $self->{skip_rels} || $self->{no_inline_rels};
-
-       $expected_count -= grep /CREATE TABLE/, @statements_implicit_rels
-           if $self->{skip_rels} || $self->{no_implicit_rels};
-
-       $expected_count -= grep /CREATE TABLE/, ($self->{vendor} =~ /sqlite/ ? @statements_advanced_sqlite : @statements_advanced), @statements_reltests
-           if $self->{skip_rels};
-
-       is $file_count, $expected_count, 'correct number of files generated';
-
-       my $warn_count = 2;
-       $warn_count++ if grep /ResultSetManager/, @loader_warnings;
-
-       $warn_count++ for grep /^Bad table or view/, @loader_warnings;
-
-       $warn_count++ for grep /renaming \S+ relation/, @loader_warnings;
+        my @loader_warnings;
+        local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
+         eval qq{
+             package $schema_class;
+             use base qw/DBIx::Class::Schema::Loader/;
+     
+             __PACKAGE__->loader_options(\%loader_opts);
+             __PACKAGE__->connection(\@connect_info);
+         };
+        ok(!$@, "Loader initialization") or diag $@;
+        find sub { return if -d; $file_count++ }, $DUMP_DIR;
+        $expected_count += grep /CREATE (?:TABLE|VIEW)/i,
+            @{ $self->{extra}{create} || [] };
+        $expected_count -= grep /CREATE TABLE/, @statements_inline_rels
+            if $self->{skip_rels} || $self->{no_inline_rels};
+        $expected_count -= grep /CREATE TABLE/, @statements_implicit_rels
+            if $self->{skip_rels} || $self->{no_implicit_rels};
+        $expected_count -= grep /CREATE TABLE/, ($self->{vendor} =~ /sqlite/ ? @statements_advanced_sqlite : @statements_advanced), @statements_reltests
+            if $self->{skip_rels};
+        is $file_count, $expected_count, 'correct number of files generated';
+        my $warn_count = 2;
+        $warn_count++ if grep /ResultSetManager/, @loader_warnings;
+        $warn_count++ for grep /^Bad table or view/, @loader_warnings;
+        $warn_count++ for grep /renaming \S+ relation/, @loader_warnings;
+        my $vendor = $self->{vendor};
+        $warn_count++ for grep /${vendor}_\S+ has no primary key/,
+            @loader_warnings;
 
-       my $vendor = $self->{vendor};
-       $warn_count++ for grep /${vendor}_\S+ has no primary key/,
-           @loader_warnings;
+        $warn_count++ for grep /\b(?!loader_test9)\w+ has no primary key/i, @loader_warnings;
 
         if($self->{skip_rels}) {
             SKIP: {
@@ -866,7 +868,8 @@ sub test_schema {
     }
 
     # test data types
-    if (my $data_type_tests = $self->{data_type_tests}) {
+    if ($self->{data_type_tests}{test_count}) {
+        my $data_type_tests = $self->{data_type_tests};
         my $columns = $data_type_tests->{columns};
 
         my $rsrc = $conn->resultset($data_type_tests->{table_moniker})->result_source;
@@ -1494,7 +1497,9 @@ sub drop_tables {
     $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc;
     $dbh->do("DROP TABLE $_") for (@tables, @tables_rescan);
 
-    $dbh->do("DROP TABLE ".$self->{data_type_tests}{table_name}) if $self->{data_type_tests};
+    if (my $data_type_table = $self->{data_type_tests}{table_name}) {
+        $dbh->do("DROP TABLE $data_type_table");
+    }
 
     $dbh->disconnect;