--- /dev/null
+#
+# 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.1
+#
+class DeleteProjectDashboards < ActiveRecord::Migration
+
+ def self.up
+ execute_java_migration('org.sonar.db.version.v61.DeleteProjectDashboards')
+ end
+end
public class DatabaseVersion {
- public static final int LAST_VERSION = 1_277;
+ public static final int LAST_VERSION = 1_300;
/**
* The minimum supported version which can be upgraded. Lower
import org.sonar.db.version.v60.PopulateUuidColumnsOfResourceIndex;
import org.sonar.db.version.v60.PopulateUuidPathColumnOnProjects;
import org.sonar.db.version.v60.RemoveUsersPasswordWhenNotLocal;
+import org.sonar.db.version.v61.DeleteProjectDashboards;
public class MigrationStepModule extends Module {
@Override
DropTreesOfSnapshots.class,
DropTreeColumnsFromSnapshots.class,
DropSnapshotIdColumnFromMeasures.class,
- AddBColumnsToProjects.class
+ AddBColumnsToProjects.class,
+
+ // 6.1
+ DeleteProjectDashboards.class
);
}
}
--- /dev/null
+/*
+ * 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.v61;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+
+public class DeleteProjectDashboards extends BaseDataChange {
+
+ public DeleteProjectDashboards(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ deleteWidgetProperties(context);
+ deleteDashboardsAndWidgets(context);
+ }
+
+ private static void deleteWidgetProperties(Context context) throws SQLException {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT w.id " +
+ "FROM widgets w " +
+ " INNER JOIN dashboards d on w.dashboard_id=d.id " +
+ "WHERE d.is_global=?")
+ .setBoolean(1, false);
+ massUpdate.update("DELETE from widget_properties WHERE widget_id=?");
+ massUpdate.rowPluralName("delete widget properties of project dashboards");
+ massUpdate.execute((row, update) -> {
+ update.setLong(1, row.getLong(1));
+ return true;
+ });
+ }
+
+ private static void deleteDashboardsAndWidgets(Context context) throws SQLException {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT id FROM dashboards d WHERE d.is_global=?").setBoolean(1, false);
+ massUpdate.update("DELETE from widgets WHERE dashboard_id=?");
+ massUpdate.update("DELETE from active_dashboards WHERE dashboard_id=?");
+ massUpdate.update("DELETE from dashboards WHERE id=?");
+ massUpdate.rowPluralName("delete project dashboards");
+ massUpdate.execute((row, update, updateIndex) -> {
+ long dashboardId = row.getLong(1);
+ switch (updateIndex) {
+ case 0:
+ case 1:
+ case 2:
+ update.setLong(1, dashboardId);
+ return true;
+ default:
+ throw new IllegalArgumentException("Unsupported update index " + updateIndex);
+ }
+ });
+ }
+}
--- /dev/null
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.db.version.v61;
+
+import javax.annotation.ParametersAreNonnullByDefault;
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1276');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1277');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1300');
+
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482');
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
public void verify_count_of_added_MigrationStep_types() {
ComponentContainer container = new ComponentContainer();
new MigrationStepModule().configure(container);
- assertThat(container.size()).isEqualTo(126);
+ assertThat(container.size()).isEqualTo(127);
}
}
--- /dev/null
+/*
+ * 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.v61;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static java.lang.String.valueOf;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DeleteProjectDashboardsTest {
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteProjectDashboardsTest.class, "schema.sql");
+
+ private DeleteProjectDashboards underTest = new DeleteProjectDashboards(db.database());
+
+ @Test
+ public void no_effect_on_empty_tables() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable("dashboards")).isEqualTo(0);
+ }
+
+ @Test
+ public void delete_project_dashboard_data() throws SQLException {
+ insertGlobalDashboards(1L, 10L, 11L);
+ insertProjectDashboards(2L, 20L, 21L);
+ insertActiveDashboards(1L, 10L, 11L, 12L);
+ insertActiveDashboards(2L, 20L, 21L, 22L);
+ insertWidgets(1L, 100L, 101L, 102L);
+ insertWidgets(2L, 200L, 201L, 202L);
+ insertWidgetProperties(100L, 1001L, 1002L, 1003L);
+ insertWidgetProperties(202L, 2021L, 2022L, 2023L);
+
+ underTest.execute();
+
+ assertIdsOfDashboardsAre(1L, 10L, 11L);
+ assertIdsOfActiveDashboardsAre(10L, 11L, 12L);
+ assertIdsOfWidgetsAre(100L, 101L, 102L);
+ assertIdsOfWidgetPropertiesAre(1001L, 1002L, 1003L);
+ }
+
+ @Test
+ public void is_reentrant() throws SQLException {
+ insertGlobalDashboards(10L, 11L, 12L);
+ insertProjectDashboards(20L, 21L, 22L);
+ underTest.execute();
+ assertIdsOfDashboardsAre(10L, 11L, 12L);
+
+ underTest.execute();
+ assertIdsOfDashboardsAre(10L, 11L, 12L);
+ }
+
+ private void insertProjectDashboards(long... ids) {
+ Arrays.stream(ids).forEach(id -> insertDashboard(id, false));
+ }
+
+ private void insertGlobalDashboards(long... ids) {
+ Arrays.stream(ids).forEach(id -> insertDashboard(id, true));
+ }
+
+ private void insertDashboard(long id, boolean isGlobal) {
+ db.executeInsert(
+ "dashboards",
+ "ID", valueOf(id),
+ "IS_GLOBAL", valueOf(isGlobal));
+ }
+
+ private void insertActiveDashboards(long dashboardId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "active_dashboards",
+ "ID", valueOf(id),
+ "DASHBOARD_ID", valueOf(dashboardId)));
+ }
+
+ private void insertWidgets(long dashboardId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "widgets",
+ "ID", valueOf(id),
+ "WIDGET_KEY", valueOf(id),
+ "DASHBOARD_ID", valueOf(dashboardId)));
+ }
+
+ private void insertWidgetProperties(long widgetId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "widget_properties",
+ "ID", valueOf(id),
+ "WIDGET_ID", valueOf(widgetId)));
+ }
+
+ private void assertIdsOfDashboardsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from dashboards").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfActiveDashboardsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from active_dashboards").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfWidgetsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from widgets").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfWidgetPropertiesAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from widget_properties").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+}
--- /dev/null
+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,
+ "IS_GLOBAL" BOOLEAN,
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP
+);
+
+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 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 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)
+);