3 * Copyright (C) 2009-2022 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.v95;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.sql.SQLException;
24 import java.util.List;
25 import org.sonar.core.util.UuidFactory;
26 import org.sonar.db.Database;
27 import org.sonar.db.DatabaseUtils;
28 import org.sonar.server.platform.db.migration.step.DataChange;
29 import org.sonar.server.platform.db.migration.step.Upsert;
31 import static org.sonar.server.platform.db.migration.version.v95.CreateRuleDescSectionsTable.RULE_DESCRIPTION_SECTIONS_TABLE;
33 public class InsertRuleDescriptionIntoRuleDescSections extends DataChange {
35 static final String DEFAULT_DESCRIPTION_KEY = "default";
37 private static final String SELECT_EXISTING_RULE_DESCRIPTIONS = "select uuid, description from rules where description is not null "
38 + "and uuid not in (select rule_uuid from " + RULE_DESCRIPTION_SECTIONS_TABLE + ")";
39 private static final String INSERT_INTO_RULE_DESC_SECTIONS = "insert into " + RULE_DESCRIPTION_SECTIONS_TABLE + " (uuid, rule_uuid, kee, description) values "
42 private final UuidFactory uuidFactory;
44 public InsertRuleDescriptionIntoRuleDescSections(Database db, UuidFactory uuidFactory) {
46 this.uuidFactory = uuidFactory;
50 protected void execute(Context context) throws SQLException {
51 try (var connection = getDatabase().getDataSource().getConnection()) {
52 if (!DatabaseUtils.tableColumnExists(connection, "rules", "description")) {
56 List<RuleDb> selectRuleDb = findExistingRuleDescriptions(context);
57 if (selectRuleDb.isEmpty()) {
60 insertRuleDescSections(context, selectRuleDb);
63 private List<RuleDb> findExistingRuleDescriptions(Context context) throws SQLException {
64 return context.prepareSelect(SELECT_EXISTING_RULE_DESCRIPTIONS)
65 .list(r -> new RuleDb(r.getString(1), r.getString(2)));
68 private void insertRuleDescSections(Context context, List<RuleDb> selectRuleDb) throws SQLException {
69 Upsert insertRuleDescSections = context.prepareUpsert(INSERT_INTO_RULE_DESC_SECTIONS);
70 for (RuleDb ruleDb : selectRuleDb) {
71 insertRuleDescSections
72 .setString(1, uuidFactory.create())
73 .setString(2, ruleDb.getUuid())
74 .setString(3, DEFAULT_DESCRIPTION_KEY)
75 .setString(4, ruleDb.getDescription())
78 insertRuleDescSections.execute().commit();
81 private static class RuleDb {
82 private final String uuid;
83 private final String description;
85 private RuleDb(String uuid, String description) {
87 this.description = description;
90 public String getUuid() {
94 public String getDescription() {