2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.db.migrations.v50;
22 import com.google.common.collect.Lists;
23 import org.apache.commons.lang.StringUtils;
24 import org.sonar.api.utils.System2;
25 import org.sonar.core.persistence.Database;
26 import org.sonar.server.db.migrations.BaseDataChange;
27 import org.sonar.server.db.migrations.MassUpdate;
28 import org.sonar.server.db.migrations.Select;
29 import org.sonar.server.db.migrations.SqlStatement;
31 import java.sql.SQLException;
32 import java.util.Date;
33 import java.util.List;
35 // TODO could be refactored to benefit from existing code of ReplaceIssueFiltersProjectKeyByUuid
36 // -> make any change of issue_filters easier
37 public class RemoveSortFieldFromIssueFiltersMigration extends BaseDataChange {
39 private static final char FIELD_SEPARATOR = '|';
40 private static final String SORT_KEY = "sort=";
41 private static final String ASC_KEY = "asc=";
43 private final System2 system;
45 public RemoveSortFieldFromIssueFiltersMigration(Database db, System2 system) {
51 public void execute(Context context) throws SQLException {
52 final Date now = new Date(system.now());
53 MassUpdate massUpdate = context.prepareMassUpdate();
54 massUpdate.select("select id,data from issue_filters where data like '%" + SORT_KEY + "%' or data like '%" + ASC_KEY +"%'");
55 massUpdate.update("update issue_filters set data=?, updated_at=? where id=?");
56 massUpdate.rowPluralName("issue filters");
57 massUpdate.execute(new MassUpdate.Handler() {
59 public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
60 String data = row.getString(2);
61 String[] fields = StringUtils.split(data, FIELD_SEPARATOR);
63 boolean found = false;
64 List<String> fieldsToKeep = Lists.newArrayList();
65 for (String field : fields) {
66 if (field.startsWith(SORT_KEY) || field.startsWith(ASC_KEY)) {
69 fieldsToKeep.add(field);
73 // data without 'sort' field
74 update.setString(1, StringUtils.join(fieldsToKeep, FIELD_SEPARATOR));
75 update.setDate(2, now);
76 update.setLong(3, row.getLong(1));