]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8354 Drop tables "active_dashboards", "dashboards", "widgets", "widget_properties"
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 7 Nov 2016 15:47:19 +0000 (16:47 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 9 Nov 2016 15:47:34 +0000 (16:47 +0100)
13 files changed:
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1420_drop_related_dashboard_tables.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v62/DropRelatedDashboardTables.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl
sonar-db/src/test/java/org/sonar/db/DbTester.java
sonar-db/src/test/java/org/sonar/db/version/DropTableBuilderTest.java [new file with mode: 0644]
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v62/DropRelatedDashboardTablesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v62/DropRelatedDashboardTablesTest/schema.sql [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1420_drop_related_dashboard_tables.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1420_drop_related_dashboard_tables.rb
new file mode 100644 (file)
index 0000000..2aa2c3d
--- /dev/null
@@ -0,0 +1,29 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 6.2
+#
+class DropRelatedDashboardTables < ActiveRecord::Migration
+
+  def self.up
+    execute_java_migration('org.sonar.db.version.v62.DropRelatedDashboardTables')
+  end
+end
index aa76ec2849dfed397c04827c41aade7638044144..3b837ed6321a255469dcd74506e059150b0abc1d 100644 (file)
@@ -43,6 +43,7 @@ import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 
 import static com.google.common.collect.Lists.newArrayList;
+import static java.lang.String.format;
 
 public class DatabaseUtils {
 
@@ -293,4 +294,27 @@ public class DatabaseUtils {
     Timestamp t = rs.getTimestamp(columnIndex);
     return rs.wasNull() ? null : new Date(t.getTime());
   }
+
+  /**
+    * @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) {
+    try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) {
+      while (rs.next()) {
+        String name = rs.getString("TABLE_NAME");
+        if (table.equalsIgnoreCase(name)) {
+          return true;
+        }
+      }
+      return false;
+    } catch (SQLException e) {
+      throw wrapSqlException(e, "Can not check that table %s exists", table);
+    }
+  }
+
+  public static IllegalStateException wrapSqlException(SQLException e, String message, Object... messageArgs) {
+    return new IllegalStateException(format(message, messageArgs), e);
+  }
 }
index 86bd8c05b1b66f76d9ad2799d751c8f77f73eb41..3dc598bce860a0bdc3b45f00906c19710e2a3a5a 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.db.MyBatis;
 
 public class DatabaseVersion {
 
-  public static final int LAST_VERSION = 1_419;
+  public static final int LAST_VERSION = 1_420;
 
   /**
    * The minimum supported version which can be upgraded. Lower
@@ -47,7 +47,11 @@ public class DatabaseVersion {
    * @see org.sonar.db.charset.ColumnDef#isInSonarQubeTable() 
    */
   public static final Set<String> OLD_DROPPED_TABLES = ImmutableSet.of(
-    "activities");
+    "active_dashboards",
+    "activities",
+    "dashboards",
+    "widgets",
+    "widget_properties");
 
   /**
    * List of all the tables.
@@ -55,7 +59,6 @@ public class DatabaseVersion {
    * for all the supported databases, particularly due to Oracle results.
    */
   public static final Set<String> TABLES = ImmutableSet.of(
-    "active_dashboards",
     "active_rules",
     "active_rule_parameters",
     "authors",
@@ -63,7 +66,6 @@ public class DatabaseVersion {
     "ce_queue",
     "ce_task_input",
     "ce_scanner_context",
-    "dashboards",
     "duplications_index",
     "events",
     "file_sources",
@@ -103,9 +105,7 @@ public class DatabaseVersion {
     "snapshots",
     "users",
     "user_roles",
-    "user_tokens",
-    "widgets",
-    "widget_properties");
+    "user_tokens");
 
   private MyBatis mybatis;
 
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/DropTableBuilder.java
new file mode 100644 (file)
index 0000000..de57385
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.Oracle;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static java.util.Objects.requireNonNull;
+import static org.sonar.db.version.Validations.TABLE_NAME_MAX_SIZE;
+import static org.sonar.db.version.Validations.checkDbIdentifier;
+
+public class DropTableBuilder {
+
+  private final Dialect dialect;
+  private final String tableName;
+
+  public DropTableBuilder(Dialect dialect, String tableName) {
+    this.dialect = requireNonNull(dialect, "dialect can't be null");
+    this.tableName = checkDbIdentifier(tableName, "Table name", TABLE_NAME_MAX_SIZE);
+  }
+
+  public List<String> build() {
+    if (Oracle.ID.equals(dialect.getId())) {
+      return dropOracleSequenceAndTriggerAndTableStatements();
+    }
+    return singletonList(dropTableStatement());
+  }
+
+  private String dropTableStatement() {
+    return "DROP TABLE " + tableName;
+  }
+
+  private List<String> dropOracleSequenceAndTriggerAndTableStatements() {
+    return asList(dropSequenceFor(tableName), dropTriggerFor(tableName), dropTableStatement());
+  }
+
+  private static String dropSequenceFor(String tableName) {
+    return "BEGIN\n" +
+      "  EXECUTE IMMEDIATE 'DROP SEQUENCE " + tableName + "_seq';\n" +
+      "EXCEPTION\n" +
+      "  WHEN OTHERS THEN\n" +
+      "    IF SQLCODE != -2289 THEN\n" +
+      "      RAISE;\n" +
+      "    END IF;\n" +
+      "END;";
+  }
+
+  private static String dropTriggerFor(String tableName) {
+    return "BEGIN\n" +
+      "  EXECUTE IMMEDIATE 'DROP TRIGGER " + tableName + "_idt';\n" +
+      "EXCEPTION\n" +
+      "  WHEN OTHERS THEN\n" +
+      "    IF SQLCODE != -4080 THEN\n" +
+      "      RAISE;\n" +
+      "    END IF;\n" +
+      "END;";
+
+  }
+
+}
index adf927f7a26519b43d2600afce3413abe823a19a..5e4014e7c0ce149dff11856ace2d9f2f54280b27 100644 (file)
@@ -167,6 +167,7 @@ import org.sonar.db.version.v62.AddOrganizationUuidToUserRoles;
 import org.sonar.db.version.v62.CreateDefaultOrganization;
 import org.sonar.db.version.v62.CreateTableOrganizations;
 import org.sonar.db.version.v62.DeletePermissionShareDashboard;
+import org.sonar.db.version.v62.DropRelatedDashboardTables;
 import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnGroupRoles;
 import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnGroups;
 import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnPermissionTemplates;
@@ -376,6 +377,7 @@ public class MigrationStepModule extends Module {
       AddOrganizationUuidToGroupRoles.class,
       PopulateOrganizationUuidOfGroupRoles.class,
       MakeOrganizationUuidNotNullOnGroupRoles.class,
-      UpdateQualityGateConditionsOnCoverage.class);
+      UpdateQualityGateConditionsOnCoverage.class,
+      DropRelatedDashboardTables.class);
   }
 }
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/DropRelatedDashboardTables.java b/sonar-db/src/main/java/org/sonar/db/version/v62/DropRelatedDashboardTables.java
new file mode 100644 (file)
index 0000000..5d41a2f
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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.v62;
+
+import com.google.common.collect.ImmutableList;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.db.version.DdlChange;
+import org.sonar.db.version.DropTableBuilder;
+
+import static org.sonar.core.util.stream.Collectors.toList;
+
+public class DropRelatedDashboardTables extends DdlChange {
+
+  private static final Logger LOGGER = Loggers.get(DropRelatedDashboardTables.class);
+
+  private static final List<String> TABLES_TO_DROP = ImmutableList.of("widget_properties", "widgets", "active_dashboards", "dashboards");
+
+  private final Database db;
+
+  public DropRelatedDashboardTables(Database db) {
+    super(db);
+    this.db = db;
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    List<String> tablesToDrop = getEffectiveTablesToDrop();
+    LOGGER.info("Removing tables {}", tablesToDrop);
+    context.execute(tablesToDrop
+      .stream()
+      .flatMap(table -> new DropTableBuilder(db.getDialect(), table).build().stream())
+      .collect(toList()));
+  }
+
+  private List<String> getEffectiveTablesToDrop() throws SQLException {
+    try (Connection connection = db.getDataSource().getConnection()) {
+      return TABLES_TO_DROP.stream()
+        .filter(table -> DatabaseUtils.tableExists(table, connection))
+        .collect(toList());
+    }
+  }
+}
index 4863aba48cd8fa67881c16cf594c285ac64a2645..31346602831ce86d8055785d70b1d9a0d108164f 100644 (file)
@@ -508,6 +508,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1416');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1417');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1418');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1419');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1420');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482');
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index e1c7c7899842e01a880838b263bcd3b39a409a28..a98df497e2461033cd8a77bc1edded3e8f01aa84 100644 (file)
@@ -56,22 +56,6 @@ CREATE TABLE "PROJECT_QPROFILES" (
 CREATE UNIQUE INDEX "UNIQ_PROJECT_QPROFILES" ON "PROJECT_QPROFILES" ("PROJECT_UUID", "PROFILE_KEY");
 
 
-CREATE TABLE "WIDGETS" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "DASHBOARD_ID" INTEGER NOT NULL,
-  "WIDGET_KEY" VARCHAR(256) NOT NULL,
-  "NAME" VARCHAR(256),
-  "DESCRIPTION" VARCHAR(1000),
-  "COLUMN_INDEX" INTEGER,
-  "ROW_INDEX" INTEGER,
-  "CONFIGURED" BOOLEAN,
-  "CREATED_AT" TIMESTAMP,
-  "UPDATED_AT" TIMESTAMP,
-  "RESOURCE_ID" INTEGER
-);
-CREATE INDEX "WIDGETS_WIDGETKEY" ON "WIDGETS" ("WIDGET_KEY");
-CREATE INDEX "WIDGETS_DASHBOARDS" ON "WIDGETS" ("DASHBOARD_ID");
-
 
 CREATE TABLE "GROUPS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
@@ -171,15 +155,6 @@ CREATE TABLE "RULES" (
 CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY");
 
 
-CREATE TABLE "WIDGET_PROPERTIES" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "WIDGET_ID" INTEGER NOT NULL,
-  "KEE" VARCHAR(100),
-  "TEXT_VALUE" VARCHAR(4000)
-);
-CREATE INDEX "WIDGET_PROPERTIES_WIDGETS" ON "WIDGET_PROPERTIES" ("WIDGET_ID");
-
-
 CREATE TABLE "EVENTS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "NAME" VARCHAR(400),
@@ -371,16 +346,6 @@ CREATE INDEX "USER_ROLES_RESOURCE" ON "USER_ROLES" ("RESOURCE_ID");
 CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES" ("USER_ID");
 
 
-CREATE TABLE "ACTIVE_DASHBOARDS" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "DASHBOARD_ID" INTEGER NOT NULL,
-  "USER_ID" INTEGER,
-  "ORDER_INDEX" INTEGER
-);
-CREATE INDEX "ACTIVE_DASHBOARDS_DASHBOARDID" ON "ACTIVE_DASHBOARDS" ("DASHBOARD_ID");
-CREATE INDEX "ACTIVE_DASHBOARDS_USERID" ON "ACTIVE_DASHBOARDS" ("USER_ID");
-
-
 CREATE TABLE "ACTIVE_RULE_PARAMETERS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "ACTIVE_RULE_ID" INTEGER NOT NULL,
@@ -410,18 +375,6 @@ CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN");
 CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT");
 
 
-CREATE TABLE "DASHBOARDS" (
-  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
-  "USER_ID" INTEGER,
-  "NAME" VARCHAR(256),
-  "DESCRIPTION" VARCHAR(1000),
-  "COLUMN_LAYOUT" VARCHAR(20),
-  "SHARED" BOOLEAN,
-  "CREATED_AT" TIMESTAMP,
-  "UPDATED_AT" TIMESTAMP
-);
-
-
 CREATE TABLE "METRICS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "NAME" VARCHAR(64) NOT NULL,
index 8a475b5de5786dc440365502ce5d116d7427b28f..2f0e95ec461c4d131f52c8a022c191d9c6ec3037 100644 (file)
@@ -510,6 +510,15 @@ public class DbTester extends ExternalResource {
     }
   }
 
+  public void assertTableDoesNotExist(String table) {
+    try (Connection connection = getConnection()) {
+      boolean tableExists = DatabaseUtils.tableExists(table, connection);
+      assertThat(tableExists).isFalse();
+    } catch (Exception e) {
+      throw new IllegalStateException("Fail to check if table exists", 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/DropTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/DropTableBuilderTest.java
new file mode 100644 (file)
index 0000000..918dcbd
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * 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 org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+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 DropTableBuilderTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void drop_tables_on_mysql() {
+    assertThat(new DropTableBuilder(new MySql(), "issues")
+      .build()).containsOnly("DROP TABLE issues");
+  }
+
+  @Test
+  public void drop_tables_on_postgresql() {
+    assertThat(new DropTableBuilder(new PostgreSql(), "issues")
+      .build()).containsOnly("DROP TABLE issues");
+  }
+
+  @Test
+  public void drop_tables_on_mssql() {
+    assertThat(new DropTableBuilder(new MsSql(), "issues")
+      .build()).containsOnly("DROP TABLE issues");
+  }
+
+  @Test
+  public void drop_tables_on_h2() {
+    assertThat(new DropTableBuilder(new H2(), "issues")
+      .build()).containsOnly("DROP TABLE issues");
+  }
+
+  @Test
+  public void drop_columns_on_oracle() {
+    assertThat(new DropTableBuilder(new Oracle(), "issues")
+      .build()).containsExactly(
+        "BEGIN\n" +
+          "  EXECUTE IMMEDIATE 'DROP SEQUENCE issues_seq';\n" +
+          "EXCEPTION\n" +
+          "  WHEN OTHERS THEN\n" +
+          "    IF SQLCODE != -2289 THEN\n" +
+          "      RAISE;\n" +
+          "    END IF;\n" +
+          "END;",
+        "BEGIN\n" +
+          "  EXECUTE IMMEDIATE 'DROP TRIGGER issues_idt';\n" +
+          "EXCEPTION\n" +
+          "  WHEN OTHERS THEN\n" +
+          "    IF SQLCODE != -4080 THEN\n" +
+          "      RAISE;\n" +
+          "    END IF;\n" +
+          "END;",
+        "DROP TABLE issues");
+  }
+
+  @Test
+  public void fail_when_dialect_is_null() throws Exception {
+    expectedException.expect(NullPointerException.class);
+    new DropTableBuilder(null, "issues");
+  }
+
+  @Test
+  public void fail_when_table_is_null() throws Exception {
+    expectedException.expect(NullPointerException.class);
+    new DropTableBuilder(new PostgreSql(), null);
+  }
+}
index 70eaab00ab7be35fa111e81dd5659b09755cc735..700344fb7a74429dbcdea1291cbb70cf2effd9c7 100644 (file)
@@ -29,6 +29,6 @@ public class MigrationStepModuleTest {
   public void verify_count_of_added_MigrationStep_types() {
     ComponentContainer container = new ComponentContainer();
     new MigrationStepModule().configure(container);
-    assertThat(container.size()).isEqualTo(160);
+    assertThat(container.size()).isEqualTo(161);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/DropRelatedDashboardTablesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/DropRelatedDashboardTablesTest.java
new file mode 100644 (file)
index 0000000..7d8fd69
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.v62;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+public class DropRelatedDashboardTablesTest {
+
+  @Rule
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, DropRelatedDashboardTablesTest.class, "schema.sql");
+
+  private DropRelatedDashboardTables underTest = new DropRelatedDashboardTables(db.database());
+
+  @Test
+  public void delete_tables() throws SQLException {
+    underTest.execute();
+
+    db.assertTableDoesNotExist("widget_properties");
+    db.assertTableDoesNotExist("widgets");
+    db.assertTableDoesNotExist("active_dashboards");
+    db.assertTableDoesNotExist("dashboards");
+  }
+
+  @Test
+  public void migration_is_re_entrant() throws Exception {
+    underTest.execute();
+    underTest.execute();
+
+    db.assertTableDoesNotExist("widget_properties");
+    db.assertTableDoesNotExist("widgets");
+    db.assertTableDoesNotExist("active_dashboards");
+    db.assertTableDoesNotExist("dashboards");
+  }
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/DropRelatedDashboardTablesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/DropRelatedDashboardTablesTest/schema.sql
new file mode 100644 (file)
index 0000000..6456acc
--- /dev/null
@@ -0,0 +1,47 @@
+CREATE TABLE "ACTIVE_DASHBOARDS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "DASHBOARD_ID" INTEGER NOT NULL,
+  "USER_ID" INTEGER,
+  "ORDER_INDEX" INTEGER
+);
+CREATE INDEX "ACTIVE_DASHBOARDS_DASHBOARDID" ON "ACTIVE_DASHBOARDS" ("DASHBOARD_ID");
+CREATE INDEX "ACTIVE_DASHBOARDS_USERID" ON "ACTIVE_DASHBOARDS" ("USER_ID");
+
+
+CREATE TABLE "DASHBOARDS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "USER_ID" INTEGER,
+  "NAME" VARCHAR(256),
+  "DESCRIPTION" VARCHAR(1000),
+  "COLUMN_LAYOUT" VARCHAR(20),
+  "SHARED" BOOLEAN,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP
+);
+
+CREATE TABLE "WIDGETS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "DASHBOARD_ID" INTEGER NOT NULL,
+  "WIDGET_KEY" VARCHAR(256) NOT NULL,
+  "NAME" VARCHAR(256),
+  "DESCRIPTION" VARCHAR(1000),
+  "COLUMN_INDEX" INTEGER,
+  "ROW_INDEX" INTEGER,
+  "CONFIGURED" BOOLEAN,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+  "RESOURCE_ID" INTEGER
+);
+CREATE INDEX "WIDGETS_WIDGETKEY" ON "WIDGETS" ("WIDGET_KEY");
+CREATE INDEX "WIDGETS_DASHBOARDS" ON "WIDGETS" ("DASHBOARD_ID");
+
+
+CREATE TABLE "WIDGET_PROPERTIES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "WIDGET_ID" INTEGER NOT NULL,
+  "KEE" VARCHAR(100),
+  "TEXT_VALUE" VARCHAR(4000)
+);
+CREATE INDEX "WIDGET_PROPERTIES_WIDGETS" ON "WIDGET_PROPERTIES" ("WIDGET_ID");
+
+