From: Jacek Date: Mon, 12 Oct 2020 11:50:39 +0000 (+0200) Subject: SONAR-13965 Update column 'change_data' of 'qprofile_changes' table to use ruleUuid... X-Git-Tag: 8.6.0.39681~167 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c3545216c4621b72d00629eacf889ff49708ac5d;p=sonarqube.git SONAR-13965 Update column 'change_data' of 'qprofile_changes' table to use ruleUuid instead of ruleId --- diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/DbVersion86.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/DbVersion86.java index 8b0e8936ba4..c8fe23774bb 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/DbVersion86.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/DbVersion86.java @@ -33,6 +33,6 @@ public class DbVersion86 implements DbVersion { .add(4103, "Drop organization_uuid from 'default_qprofiles' table", DropOrganizationFromDefaultQProfiles.class) .add(4104, "Add primary key to the table 'default_qprofiles", AddPrimaryKeyToDefaultQProfiles.class) .add(4105, "Drop 'organization_uuid' in 'rules_metadata'", DropOrganizationInRulesMetadata.class) - ; + .add(4106, "Update 'change_data' column of 'qprofile_changes' table to use ruleUuid", UpdateChangeDataOfQProfileChanges.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChanges.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChanges.java new file mode 100644 index 00000000000..7c6d76830d0 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChanges.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v86; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class UpdateChangeDataOfQProfileChanges extends DataChange { + + public UpdateChangeDataOfQProfileChanges(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select qpc.kee, qpc.change_data " + + "from qprofile_changes qpc where qpc.change_data like '%ruleId=%'"); + + massUpdate.update("update qprofile_changes " + + "set change_data = replace(change_data, 'ruleId', 'ruleUuid') where kee = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, row.getString(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest.java new file mode 100644 index 00000000000..e61eda7adf0 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest.java @@ -0,0 +1,88 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v86; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.Uuids; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UpdateChangeDataOfQProfileChangesTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(UpdateChangeDataOfQProfileChangesTest.class, "schema.sql"); + + private DataChange underTest = new UpdateChangeDataOfQProfileChanges(db.database()); + + @Test + public void change_ruleId_to_ruleUuid() throws SQLException { + insertQProfileChanges("key-1", "severity=MAJOR;ruleUuid=AXJVoXWDOFGkZYIS4z4D"); + insertQProfileChanges("key-2", "severity=MAJOR;ruleId=12915"); + insertQProfileChanges("key-3", "severity=MAJOR;ruleId=12915;something"); + insertQProfileChanges("key-4", "ruleId=12915;something"); + + underTest.execute(); + + assertThatQProfileMigrated("key-1", "severity=MAJOR;ruleUuid=AXJVoXWDOFGkZYIS4z4D"); + assertThatQProfileMigrated("key-2", "severity=MAJOR;ruleUuid=12915"); + assertThatQProfileMigrated("key-3", "severity=MAJOR;ruleUuid=12915;something"); + assertThatQProfileMigrated("key-4", "ruleUuid=12915;something"); + } + + @Test + public void migration_is_reentrant() throws SQLException { + insertQProfileChanges("key-1", "severity=MAJOR;ruleUuid=AXJVoXWDOFGkZYIS4z4D"); + insertQProfileChanges("key-2", "severity=MAJOR;ruleId=12915"); + insertQProfileChanges("key-3", "severity=MAJOR;ruleId=12915;something"); + insertQProfileChanges("key-4", "ruleId=12915;something"); + + underTest.execute(); + // re-entrant + underTest.execute(); + + assertThatQProfileMigrated("key-1", "severity=MAJOR;ruleUuid=AXJVoXWDOFGkZYIS4z4D"); + assertThatQProfileMigrated("key-2", "severity=MAJOR;ruleUuid=12915"); + assertThatQProfileMigrated("key-3", "severity=MAJOR;ruleUuid=12915;something"); + assertThatQProfileMigrated("key-4", "ruleUuid=12915;something"); + } + + private void assertThatQProfileMigrated(String key, String changeData) { + assertThat(db.select("select qpc.kee, qpc.change_data " + + "from qprofile_changes qpc where qpc.kee = '" + key + "'").stream().findFirst()) + .isNotEmpty() + .hasValueSatisfying(stringObjectMap -> assertThat(stringObjectMap) + .extractingByKeys("KEE", "CHANGE_DATA") + .contains(key, changeData)); + + } + + private void insertQProfileChanges(String kee, String changeData) { + db.executeInsert("qprofile_changes", + "kee", kee, + "rules_profile_uuid", Uuids.createFast(), + "change_type", "any", + "change_data", changeData, + "created_at", System.currentTimeMillis()); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest/schema.sql new file mode 100644 index 00000000000..5cb1137f8de --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v86/UpdateChangeDataOfQProfileChangesTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "QPROFILE_CHANGES"( + "KEE" VARCHAR(40) NOT NULL, + "RULES_PROFILE_UUID" VARCHAR(255) NOT NULL, + "CHANGE_TYPE" VARCHAR(20) NOT NULL, + "USER_UUID" VARCHAR(255), + "CHANGE_DATA" CLOB, + "CREATED_AT" BIGINT NOT NULL +); +ALTER TABLE "QPROFILE_CHANGES" ADD CONSTRAINT "PK_QPROFILE_CHANGES" PRIMARY KEY("KEE"); +CREATE INDEX "QP_CHANGES_RULES_PROFILE_UUID" ON "QPROFILE_CHANGES"("RULES_PROFILE_UUID");