Skip to content
Snippets Groups Projects
Commit bc110070 authored by Raphael Das Gupta's avatar Raphael Das Gupta
Browse files

Merge branch 'on_error_stop' into 'master'

cause SQL scripts to stop immediately when unexpected errors occur

See merge request ifs/AngProj!11
parents cd4a7a34 9d1f1d72
Branches
No related tags found
No related merge requests found
......@@ -60,3 +60,7 @@ CREATE DATABASE :database WITH OWNER :user ENCODING 'UTF8';
-- query the database
\ir 5_queries.sql
\unset ECHO
-- demonstrate constraints in action
\ir 6_constraints_tests.sql
......@@ -82,31 +82,3 @@ SELECT * FROM GutVerdienende;
-- forget all changes made since BEGIN
ROLLBACK;
-- Constraints Tests
-- do not alter contents of the database persistently
BEGIN;
ALTER TABLE Angestellter
ADD CONSTRAINT check_Salaer
CHECK (Salaer between 1000 and 20000);
UPDATE angestellter SET salaer=30000 where persnr = 1100;
select * from angestellter WHERE persnr = 1100;
-- forget all changes made since BEGIN
ROLLBACK;
-- do not alter contents of the database persistently
BEGIN;
ALTER TABLE ProjektZuteilung
ADD CONSTRAINT check_Zeitanteil
CHECK (Zeitanteil between 10 and 90);
UPDATE ProjektZuteilung SET Zeitanteil=99 where persnr = 1100;
select * from ProjektZuteilung WHERE persnr = 1100;
-- forget all changes made since BEGIN
ROLLBACK;
DO
$$BEGIN
/* Exceptions of type integrity_constraint_violation within this block
* are intercepted and don't make the script fail.
*/
-- Add a constraint:
ALTER TABLE Angestellter
ADD CONSTRAINT check_Salaer
CHECK (Salaer between 1000 and 20000);
-- Test the constraint:
UPDATE angestellter SET salaer=30000 where persnr = 1100;
/* The UPDATE above fails, thereby making execution jump
* into the EXCEPTION block below and causing a ROLLBACK
* of all changes within the block so far,
* incl. the addition of the constraint.
*/
-- This exception is therefore never being raised:
RAISE EXCEPTION 'The constraint did not work.';
EXCEPTION
WHEN integrity_constraint_violation THEN
-- Swallow the exception, but do print out its error message:
RAISE NOTICE 'The constraint worked and made the UPDATE fail with message: %', SQLERRM;
END;$$;
DO
$$BEGIN
/* Exceptions of type integrity_constraint_violation within this block
* are intercepted and don't make the script fail.
*/
-- Add a constraint:
ALTER TABLE ProjektZuteilung
ADD CONSTRAINT check_Zeitanteil
CHECK (Zeitanteil between 10 and 90);
-- Test the constraint:
UPDATE ProjektZuteilung SET Zeitanteil=99 where persnr = 1100;
/* The UPDATE above fails, thereby making execution jump
* into the EXCEPTION block below and causing a ROLLBACK
* of all changes within the block so far,
* incl. the addition of the constraint.
*/
-- This exception is therefore never being raised:
RAISE EXCEPTION 'The constraint did not work.';
EXCEPTION
WHEN integrity_constraint_violation THEN
-- Swallow the exception, but do print out its error message:
RAISE NOTICE 'The constraint worked and made the UPDATE fail with message: %', SQLERRM;
END;$$;
......@@ -142,7 +142,7 @@ Folgende Schritte werden dabei durchlaufen:
ausgeführt werden):
``` bash
psql -U postgres -f 0_runAllScripts.sql
psql -U postgres -v ON_ERROR_STOP=on -f 0_runAllScripts.sql
```
- Windows
......@@ -162,7 +162,7 @@ Folgende Schritte werden dabei durchlaufen:
Datenbankbefehle auszuführen.
``` bash
sudo -u postgres psql -f 0_runAllScripts.sql
sudo -u postgres psql -v ON_ERROR_STOP=on -f 0_runAllScripts.sql
```
Alternativ kann man sich für den Linux-Login einen
......
@echo off
>nul chcp 1252
psql -U postgres -f 0_runAllScripts.sql
psql -U postgres -v ON_ERROR_STOP=on -f 0_runAllScripts.sql
pause
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment