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.

IssueChangelogMigrationStep.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.db.migrations.v43;
  21. import java.sql.SQLException;
  22. import java.util.Date;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.db.Database;
  29. import org.sonar.db.property.PropertiesDao;
  30. import org.sonar.server.db.migrations.BaseDataChange;
  31. import org.sonar.server.db.migrations.MassUpdate;
  32. import org.sonar.server.db.migrations.Select;
  33. import org.sonar.server.db.migrations.SqlStatement;
  34. import com.google.common.annotations.VisibleForTesting;
  35. import com.google.common.base.Strings;
  36. /**
  37. * Used in the Active Record Migration 514
  38. *
  39. * @since 4.3
  40. */
  41. public class IssueChangelogMigrationStep extends BaseDataChange {
  42. private final WorkDurationConvertor workDurationConvertor;
  43. private final System2 system2;
  44. private final Pattern pattern = Pattern.compile("technicalDebt=(\\d*)\\|(\\d*)", Pattern.CASE_INSENSITIVE);
  45. public IssueChangelogMigrationStep(Database database, System2 system2, PropertiesDao propertiesDao) {
  46. this(database, system2, new WorkDurationConvertor(propertiesDao));
  47. }
  48. @VisibleForTesting
  49. IssueChangelogMigrationStep(Database database, System2 system2, WorkDurationConvertor convertor) {
  50. super(database);
  51. this.workDurationConvertor = convertor;
  52. this.system2 = system2;
  53. }
  54. @Override
  55. public void execute(Context context) throws SQLException {
  56. workDurationConvertor.init();
  57. final Date now = new Date(system2.now());
  58. MassUpdate massUpdate = context.prepareMassUpdate();
  59. massUpdate.select("SELECT ic.id, ic.change_data FROM issue_changes ic " +
  60. "WHERE ic.change_type = 'diff' AND ic.change_data LIKE '%technicalDebt%'");
  61. massUpdate.update("UPDATE issue_changes SET change_data=?,updated_at=? WHERE id=?");
  62. massUpdate.execute(new MassUpdate.Handler() {
  63. @Override
  64. public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
  65. Long id = row.getNullableLong(1);
  66. String changeData = row.getNullableString(2);
  67. update.setString(1, convertChangelog(changeData));
  68. update.setDate(2, now);
  69. update.setLong(3, id);
  70. return true;
  71. }
  72. });
  73. }
  74. @VisibleForTesting
  75. @CheckForNull
  76. String convertChangelog(@Nullable String data) {
  77. if (data == null) {
  78. return null;
  79. }
  80. Matcher matcher = pattern.matcher(data);
  81. StringBuffer sb = new StringBuffer();
  82. if (matcher.find()) {
  83. String replacement = "technicalDebt=";
  84. String oldValue = matcher.group(1);
  85. if (!Strings.isNullOrEmpty(oldValue)) {
  86. long oldDebt = workDurationConvertor.createFromLong(Long.parseLong(oldValue));
  87. replacement += Long.toString(oldDebt);
  88. }
  89. replacement += "|";
  90. String newValue = matcher.group(2);
  91. if (!Strings.isNullOrEmpty(newValue)) {
  92. long newDebt = workDurationConvertor.createFromLong(Long.parseLong(newValue));
  93. replacement += Long.toString(newDebt);
  94. }
  95. matcher.appendReplacement(sb, replacement);
  96. }
  97. matcher.appendTail(sb);
  98. return sb.toString();
  99. }
  100. }