fix NUMERIC/DECIMAL size for postgres
Rafael Kitover [Sun, 3 Jan 2010 21:23:37 +0000 (21:23 +0000)]
Changes
lib/DBIx/Class/Schema/Loader/DBI/Pg.pm
t/12pg_common.t

diff --git a/Changes b/Changes
index aeac0b9..4537739 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,7 @@
 Revision history for Perl extension DBIx::Class::Schema::Loader
 
+        - fix NUMERIC/DECIMAL size column_info for postgres
+
 0.04999_13  2010-01-03 12:32:25
         - exclude 'size' column_info for postgres when unnecessary, and
           use the correct precision for varying types (except NUMERIC)
index 501e1dd..3ad65ca 100644 (file)
@@ -164,6 +164,14 @@ EOF
 
             $result->{$col}{size} = $precision;
         }
+        elsif ($data_type =~ /^(?:numeric|decimal)\z/i) {
+            my $size = $result->{$col}{size};
+            $size =~ s/\s*//g;
+
+            my ($scale, $precision) = split /,/, $size;
+
+            $result->{$col}{size} = [ $precision, $scale ];
+        }
     }
 
     return $result;
index 74a6784..109bb33 100644 (file)
@@ -29,7 +29,7 @@ my $tester = dbixcsl_common_tests->new(
                 COMMENT ON COLUMN pg_loader_test1.value IS 'The Column'
             },
             # Test to make sure data_types that don't need a size => don't
-            # have one.
+            # have one and check varying types have the correct size.
             q{
                 CREATE TABLE pg_loader_test2 (
                     id SERIAL NOT NULL PRIMARY KEY,
@@ -84,14 +84,16 @@ my $tester = dbixcsl_common_tests->new(
                     a_character_varying_with_precision CHARACTER VARYING(2),
                     a_varchar_with_precision VARCHAR(2),
                     a_character_with_precision CHARACTER(2),
-                    a_char_with_precision CHAR(2)
+                    a_char_with_precision CHAR(2),
+                    the_numeric NUMERIC(6, 3),
+                    the_decimal DECIMAL(6, 3)
                 )
             },
 # XXX figure out what to do with DECIMAL(precision, scale) aka
 # NUMERIC(precision, scale)
         ],
         drop  => [ qw/ pg_loader_test1 pg_loader_test2 / ],
-        count => 54,
+        count => 56,
         run   => sub {
             my ($schema, $monikers, $classes) = @_;
 
@@ -110,7 +112,7 @@ my $tester = dbixcsl_common_tests->new(
                 'column comment and attrs';
 
             my $rsrc = $schema->resultset('PgLoaderTest2')->result_source;
-            my @type_columns = grep $_ ne 'id', $rsrc->columns;
+            my @type_columns = grep /^a/, $rsrc->columns;
             my @without_precision = grep !/_with_precision\z/, @type_columns;
             my @with_precision    = grep  /_with_precision\z/, @type_columns;
             my %with_precision;
@@ -136,6 +138,12 @@ my $tester = dbixcsl_common_tests->new(
                 is $size, 2,
                   "$data_type with precision has a correct 'size' column_info";
             }
+
+            is_deeply $rsrc->column_info('the_numeric')->{size}, [6,3],
+                'size for NUMERIC(precision, scale) is correct';
+
+            is_deeply $rsrc->column_info('the_decimal')->{size}, [6,3],
+                'size for DECIMAL(precision, scale) is correct';
         },
     },
 );