3 * Copyright (C) 2009-2019 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.v70;
22 import java.sql.SQLException;
23 import org.sonar.api.config.Configuration;
24 import org.sonar.db.Database;
25 import org.sonar.db.dialect.H2;
26 import org.sonar.db.dialect.MsSql;
27 import org.sonar.db.dialect.MySql;
28 import org.sonar.db.dialect.Oracle;
29 import org.sonar.db.dialect.PostgreSql;
30 import org.sonar.server.platform.db.migration.step.DataChange;
31 import org.sonar.server.platform.db.migration.step.MassUpdate;
33 public class DeletePersonAndFileMeasures extends DataChange {
35 private final Configuration configuration;
37 public DeletePersonAndFileMeasures(Database db, Configuration configuration) {
39 this.configuration = configuration;
43 protected void execute(Context context) throws SQLException {
44 if (configuration.getBoolean("sonar.sonarcloud.enabled").orElse(false)) {
45 // clean-up will be done in background so that interruption of service
46 // is reduced during upgrade
49 MassUpdate massUpdate = context.prepareMassUpdate();
50 massUpdate.select("select uuid from snapshots");
51 massUpdate.rowPluralName("snapshots");
52 massUpdate.update(getDeleteSql()).setBatchSize(1);
54 massUpdate.execute((row, update) -> {
55 String analysisUuid = row.getString(1);
56 update.setString(1, analysisUuid);
57 if (getDialect().getId().equals(Oracle.ID)) {
58 update.setString(2, analysisUuid);
64 private String getDeleteSql() {
65 switch (getDialect().getId()) {
68 return "delete pm from project_measures pm " +
69 "inner join projects c on c.uuid = pm.component_uuid " +
70 "where pm.analysis_uuid = ? " +
71 "and (c.qualifier in ('UTS', 'FIL') or pm.person_id is not null)";
73 return "delete from project_measures " +
75 " select pm.id from project_measures pm " +
76 " inner join projects c on c.uuid = pm.component_uuid " +
77 " where pm.analysis_uuid = ? " +
78 " and (c.qualifier in ('UTS', 'FIL') or pm.person_id is not null) " +
81 return "delete from project_measures pm " +
83 "where pm.analysis_uuid = ? " +
84 "and c.uuid = pm.component_uuid " +
85 "and (c.qualifier in ('UTS', 'FIL') or pm.person_id is not null) ";
87 return "delete from project_measures pm where exists (" +
88 " select 1 from project_measures pm2 " +
89 " inner join projects c on c.uuid = pm2.component_uuid " +
90 " where pm2.analysis_uuid = ? " +
91 " and (c.qualifier in ('UTS', 'FIL') or pm.person_id is not null) " +
92 " and pm.id = pm2.id" +
93 ") and pm.analysis_uuid = ?";
95 throw new IllegalStateException("Unsupported DB dialect: " + getDialect());