In my open-source database migration project Flyway, I have a feature that cleans all objects in the current database schema, without dropping the schema itself.
The typical implementation works as follows:
- List all objects
- Generate drop statements for these objects
Oracle Spatial Indexes have been causing me a lot of grief though.
How can I reliably enumerate them in order to produce DROP INDEX xyz statements?
Note: This must work on both XE, 10g and 11g. All references in the MDSYS schema must be gone.
My current solution looks like this:
On XE:
- DELETE FROM mdsys.user_sdo_geom_metadata
- DELETE FROM mdsys.sdo_index_metadata_table WHERE sdo_index_owner = USER
- SELECT object_type, object_name FROM user_objects WHERE object_type = 'TABLE'
- DROP *table_name* CASCADE CONSTRAINTS PURGE /* for all tables */
On Oracle 10g:
- DELETE FROM mdsys.user_sdo_geom_metadata
- SELECT object_type, object_name FROM user_objects WHERE object_type = 'TABLE' and object_name not like 'MDRT_%$'
- DROP *table_name* CASCADE CONSTRAINTS PURGE /* for all tables */
10g seems to cascade the removal of the metadata in MDSYS.sdo_index_metadata_table and the removal of the spatial index tables (MDRT_1234$ and the like).
XE doesn't.
Both 10g and XE don't cascade the removal of the metadata in MDSYS.user_sdo_geom_metadata