18
18
19
19
/**
20
20
* @author Christian Beikov
21
+ * @author Loïc Lefèvre
21
22
*/
22
23
public class OracleDatabaseCleaner implements DatabaseCleaner {
23
24
@@ -33,6 +34,9 @@ public class OracleDatabaseCleaner implements DatabaseCleaner {
33
34
"'XDB'," +
34
35
"'WMSYS'" ;
35
36
37
+ private static final int SEQUENCE_DOES_NOT_EXIST = 2289 ;
38
+ private static final int TABLE_OR_VIEW_DOES_NOT_EXIST = 942 ;
39
+
36
40
private final List <String > ignoredTables = new ArrayList <>();
37
41
private final Map <String , List <String >> cachedTruncateTableSqlPerSchema = new HashMap <>();
38
42
private final Map <String , List <String >> cachedConstraintDisableSqlPerSchema = new HashMap <>();
@@ -63,7 +67,7 @@ public void clearAllSchemas(Connection connection) {
63
67
statement -> {
64
68
try {
65
69
return statement .executeQuery (
66
- "SELECT 'DROP TABLE ' || owner || '.\" ' || table_name || '\" CASCADE CONSTRAINTS' " +
70
+ "SELECT 'DROP TABLE \" ' || owner || '\" .\" ' || table_name || '\" CASCADE CONSTRAINTS PURGE ' " +
67
71
"FROM all_tables " +
68
72
// Only look at tables owned by the current user
69
73
"WHERE owner = sys_context('USERENV', 'SESSION_USER')" +
@@ -74,7 +78,7 @@ public void clearAllSchemas(Connection connection) {
74
78
// Exclude the tables with names starting like 'DEF$_'
75
79
" AND table_name NOT LIKE 'DEF$\\ _%' ESCAPE '\\ '" +
76
80
" 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$%'"
78
82
);
79
83
}
80
84
catch (SQLException sqlException ) {
@@ -93,18 +97,21 @@ public void clearSchema(Connection connection, String schemaName) {
93
97
connection ,
94
98
statement -> {
95
99
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 )
108
115
);
109
116
}
110
117
catch (SQLException sqlException ) {
@@ -129,13 +136,21 @@ private void clearSchema0(Connection c, Function<Statement, ResultSet> sqlProvid
129
136
130
137
LOG .log ( Level .FINEST , "Dropping schema objects: START" );
131
138
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
+ }
133
152
}
134
153
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" );
139
154
}
140
155
catch (SQLException e ) {
141
156
try {
@@ -250,10 +265,6 @@ private void clearData0(Connection connection, String schemaName, Function<State
250
265
s .execute ( sql );
251
266
}
252
267
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" );
257
268
}
258
269
catch (SQLException e ) {
259
270
try {
@@ -266,5 +277,4 @@ private void clearData0(Connection connection, String schemaName, Function<State
266
277
throw new RuntimeException ( e );
267
278
}
268
279
}
269
-
270
280
}
0 commit comments