From eb451f2c3aafc231cf05c81ff3e94f73b98ee586 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 7 Jul 2020 12:13:36 -0500 Subject: SONAR-13594 Upgrade to 8.4 fails on MS SQL Server --- .../src/main/java/org/sonar/db/DatabaseUtils.java | 49 +++++++++++++++------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'server/sonar-db-core/src/main/java') diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java index fff31a94824..ff53e51f5d8 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java @@ -39,6 +39,7 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.function.IntFunction; @@ -282,10 +283,10 @@ public class DatabaseUtils { } /** - * @param table case-insensitive name of table - * @return true if a table exists with this name, otherwise false - * @throws SQLException - */ + * @param table case-insensitive name of table + * @return true if a table exists with this name, otherwise false + * @throws SQLException + */ public static boolean tableExists(String table, Connection connection) { return doTableExists(table, connection) || doTableExists(table.toLowerCase(Locale.ENGLISH), connection) || @@ -310,25 +311,43 @@ public class DatabaseUtils { } } - public static boolean indexExists(String table, String index, Connection connection) { - return doIndexExists(table, index, connection) || - doIndexExists(table.toLowerCase(Locale.ENGLISH), index, connection) || - doIndexExists(table.toUpperCase(Locale.ENGLISH), index, connection); + public static boolean indexExistsIgnoreCase(String table, String index, Connection connection) { + return doIndexExistsIgnoreIndexCase(table, index, connection) || + doIndexExistsIgnoreIndexCase(table.toLowerCase(Locale.ENGLISH), index, connection) || + doIndexExistsIgnoreIndexCase(table.toUpperCase(Locale.ENGLISH), index, connection); + } + + private static boolean doIndexExistsIgnoreIndexCase(String table, String index, Connection connection) { + return findIndex(connection, table, index).isPresent(); } - private static boolean doIndexExists(String table, String index, Connection connection) { + /** + * Finds an index by searching by its lower case or upper case name. If an index is found, it's name is returned with the matching case. + * This is useful when we need to drop an index that could exist with either lower case or upper case name. + * See SONAR-13594 + */ + public static Optional findExistingIndex(Connection connection, String tableName, String indexName) { + Optional result = findIndex(connection, tableName.toLowerCase(Locale.US), indexName); + if (result.isPresent()) { + return result; + } + // in tests, tables have uppercase name + return findIndex(connection, tableName.toUpperCase(Locale.US), indexName); + } + + private static Optional findIndex(Connection connection, String tableName, String indexName) { String schema = getSchema(connection); - try (ResultSet rs = connection.getMetaData().getIndexInfo(connection.getCatalog(), schema, table, false, true)) { + try (ResultSet rs = connection.getMetaData().getIndexInfo(connection.getCatalog(), schema, tableName, false, true)) { while (rs.next()) { - String indexName = rs.getString("INDEX_NAME"); - if (index.equalsIgnoreCase(indexName)) { - return true; + String idx = rs.getString("INDEX_NAME"); + if (indexName.equalsIgnoreCase(idx)) { + return Optional.of(idx); } } - return false; + return Optional.empty(); } catch (SQLException e) { - throw wrapSqlException(e, "Can not check that table %s exists", table); + throw wrapSqlException(e, "Can not check that table %s exists", tableName); } } -- cgit v1.2.3