Re: How to *really* quit psql? - Mailing list pgsql-hackers

From Fabien COELHO
Subject Re: How to *really* quit psql?
Date
Msg-id 90298ea-cc54-bed0-a9c1-62abc1579663@mines-paristech.fr
Whole thread Raw
In response to Re: How to *really* quit psql?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hello Tom,

>>   - when the current script is included from something,
>>     you quit the current script and proceed after the \i of next -f, BAD
>
>> Question: is there any way to really abort a psql script from an included
>> file?
>
> Under what circumstances would it be appropriate for a script to take
> it on itself to decide that?

The use case is psql scripts which update or cleanup an application 
schema. For security, some of these scripts check for conditions (eg, we 
are not in production, the application schema is in the expected version, 
whatever…) and should abort if the conditions are not okay. As checking 
for the conditions requires a few lines of code and is always the same, a 
simple approach is to include another script which does the check and 
aborts the run if necessary, eg:

```sql
-- this script should not run in "prod"!
\ir not_in_prod.sql
-- should have aborted if it is a "prod" version.
DROP TABLE AllMyUsers CASCADE;
DROP TABLE QuiteImportantData CASCADE;
```

> It has no way of knowing what the next -f option is or what the user 
> intended.

The intention of the user who wrote the script is to abort in some cases, 
to avoid damaging the database contents.

-- 
Fabien.

pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: More efficient build farm animal wakeup?
Next
From: Fabien COELHO
Date:
Subject: Re: How to *really* quit psql?