From: Dagfinn Ilmari Mannsåker Date: Tue, 8 Sep 2015 17:15:30 +0000 (+0100) Subject: Fix multi-line comments in PostgreSQL producer X-Git-Tag: v0.11022~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Translator.git;a=commitdiff_plain;h=31bed2c0d1ce8ccda214ec8da1a059a51820bf28 Fix multi-line comments in PostgreSQL producer --- diff --git a/Changes b/Changes index 4d36f95..bb9d043 100644 --- a/Changes +++ b/Changes @@ -14,7 +14,7 @@ Changes for SQL::Translator * Fix parsing of strings with leading whitespace for MySQL, Oracle, PostgreSQL, SQLServer and SQLite * Fix parsing of MySQL column comments (RT#83380) - * Fix multi-line comments in YAML and JSON producers + * Fix multi-line comments in YAML, JSON and PostgreSQL producers 0.11021 2015-01-29 diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 29d152a..43c09c6 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -295,11 +295,9 @@ sub create_table push @comments, "--\n-- Table: $table_name\n--\n" unless $no_comments; - if ( $table->comments and !$no_comments ){ - my $c = "-- Comments: \n-- "; - $c .= join "\n-- ", $table->comments; - $c .= "\n--\n"; - push @comments, $c; + if ( !$no_comments and my $comments = $table->comments ) { + $comments =~ s/^/-- /mg; + push @comments, "-- Comments:\n$comments\n--\n"; } # @@ -435,9 +433,11 @@ sub create_view { $field_name_scope{$table_name} ||= {}; my $field_name = $field->name; - my $field_comments = $field->comments - ? "-- " . $field->comments . "\n " - : ''; + my $field_comments = ''; + if (my $comments = $field->comments) { + $comments =~ s/(?quote($field_name); diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index bf57a01..8f830bf 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -25,8 +25,10 @@ use SQL::Translator; my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; { - my $table = SQL::Translator::Schema::Table->new( name => 'foo.bar' ); + my $table = SQL::Translator::Schema::Table->new( name => 'foo.bar', + comments => [ "multi\nline",'single line' ] ); my $field = SQL::Translator::Schema::Field->new( name => 'baz', + comments => [ "multi\nline",'single line' ], table => $table, data_type => 'VARCHAR', size => 10, @@ -39,7 +41,25 @@ my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; my ($create, $fks) = SQL::Translator::Producer::PostgreSQL::create_table($table, { quote_table_names => q{"} }); is($table->name, 'foo.bar'); - my $expected = "--\n-- Table: foo.bar\n--\nCREATE TABLE \"foo\".\"bar\" (\n \"baz\" character varying(10) DEFAULT 'quux' NOT NULL\n)"; + my $expected = <