diff --git a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm index c751fc5d..2c54f714 100644 --- a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm @@ -121,20 +121,32 @@ ORDER BY 1; my %column_by_attrid; while (my $columnhash = $column_select->fetchrow_hashref ) { - - #data_type seems to not be populated; perhaps there needs to - #be a mapping of query output to reserved constants in sqlt? - + my $type = $$columnhash{'typname'}; + # For the case of character varying(50), atttypmod will be 54 and the (50) + # will be listed as part of the type. For numeric(8,5) the atttypmod will + # be a meaningless large number. To make this compatible with the + # rest of SQL::Translator, remove the size from the type and change the + # size to whatever was removed from the type. + my @size= ($type =~ s/\(([0-9,]+)\)$//)? (split /,/, $1) : (); my $col = $table->add_field( name => $$columnhash{'attname'}, - default_value => $$columnhash{'adsrc'}, - data_type => $$columnhash{'typname'}, + data_type => $type, order => $$columnhash{'attnum'}, ) || die $table->error; - - $col->{size} = [$$columnhash{'length'}] - if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF; - $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1; + $col->size(\@size) if @size; + # default values are a DDL expression. Convert the obvious ones like '...'::text + # to a plain value and let the rest be scalarrefs. + my $default= $$columnhash{'adsrc'}; + if (defined $default) { + if ($default =~ /^[0-9.]+$/) { $col->default_value($default) } + elsif ($default =~ /^'(.*?)'(::\Q$type\E)?$/) { + my $str= $1; + $str =~ s/''/'/g; + $col->default_value($str); + } + else { $col->default_value(\$default) } + } + $col->is_nullable( $$columnhash{'attnotnull'} ? 0 : 1 ); $col->comments($$columnhash{'description'}) if $$columnhash{'description'}; $column_by_attrid{$$columnhash{'attnum'}}= $$columnhash{'attname'}; } diff --git a/t/66-postgres-dbi-parser.t b/t/66-postgres-dbi-parser.t index aae51622..08118ff8 100644 --- a/t/66-postgres-dbi-parser.t +++ b/t/66-postgres-dbi-parser.t @@ -53,8 +53,9 @@ my $sql = q[ CREATE TABLE sqlt_products_1 ( product_no integer, - name text, - price numeric + name text default '['''']', + price numeric(8,4) default 0.0, + created_at timestamp without time zone default now() ); -- drop a column, to not have a linear id @@ -93,17 +94,16 @@ my $f1 = shift @t1_fields; is( $f1->name, 'f_serial', 'First field is "f_serial"' ); is( $f1->data_type, 'integer', 'Field is an integer' ); is( $f1->is_nullable, 0, 'Field cannot be null' ); -is( $f1->default_value, "nextval('sqlt_test1_f_serial_seq'::regclass)", 'Default value is nextval()' ); +is( ${$f1->default_value}, "nextval('sqlt_test1_f_serial_seq'::regclass)", 'Default value is nextval()' ); is( $f1->is_primary_key, 1, 'Field is PK' ); #FIXME: not set to auto-increment? maybe we can guess auto-increment behavior by looking at the default_value (i.e. it call function nextval() ) #is( $f1->is_auto_increment, 1, 'Field is auto increment' ); my $f2 = shift @t1_fields; is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' ); -is( $f2->data_type, 'character varying(255)', 'Field is a character varying(255)' ); +is( $f2->data_type, 'character varying', 'Field is a character varying(255)' ); is( $f2->is_nullable, 1, 'Field can be null' ); -#FIXME: should not be 255? -is( $f2->size, 259, 'Size is "259"' ); +is( $f2->size, 255, 'Size is "255"' ); is( $f2->default_value, undef, 'Default value is undefined' ); is( $f2->is_primary_key, 0, 'Field is not PK' ); is( $f2->is_auto_increment, 0, 'Field is not auto increment' ); @@ -114,7 +114,7 @@ is( $f3->name, 'f_text', 'Third field is "f_text"' ); is( $f3->data_type, 'text', 'Field is a text' ); is( $f3->is_nullable, 1, 'Field can be null' ); is( $f3->size, 0, 'Size is 0' ); -is( $f3->default_value, "'FOO'::text", 'Default value is "FOO"' ); +is( $f3->default_value, 'FOO', 'Default value is "FOO"' ); is( $f3->is_primary_key, 0, 'Field is not PK' ); is( $f3->is_auto_increment, 0, 'Field is not auto increment' ); is( $f3->comments, 'this is a comment on a field of the first table', 'There is a comment on the third field'); @@ -165,12 +165,31 @@ my $fk_ref1 = $t2_f3->foreign_key_reference; isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' ); is( $fk_ref1->reference_table, 'sqlt_test1', 'FK is to "sqlt_test1" table' ); +my $t3 = $schema->get_table("sqlt_products_1"); + +my $t3_f2= $t3->get_field('name'); +is( $t3_f2->data_type, 'text', 'Second field, type "text"' ); +is( $t3_f2->default_value, q{['']}, 'default value is json array of empty string' ); + +my $t3_f3= $t3->get_field('price'); +is( $t3_f3->name, 'price', 'Third field is "price"' ); +is( $t3_f3->data_type, 'numeric', 'Third field type "numeric"' ); +is_deeply( [$t3_f3->size], [8,4], 'Third field size "(8,4)"' ); +is( $t3_f3->default_value, '0.0', 'Third field default "0.0"' ); + +my $t3_f4= $t3->get_field('created_at'); +is( $t3_f4->name, 'created_at', 'fourth field is "created_at"' ); +is( $t3_f4->data_type, 'timestamp without time zone', 'type is "timestamp without time zone"' ); +is( $t2_f3->size, 0, 'Size is "0"' ); +is_deeply( $t3_f4->default_value, \"now()", 'default \\"now()"' ); + my @t2_constraints = $t2->get_constraints; is( scalar @t2_constraints, 1, "One constraint on table" ); my $t2_c1 = shift @t2_constraints; is( $t2_c1->type, FOREIGN_KEY, "Constraint is a FK" ); + $dbh->rollback; $dbh->disconnect;