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.v70;
22 import java.sql.SQLException;
23 import org.sonar.api.utils.System2;
24 import org.sonar.core.util.Uuids;
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;
29 public class PopulateLiveMeasures extends DataChange {
31 private final System2 system2;
33 public PopulateLiveMeasures(Database db, System2 system2) {
35 this.system2 = system2;
39 protected void execute(Context context) throws SQLException {
40 long now = system2.now();
41 // reentrancy of migration
42 context.prepareUpsert("TRUNCATE TABLE live_measures").execute();
44 MassUpdate massUpdate = context.prepareMassUpdate();
45 massUpdate.select("SELECT p.uuid, p.project_uuid, pm.metric_id, pm.value, pm.text_value, pm.variation_value_1, pm.measure_data " +
46 "FROM project_measures pm " +
47 "INNER JOIN projects p on p.uuid = pm.component_uuid " +
48 "INNER JOIN snapshots s on s.uuid = pm.analysis_uuid " +
52 massUpdate.update("INSERT INTO live_measures "
53 + "(uuid, component_uuid, project_uuid, metric_id, value, text_value, variation, measure_data, created_at, updated_at) "
54 + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
56 massUpdate.rowPluralName("live measures");
57 massUpdate.execute((row, update) -> {
58 update.setString(1, Uuids.create());
59 update.setString(2, row.getString(1));
60 update.setString(3, row.getString(2));
61 update.setInt(4, row.getInt(3));
62 update.setDouble(5, row.getNullableDouble(4));
63 update.setString(6, row.getString(5));
64 update.setDouble(7, row.getNullableDouble(6));
65 update.setBytes(8, row.getNullableBytes(7));
66 update.setLong(9, now);
67 update.setLong(10, now);