1818
1919/**
2020 * @author Christian Beikov
21+ * @author Loïc Lefèvre
2122 */
2223public class OracleDatabaseCleaner implements DatabaseCleaner {
2324
@@ -33,6 +34,9 @@ public class OracleDatabaseCleaner implements DatabaseCleaner {
3334 "'XDB'," +
3435 "'WMSYS'" ;
3536
37+ private static final int SEQUENCE_DOES_NOT_EXIST = 2289 ;
38+ private static final int TABLE_OR_VIEW_DOES_NOT_EXIST = 942 ;
39+
3640 private final List <String > ignoredTables = new ArrayList <>();
3741 private final Map <String , List <String >> cachedTruncateTableSqlPerSchema = new HashMap <>();
3842 private final Map <String , List <String >> cachedConstraintDisableSqlPerSchema = new HashMap <>();
@@ -63,7 +67,7 @@ public void clearAllSchemas(Connection connection) {
6367 statement -> {
6468 try {
6569 return statement .executeQuery (
66- "SELECT 'DROP TABLE ' || owner || '.\" ' || table_name || '\" CASCADE CONSTRAINTS' " +
70+ "SELECT 'DROP TABLE \" ' || owner || '\" .\" ' || table_name || '\" CASCADE CONSTRAINTS PURGE ' " +
6771 "FROM all_tables " +
6872 // Only look at tables owned by the current user
6973 "WHERE owner = sys_context('USERENV', 'SESSION_USER')" +
@@ -74,7 +78,7 @@ public void clearAllSchemas(Connection connection) {
7478 // Exclude the tables with names starting like 'DEF$_'
7579 " AND table_name NOT LIKE 'DEF$\\ _%' ESCAPE '\\ '" +
7680 " UNION ALL " +
77- "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$%'"
81+ "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$%'"
7882 );
7983 }
8084 catch (SQLException sqlException ) {
@@ -93,18 +97,21 @@ public void clearSchema(Connection connection, String schemaName) {
9397 connection ,
9498 statement -> {
9599 try {
96- return statement .executeQuery (
97- "SELECT 'DROP TABLE ' || owner || '.\" ' || table_name || '\" CASCADE CONSTRAINTS' " +
98- "FROM all_tables " +
99- "WHERE owner = '" + schemaName + "'" +
100- // Normally, user tables aren't in sysaux
101- " AND tablespace_name NOT IN ('SYSAUX')" +
102- // Apparently, user tables have global stats off
103- " AND global_stats = 'NO'" +
104- // Exclude the tables with names starting like 'DEF$_'
105- " AND table_name NOT LIKE 'DEF$\\ _%' ESCAPE '\\ '" +
106- " UNION ALL " +
107- "SELECT 'DROP SEQUENCE ' || sequence_owner || '.' || sequence_name FROM all_sequences WHERE sequence_owner = '" + schemaName + "'"
100+ return statement .executeQuery ( String .format ("""
101+ SELECT 'DROP TABLE "' || owner || '"."' || table_name || '" CASCADE CONSTRAINTS PURGE'
102+ FROM all_tables
103+ WHERE owner = '%s'
104+ -- Normally, user tables aren't in sysaux
105+ AND tablespace_name NOT IN ('SYSAUX')
106+ -- Apparently, user tables have global stats off
107+ AND global_stats = 'NO'
108+ -- Exclude the tables with names starting like 'DEF$_'
109+ AND table_name NOT LIKE 'DEF$\\ _%' ESCAPE '\\ '
110+ UNION ALL
111+ SELECT 'DROP SEQUENCE "' || sequence_owner || '"."' || sequence_name || '"'
112+ FROM all_sequences
113+ WHERE sequence_owner = '%s'
114+ """ , schemaName , schemaName )
108115 );
109116 }
110117 catch (SQLException sqlException ) {
@@ -129,13 +136,21 @@ private void clearSchema0(Connection c, Function<Statement, ResultSet> sqlProvid
129136
130137 LOG .log ( Level .FINEST , "Dropping schema objects: START" );
131138 for ( String sql : sqls ) {
132- s .execute ( sql );
139+ try {
140+ s .execute ( sql );
141+ }
142+ catch (SQLException sqlException ) {
143+ switch ( sqlException .getErrorCode () ) {
144+ case SEQUENCE_DOES_NOT_EXIST :
145+ case TABLE_OR_VIEW_DOES_NOT_EXIST :
146+ // it's fine, object has been dropped already
147+ break ;
148+ default :
149+ throw sqlException ;
150+ }
151+ }
133152 }
134153 LOG .log ( Level .FINEST , "Dropping schema objects: END" );
135-
136- LOG .log ( Level .FINEST , "Committing: START" );
137- c .commit ();
138- LOG .log ( Level .FINEST , "Committing: END" );
139154 }
140155 catch (SQLException e ) {
141156 try {
@@ -250,10 +265,6 @@ private void clearData0(Connection connection, String schemaName, Function<State
250265 s .execute ( sql );
251266 }
252267 LOG .log ( Level .FINEST , "Enabling foreign keys: END" );
253-
254- LOG .log ( Level .FINEST , "Committing: START" );
255- connection .commit ();
256- LOG .log ( Level .FINEST , "Committing: END" );
257268 }
258269 catch (SQLException e ) {
259270 try {
@@ -266,5 +277,4 @@ private void clearData0(Connection connection, String schemaName, Function<State
266277 throw new RuntimeException ( e );
267278 }
268279 }
269-
270280}
0 commit comments