From: Craig Addleman <craiga-@-sharechive.com>
To: pgsql-sql@postgresql.org
Subject: Re: DROP TYPE without error?
Date: Thu, 7 Apr 2005 13:24:53 -0700
Message-ID: <20050407202453.GA3821@craiga-linux.sharechive.com> (raw)
In-Reply-To: <6C0CF58A187DA5479245E0830AF84F4208057C@poweredge.attiksystem.ch>
References: <6C0CF58A187DA5479245E0830AF84F4208057C@poweredge.attiksystem.ch>
I was confronted with a similar problem. I have several scripts which create
or modify schemas, and each run in a single transaction. So, dropping a
non-existent TYPE will produce a show-stopping error. I wrote this function,
and others for various database objects:
CREATE OR REPLACE FUNCTION dba_droptype(varchar) RETURNS boolean AS '
DECLARE
p_type ALIAS FOR $1;
v_exists boolean;
BEGIN
SELECT INTO v_exists TRUE WHERE EXISTS(
SELECT 1 FROM pg_type
WHERE typname = p_type::name);
IF v_exists THEN
RAISE NOTICE ''Dropping TYPE %'', p_type;
EXECUTE ''DROP TYPE '' || p_type || '' CASCADE'';
END IF;
RETURN FOUND;
END;
' LANGUAGE 'plpgsql';
COMMENT ON FUNCTION dba_droptype(varchar) IS '
Usage: SELECT dba_drop_type(type_name)
Checks for existence of a type and drops it if found.
Implements DROP TYPE CASCADE; if a function or other object
depends on the type, that object will also be dropped.
Returns TRUE if successful, returns FALSE if type is
not found.';
--
Craig Addleman
DBA
ShareChive LLC
* Philippe Lang <philippe.lang@attiksystem.ch> [2005-04-06 05:59]:
> Hi,
>
> Since it is not possible to use CREATE OR REPLACE TYPE, is there a way
> of using DROP TYPE on a non-existing type, without causing the entire
> script to abort? It may sound crazy to ask for this, but it could be
> really useful in my case, where Pl/Pgsql and Pl/Perl code is being
> generated automatically, based on data found in a database.
>
> Thanks
>
> -----------------
> Philippe Lang
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: craiga-@-sharechive.com
Subject: Re: DROP TYPE without error?
In-Reply-To: <20050407202453.GA3821@craiga-linux.sharechive.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox