diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-12-11 14:53:17 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-12-11 14:53:17 +0100 |
commit | 63e79feb4bc099a53bfbc5d3b8fd1cb023c614a2 (patch) | |
tree | ec15887f4650ae16663cbbedcc6bdc07fa8c0d29 | |
parent | 9422c6fb84408d764d09b3802911f932cc0fcc61 (diff) | |
download | sonarqube-63e79feb4bc099a53bfbc5d3b8fd1cb023c614a2.tar.gz sonarqube-63e79feb4bc099a53bfbc5d3b8fd1cb023c614a2.zip |
Db migration to remove sort criteria from existing issue filters
10 files changed, 246 insertions, 3 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/BaseDataChange.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/BaseDataChange.java index 6fbf7c8a19c..b90886eb3f4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/BaseDataChange.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/BaseDataChange.java @@ -29,7 +29,7 @@ public abstract class BaseDataChange implements DataChange, DatabaseMigration { private final Database db; - protected BaseDataChange(Database db) { + public BaseDataChange(Database db) { this.db = db; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java index 6bbc494844d..164b766f87e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java @@ -72,7 +72,8 @@ public interface DatabaseMigrations { ReplaceIssueFiltersProjectKeyByUuid.class, FeedSnapshotSourcesUpdatedAt.class, FeedFileSources.class, - FeedIssueLongDates.class + FeedIssueLongDates.class, + RemoveSortFieldFromIssueFiltersMigration.class ); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigration.java new file mode 100644 index 00000000000..46c05eca0e3 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigration.java @@ -0,0 +1,82 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.db.migrations.v50; + +import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.Database; +import org.sonar.server.db.migrations.BaseDataChange; +import org.sonar.server.db.migrations.MassUpdate; +import org.sonar.server.db.migrations.Select; +import org.sonar.server.db.migrations.SqlStatement; + +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + +// TODO could be refactored to benefit from existing code of ReplaceIssueFiltersProjectKeyByUuid +// -> make any change of issue_filters easier +public class RemoveSortFieldFromIssueFiltersMigration extends BaseDataChange { + + private static final char FIELD_SEPARATOR = '|'; + private static final String SORT_KEY = "sort="; + private static final String ASC_KEY = "asc="; + + private final System2 system; + + public RemoveSortFieldFromIssueFiltersMigration(Database db, System2 system) { + super(db); + this.system = system; + } + + @Override + public void execute(Context context) throws SQLException { + final Date now = new Date(system.now()); + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id,data from issue_filters where data like '%" + SORT_KEY + "%' or data like '%" + ASC_KEY +"%'"); + massUpdate.update("update issue_filters set data=?, updated_at=? where id=?"); + massUpdate.rowPluralName("issue filters"); + massUpdate.execute(new MassUpdate.Handler() { + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + String data = row.getString(2); + String[] fields = StringUtils.split(data, FIELD_SEPARATOR); + + boolean found = false; + List<String> fieldsToKeep = Lists.newArrayList(); + for (String field : fields) { + if (field.startsWith(SORT_KEY) || field.startsWith(ASC_KEY)) { + found = true; + } else { + fieldsToKeep.add(field); + } + } + if (found) { + // data without 'sort' field + update.setString(1, StringUtils.join(fieldsToKeep, FIELD_SEPARATOR)); + update.setDate(2, now); + update.setLong(3, row.getLong(1)); + } + return found; + } + }); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java new file mode 100644 index 00000000000..8307231468d --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.db.migrations.v50; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.TestDatabase; +import org.sonar.server.db.migrations.DatabaseMigration; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RemoveSortFieldFromIssueFiltersMigrationTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(RemoveSortFieldFromIssueFiltersMigrationTest.class, "schema.sql"); + + DatabaseMigration migration; + System2 system = mock(System2.class); + + @Before + public void setUp() throws Exception { + db.executeUpdateSql("truncate table issue_filters"); + migration = new RemoveSortFieldFromIssueFiltersMigration(db.database(), system); + when(system.now()).thenReturn(DateUtils.parseDate("2014-10-29").getTime()); + } + + @Test + public void execute() throws Exception { + db.prepareDbUnit(getClass(), "execute.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters"); + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute-result.xml new file mode 100644 index 00000000000..444d7d5f37a --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute-result.xml @@ -0,0 +1,33 @@ +<dataset> + + <issue_filters + id="1" + name="No sort field" + user_login="stephane" + shared="[true]" + description="no not touch" + data="projectUuids=ABCD" + created_at="2013-06-10" + updated_at="2013-06-10" /> + + <issue_filters + id="2" + name="Has sort field" + user_login="michael" + shared="[false]" + description="to be updated" + data="statuses=OPEN|projectUuids=ABC" + created_at="2013-06-10" + updated_at="2014-10-29 00:00:00.0" /> + + <issue_filters + id="3" + name="corner-case" + user_login="michael" + shared="[true]" + description="do not touch" + data="statuses=CLOSED|resort=true" + created_at="2013-06-10" + updated_at="2013-06-10" /> + +</dataset> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute.xml new file mode 100644 index 00000000000..6ba632e10cb --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/execute.xml @@ -0,0 +1,33 @@ +<dataset> + + <issue_filters + id="1" + name="No sort field" + user_login="stephane" + shared="[true]" + description="no not touch" + data="projectUuids=ABCD" + created_at="2013-06-10" + updated_at="2013-06-10" /> + + <issue_filters + id="2" + name="Has sort field" + user_login="michael" + shared="[false]" + description="to be updated" + data="statuses=OPEN|sort=SEVERITY|asc=true|projectUuids=ABC" + created_at="2013-06-10" + updated_at="2013-06-10" /> + + <issue_filters + id="3" + name="corner-case" + user_login="michael" + shared="[true]" + description="do not touch" + data="statuses=CLOSED|resort=true" + created_at="2013-06-10" + updated_at="2013-06-10" /> + +</dataset> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/schema.sql b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/schema.sql new file mode 100644 index 00000000000..0627153a62d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/RemoveSortFieldFromIssueFiltersMigrationTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "ISSUE_FILTERS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "SHARED" BOOLEAN NOT NULL DEFAULT FALSE, + "USER_LOGIN" VARCHAR(255), + "DESCRIPTION" VARCHAR(4000), + "DATA" CLOB(2147483647), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb new file mode 100644 index 00000000000..8978b97225c --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb @@ -0,0 +1,27 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# SonarQube is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +class RemoveSortFieldFromIssueFilters < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.server.db.migrations.v50.RemoveSortFieldFromIssueFiltersMigration') + end +end + diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index a0677316258..23736c1892b 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -33,7 +33,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 720; + public static final int LAST_VERSION = 721; /** * List of all the tables. diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 29605abdc40..3cf67c0abad 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -278,6 +278,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('717'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('718'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('719'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('720'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('721'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; |