diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cleaner/OracleDatabaseCleaner.java b/hibernate-testing/src/main/java/org/hibernate/testing/cleaner/OracleDatabaseCleaner.java index 02a73785e35d..6e0f0fd0c439 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cleaner/OracleDatabaseCleaner.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cleaner/OracleDatabaseCleaner.java @@ -18,6 +18,7 @@ /** * @author Christian Beikov + * @author Loïc Lefèvre */ public class OracleDatabaseCleaner implements DatabaseCleaner { @@ -33,6 +34,9 @@ public class OracleDatabaseCleaner implements DatabaseCleaner { "'XDB'," + "'WMSYS'"; + private static final int SEQUENCE_DOES_NOT_EXIST = 2289; + private static final int TABLE_OR_VIEW_DOES_NOT_EXIST = 942; + private final List ignoredTables = new ArrayList<>(); private final Map> cachedTruncateTableSqlPerSchema = new HashMap<>(); private final Map> cachedConstraintDisableSqlPerSchema = new HashMap<>(); @@ -63,7 +67,7 @@ public void clearAllSchemas(Connection connection) { statement -> { try { return statement.executeQuery( - "SELECT 'DROP TABLE ' || owner || '.\"' || table_name || '\" CASCADE CONSTRAINTS' " + + "SELECT 'DROP TABLE \"' || owner || '\".\"' || table_name || '\" CASCADE CONSTRAINTS PURGE' " + "FROM all_tables " + // Only look at tables owned by the current user "WHERE owner = sys_context('USERENV', 'SESSION_USER')" + @@ -74,7 +78,7 @@ public void clearAllSchemas(Connection connection) { // Exclude the tables with names starting like 'DEF$_' " AND table_name NOT LIKE 'DEF$\\_%' ESCAPE '\\'" + " UNION ALL " + - "SELECT 'DROP SEQUENCE ' || sequence_owner || '.' || sequence_name FROM all_sequences WHERE sequence_owner = sys_context('USERENV', 'SESSION_USER') and sequence_name not like 'ISEQ$$%' and sequence_name not like 'MVIEW$%'" + "SELECT 'DROP SEQUENCE \"' || sequence_owner || '\".\"' || sequence_name || '\"' FROM all_sequences WHERE sequence_owner = sys_context('USERENV', 'SESSION_USER') and sequence_name not like 'ISEQ$$%' and sequence_name not like 'MVIEW$%'" ); } catch (SQLException sqlException) { @@ -93,18 +97,21 @@ public void clearSchema(Connection connection, String schemaName) { connection, statement -> { try { - return statement.executeQuery( - "SELECT 'DROP TABLE ' || owner || '.\"' || table_name || '\" CASCADE CONSTRAINTS' " + - "FROM all_tables " + - "WHERE owner = '" + schemaName + "'" + - // Normally, user tables aren't in sysaux - " AND tablespace_name NOT IN ('SYSAUX')" + - // Apparently, user tables have global stats off - " AND global_stats = 'NO'" + - // Exclude the tables with names starting like 'DEF$_' - " AND table_name NOT LIKE 'DEF$\\_%' ESCAPE '\\'" + - " UNION ALL " + - "SELECT 'DROP SEQUENCE ' || sequence_owner || '.' || sequence_name FROM all_sequences WHERE sequence_owner = '" + schemaName + "'" + return statement.executeQuery( String.format(""" + SELECT 'DROP TABLE "' || owner || '"."' || table_name || '" CASCADE CONSTRAINTS PURGE' + FROM all_tables + WHERE owner = '%s' + -- Normally, user tables aren't in sysaux + AND tablespace_name NOT IN ('SYSAUX') + -- Apparently, user tables have global stats off + AND global_stats = 'NO' + -- Exclude the tables with names starting like 'DEF$_' + AND table_name NOT LIKE 'DEF$\\_%%' ESCAPE '\\' + UNION ALL + SELECT 'DROP SEQUENCE "' || sequence_owner || '"."' || sequence_name || '"' + FROM all_sequences + WHERE sequence_owner = '%s' + """, schemaName, schemaName ) ); } catch (SQLException sqlException) { @@ -129,13 +136,21 @@ private void clearSchema0(Connection c, Function sqlProvid LOG.log( Level.FINEST, "Dropping schema objects: START" ); for ( String sql : sqls ) { - s.execute( sql ); + try { + s.execute( sql ); + } + catch(SQLException sqlException) { + switch ( sqlException.getErrorCode() ) { + case SEQUENCE_DOES_NOT_EXIST: + case TABLE_OR_VIEW_DOES_NOT_EXIST: + // it's fine, object has been dropped already + break; + default: + throw sqlException; + } + } } LOG.log( Level.FINEST, "Dropping schema objects: END" ); - - LOG.log( Level.FINEST, "Committing: START" ); - c.commit(); - LOG.log( Level.FINEST, "Committing: END" ); } catch (SQLException e) { try { @@ -250,10 +265,6 @@ private void clearData0(Connection connection, String schemaName, Function