-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UpgradeQualityTemplateLoadedTemplates.java
69 lines (60 loc) · 2.85 KB
/
UpgradeQualityTemplateLoadedTemplates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v64;
import java.security.MessageDigest;
import java.sql.SQLException;
import org.apache.commons.codec.digest.DigestUtils;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;
import org.sonar.server.platform.db.migration.version.v63.DefaultOrganizationUuid;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.codec.binary.Hex.encodeHexString;
public class UpgradeQualityTemplateLoadedTemplates extends DataChange {
private static final String QUALITY_PROFILE_TYPE = "QUALITY_PROFILE";
private final DefaultOrganizationUuid defaultOrganizationUuid;
public UpgradeQualityTemplateLoadedTemplates(Database db, DefaultOrganizationUuid defaultOrganizationUuid) {
super(db);
this.defaultOrganizationUuid = defaultOrganizationUuid;
}
@Override
protected void execute(Context context) throws SQLException {
String defaultOrganizationUuid = this.defaultOrganizationUuid.getAndCheck(context);
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate
.select("select id,kee from loaded_templates where template_type=?")
.setString(1, QUALITY_PROFILE_TYPE);
massUpdate.rowPluralName("loaded templates for quality profiles");
massUpdate.update("update loaded_templates set template_type=?,kee=? where id=?");
MessageDigest md5Digest = DigestUtils.getMd5Digest();
massUpdate.execute((row, update) -> {
int id = row.getInt(1);
String key = row.getString(2);
update.setString(1, computeLoadedTemplateType(key, md5Digest));
update.setString(2, defaultOrganizationUuid);
update.setInt(3, id);
return true;
});
}
private static String computeLoadedTemplateType(String currentKey, MessageDigest messageDigest) {
return format("%s.%s", QUALITY_PROFILE_TYPE, encodeHexString(messageDigest.digest(currentKey.getBytes(UTF_8))));
}
}