3 * Copyright (C) 2009-2017 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.v60;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import org.sonar.db.Database;
26 import org.sonar.server.platform.db.migration.step.DataChange;
27 import org.sonar.server.platform.db.migration.step.MassUpdate;
28 import org.sonar.server.platform.db.migration.step.Select;
29 import org.sonar.server.platform.db.migration.step.SqlStatement;
31 public class PopulateAnalysisUuidOnMeasures extends DataChange {
33 public PopulateAnalysisUuidOnMeasures(Database db) {
38 public void execute(Context context) throws SQLException {
39 Map<Long, String> rootSnapshotUuids = loadRootSnapshotUuids(context);
42 MassUpdate massUpdate = context.prepareMassUpdate();
43 // mysql can take hours if the 2 requests are merged into a single one
44 massUpdate.select("select distinct m.snapshot_id as sId, s.root_snapshot_id as rootSid " +
45 "from project_measures m " +
46 "inner join snapshots s on m.snapshot_id = s.id " +
47 "where m.analysis_uuid is null " +
49 "select distinct m.snapshot_id as sId, s.root_snapshot_id as rootSid " +
50 "from project_measures m " +
51 "inner join snapshots s on m.snapshot_id=s.id " +
52 "where m.analysis_uuid is null"
54 massUpdate.update("update project_measures set analysis_uuid=? where snapshot_id = ? and analysis_uuid is null");
55 massUpdate.rowPluralName("measures");
56 massUpdate.execute((row, update) -> handleRow(row, update, rootSnapshotUuids));
59 private static Map<Long, String> loadRootSnapshotUuids(Context context) throws SQLException {
60 Map<Long, String> snapshotUuidsByIds = new HashMap<>();
61 context.prepareSelect("select distinct id, uuid from snapshots where depth=0")
62 .scroll(row -> snapshotUuidsByIds.put(row.getLong(1), row.getString(2)));
63 return snapshotUuidsByIds;
66 private static boolean handleRow(Select.Row row, SqlStatement update, Map<Long, String> rootSnapshotUuids) throws SQLException {
67 long snapshotId = row.getLong(1);
68 Long rootSnapshotId = row.getNullableLong(2);
69 String analysisUuid = rootSnapshotUuids.get(rootSnapshotId == null ? snapshotId : rootSnapshotId);
70 if (analysisUuid == null) {
74 update.setString(1, analysisUuid);
75 update.setLong(2, snapshotId);