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 java.sql.SQLException;
23 import org.sonar.db.Database;
24 import org.sonar.db.DatabaseUtils;
25 import org.sonar.server.platform.db.migration.step.DataChange;
26 import org.sonar.server.platform.db.migration.step.MassUpdate;
28 public class PopulateRulesMetadataInRuleTable extends DataChange {
30 private static final String RULES_METADATA_TABLE_NAME = "rules_metadata";
32 public PopulateRulesMetadataInRuleTable(Database db) {
37 protected void execute(Context context) throws SQLException {
38 if (ruleMetadataTableExists()) {
39 MassUpdate massUpdate = context.prepareMassUpdate();
40 massUpdate.select("select note_data, note_user_uuid, note_created_at, note_updated_at, remediation_function," +
41 " remediation_gap_mult, remediation_base_effort, tags, ad_hoc_name, ad_hoc_description, ad_hoc_severity, ad_hoc_type, rule_uuid from rules_metadata");
43 massUpdate.update("update rules set note_data = ?, note_user_uuid = ?, note_created_at = ?," +
44 " note_updated_at = ?, remediation_function = ?, remediation_gap_mult = ?, remediation_base_effort = ?," +
45 " tags = ?, ad_hoc_name = ?, ad_hoc_description = ?, ad_hoc_severity = ?, ad_hoc_type = ? where uuid = ?");
47 massUpdate.execute((row, update) -> {
48 update.setBytes(1, row.getBytes(1)); // note_data
49 update.setString(2, row.getString(2)); // note_user_uuid
50 update.setLong(3, row.getLong(3)); // note_created_at
51 update.setLong(4, row.getLong(4)); // note_updated_at
52 update.setString(5, row.getString(5)); // remediation_function
53 update.setString(6, row.getString(6)); // remediation_gap_mult
54 update.setString(7, row.getString(7)); // remediation_base_effort
55 update.setString(8, row.getString(8)); // tags
56 update.setString(9, row.getString(9)); // ad_hoc_name
57 update.setString(10, row.getString(10)); // ad_hoc_description
58 update.setString(11, row.getString(11)); // ad_hoc_severity
59 update.setInt(12, row.getInt(12)); // ad_hoc_type
60 update.setString(13, row.getString(13)); // where clause (rule uuid)
67 private boolean ruleMetadataTableExists() throws SQLException {
68 try (var connection = getDatabase().getDataSource().getConnection()) {
69 return DatabaseUtils.tableExists(RULES_METADATA_TABLE_NAME, connection);