From d1a7a0173da71f1ee40e2b3cc74396fe7747b4c8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 28 Nov 2016 12:19:03 +0100 Subject: [PATCH] Speed-up drop of DB table on Oracle --- sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java | 9 ++++++++- .../src/test/java/org/sonar/db/DatabaseUtilsTest.java | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java index 3b837ed6321..1aac86b66ab 100644 --- a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java @@ -25,6 +25,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import com.google.common.collect.Sets; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -49,6 +50,11 @@ public class DatabaseUtils { public static final int PARTITION_SIZE_FOR_ORACLE = 1000; + /** + * @see DatabaseMetaData#getTableTypes() + */ + private static final String[] TABLE_TYPE = {"TABLE"}; + public static void closeQuietly(@Nullable Connection connection) { if (connection != null) { try { @@ -301,7 +307,8 @@ public class DatabaseUtils { * @throws SQLException */ public static boolean tableExists(String table, Connection connection) { - try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) { + // table type is used to speed-up Oracle by removing introspection of system tables and aliases. + try (ResultSet rs = connection.getMetaData().getTables(null, null, null, TABLE_TYPE)) { while (rs.next()) { String name = rs.getString("TABLE_NAME"); if (table.equalsIgnoreCase(name)) { diff --git a/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java b/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java index 1855803dcc3..4003cd443be 100644 --- a/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java @@ -327,4 +327,14 @@ public class DatabaseUtilsTest { assertThat(buildLikeValue("like-\\_%/-value", AFTER)).isEqualTo(escapedValue + wildcard); assertThat(buildLikeValue("like-\\_%/-value", BEFORE_AND_AFTER)).isEqualTo(wildcard + escapedValue + wildcard); } + + @Test + public void tableExists_returns_true_if_table_is_referenced_in_db_metadata() throws Exception { + try (Connection connection = dbTester.openConnection()) { + assertThat(DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", connection)).isTrue(); + assertThat(DatabaseUtils.tableExists("schema_migrations", connection)).isTrue(); + assertThat(DatabaseUtils.tableExists("schema_MIGRATIONS", connection)).isTrue(); + assertThat(DatabaseUtils.tableExists("foo", connection)).isFalse(); + } + } } -- 2.39.5