From: Julien Lancelot Date: Tue, 8 Dec 2015 08:47:37 +0000 (+0100) Subject: SONAR-7030 Add migration to update LOADED_TEMPLATES X-Git-Tag: 5.3-RC1~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1a45e47fdeca85e1884775243187e32ab294c8e3;p=sonarqube.git SONAR-7030 Add migration to update LOADED_TEMPLATES 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' --- 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 index 00000000000..a0cb8296803 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1006_update_custom_dashboard_in_loaded_templates.rb @@ -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 + + diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 7bc2b344ce2..85915c7b673 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -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 diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 5f36fdbfc05..a47fa87740f 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -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 index 00000000000..f63e62112cd --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplates.java @@ -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; + } + } +} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 89350292182..363a767dccd 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -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; diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index c9a71b7dec6..e3915dfc30c 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -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 index 00000000000..51cabba2f3b --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest.java @@ -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 index 00000000000..f5529569c1c --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate-result.xml @@ -0,0 +1,7 @@ + + + + + + + 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 index 00000000000..696a1f86c8b --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/migrate.xml @@ -0,0 +1,9 @@ + + + + + + + + + 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 index 00000000000..f5529569c1c --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/not_migrate_already_migrated_data.xml @@ -0,0 +1,7 @@ + + + + + + + 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 index 00000000000..a0de1fe51dd --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v53/UpdateCustomDashboardInLoadedTemplatesTest/schema.sql @@ -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) +);