Database DevOps for Oracle

Find invalid objects

An object can be rendered "invalid" if it references objects that don't exist. This is a common occurrence when objects are renamed without also renaming the references in dependent objects.

If an object is invalid it cannot compile and therefore the existence of invalid objects in your database is a good reason to fail the build.

Fortunately it is relatively straightforward to identify invalid objects in Oracle using the following query.

get_invalid_objects.sql Toggle source code

  1. SELECT 'Invalid Object', object_type, object_name
  2. FROM dba_objects
  3. WHERE status != 'VALID'
  4. AND owner = 'SOCO_TEST'
  5. ORDER BY object_type;

If invalid objects have slipped into production, after resolving the issue it is recommended for performance reasons to explicitly recompile rather than relying on run-time recompilation.

Worked example

Here is an example of how to fail the build if invalid objects are detected.

  1. We start by embedding the above SQL, saves it to a file, get_invalid_objects.sql so we can pass this into sqlplus.
  2. Using sqlplus, run get_invalid_objects.sql against our test database.
  3. Using the find command, search the output for "Invalid Object" and, if found, set the ERRORLEVEL to a value that will fail the build.

CI Build - Invalid Objects Toggle source code

  1. rem Save the script that lists invalid objects to a file
  2. echo SELECT 'Invalid Object', object_type, object_name FROM dba_objects WHERE status != 'VALID' AND owner = 'SOCO_TEST' ORDER BY object_type; > get_invalid_objects.sql
  3.  
  4. rem Execute the script on the database
  5. echo on
  6. Call exit | sqlplus SOCO_TEST/demopassword@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=XE))) @get_invalid_objects.sql > Artifacts/invalid_objects.txt
  7. echo off
  8.  
  9. rem Type the output of the invalid objects query to the console
  10. type Artifacts\invalid_objects.txt
  11.  
  12. rem Now search for instances of "Invalid Object"
  13. call find /c "Invalid Object" Artifacts/invalid_objects.txt
  14.  
  15. for /f %%A in ('find /c "Invalid Object" ^< Artifacts/invalid_objects.txt') do (
  16. if %%A == 0 (
  17. echo No Invalid Objects
  18. SET ERRORLEVEL=0
  19. ) else (
  20. echo Invalid Objects found
  21. SET ERRORLEVEL=1
  22. )
  23. )

Didn't find what you were looking for?