Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @author Christian Beikov
* @author Loïc Lefèvre
*/
public class OracleDatabaseCleaner implements DatabaseCleaner {

Expand All @@ -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<String> ignoredTables = new ArrayList<>();
private final Map<String, List<String>> cachedTruncateTableSqlPerSchema = new HashMap<>();
private final Map<String, List<String>> cachedConstraintDisableSqlPerSchema = new HashMap<>();
Expand Down Expand Up @@ -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')" +
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -129,13 +136,21 @@ private void clearSchema0(Connection c, Function<Statement, ResultSet> 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 {
Expand Down Expand Up @@ -250,10 +265,6 @@ private void clearData0(Connection connection, String schemaName, Function<State
s.execute( sql );
}
LOG.log( Level.FINEST, "Enabling foreign keys: END" );

LOG.log( Level.FINEST, "Committing: START" );
connection.commit();
LOG.log( Level.FINEST, "Committing: END" );
}
catch (SQLException e) {
try {
Expand All @@ -266,5 +277,4 @@ private void clearData0(Connection connection, String schemaName, Function<State
throw new RuntimeException( e );
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ test {
// Log a statement for each test.
// Used in the Travis build so that Travis doesn't end up panicking because there's no output for a long time.
testLogging {
displayGranularity 1
events "passed", "skipped", "failed"
exceptionFormat = 'full'
}
Expand Down