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.Connection;
23 import java.sql.SQLException;
24 import org.sonar.db.Database;
25 import org.sonar.db.DatabaseUtils;
26 import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
27 import org.sonar.server.platform.db.migration.step.DdlChange;
29 import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
30 import static org.sonar.server.platform.db.migration.def.ClobColumnDef.newClobColumnDefBuilder;
31 import static org.sonar.server.platform.db.migration.def.TinyIntColumnDef.newTinyIntColumnDefBuilder;
32 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.USER_UUID_SIZE;
33 import static org.sonar.server.platform.db.migration.version.v00.CreateInitialSchema.newVarcharColumnBuilder;
35 public class AddRulesMetadataColumnsToRulesTable extends DdlChange {
37 private static final String TABLE_NAME = "rules";
40 public AddRulesMetadataColumnsToRulesTable(Database db) {
45 public void execute(Context context) throws SQLException {
46 try (var connection = getDatabase().getDataSource().getConnection()) {
47 createNoteDataAt(context, connection);
48 createNoteUserUuid(context, connection);
49 createNoteCreatedAt(context, connection);
50 createNoteUpdatedAt(context, connection);
51 createRemediationFunction(context, connection);
52 createRemediationGapMult(context, connection);
53 createRemediationBaseEffort(context, connection);
54 createTags(context, connection);
55 createAdHocName(context, connection);
56 createAdHocDescription(context, connection);
57 createAdHocSeverity(context, connection);
58 createAdHocType(context, connection);
62 private void createAdHocType(Context context, Connection connection) {
63 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "ad_hoc_type")) {
64 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
65 .addColumn(newTinyIntColumnDefBuilder().setColumnName("ad_hoc_type").setIsNullable(true).build())
70 private void createAdHocSeverity(Context context, Connection connection) {
71 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "ad_hoc_severity")) {
72 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
73 .addColumn(newVarcharColumnBuilder("ad_hoc_severity").setLimit(10).setIsNullable(true).build())
78 private void createAdHocDescription(Context context, Connection connection) {
79 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "ad_hoc_description")) {
80 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
81 .addColumn(newClobColumnDefBuilder().setColumnName("ad_hoc_description").setIsNullable(true).build())
86 private void createAdHocName(Context context, Connection connection) {
87 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "ad_hoc_name")) {
88 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
89 .addColumn(newVarcharColumnBuilder("ad_hoc_name").setLimit(200).setIsNullable(true).build())
94 private void createTags(Context context, Connection connection) {
95 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "tags")) {
96 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
97 .addColumn(newVarcharColumnBuilder("tags").setLimit(4_000).setIsNullable(true).build())
102 private void createRemediationBaseEffort(Context context, Connection connection) {
103 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "remediation_base_effort")) {
104 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
105 .addColumn(newVarcharColumnBuilder("remediation_base_effort").setLimit(20).setIsNullable(true).build())
110 private void createRemediationGapMult(Context context, Connection connection) {
111 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "remediation_gap_mult")) {
112 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
113 .addColumn(newVarcharColumnBuilder("remediation_gap_mult").setLimit(20).setIsNullable(true).build())
118 private void createRemediationFunction(Context context, Connection connection) {
119 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "remediation_function")) {
120 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
121 .addColumn(newVarcharColumnBuilder("remediation_function").setLimit(20).setIsNullable(true).build())
126 private void createNoteUpdatedAt(Context context, Connection connection) {
127 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "note_updated_at")) {
128 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
129 .addColumn(newBigIntegerColumnDefBuilder().setColumnName("note_updated_at").setIsNullable(true).build())
134 private void createNoteCreatedAt(Context context, Connection connection) {
135 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "note_created_at")) {
136 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
137 .addColumn(newBigIntegerColumnDefBuilder().setColumnName("note_created_at").setIsNullable(true).build())
142 private void createNoteUserUuid(Context context, Connection connection) {
143 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "note_user_uuid")) {
144 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
145 .addColumn(newVarcharColumnBuilder("note_user_uuid").setLimit(USER_UUID_SIZE).setIsNullable(true).build())
150 private void createNoteDataAt(Context context, Connection connection) {
151 if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "note_data")) {
152 context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
153 .addColumn(newClobColumnDefBuilder().setColumnName("note_data").setIsNullable(true).build())