2018-09-28 13:52:54 +03:00
|
|
|
module Hasura.RQL.DDL.Utils
|
|
|
|
( clearHdbViews
|
|
|
|
) where
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-10-19 05:15:28 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import Hasura.Prelude ((<>))
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
clearHdbViews :: Q.Tx ()
|
2018-10-19 05:15:28 +03:00
|
|
|
clearHdbViews = Q.multiQ (Q.fromText (clearHdbOnlyViews <> clearHdbViewsFunc))
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-19 05:15:28 +03:00
|
|
|
clearHdbOnlyViews :: T.Text
|
2018-09-05 14:26:46 +03:00
|
|
|
clearHdbOnlyViews =
|
2018-06-27 16:11:32 +03:00
|
|
|
"DO $$ DECLARE \
|
|
|
|
\ r RECORD; \
|
|
|
|
\ BEGIN \
|
|
|
|
\ FOR r IN (SELECT viewname FROM pg_views WHERE schemaname = 'hdb_views') LOOP \
|
|
|
|
\ EXECUTE 'DROP VIEW IF EXISTS hdb_views.' || quote_ident(r.viewname) || ' CASCADE'; \
|
|
|
|
\ END LOOP; \
|
2018-09-05 14:26:46 +03:00
|
|
|
\ END $$; "
|
|
|
|
|
|
|
|
|
2018-10-19 05:15:28 +03:00
|
|
|
clearHdbViewsFunc :: T.Text
|
2018-09-05 14:26:46 +03:00
|
|
|
clearHdbViewsFunc =
|
|
|
|
"DO $$ DECLARE \
|
|
|
|
\ _sql text; \
|
|
|
|
\ BEGIN \
|
|
|
|
\ SELECT INTO _sql \
|
|
|
|
\ string_agg('DROP FUNCTION hdb_views.' || quote_ident(r.routine_name) || '() CASCADE;' \
|
|
|
|
\ , E'\n') \
|
|
|
|
\ FROM information_schema.routines r \
|
|
|
|
\ WHERE r.specific_schema = 'hdb_views'; \
|
|
|
|
\ IF _sql IS NOT NULL THEN \
|
|
|
|
\ EXECUTE _sql; \
|
|
|
|
\ END IF; \
|
|
|
|
\ END $$; "
|