From: Cedric Carree Date: Tue, 3 Jan 2012 16:53:56 +0000 (+0100) Subject: SQLT::Parser::PostgreSQL parses table def with default values X-Git-Tag: v0.11011~51 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=88ad825597d4eee0bf3c93aa81738f82cc583fae;p=dbsrgits%2FSQL-Translator.git SQLT::Parser::PostgreSQL parses table def with default values Actually the parser used to skip table definition which includes fields with explicitely casted default values For example, the following field definition was not working (due to the "::text" part) CREATE TABLE foo ( bar TEXT DEFAULT 'this is casted default text'::TEXT ); --- diff --git a/Changes b/Changes index afbae7c..f28761e 100644 --- a/Changes +++ b/Changes @@ -5,7 +5,8 @@ * Add column and table comments in SQLT::Parser::DBI::PostgreSQL(patch from Andrew Pam) * Fixed alter_drop_constraint for foreign keys and applying multiple changes via alter_field to a column in Postgres Producer -* added a working mechanism for naming foreign keys in the PostgreSQL producer +* Added a working mechanism for naming foreign keys in the PostgreSQL producer +* Fix PostgreSQL ignoring default values with specified data type * Fix possible name duplication in SQLlite producer * Oracle does not accept ON DELETE/UPDATE RESTRICT (though it is the actual default) fix by not adding the ON DELETE/UPDATE clause at all diff --git a/lib/SQL/Translator/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index 8bb119b..77edd1c 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -936,7 +936,7 @@ create_table : CREATE TABLE create_index : CREATE /index/i -default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ +default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ ( '::' data_type )(?) { my $val = defined $item[2] ? $item[2] : ''; $val =~ s/^'|'$//g; diff --git a/t/14postgres-parser.t b/t/14postgres-parser.t index 96390bf..6448196 100644 --- a/t/14postgres-parser.t +++ b/t/14postgres-parser.t @@ -21,10 +21,10 @@ my $sql = q{ f_varchar character varying (255), f_double double precision, f_bigint bigint not null, - f_char character(10) default 'FOO', + f_char character(10) default 'FOO'::character(10), f_bool boolean, f_bin bytea, - f_tz timestamp, + f_tz timestamp default '1970-01-01 00:00:00'::TIMESTAMP, f_text text, f_fk1 integer not null references t_test2 (f_id), f_dropped text, @@ -182,7 +182,7 @@ is( $f8->name, 'f_tz', 'Eighth field is "f_tz"' ); is( $f8->data_type, 'timestamp', 'Field is a timestamp' ); is( $f8->is_nullable, 1, 'Field can be null' ); is( $f8->size, 0, 'Size is "0"' ); -is( $f8->default_value, undef, 'Default value is undefined' ); +is( $f8->default_value, '1970-01-01 00:00:00', 'Default value is 1970-01-01 00:00:00' ); is( $f8->is_primary_key, 0, 'Field is not PK' ); my $f9 = shift @t1_fields;