From 446a27fd0bb4c449db017760dc11ec92da35cb3c Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 13 Jul 2020 11:15:20 -0500 Subject: [PATCH] SONAR-13613 Fix code duplication --- ...ateActiveRuleParametersActiveRuleUuid.java | 11 +---- ...PopulateOrganizationsDefaultGroupUuid.java | 27 +++++------ .../PopulateQProfileEditGroupsGroupUuid.java | 12 +---- .../PopulateActiveRulesRuleUuidColumn.java | 11 +---- ...ulateDeprecatedRuleKeysRuleUuidColumn.java | 11 +---- .../PopulateGroupsUsersUserUuid.java | 7 ++- .../PopulateOrganizationMembersUserUuid.java | 13 +++--- .../PopulateQProfileEditUsersUserUuid.java | 11 +---- .../version/v84/util/OrphanData.java | 45 +++++++++++++++++++ 9 files changed, 81 insertions(+), 67 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/util/OrphanData.java diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/activerules/PopulateActiveRuleParametersActiveRuleUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/activerules/PopulateActiveRuleParametersActiveRuleUuid.java index 4f221ac9ef9..868f73853e5 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/activerules/PopulateActiveRuleParametersActiveRuleUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/activerules/PopulateActiveRuleParametersActiveRuleUuid.java @@ -23,6 +23,7 @@ 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; +import org.sonar.server.platform.db.migration.version.v84.util.OrphanData; public class PopulateActiveRuleParametersActiveRuleUuid extends DataChange { @@ -45,14 +46,6 @@ public class PopulateActiveRuleParametersActiveRuleUuid extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select uuid from active_rule_parameters where active_rule_uuid is null"); - massUpdate.update("delete from active_rule_parameters where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - return true; - }); + OrphanData.delete(context, "active_rule_parameters", "active_rule_uuid"); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/organizations/PopulateOrganizationsDefaultGroupUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/organizations/PopulateOrganizationsDefaultGroupUuid.java index bb5d04c6329..28d4a8298f3 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/organizations/PopulateOrganizationsDefaultGroupUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/organizations/PopulateOrganizationsDefaultGroupUuid.java @@ -30,6 +30,19 @@ public class PopulateOrganizationsDefaultGroupUuid extends DataChange { super(db); } + private static void removeOrphans(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select o.uuid, g.uuid from organizations o join groups g on o.default_group_id = g.id"); + massUpdate.update("update organizations set default_group_uuid = ? where uuid = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, row.getString(2)); + update.setString(2, row.getString(1)); + return true; + }); + } + @Override protected void execute(Context context) throws SQLException { MassUpdate massUpdate = context.prepareMassUpdate(); @@ -46,18 +59,6 @@ public class PopulateOrganizationsDefaultGroupUuid extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select o.uuid, g.uuid " + - "from organizations o " + - "join groups g on o.default_group_id = g.id"); - - massUpdate.update("update organizations set default_group_uuid = ? where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(2)); - update.setString(2, row.getString(1)); - return true; - }); + removeOrphans(context); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/qprofileeditgroups/PopulateQProfileEditGroupsGroupUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/qprofileeditgroups/PopulateQProfileEditGroupsGroupUuid.java index 37d7e5a6331..2781be290ad 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/qprofileeditgroups/PopulateQProfileEditGroupsGroupUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/groups/qprofileeditgroups/PopulateQProfileEditGroupsGroupUuid.java @@ -23,6 +23,7 @@ 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; +import org.sonar.server.platform.db.migration.version.v84.util.OrphanData; public class PopulateQProfileEditGroupsGroupUuid extends DataChange { @@ -46,15 +47,6 @@ public class PopulateQProfileEditGroupsGroupUuid extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select qeg.uuid from qprofile_edit_groups qeg where qeg.group_uuid is null"); - - massUpdate.update("delete from qprofile_edit_groups where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - return true; - }); + OrphanData.delete(context, "qprofile_edit_groups", "group_uuid"); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumn.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumn.java index 8f2b3a06565..485212f9a5d 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumn.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/activerules/PopulateActiveRulesRuleUuidColumn.java @@ -23,6 +23,7 @@ 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; +import org.sonar.server.platform.db.migration.version.v84.util.OrphanData; public class PopulateActiveRulesRuleUuidColumn extends DataChange { @@ -46,14 +47,6 @@ public class PopulateActiveRulesRuleUuidColumn extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select uuid from active_rules where rule_uuid is null"); - massUpdate.update("delete from active_rules where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - return true; - }); + OrphanData.delete(context, "active_rules", "rule_uuid"); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java index 506ec9735ed..be7a6c74142 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java @@ -23,6 +23,7 @@ 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; +import org.sonar.server.platform.db.migration.version.v84.util.OrphanData; public class PopulateDeprecatedRuleKeysRuleUuidColumn extends DataChange { @@ -46,14 +47,6 @@ public class PopulateDeprecatedRuleKeysRuleUuidColumn extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select uuid from deprecated_rule_keys where rule_uuid is null"); - massUpdate.update("delete from deprecated_rule_keys where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - return true; - }); + OrphanData.delete(context, "deprecated_rule_keys", "rule_uuid"); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuid.java index 02c1a80abfc..0dd39530feb 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/groupsusers/PopulateGroupsUsersUserUuid.java @@ -50,7 +50,11 @@ public class PopulateGroupsUsersUserUuid extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); + remoteOrphanEntries(context); + } + + private static void remoteOrphanEntries(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); massUpdate.select("select group_uuid, user_id from groups_users where user_uuid is null"); massUpdate.update("delete from groups_users where group_uuid = ? and user_id = ?"); @@ -58,7 +62,6 @@ public class PopulateGroupsUsersUserUuid extends DataChange { massUpdate.execute((row, update) -> { update.setString(1, row.getString(1)); update.setLong(2, row.getLong(2)); - return true; }); } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuid.java index aec75a73ca3..33b913bc32d 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/organizationmembers/PopulateOrganizationMembersUserUuid.java @@ -51,16 +51,17 @@ public class PopulateOrganizationMembersUserUuid extends DataChange { return true; }); + MassUpdate.Handler removeOrphanHandler = (row, update) -> { + update.setString(1, row.getString(1)); + update.setLong(2, row.getLong(2)); + + return true; + }; massUpdate = context.prepareMassUpdate(); massUpdate.select("select organization_uuid, user_id from organization_members where user_uuid is null"); massUpdate.update("delete from organization_members where organization_uuid = ? and user_id = ?"); - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - update.setLong(2, row.getLong(2)); - - return true; - }); + massUpdate.execute(removeOrphanHandler); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuid.java index bb40326d801..458dd567239 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuid.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/users/fk/qprofileeditusers/PopulateQProfileEditUsersUserUuid.java @@ -23,6 +23,7 @@ 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; +import org.sonar.server.platform.db.migration.version.v84.util.OrphanData; public class PopulateQProfileEditUsersUserUuid extends DataChange { @@ -49,14 +50,6 @@ public class PopulateQProfileEditUsersUserUuid extends DataChange { return true; }); - massUpdate = context.prepareMassUpdate(); - - massUpdate.select("select uuid from qprofile_edit_users where user_uuid is null"); - massUpdate.update("delete from qprofile_edit_users where uuid = ?"); - - massUpdate.execute((row, update) -> { - update.setString(1, row.getString(1)); - return true; - }); + OrphanData.delete(context, "qprofile_edit_users", "user_uuid"); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/util/OrphanData.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/util/OrphanData.java new file mode 100644 index 00000000000..229b241f1d5 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v84/util/OrphanData.java @@ -0,0 +1,45 @@ +/* + * 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.v84.util; + +import java.sql.SQLException; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class OrphanData { + private OrphanData() { + // static only + } + /** + * Deletes from a table entries that contain a null foreign key. + * Primary key of the table must be 'uuid' + */ + public static void delete(DataChange.Context context, String table, String foreignKeyColumn) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select uuid from " + table + " where " + foreignKeyColumn + " is null"); + massUpdate.update("delete from " + table + " where " + " uuid = ?"); + + massUpdate.execute((row, update) -> { + update.setString(1, row.getString(1)); + return true; + }); + } +} -- 2.39.5