I’ve got a view in my PostgreSQL, which can be both normal or materialized, depending on some circumstances. I'm trying to write a query that would drop the view for sure and with no errors no matter what type it has got at the moment. However, this does not seem to be easy. When I try to use the following code, I get an error:
DROP VIEW IF EXISTS {{ schema }}.{{ viewName }};
DROP MATERIALIZED VIEW IF EXISTS {{ schema }}.{{ viewName }};
SQLSTATE[42809]: Wrong object type: 7 ERROR: "{{ viewName }}" is not a view
HINT: Use DROP MATERIALIZED VIEW to remove a materialized view.
Thus, it looks like ‘IF EXISTS’ results true in the first row when the view is materialized, DROP command launches, but fails because the type of the view is wrong (it is not normal). Could anyone suggest a universal workaround that would both work for both materialized and normal view?