SET DEFINE OFF
WHENEVER SQLERROR EXIT SQL.SQLCODE

-- SRD_SUPPORT: drop every object the platform owns and reinstall from the numbered scripts.
-- The schema is the platform's alone (Software Architecture § 8 rule 6; D47: one user, owner and
-- runtime alike), so "everything in USER_TABLES" is the platform's table set and nothing else.
-- Never run against a schema that holds anything but SRD_SUPPORT.

DECLARE
    v_schema VARCHAR2(128);
BEGIN
    SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') INTO v_schema FROM DUAL;
    IF v_schema <> 'SRD_SUPPORT' THEN
        RAISE_APPLICATION_ERROR(-20001, 'install-clean.sql must run as SRD_SUPPORT');
    END IF;

    FOR existing_view IN (SELECT VIEW_NAME FROM USER_VIEWS)
    LOOP
        EXECUTE IMMEDIATE 'DROP VIEW "SRD_SUPPORT"."' || existing_view.VIEW_NAME || '"';
    END LOOP;

    FOR existing_table IN (SELECT TABLE_NAME FROM USER_TABLES)
    LOOP
        EXECUTE IMMEDIATE 'DROP TABLE "SRD_SUPPORT"."' || existing_table.TABLE_NAME || '" CASCADE CONSTRAINTS PURGE';
    END LOOP;

    FOR existing_seq IN (SELECT SEQUENCE_NAME FROM USER_SEQUENCES)
    LOOP
        EXECUTE IMMEDIATE 'DROP SEQUENCE "SRD_SUPPORT"."' || existing_seq.SEQUENCE_NAME || '"';
    END LOOP;
END;
/

@@upgrade.sql
