From: Andrew Beverley Date: Tue, 29 Sep 2015 09:19:00 +0000 (+0100) Subject: Fix incorrect serial instead of bigserial in PG X-Git-Tag: v0.11022~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d430e0976994de076ebb0602651e9e0eca332da;p=dbsrgits%2FSQL-Translator.git Fix incorrect serial instead of bigserial in PG When generating PostgreSQL, auto-incrementing bigint columns are incorrectly converted to serial types. Instead they should be converted to bigserial. --- diff --git a/Changes b/Changes index 49a2991..1bcc13a 100644 --- a/Changes +++ b/Changes @@ -16,6 +16,8 @@ Changes for SQL::Translator * Fix parsing of MySQL column comments (RT#83380) * Fix multi-line comments in YAML, JSON and PostgreSQL producers * Fix identifier quoting in PostgreSQL diff producer + * Fix incorrect type conversion from various BigInt AutoIncrement to the + PostgreSQL-specific bigserial (GH#72) * Fix missing semicolons between PostGIS statements * Fix string and identifier quoting in PostGIS statements * Fix intermittent test failures (RT#108460) diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 7685587..455f745 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -651,7 +651,7 @@ sub convert_datatype $data_type = 'character varying'; } elsif ( $field->is_auto_increment ) { - if ( defined $size[0] && $size[0] > 11 ) { + if ( (defined $size[0] && $size[0] > 11) or $data_type eq 'bigint' ) { $data_type = 'bigserial'; } else { diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index 8f830bf..9c50db7 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -250,6 +250,37 @@ is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'A my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2); is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); +my $field_serial = SQL::Translator::Schema::Field->new( name => 'serial_field', + table => $table, + data_type => 'INT', + is_auto_increment => 1, + is_nullable => 0 ); + +my $field_serial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_serial); + +is($field_serial_sql, 'serial_field serial NOT NULL', 'Create serial field works'); + +my $field_bigserial = SQL::Translator::Schema::Field->new( name => 'bigserial_field', + table => $table, + data_type => 'BIGINT', + is_auto_increment => 1, + is_nullable => 0 ); + +my $field_bigserial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_bigserial); + +is($field_bigserial_sql, 'bigserial_field bigserial NOT NULL', 'Create bigserial field works (from bigint type)'); + +$field_bigserial = SQL::Translator::Schema::Field->new( name => 'bigserial_field', + table => $table, + data_type => 'INT', + is_auto_increment => 1, + is_nullable => 0, + size => 12 ); + +$field_bigserial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_bigserial); + +is($field_bigserial_sql, 'bigserial_field bigserial NOT NULL', 'Create bigserial field works (based on size)'); + my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field', table => $table, data_type => 'TIME',