@@ -279,12 +279,33 @@ public SQLConnection createDatabase(PostgresGlobalState globalState) throws SQLE
279
279
}
280
280
Connection con = DriverManager .getConnection ("jdbc:" + entryURL , username , password );
281
281
globalState .getState ().logStatement (String .format ("\\ c %s;" , entryDatabaseName ));
282
- globalState .getState ().logStatement ("DROP DATABASE IF EXISTS " + databaseName );
283
- createDatabaseCommand = getCreateDatabaseCommand (globalState );
284
- globalState .getState ().logStatement (createDatabaseCommand );
282
+
283
+ String dropCommand = "DROP DATABASE" ;
284
+ boolean forceDrop = Randomly .getBoolean ();
285
+ if (forceDrop ) {
286
+ dropCommand += " FORCE" ;
287
+ }
288
+ dropCommand += " IF EXISTS " + databaseName ;
289
+
290
+ globalState .getState ().logStatement (dropCommand + ";" );
285
291
try (Statement s = con .createStatement ()) {
286
- s .execute ("DROP DATABASE IF EXISTS " + databaseName );
292
+ s .execute (dropCommand );
293
+ } catch (SQLException e ) {
294
+ // If force fails, fall back to regular drop
295
+ if (forceDrop ) {
296
+ String fallbackDrop = "DROP DATABASE IF EXISTS " + databaseName ;
297
+ globalState .getState ().logStatement (fallbackDrop + ";" );
298
+ try (Statement s = con .createStatement ()) {
299
+ s .execute (fallbackDrop );
300
+ }
301
+ } else {
302
+ throw e ;
303
+ }
287
304
}
305
+
306
+ // Create database section
307
+ createDatabaseCommand = getCreateDatabaseCommand (globalState );
308
+ globalState .getState ().logStatement (createDatabaseCommand + ";" );
288
309
try (Statement s = con .createStatement ()) {
289
310
s .execute (createDatabaseCommand );
290
311
}
0 commit comments