aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-06 21:00:39 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-12 13:05:19 +0100
commit1b16981bab6f9309b80fa08dde67aef9a2b3dc1d (patch)
treebbfd2027488b8f151aedd07ecea183fe09c7c7c1 /sonar-db
parentcb07ea331a5e4e7bc650d59dd2e7583e82658ad8 (diff)
downloadsonarqube-1b16981bab6f9309b80fa08dde67aef9a2b3dc1d.tar.gz
sonarqube-1b16981bab6f9309b80fa08dde67aef9a2b3dc1d.zip
SONAR-5471 add DropIndexBuilder
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java75
-rw-r--r--sonar-db/src/test/java/org/sonar/db/DbTester.java18
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java98
3 files changed, 190 insertions, 1 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java
new file mode 100644
index 00000000000..091a5085532
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/version/DropIndexBuilder.java
@@ -0,0 +1,75 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.version;
+
+import java.util.List;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
+
+import static java.util.Collections.singletonList;
+import static org.sonar.db.version.Validations.validateIndexName;
+import static org.sonar.db.version.Validations.validateTableName;
+
+public class DropIndexBuilder {
+
+ private final Dialect dialect;
+ private String tableName;
+ private String indexName;
+
+ public DropIndexBuilder(Dialect dialect) {
+ this.dialect = dialect;
+ }
+
+ public DropIndexBuilder setTable(String s) {
+ this.tableName = s;
+ return this;
+ }
+
+ public DropIndexBuilder setName(String s) {
+ this.indexName = s;
+ return this;
+ }
+
+ public List<String> build() {
+ validateTableName(tableName);
+ validateIndexName(indexName);
+ return singletonList(createSqlStatement());
+ }
+
+ private String createSqlStatement() {
+ switch (dialect.getId()) {
+ case MsSql.ID:
+ case MySql.ID:
+ return "DROP INDEX " + indexName + " ON " + tableName;
+ case Oracle.ID:
+ return "DROP INDEX " + indexName;
+ case H2.ID:
+ case PostgreSql.ID:
+ return "DROP INDEX IF EXISTS " + indexName;
+ default:
+ throw new IllegalStateException("Unsupported dialect for drop of index: " + dialect);
+ }
+ }
+
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/DbTester.java b/sonar-db/src/test/java/org/sonar/db/DbTester.java
index bbd076c3d06..deb8b1674fd 100644
--- a/sonar-db/src/test/java/org/sonar/db/DbTester.java
+++ b/sonar-db/src/test/java/org/sonar/db/DbTester.java
@@ -549,7 +549,7 @@ public class DbTester extends ExternalResource {
private void assertIndexImpl(String tableName, String indexName, boolean expectedUnique, String expectedColumn, String... expectedSecondaryColumns) {
try (Connection connection = getConnection();
- ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, true)) {
+ ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
List<String> onColumns = new ArrayList<>();
while (rs.next()) {
if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) {
@@ -564,6 +564,22 @@ public class DbTester extends ExternalResource {
}
}
+ /**
+ * Verify that index with name {@code indexName} does not exist on the table {@code tableName}
+ */
+ public void assertIndexDoesNotExist(String tableName, String indexName) {
+ try (Connection connection = getConnection();
+ ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) {
+ List<String> indices = new ArrayList<>();
+ while (rs.next()) {
+ indices.add(rs.getString("INDEX_NAME").toLowerCase(Locale.ENGLISH));
+ }
+ assertThat(indices).doesNotContain(indexName);
+ } catch (SQLException e) {
+ throw new IllegalStateException("Fail to check existence of index", e);
+ }
+ }
+
public void assertPrimaryKey(String tableName, @Nullable String expectedPkName, String columnName, String... otherColumnNames) {
try (Connection connection = getConnection()) {
PK pk = pkOf(connection, tableName.toUpperCase(Locale.ENGLISH));
diff --git a/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java
new file mode 100644
index 00000000000..7792248ce1c
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/DropIndexBuilderTest.java
@@ -0,0 +1,98 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.version;
+
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.MySql;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DropIndexBuilderTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void drop_index_in_table() {
+ verifySql(new H2(), "DROP INDEX IF EXISTS issues_key");
+ verifySql(new MsSql(), "DROP INDEX issues_key ON issues");
+ verifySql(new MySql(), "DROP INDEX issues_key ON issues");
+ verifySql(new Oracle(), "DROP INDEX issues_key");
+ verifySql(new PostgreSql(), "DROP INDEX IF EXISTS issues_key");
+ }
+
+ private static void verifySql(Dialect dialect, String expectedSql) {
+ List<String> actual = new DropIndexBuilder(dialect)
+ .setTable("issues")
+ .setName("issues_key")
+ .build();
+ assertThat(actual).containsExactly(expectedSql);
+ }
+
+ @Test
+ public void throw_NPE_if_table_name_is_missing() {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("Table name cannot be null");
+
+ new DropIndexBuilder(new H2())
+ .setName("issues_key")
+ .build();
+ }
+
+ @Test
+ public void throw_IAE_if_table_name_is_not_valid() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Table name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'");
+
+ new DropIndexBuilder(new H2())
+ .setTable("(not valid)")
+ .setName("issues_key")
+ .build();
+ }
+
+ @Test
+ public void throw_NPE_if_index_name_is_missing() {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("Index name cannot be null");
+
+ new DropIndexBuilder(new H2())
+ .setTable("issues")
+ .build();
+ }
+
+ @Test
+ public void throw_IAE_if_index_name_is_not_valid() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Index name must be lower case and contain only alphanumeric chars or '_', got '(not valid)'");
+
+ new DropIndexBuilder(new H2())
+ .setTable("issues")
+ .setName("(not valid)")
+ .build();
+ }
+}