使用after create 建立trigger記錄procedure PL/SQL程式碼變更

tolywang發表於2011-03-01

 How does one keep a history of PL/SQL code changes?
One can build a history of PL/SQL code changes by setting up an AFTER CREATE schema (or database) level trigger (available from Oracle 8.1.7). This will allow you to easily revert to previous code should someone make any catastrophic changes. Look at this example:

CREATE TABLE SOURCE_HIST                    -- Create history table
  AS SELECT SYSDATE CHANGE_DATE, ALL_SOURCE.*
  FROM   ALL_SOURCE WHERE 1=2;

CREATE OR REPLACE TRIGGER change_hist        -- Store code in hist table
  AFTER CREATE ON SCOTT.SCHEMA          -- Change SCOTT to your schema name
DECLARE
BEGIN
  IF ORA_DICT_OBJ_TYPE in ('PROCEDURE', 'FUNCTION',
                           'PACKAGE',   'PACKAGE BODY',
                           'TYPE',      'TYPE BODY')
  THEN
     -- Store old code in SOURCE_HIST table
     INSERT INTO SOURCE_HIST
            SELECT sysdate, all_source.* FROM ALL_SOURCE
             WHERE  TYPE = ORA_DICT_OBJ_TYPE  -- DICTIONARY_OBJ_TYPE IN 8i
               AND  NAME = ORA_DICT_OBJ_NAME; -- DICTIONARY_OBJ_NAME IN 8i
  END IF;
EXCEPTION
  WHEN OTHERS THEN
       raise_application_error(-20000, SQLERRM);
END;
/
show errors
A better approach is to create an external CVS or SVN repository for the scripts that install the PL/SQL code. The canonical version of what's in the database must match the latest CVS/SVN version or else someone would be cheating.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-688164/,如需轉載,請註明出處,否則將追究法律責任。

相關文章