]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7030 Add migration to update LOADED_TEMPLATES
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 8 Dec 2015 08:47:37 +0000 (09:47 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 8 Dec 2015 11:28:13 +0000 (12:28 +0100)
In order to not have 2 default dashboards 'Custom', the entry in LOADED_TEMPLATES is renamed from the old name 'Dashboard' to the new one 'Custom'

server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1006_update_custom_dashboard_in_loaded_templates.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplates.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java
sonar-db/src/test/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest.java [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate-result.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/not_migrate_already_migrated_data.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/schema.sql [new file with mode: 0644]

diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1006_update_custom_dashboard_in_loaded_templates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1006_update_custom_dashboard_in_loaded_templates.rb
new file mode 100644 (file)
index 0000000..a0cb829
--- /dev/null
@@ -0,0 +1,33 @@
+#
+# 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 5.3
+# SONAR-7030
+#
+class UpdateCustomDashboardInLoadedTemplates < ActiveRecord::Migration
+
+  def self.up
+    execute_java_migration('org.sonar.db.version.v53.UpdateCustomDashboardInLoadedTemplates')
+  end
+
+end
+
+
index 7bc2b344ce235ab6261880747a40acb585ba5c87..85915c7b67383854a4f364ae7ef8aaee8d849575 100644 (file)
@@ -29,7 +29,7 @@ import org.sonar.db.MyBatis;
 
 public class DatabaseVersion {
 
-  public static final int LAST_VERSION = 1005;
+  public static final int LAST_VERSION = 1006;
 
   /**
    * The minimum supported version which can be upgraded. Lower
index 5f36fdbfc05f0b29f71bb755b4f9e9dbad9d11b5..a47fa87740f07e8ebc15b9063d9265d2740bc312 100644 (file)
@@ -60,6 +60,7 @@ import org.sonar.db.version.v52.RemoveDuplicatedComponentKeys;
 import org.sonar.db.version.v52.RemoveRuleMeasuresOnIssues;
 import org.sonar.db.version.v52.RemoveSnapshotLibraries;
 import org.sonar.db.version.v53.FixMsSqlCollation;
+import org.sonar.db.version.v53.UpdateCustomDashboardInLoadedTemplates;
 
 public class MigrationStepModule extends Module {
   @Override
@@ -113,6 +114,8 @@ public class MigrationStepModule extends Module {
       RemoveRuleMeasuresOnIssues.class,
 
       // 5.3
-      FixMsSqlCollation.class);
+      FixMsSqlCollation.class,
+      UpdateCustomDashboardInLoadedTemplates.class
+    );
   }
 }
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplates.java b/sonar-db/src/main/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplates.java
new file mode 100644 (file)
index 0000000..f63e621
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+package org.sonar.db.version.v53;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+import org.sonar.db.version.Select;
+import org.sonar.db.version.SqlStatement;
+
+/**
+ * As the default dashboard 'Dashboard' has been renamed to 'Custom', the corresponding entry in LOADED_TEMPLATES is updated,
+ * in order to not have this dashboard twice.
+ */
+public class UpdateCustomDashboardInLoadedTemplates extends BaseDataChange {
+
+  public UpdateCustomDashboardInLoadedTemplates(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    MassUpdate update = context.prepareMassUpdate().rowPluralName("loaded templates");
+    update.select("SELECT l.id FROM loaded_templates l WHERE l.kee='Dashboard' and l.template_type='DASHBOARD'");
+    update.update("UPDATE loaded_templates SET kee=? WHERE id=?");
+    update.execute(MigrationHandler.INSTANCE);
+  }
+
+  private enum MigrationHandler implements MassUpdate.Handler {
+    INSTANCE;
+
+    @Override
+    public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
+      update.setString(1, "Custom");
+      update.setLong(2, row.getLong(1));
+      return true;
+    }
+  }
+}
index 893502921820342260d850b2ee5ea13f1ec6eb39..363a767dccd5c42e2bea5d1e49a1daf742bc5d80 100644 (file)
@@ -366,6 +366,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1002');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1003');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1004');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1005');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1006');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index c9a71b7dec647476781f6dee9e4b476e0b3d5d97..e3915dfc30cab72580378a124a4c6aadcd48b82f 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(42);
+    assertThat(container.size()).isEqualTo(43);
   }
 }
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest.java
new file mode 100644 (file)
index 0000000..51cabba
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+package org.sonar.db.version.v53;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.version.MigrationStep;
+
+public class UpdateCustomDashboardInLoadedTemplatesTest {
+
+  static final String TABLE = "loaded_templates";
+
+  @Rule
+  public DbTester db = DbTester.createForSchema(System2.INSTANCE, UpdateCustomDashboardInLoadedTemplatesTest.class, "schema.sql");
+
+  MigrationStep migration;
+
+  @Before
+  public void setUp() {
+    db.executeUpdateSql("truncate table " + TABLE);
+
+    migration = new UpdateCustomDashboardInLoadedTemplates(db.database());
+  }
+
+  @Test
+  public void migrate_empty_db() throws Exception {
+    migration.execute();
+  }
+
+  @Test
+  public void migrate() throws Exception {
+    db.prepareDbUnit(this.getClass(), "migrate.xml");
+    migration.execute();
+    db.assertDbUnit(this.getClass(), "migrate-result.xml", TABLE);
+  }
+
+  @Test
+  public void not_migrate_already_migrated_data() throws Exception {
+    db.prepareDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml");
+    migration.execute();
+    db.assertDbUnit(this.getClass(), "not_migrate_already_migrated_data.xml", TABLE);
+  }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate-result.xml b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate-result.xml
new file mode 100644 (file)
index 0000000..f552956
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <loaded_templates id="1" kee="Custom" template_type="DASHBOARD"/>
+
+  <loaded_templates id="2" kee="Dashboard" template_type="QUALITY_PROFILE"/>
+
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate.xml b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate.xml
new file mode 100644 (file)
index 0000000..696a1f8
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <!-- The kee should be updated to 'Custom' -->
+  <loaded_templates id="1" kee="Dashboard" template_type="DASHBOARD"/>
+
+  <!-- Nothing should be done as it's not a dashboard -->
+  <loaded_templates id="2" kee="Dashboard" template_type="QUALITY_PROFILE"/>
+
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/not_migrate_already_migrated_data.xml b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/not_migrate_already_migrated_data.xml
new file mode 100644 (file)
index 0000000..f552956
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+
+  <loaded_templates id="1" kee="Custom" template_type="DASHBOARD"/>
+
+  <loaded_templates id="2" kee="Dashboard" template_type="QUALITY_PROFILE"/>
+
+</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/schema.sql
new file mode 100644 (file)
index 0000000..a0de1fe
--- /dev/null
@@ -0,0 +1,5 @@
+CREATE TABLE "LOADED_TEMPLATES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "KEE" VARCHAR(200),
+  "TEMPLATE_TYPE" VARCHAR(15)
+);