Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'CREATE OR REPLACE FUNCTION' syntax when body is changed #88

Open
KES777 opened this issue Apr 6, 2017 · 0 comments
Open

Use 'CREATE OR REPLACE FUNCTION' syntax when body is changed #88

KES777 opened this issue Apr 6, 2017 · 0 comments

Comments

@KES777
Copy link
Contributor

KES777 commented Apr 6, 2017

This issues related to #82

When body of function is changed but on this function depend other objects we got next error:

Exception: DBD::Pg::db do failed: ERROR:  cannot drop function make_prow() because other objects depend on it

The produced upgrade/downgrade SQLs are like:

DROP FUNCTION make_prow ();
CREATE FUNCTION "make_prow" ();

According to the DOC

If you drop and then recreate a function, the new function is not the same entity as the old; you will have to drop existing rules, views, triggers, etc. that refer to the old function. Use CREATE OR REPLACE FUNCTION to change a function definition without breaking objects that refer to the function. Also, ALTER FUNCTION can be used to change most of the auxiliary properties of an existing function.

So here instead of DROP/CREATE we should use 'CREATE OR REPLACE FUNCTION' when {add_drop_procedure} option is supplied.

The patch:

--- a/lib/SQL/Translator/Producer/PostgreSQL.pm
+++ b/lib/SQL/Translator/Producer/PostgreSQL.pm
@@ -713,10 +713,7 @@ sub create_procedure {
 
   my @statements;
 
-  push @statements, drop_procedure( $procedure )
-    if $options->{add_drop_procedure};
-
-  my $sql = 'CREATE FUNCTION ';
+  my $sql = 'CREATE '. ($options->{add_drop_procedure} ? 'OR REPLACE ' : '') .'FUNCTION ';
   $sql .= $generator->quote($procedure->name);
   $sql .= ' (';
   my @args = ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant