My application uses a PostgreSQL 9.1 database. We’re in our beta phase and we occaisionally find bugs in our PostgreSQL schema. We’ve got an installer written and working, but now we’re at the point where there are installations out there that will need to be updated when we make our next release.
I posted an earlier question here asking how to get my script to make decisions. The answer I went with was to use the DO statement. This is working great, but I’ve run into a problem.
We’ve added a table to our database that keeps track of the last version that was installed. My script looks like this:
DO $$$
BEGIN
IF NOT EXISTS ( SELECT * FROM "CarSystem"."CarSystem_Versions" WHERE "VersionId" = 2 ) THEN
-- Schema update statements go here
END IF;
END;
$$$ LANGUAGE plpgSQL;
Most of the statements are simple thngs like CREATE or DROP statements. But one of the things I have to do in this next release is apply a code change to a stored function. The function is also written in plpgSQL. I used three (3) $$$ symbols in the code above because this stored procedure uses two (2) $$ symbols, because I figured this would make the nested plpgSQL look different and not make the second set of dollar signs terminate the first.
Yet I’m getting an error when PostgreSQL hits the DECLARE statement in my stored procedure. It doesn’t see the text of the function as being quoted.
What is the correct way of dong this? Should I just put the stored procedure outside of this DO block and use a CREATE OR REPLACE statemnt to declare it? Or is there a way I can keep the code inside the condition?
Tony
Try:
4.1.2.4. Dollar-quoted String Constants