-- Drop policies first (they reference the visibility helpers), then the helpers.

DROP POLICY IF EXISTS holidays_select          ON paliad.holidays;
DROP POLICY IF EXISTS deadline_rules_select    ON paliad.deadline_rules;
DROP POLICY IF EXISTS proceeding_types_select  ON paliad.proceeding_types;

DROP POLICY IF EXISTS notizen_all        ON paliad.notizen;

DROP POLICY IF EXISTS akten_events_all   ON paliad.akten_events;
DROP POLICY IF EXISTS dokumente_all      ON paliad.dokumente;

DROP POLICY IF EXISTS termine_delete     ON paliad.termine;
DROP POLICY IF EXISTS termine_update     ON paliad.termine;
DROP POLICY IF EXISTS termine_insert     ON paliad.termine;
DROP POLICY IF EXISTS termine_select     ON paliad.termine;

DROP POLICY IF EXISTS fristen_all        ON paliad.fristen;
DROP POLICY IF EXISTS parteien_all       ON paliad.parteien;

DROP POLICY IF EXISTS akten_delete       ON paliad.akten;
DROP POLICY IF EXISTS akten_update       ON paliad.akten;
DROP POLICY IF EXISTS akten_insert       ON paliad.akten;
DROP POLICY IF EXISTS akten_select       ON paliad.akten;

DROP POLICY IF EXISTS users_insert_self  ON paliad.users;
DROP POLICY IF EXISTS users_update_self  ON paliad.users;
DROP POLICY IF EXISTS users_select_all   ON paliad.users;

-- Now safe to drop the polymorphic notiz visibility helper.
DROP FUNCTION IF EXISTS paliad.notiz_is_visible(uuid, uuid, uuid, uuid);

ALTER TABLE paliad.holidays          DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.deadline_rules    DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.proceeding_types  DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.notizen           DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.akten_events      DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.dokumente         DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.termine           DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.fristen           DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.parteien          DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.akten             DISABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.users             DISABLE ROW LEVEL SECURITY;
