3 * Copyright (C) 2009-2019 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.v61;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.sonar.api.utils.System2;
25 import org.sonar.db.Database;
26 import org.sonar.server.platform.db.migration.step.DataChange;
27 import org.sonar.server.platform.db.migration.step.Upsert;
29 public class RemoveViewsDefinitionFromProperties extends DataChange {
31 private static final String VIEWS_DEFINITION_PROPERTY_KEY = "views.def";
32 private static final int VARCHAR_MAX_LENGTH = 4000;
34 private final System2 system2;
36 public RemoveViewsDefinitionFromProperties(Database db, System2 system2) {
38 this.system2 = system2;
42 public void execute(Context context) throws SQLException {
43 String property = context
44 .prepareSelect("select text_value from properties where prop_key=?")
45 .setString(1, VIEWS_DEFINITION_PROPERTY_KEY)
46 .get(row -> row.getNullableString(1));
47 Integer hasInternalProperty = context
48 .prepareSelect("select 1 from internal_properties where kee=?")
49 .setString(1, VIEWS_DEFINITION_PROPERTY_KEY)
50 .get(row -> row.getNullableInt(1));
52 if (hasInternalProperty == null) {
53 addToInternalProperties(context, property);
55 deleteFromProperties(context);
58 private void addToInternalProperties(Context context, @Nullable String property) throws SQLException {
59 if (property != null) {
60 boolean mustBeStoredInClob = property.length() > VARCHAR_MAX_LENGTH;
61 try (Upsert insert = context.prepareUpsert("insert into internal_properties" +
62 " (kee, is_empty, " + (mustBeStoredInClob ? "clob_value" : "text_value") + ", created_at)" +
65 long now = system2.now();
67 .setString(1, "views.def")
69 .setString(3, property)
77 private static void deleteFromProperties(Context context) throws SQLException {
78 try (Upsert delete = context.prepareUpsert("delete from properties where prop_key=?")) {
80 .setString(1, VIEWS_DEFINITION_PROPERTY_KEY)