The goal is to execute a script hi.sh after an assertion in SQL script.
hi.sh contains:
#!/bin/bash
echo 'Hello World'
I'm trying to execute hi.sh within DO:
DO $$
BEGIN
ASSERT EXISTS (SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'public'), '"Public" schema is empty';
\! ./hi.sh
END$$;
It should raise error '"Public" schema is empty' and not execute hi.sh but once there are entries in public schema, hi.sh must be executed.
Running the sql script raises error:
psql:assert.sql:5: ERROR: syntax error at or near "\"
LINE 4: \! ./hi.sh
^
However, it will execute shell script once '\!' line is outside DO, like this:
\! ./hi.sh
How can we create a simple SQL script for executing hi.sh based on assertion?