3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v82;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.sonar.db.Database;
25 import org.sonar.server.platform.db.migration.step.DataChange;
26 import org.sonar.server.platform.db.migration.step.MassUpdate;
28 public class DeleteSecurityReviewRatingProjectMeasures extends DataChange {
30 private static final String SECURITY_REVIEW_RATING_METRIC_KEY = "security_review_rating";
31 private static final String SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY = "security_review_rating_effort";
32 private static final String SELECT_COMPONENTS_STATEMENT = "select c.uuid from components c where c.scope in ('PRJ')";
34 public DeleteSecurityReviewRatingProjectMeasures(Database db) {
39 protected void execute(Context context) throws SQLException {
40 Integer reviewRatingId = getMetricId(context, SECURITY_REVIEW_RATING_METRIC_KEY);
41 Integer reviewRatingEffortId = getMetricId(context, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY);
42 if (reviewRatingId != null) {
43 deleteFromProjectMeasures(context, reviewRatingId, reviewRatingEffortId);
48 private static Integer getMetricId(Context context, String metricName) throws SQLException {
49 return context.prepareSelect("select id from metrics where name = ?")
50 .setString(1, metricName)
51 .get(row -> row.getNullableInt(1));
54 private static void deleteFromProjectMeasures(Context context, Integer reviewRatingId, @Nullable Integer reviewRatingEffortId) throws SQLException {
55 MassUpdate deleteFromProjectMeasures = context.prepareMassUpdate();
57 deleteFromProjectMeasures.select(SELECT_COMPONENTS_STATEMENT);
58 if (reviewRatingEffortId != null) {
59 deleteFromProjectMeasures.update("delete from project_measures where component_uuid = ? and metric_id in (?, ?)");
61 deleteFromProjectMeasures.update("delete from project_measures where component_uuid = ? and metric_id = ?");
64 deleteFromProjectMeasures.execute((row, update) -> {
65 String componentUuid = row.getString(1);
66 update.setString(1, componentUuid)
67 .setInt(2, reviewRatingId);
68 if (reviewRatingEffortId != null) {
69 update.setInt(3, reviewRatingEffortId);