HHH-14446 Add PostgresqlDatabaseCleaner checks

Since the PostgreSQL JDBC driver is also used for CockroachDB, we need to test explicitly if the database is indeed PostgreSQL.
This commit is contained in:
Karel Maesen 2021-02-12 15:14:16 +01:00 committed by Christian Beikov
parent 0d140cc30c
commit 6cead49fec
1 changed files with 17 additions and 1 deletions

View File

@ -31,7 +31,8 @@ public class PostgreSQLDatabaseCleaner implements DatabaseCleaner {
@Override @Override
public boolean isApplicable(Connection connection) { public boolean isApplicable(Connection connection) {
try { try {
return connection.getMetaData().getDatabaseProductName().startsWith( "PostgreSQL" ); return connection.getMetaData().getDatabaseProductName().startsWith( "PostgreSQL" )
&& isPostgresql( connection );
} }
catch (SQLException e) { catch (SQLException e) {
throw new RuntimeException( "Could not resolve the database metadata!", e ); throw new RuntimeException( "Could not resolve the database metadata!", e );
@ -211,4 +212,19 @@ public class PostgreSQLDatabaseCleaner implements DatabaseCleaner {
} }
} }
// We need this check to differentiate between Postgresql and Cockroachdb
private boolean isPostgresql(Connection connection) {
try (Statement stmt = connection.createStatement()) {
ResultSet rs = stmt.executeQuery( "select version() " );
while ( rs.next() ) {
String version = rs.getString( 1 );
return version.contains( "PostgreSQL" );
}
}
catch (SQLException e) {
throw new RuntimeException( e );
}
return false;
}
} }