2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.server.db.migrations.v44;
23 import org.apache.commons.lang.ObjectUtils;
24 import org.sonar.api.utils.text.JsonWriter;
25 import org.sonar.core.UtcDateUtils;
26 import org.sonar.core.persistence.DbSession;
27 import org.sonar.core.persistence.migration.v44.Migration44Mapper;
28 import org.sonar.core.persistence.migration.v44.ProfileMeasure;
29 import org.sonar.core.persistence.migration.v44.QProfileDto44;
30 import org.sonar.server.db.DbClient;
31 import org.sonar.server.db.migrations.DatabaseMigration;
33 import java.io.StringWriter;
34 import java.util.Date;
37 * Feed the new columns RULES_PROFILE.KEE and PARENT_KEE.
41 public class ConvertProfileMeasuresMigration implements DatabaseMigration {
43 private final DbClient db;
45 public ConvertProfileMeasuresMigration(DbClient db) {
50 public void execute() {
51 DbSession session = db.openSession(false);
54 Date now = new Date();
55 Migration44Mapper mapper = session.getMapper(Migration44Mapper.class);
56 for (ProfileMeasure profileMeasure : mapper.selectProfileMeasures()) {
57 Integer version = mapper.selectProfileVersion(profileMeasure.getSnapshotId());
58 QProfileDto44 profile = mapper.selectProfileById(profileMeasure.getProfileId());
59 if (profile != null) {
61 if (version != null) {
62 date = (Date)ObjectUtils.defaultIfNull(
63 mapper.selectProfileVersionDate(profileMeasure.getProfileId(), version), now);
65 // see format of JSON in org.sonar.batch.rule.UsedQProfiles
66 StringWriter writer = new StringWriter();
67 JsonWriter json = JsonWriter.of(writer);
71 .prop("key", profile.getKee())
72 .prop("language", profile.getLanguage())
73 .prop("name", profile.getName())
74 .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(date))
78 mapper.updateProfileMeasure(profileMeasure.getId(), writer.toString());