From: Peter Rabbitson Date: Tue, 9 Jun 2009 21:04:41 +0000 (+0000) Subject: Proper support for size in pg timestamp columns (patch by mo) X-Git-Tag: v0.11008~154 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad2587769ab392b8916e17bf9b3afb3e2efdfe99;p=dbsrgits%2FSQL-Translator.git Proper support for size in pg timestamp columns (patch by mo) --- diff --git a/AUTHORS b/AUTHORS index f73325f..291046d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,6 +18,7 @@ The following people have contributed to the SQLFairy project: - Ken Youens-Clark - Mark Addison - Mikey Melillo +- Moritz Onken - Paul Harrington - Peter Rabbitson - Ross Smith II diff --git a/Changes b/Changes index 38681b6..6fdbc75 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,8 @@ # ---------------------------------------------------------- * Adjust two tests so they pass under prove -l * Add missing dependency on IO::Scalar +* Added support for "time(stamp) (p) with time zone" for Pg producer (mo) + # ---------------------------------------------------------- # 0.09005 2009-06-08 diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 35de753..8c0ec5d 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -680,7 +680,7 @@ sub convert_datatype $data_type; } - if ( $data_type =~ /timestamp/i ) { + if ( $data_type =~ /^time/i || $data_type =~ /^interval/i ) { if ( defined $size[0] && $size[0] > 6 ) { $size[0] = 6; } @@ -711,12 +711,13 @@ sub convert_datatype } } - if ( defined $size[0] && $size[0] > 0 ) { - $data_type .= '(' . join( ',', @size ) . ')'; - } - elsif (defined $size[0] && $data_type eq 'timestamp' ) { - $data_type .= '(' . join( ',', @size ) . ')'; + if (defined $size[0] && $size[0] > 0 && $data_type =~ /^time/i ) { + $data_type =~ s/^(time.*?)( with.*)?$/$1($size[0])/; + $data_type .= $2 if(defined $2); + } elsif ( defined $size[0] && $size[0] > 0 ) { + $data_type .= '(' . join( ',', @size ) . ')'; } + return $data_type; diff --git a/t/46xml-to-pg.t b/t/46xml-to-pg.t index 3df7849..dcdb01f 100644 --- a/t/46xml-to-pg.t +++ b/t/46xml-to-pg.t @@ -45,7 +45,7 @@ CREATE TABLE "Basic" ( -- Hello emptytagdef "emptytagdef" character varying DEFAULT '', "another_id" integer DEFAULT '2', - "timest" timestamp(0), + "timest" timestamp, PRIMARY KEY ("id"), CONSTRAINT "emailuniqueindex" UNIQUE ("email") ); diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index c4bb28d..91749ea 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -14,7 +14,7 @@ use FindBin qw/$Bin/; #============================================================================= BEGIN { - maybe_plan(17, + maybe_plan(22, 'SQL::Translator::Producer::PostgreSQL', 'Test::Differences', ) @@ -156,6 +156,73 @@ $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6, is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL'); +my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field', + table => $table, + data_type => 'timestamp with time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 }); + +is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision'); + +my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field', + table => $table, + data_type => 'time with time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 }); + +is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision'); + +my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field', + table => $table, + data_type => 'interval', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 }); + +is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision'); + + +my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field', + table => $table, + data_type => 'time without time zone', + size => 6, + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 }); + +is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision'); + + + +my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field', + table => $table, + data_type => 'timestamp', + is_auto_increment => 0, + is_nullable => 0, + is_foreign_key => 0, + is_unique => 0 ); + +my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 }); + +is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision'); + { # let's test default values! -- rjbs, 2008-09-30