aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-core
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2019-06-26 15:56:09 +0200
committerSonarTech <sonartech@sonarsource.com>2019-06-28 20:21:12 +0200
commit515876435fd5a935147ea0e52b26ac798c269541 (patch)
treee67e04b6a84c5da4e8a1e43220d1fc48623d1509 /server/sonar-db-core
parent98d70ead6633d2614890c83fe36709485d149ecd (diff)
downloadsonarqube-515876435fd5a935147ea0e52b26ac798c269541.tar.gz
sonarqube-515876435fd5a935147ea0e52b26ac798c269541.zip
SONAR-12127 optimize migrations on LIVE_MEASURES and PROJECT_MEASURES
Diffstat (limited to 'server/sonar-db-core')
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java50
-rw-r--r--server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java10
2 files changed, 48 insertions, 12 deletions
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 cc6a884b9b7..cd2a7d3a429 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
@@ -294,17 +294,7 @@ public class DatabaseUtils {
}
private static boolean doTableExists(String table, Connection connection) {
- String schema = null;
-
- try {
- // Using H2 with a JDBC TCP connection is throwing an exception
- // See org.h2.engine.SessionRemote#getCurrentSchemaName()
- if (!"H2 JDBC Driver".equals(connection.getMetaData().getDriverName())) {
- schema = connection.getSchema();
- }
- } catch (SQLException e) {
- Loggers.get(DatabaseUtils.class).warn("Fail to determine schema. Keeping it null for searching tables", e);
- }
+ String schema = getSchema(connection);
// table type is used to speed-up Oracle by removing introspection of system tables and aliases.
try (ResultSet rs = connection.getMetaData().getTables(connection.getCatalog(), schema, table, TABLE_TYPE)) {
@@ -320,6 +310,44 @@ 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);
+ }
+
+ private static boolean doIndexExists(String table, String index, Connection connection) {
+ String schema = getSchema(connection);
+
+ try (ResultSet rs = connection.getMetaData().getIndexInfo(connection.getCatalog(), schema, table, false, true)) {
+ while (rs.next()) {
+ String indexName = rs.getString("INDEX_NAME");
+ if (index.equalsIgnoreCase(indexName)) {
+ return true;
+ }
+ }
+ return false;
+ } catch (SQLException e) {
+ throw wrapSqlException(e, "Can not check that table %s exists", table);
+ }
+ }
+
+ @CheckForNull
+ private static String getSchema(Connection connection) {
+ String schema = null;
+
+ try {
+ // Using H2 with a JDBC TCP connection is throwing an exception
+ // See org.h2.engine.SessionRemote#getCurrentSchemaName()
+ if (!"H2 JDBC Driver".equals(connection.getMetaData().getDriverName())) {
+ schema = connection.getSchema();
+ }
+ } catch (SQLException e) {
+ Loggers.get(DatabaseUtils.class).warn("Fail to determine schema. Keeping it null for searching tables", e);
+ }
+ return schema;
+ }
+
public static IllegalStateException wrapSqlException(SQLException e, String message, Object... messageArgs) {
return new IllegalStateException(format(message, messageArgs), e);
}
diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java b/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
index 3ea4852ccf5..23ae8e7dd32 100644
--- a/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
+++ b/server/sonar-db-core/src/test/java/org/sonar/db/AbstractDbTester.java
@@ -405,9 +405,17 @@ public class AbstractDbTester<T extends CoreTestDb> extends ExternalResource {
}
public void assertTableDoesNotExist(String table) {
+ assertTableExists(table, false);
+ }
+
+ public void assertTableExists(String table) {
+ assertTableExists(table, true);
+ }
+
+ private void assertTableExists(String table, boolean expected) {
try (Connection connection = getConnection()) {
boolean tableExists = DatabaseUtils.tableExists(table, connection);
- assertThat(tableExists).isFalse();
+ assertThat(tableExists).isEqualTo(expected);
} catch (Exception e) {
throw new IllegalStateException("Fail to check if table exists", e);
}