From: Amiri Barksdale at Home Date: Wed, 4 May 2011 18:58:59 +0000 (-0700) Subject: Fix CREATE VIEW syntax. X-Git-Tag: v0.11010~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0e7580187aea709181804514465931b90f0145cd;p=dbsrgits%2FSQL-Translator.git Fix CREATE VIEW syntax. --- diff --git a/AUTHORS b/AUTHORS index 01a0a66..0ee6d21 100644 --- a/AUTHORS +++ b/AUTHORS @@ -35,6 +35,7 @@ The following people have contributed to the SQLFairy project: - Vincent Bachelier - Wallace Reis - Ying Zhang +- Amiri Barksdale If you would like to contribute to the project, you can send patches to the developers mailing list: diff --git a/Changes b/Changes index bfb547c..8f1c6e4 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ + +* Add "if exists" to drop view statements in Pg. + # ---------------------------------------------------------- # 0.11009 2011-09-02 +# ---------------------------------------------------------- * Fix MySQL producer to properly quote all table names on output (patch from geistteufel) diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 4b883c7..c01b2ea 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -212,6 +212,7 @@ sub produce { for my $view ( $schema->get_views ) { push @table_defs, create_view($view, { + postgres_version => $postgres_version, add_drop_view => $add_drop_table, quote_table_names => $qt, quote_field_names => $qf, @@ -423,6 +424,7 @@ sub create_view { my ($view, $options) = @_; my $qt = $options->{quote_table_names} || ''; my $qf = $options->{quote_field_names} || ''; + my $postgres_version = $options->{postgres_version} || 0; my $add_drop_view = $options->{add_drop_view}; my $view_name = $view->name; @@ -431,7 +433,13 @@ sub create_view { my $create = ''; $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n" unless $options->{no_comments}; - $create .= "DROP VIEW ${qt}${view_name}${qt};\n" if $add_drop_view; + if ($add_drop_view) { + if ($postgres_version >= 8.002) { + $create .= "DROP VIEW IF EXISTS ${qt}${view_name}${qt};\n"; + } else { + $create .= "DROP VIEW ${qt}${view_name}${qt};\n"; + } + } $create .= 'CREATE'; my $extra = $view->extra;