You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PopulateNclocForProjects.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.platform.db.migration.version.v99;
  21. import java.sql.SQLException;
  22. import org.sonar.db.Database;
  23. import org.sonar.server.platform.db.migration.step.DataChange;
  24. import org.sonar.server.platform.db.migration.step.MassUpdate;
  25. public class PopulateNclocForProjects extends DataChange {
  26. private static final String SELECT_QUERY = """
  27. SELECT b.project_uuid AS projectUuid, max(lm.value) AS maxncloc
  28. FROM live_measures lm
  29. INNER JOIN metrics m ON m.uuid = lm.metric_uuid
  30. INNER JOIN project_branches b ON b.uuid = lm.component_uuid
  31. INNER JOIN projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK'
  32. WHERE m.name = 'ncloc'
  33. GROUP BY b.project_uuid
  34. """;
  35. public PopulateNclocForProjects(Database db) {
  36. super(db);
  37. }
  38. @Override
  39. protected void execute(Context context) throws SQLException {
  40. MassUpdate massUpdate = context.prepareMassUpdate();
  41. massUpdate.select(SELECT_QUERY);
  42. massUpdate.update("update projects set ncloc = ? where uuid = ?");
  43. massUpdate.execute((row, update) -> {
  44. String uuid = row.getString(1);
  45. Long ncloc = row.getLong(2);
  46. update.setLong(1, ncloc);
  47. update.setString(2, uuid);
  48. return true;
  49. });
  50. }
  51. }