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