Rem
Rem $Header: emcore/source/oracle/sysman/emdrep/sql/core/13.2.0.0/events/events_types.sql rbali_oneoff_updated/1 2025/03/14 06:41:13 rbali Exp $
Rem
Rem events_types.sql
Rem
Rem Copyright (c) 2015, 2025, Oracle and/or its affiliates. 
Rem All rights reserved.

Rem BEGIN EMRU_FILE_METADATA
Rem EMRU_SOURCE_FILE:emcore/source/oracle/sysman/emdrep/sql/core/13.2.0.0/events/events_types.sql
Rem EMRU_UNIX_DELIVERABLES:
Rem emcore/source/oracle/sysman/emdrep/sql/core/13.2.0.0/events/events_types.sql

Rem EMRU_FILE_POSTINSTALL:<!!! enter the view based location of the executable file. If there is NONE leave as-is !!!>
Rem EMRU_GENERIC_FLAG:NONE
Rem END EMRU_FILE_METADATA

Rem
Rem    NAME
Rem      events_types.sql
Rem
Rem    DESCRIPTION
Rem      events types upgrade script
Rem
Rem    NOTES
Rem      <other useful comments, qualifications, etc.>
Rem
Rem    BEGIN SQL_FILE_METADATA 
Rem    SQL_SOURCE_FILE: emcore/source/oracle/sysman/emdrep/sql/core/13.2.0.0/events/events_types.sql 
Rem    SQL_SHIPPED_FILE: 
Rem    SQL_PHASE: 
Rem    SQL_STARTUP_MODE: NORMAL 
Rem    SQL_IGNORABLE_ERRORS: NONE 
Rem    SQL_CALLING_FILE: 
Rem    END SQL_FILE_METADATA
Rem
Rem    MODIFIED   (MM/DD/YY)
Rem    gbafna      01/07/25 - make synonyms noneditionable
Rem    ziziwang    05/15/15 - Created
Rem

ALTER SESSION SET CURRENT_SCHEMA=SYSMAN;
-- Add ticket_type to em_incident_obj
-- Following the Alter type template in 12.1.0.3/events/events_types.sql
DECLARE
  l_sql VARCHAR2(4000);
  l_created_type_names SYSMAN.MGMT_SHORT_STRING_ARRAY := SYSMAN.MGMT_SHORT_STRING_ARRAY();
  TYPE l_dependency IS TABLE OF VARCHAR2(30);
  l_type_dependency l_dependency := l_dependency();
  MODULE_NAME CONSTANT VARCHAR2(20) := 'EM_EVENT_UPGRADE' ;
  SCHEMA_SYSMAN_TYPES CONSTANT VARCHAR2(12) := 'SYSMAN_TYPES';

--
-- PURPOSE
--   Recompiles invalid dependent objects before retrying
--
PROCEDURE recompile_dependendents(p_type_name IN VARCHAR2)
IS
l_compile_obj varchar2(19);
l_compile_subobj varchar2(19);
l_compile_cmd    varchar2(256);
BEGIN
  -- Look at all of the invalid objects and log them using a debug-level logger command
  FOR invalid_obj_rec IN (SELECT * 
                            FROM all_objects 
                           WHERE status <> 'VALID'
                            AND object_name IN (SELECT name 
                                                  FROM all_dependencies 
                                                  WHERE referenced_name = p_type_name)
                            AND owner = SCHEMA_SYSMAN_TYPES)
  LOOP 
    SYSMAN.MGMT_EMDEP_LOGGER.log_message(p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_DEBUG,
                                         p_component_name => MODULE_NAME,
                                         p_message_id => SQLCODE,
                                         p_message => 'Invalid objects: '  ||
                                         invalid_obj_rec.owner || ' ' ||
                                         invalid_obj_rec.object_name || ' ' ||
                                         invalid_obj_rec.object_type || ' ' ||
                                         invalid_obj_rec.status || ' ' ||
                                         to_char(invalid_obj_rec.last_ddl_time,'yyyymmdd hh24:mi:ss') );

    -- try to recompile
    IF invalid_obj_rec.object_type in ('PACKAGE', 'PACKAGE BODY', 'PROCEDURE', 'FUNCTION','TRIGGER') 
    THEN
      l_compile_obj := case invalid_obj_rec.object_type when 'PACKAGE BODY' then  'PACKAGE' else invalid_obj_rec.object_type end;
      l_compile_subobj := case invalid_obj_rec.object_type when 'PACKAGE BODY' then 'BODY' else null end;
      l_compile_cmd := 'ALTER ' || l_compile_obj || ' ' || invalid_obj_rec.owner || '.' || invalid_obj_rec.object_name || ' COMPILE ' || l_compile_subobj;
      BEGIN
        EXECUTE IMMEDIATE l_compile_cmd;
        SYSMAN.MGMT_EMDEP_LOGGER.log_message(p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_DEBUG,
                                          p_component_name => MODULE_NAME,
                                          p_message_id => SQLCODE,
                                          p_message => 'RECOMPILED OBJECT : '  || l_compile_cmd);
      EXCEPTION 
        -- If there are problems recompiling the dependent objects, log a severe error and stop trying
        WHEN OTHERS THEN 
          SYSMAN.MGMT_EMDEP_LOGGER.log_message(p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_SEVERE,
                                               p_component_name => MODULE_NAME,
                                               p_message_id => SQLCODE,
                                               p_message => 'ERROR: COULD NOT EXECUTE : '  || l_compile_cmd);
        RAISE;
      END;
    END IF;
  END LOOP;
END recompile_dependendents;

-- Utility for determining if max time allowed has elapsed
FUNCTION max_time_elapsed(p_type_name IN VARCHAR2,
                          p_failure1_count IN NUMBER,
                          p_failure2_count IN NUMBER, 
                          p_failure3_count IN NUMBER,
                          p_start_time IN NUMBER,
                          p_max_time_in_seconds IN NUMBER)
RETURN BOOLEAN
IS
l_elapsed_time NUMBER;
BEGIN
  l_elapsed_time := (dbms_utility.get_time -  p_start_time)/100;  

  IF l_elapsed_time >= p_max_time_in_seconds
  THEN
    -- Log an error for the failed operation
    SYSMAN.MGMT_EMDEP_LOGGER.log_message
      (p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_SEVERE, 
       p_component_name => MODULE_NAME, 
       p_message_id => SQLCODE, 
       p_message => 'Failed to recover from intermittent errors while altering type: ' || 
                    p_type_name ||
                    ' [ORA-04020 count: ' || p_failure1_count ||
                    ' ][ORA-22324 count: ' || p_failure2_count || ']' ||
                    ' ][ORA-22933 count: ' || p_failure3_count || ']' ||
                    ' elapsed_time: ' || round(l_elapsed_time, 2));

    RETURN TRUE;
  END IF;

  RETURN FALSE;
END max_time_elapsed;

--
-- PURPOSE
-- internal function for events type upgrade retry
-- when error is encountered
--
PROCEDURE schema_upgrade_with_retry(p_alter_type_ddl IN VARCHAR2,
                                    p_type_name IN VARCHAR2)
IS
l_max_elapsed_time NUMBER := 900;
l_start_time NUMBER :=0;
l_elapsed_time NUMBER;
l_failure1_count NUMBER := 0;
l_failure2_count NUMBER := 0;
l_failure3_count NUMBER := 0;
deadlock_detected EXCEPTION;
compilation_error EXCEPTION;
table_error EXCEPTION;
PRAGMA EXCEPTION_INIT(deadlock_detected, -4020);
PRAGMA EXCEPTION_INIT(compilation_error,-22324);
PRAGMA EXCEPTION_INIT(table_error,-22933);
BEGIN
  l_start_time :=  DBMS_UTILITY.GET_TIME;
  LOOP
    BEGIN
      EXECUTE IMMEDIATE p_alter_type_ddl;

      -- Log a warning message if an exception occurred in the past to indicate completion of operation
      IF l_failure1_count + l_failure2_count + l_failure3_count > 0
      THEN
        l_elapsed_time := (dbms_utility.get_time -  l_start_time)/100;  

        SYSMAN.MGMT_EMDEP_LOGGER.log_message
            (p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_WARNING, 
             p_component_name => MODULE_NAME, 
             p_message_id => SQLCODE, 
             p_message => 'Recovered from intermittent errors while altering type: ' || p_type_name ||
                          ' [ORA-04020 count: ' || l_failure1_count ||
                          ' ][ORA-22324 count: ' || l_failure2_count || ']' ||
                          ' ][ORA-22933 count: ' || l_failure3_count || ']' ||
                          ' elapsed_time: ' || round(l_elapsed_time, 2));
      END IF;

      -- If the code reaches here, type changes have been completed, so exit the loop.
      EXIT;

    EXCEPTION 
      --
      -- Rarely ORA-04020 is raised when executing the 'alter type' statements.
      -- pid#7144356 has tracked this issue.
      --
      WHEN deadlock_detected THEN

        -- Log a warning message for the first exception raised
        IF l_failure1_count = 0
        THEN
          SYSMAN.MGMT_EMDEP_LOGGER.log_message
              (p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_WARNING, 
               p_component_name => MODULE_NAME, 
               p_message_id => SQLCODE, 
               p_message => 'Encountered ORA-04020 exception, while altering type: ' || p_type_name || '. Will try to recover by retrying the operation periodically for a short period of time.') ;
        END IF;

        l_failure1_count := l_failure1_count + 1;
  
        IF max_time_elapsed(p_type_name, l_failure1_count, l_failure2_count, l_failure3_count,
                            l_start_time, l_max_elapsed_time)
        THEN
          RAISE;
        END IF;

      -- has not been compiled properly.  Try to recompile those dependent objects before retrying the original TYPE alteration.
      WHEN compilation_error THEN

        -- Log a warning message for the first exception raised
        IF l_failure2_count = 0
        THEN
          SYSMAN.MGMT_EMDEP_LOGGER.log_message
              (p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_WARNING,
               p_component_name => MODULE_NAME,
               p_message_id => SQLCODE,
               p_message => SQLERRM ||' Encountered ORA-22324 exception, while altering type: ' || p_type_name || '. Will try to recover by retrying the operation periodically for a short period of time.') ;
        END IF;

        l_failure2_count := l_failure2_count + 1;

        recompile_dependendents(p_type_name);

        IF max_time_elapsed(p_type_name, l_failure1_count, l_failure2_count, l_failure3_count,
                            l_start_time, l_max_elapsed_time)
        THEN
          RAISE;
        END IF;

      -- has not been compiled properly.  Try to recompile those dependent objects before retrying the original TYPE alteration.
      WHEN table_error THEN

        -- Log a warning message for the first exception raised
        IF l_failure3_count = 0
        THEN
          SYSMAN.MGMT_EMDEP_LOGGER.log_message
              (p_log_level => SYSMAN.MGMT_EMDEP_LOGGER.G_LOG_LEVEL_WARNING,
               p_component_name => MODULE_NAME,
               p_message_id => SQLCODE,
               p_message => SQLERRM ||' Encountered ORA-22933 exception, while altering type: ' || p_type_name || '. Will try to recover by retrying the operation periodically for a short period of time.') ;
        END IF;

        l_failure3_count := l_failure3_count + 1;

        recompile_dependendents(p_type_name);

        IF max_time_elapsed(p_type_name, l_failure1_count, l_failure2_count, l_failure3_count,
                            l_start_time, l_max_elapsed_time)
        THEN
          RAISE;
        END IF;

      -- NOTE: Ensure that other exceptions are raised, per recommendation from schema review team.
      -- PLSQL engine intermittently fails to raise other exceptions without the explict the block below.
      WHEN OTHERS THEN
        RAISE;
    END;
  END LOOP;
END schema_upgrade_with_retry ;

PROCEDURE drop_synonym(p_synonym IN VARCHAR2)
IS
BEGIN
  EXECUTE IMMEDIATE 'DROP SYNONYM ' || p_synonym;
EXCEPTION WHEN OTHERS THEN 
  IF (SQLCODE = -1434) 
  THEN
    NULL;
  ELSE
    SYSMAN.MGMT_EMDEP_LOGGER.log_message(1, 'EBR_UPGRADE', 'Failed to Drop Synonym ' || p_synonym, 'Error : ' || SQLERRM);
    RAISE;
  END IF;
END drop_synonym;

BEGIN
  EXECUTE IMMEDIATE 'ALTER SESSION SET CURRENT_SCHEMA=&EM_REPOS_USER';

  drop_synonym('EM_INCIDENT_OBJ');
  EXECUTE IMMEDIATE q'[select DISTINCT(NAME) FROM DBA_DEPENDENCIES 
    start with REFERENCED_NAME='EM_INCIDENT_OBJ' and TYPE='TYPE' CONNECT BY NOCYCLE PRIOR NAME = REFERENCED_NAME 
      and TYPE = 'TYPE']' BULK COLLECT INTO l_type_dependency;

  IF l_type_dependency.COUNT > 0 THEN
    FOR i IN l_type_dependency.first .. l_type_dependency.last LOOP
      drop_synonym(l_type_dependency(i));
    END LOOP;
  END IF;

  l_sql := 'ALTER TYPE SYSMAN_TYPES.EM_INCIDENT_OBJ ADD ATTRIBUTE (
                            TICKET_TYPE VARCHAR2(20)) CASCADE 
                            NOT INCLUDING TABLE DATA';

  schema_upgrade_with_retry(p_alter_type_ddl => l_sql, p_type_name => 'EM_INCIDENT_OBJ');

  EXECUTE IMMEDIATE 'CREATE OR REPLACE NONEDITIONABLE SYNONYM EM_INCIDENT_OBJ FOR SYSMAN_TYPES.EM_INCIDENT_OBJ';

  FOR i IN l_type_dependency.first .. l_type_dependency.last LOOP
    EXECUTE IMMEDIATE 'CREATE OR REPLACE NONEDITIONABLE SYNONYM ' || l_type_dependency(i) || ' FOR SYSMAN_TYPES.' || l_type_dependency(i);
  END LOOP;

END;
/


