From: Tina Mueller Date: Sat, 28 Apr 2012 01:31:49 +0000 (+0200) Subject: add support for "DEFAULT (\d+)::data_type" in PostgreSQL Parser X-Git-Tag: v0.11011~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a68deb7dab91f92f1668fe0cded865ddd9224c90;p=dbsrgits%2FSQL-Translator.git add support for "DEFAULT (\d+)::data_type" in PostgreSQL Parser --- diff --git a/AUTHORS b/AUTHORS index c29a0b1..e8e9683 100644 --- a/AUTHORS +++ b/AUTHORS @@ -50,6 +50,7 @@ The following people have contributed to the SQLFairy project: - Stephen Bennett - Stephen Clouse - SymKat +- Tina Müller - Vincent Bachelier - Wallace Reis - Ying Zhang diff --git a/Changes b/Changes index 9fbda65..7b3d0e1 100644 --- a/Changes +++ b/Changes @@ -27,6 +27,7 @@ via alter_field to a column in Postgres Producer * Added a working mechanism for naming foreign keys in the PostgreSQL producer * Fix PostgreSQL ignoring default values with specified data type +* Fix PostgreSQL parser support for (N)::int defaults (patch by Tina Müller) * 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 29f26ab..ac59c4b 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -931,10 +931,11 @@ create_table : CREATE TABLE create_index : CREATE /index/i -default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ ( '::' data_type )(?) +default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+|\(\d+\)/ ( '::' data_type )(?) { my $val = defined $item[2] ? $item[2] : ''; $val =~ s/^'|'$//g; + $val =~ s/^\((\d+)\)\z/$1/; # for example (0)::smallint $return = { supertype => 'constraint', type => 'default', diff --git a/t/14postgres-parser.t b/t/14postgres-parser.t index fd60cb0..3e7e4ea 100644 --- a/t/14postgres-parser.t +++ b/t/14postgres-parser.t @@ -8,7 +8,7 @@ use SQL::Translator::Schema::Constants; use Test::SQL::Translator qw(maybe_plan); BEGIN { - maybe_plan(134, 'SQL::Translator::Parser::PostgreSQL'); + maybe_plan(140, 'SQL::Translator::Parser::PostgreSQL'); SQL::Translator::Parser::PostgreSQL->import('parse'); } @@ -36,6 +36,7 @@ my $sql = q{ f_id integer NOT NULL, f_varchar varchar(25), f_int smallint, + f_smallint smallint default (0)::smallint, primary key (f_id), check (f_int between 1 and 5) ); @@ -258,7 +259,7 @@ my $t2 = shift @tables; is( $t2->name, 't_test2', 'Table t_test2 exists' ); my @t2_fields = $t2->get_fields; -is( scalar @t2_fields, 3, '3 fields in t_test2' ); +is( scalar @t2_fields, 4, '4 fields in t_test2' ); my $t2_f1 = shift @t2_fields; is( $t2_f1->name, 'f_id', 'First field is "f_id"' ); @@ -284,6 +285,15 @@ is( $t2_f3->size, 5, 'Size is "5"' ); is( $t2_f3->default_value, undef, 'Default value is undefined' ); is( $t2_f3->is_primary_key, 0, 'Field is not PK' ); +my $t2_f4 = shift @t2_fields; +is( $t2_f4->name, 'f_smallint', 'Fourth field is "f_smallint"' ); +is( $t2_f4->data_type, 'integer', 'Field is an integer' ); +is( $t2_f4->is_nullable, 1, 'Field can be null' ); +is( $t2_f4->size, 5, 'Size is "5"' ); +is( $t2_f4->default_value, 0, 'Default value is 0' ); +is( $t2_f4->is_primary_key, 0, 'Field is not PK' ); + + my @t2_constraints = $t2->get_constraints; is( scalar @t2_constraints, 3, "Three constraints on table" );