diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-04-29 16:05:54 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-05-03 14:26:31 +0200 |
commit | a474078fe7086eae9d30fa506ce77f525ac199d3 (patch) | |
tree | cd9568aed3108423cf06f5a0b0a2e6b6093f5372 /sonar-db | |
parent | e1f3c47a66989f592545792c8cc3daa78eccebe1 (diff) | |
download | sonarqube-a474078fe7086eae9d30fa506ce77f525ac199d3.tar.gz sonarqube-a474078fe7086eae9d30fa506ce77f525ac199d3.zip |
SONAR-7212 Add migration to remove default assignee properties on removed users
Diffstat (limited to 'sonar-db')
8 files changed, 194 insertions, 3 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 8f51cc19382..64d79499016 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -29,7 +29,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1125; + public static final int LAST_VERSION = 1200; /** * The minimum supported version which can be upgraded. Lower diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 2ba4db579a7..cd2a0d76dd5 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -82,6 +82,7 @@ import org.sonar.db.version.v55.FeedActiveRulesLongDateColumns; import org.sonar.db.version.v55.FeedIssueTypes; import org.sonar.db.version.v55.FeedRulesLongDateColumns; import org.sonar.db.version.v55.FeedRulesTypes; +import org.sonar.db.version.v56.RemoveDefaultAssigneePropertiesOnDisabledUsers; public class MigrationStepModule extends Module { @Override @@ -161,7 +162,10 @@ public class MigrationStepModule extends Module { FeedRulesTypes.class, DeleteMeasuresWithRuleId.class, DeleteManualIssues.class, - DeleteManualRules.class + DeleteManualRules.class, + + // 5.6 + RemoveDefaultAssigneePropertiesOnDisabledUsers.class ); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsers.java b/sonar-db/src/main/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsers.java new file mode 100644 index 00000000000..b3942a65e05 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsers.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.db.version.v56; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.BaseDataChange; +import org.sonar.db.version.MassUpdate; +import org.sonar.db.version.Select; +import org.sonar.db.version.SqlStatement; + +/** + * Remove properties "sonar.issues.defaultAssigneeLogin" when values are on removed users + */ +public class RemoveDefaultAssigneePropertiesOnDisabledUsers extends BaseDataChange { + + private static final String DEFAULT_ISSUE_ASSIGNEE_PROP = "sonar.issues.defaultAssigneeLogin"; + + public RemoveDefaultAssigneePropertiesOnDisabledUsers(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT p.id FROM properties p " + + "INNER JOIN users u ON u.login=p.text_value AND u.active=? " + + "WHERE p.prop_key=?") + .setBoolean(1, false) + .setString(2, DEFAULT_ISSUE_ASSIGNEE_PROP); + massUpdate.update("DELETE FROM properties WHERE id=?"); + massUpdate.rowPluralName("default assignee properties on removed users"); + massUpdate.execute(new MigrationHandler()); + } + + private static class MigrationHandler implements MassUpdate.Handler { + + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + Long id = row.getNullableLong(1); + update.setLong(1, id); + return true; + } + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v56/package-info.java b/sonar-db/src/main/java/org/sonar/db/version/v56/package-info.java new file mode 100644 index 00000000000..c3a55a0eb13 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v56/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.db.version.v56; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 72e8889de26..c6dccde162f 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -403,6 +403,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1122'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1123'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1124'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1125'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1200'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index 20c4b0d3537..91bda96d3de 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -29,6 +29,6 @@ public class MigrationStepModuleTest { public void verify_count_of_added_MigrationStep_types() { ComponentContainer container = new ComponentContainer(); new MigrationStepModule().configure(container); - assertThat(container.size()).isEqualTo(64); + assertThat(container.size()).isEqualTo(65); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest.java b/sonar-db/src/test/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest.java new file mode 100644 index 00000000000..b200b6cd022 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest.java @@ -0,0 +1,74 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.db.version.v56; + +import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RemoveDefaultAssigneePropertiesOnDisabledUsersTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, RemoveDefaultAssigneePropertiesOnDisabledUsersTest.class, "schema.sql"); + + private MigrationStep migration = new RemoveDefaultAssigneePropertiesOnDisabledUsers(db.database());; + + @Test + public void delete_properties() throws Exception { + insertUser("user1", false); + insertUser("user2", true); + + insertProperty("sonar.issues.defaultAssigneeLogin", "user1", 10L); + insertProperty("sonar.issues.defaultAssigneeLogin", "user1", 11L); + insertProperty("sonar.other.property", "user1", 11L); + insertProperty("sonar.issues.defaultAssigneeLogin", "user2", 13L); + + migration.execute(); + + List<Map<String, Object>> rows = db.select("select prop_key, text_value from properties"); + assertThat(rows).containsOnly( + ImmutableMap.<String, Object>of("PROP_KEY", "sonar.issues.defaultAssigneeLogin","TEXT_VALUE", "user2"), + ImmutableMap.<String, Object>of("PROP_KEY", "sonar.other.property","TEXT_VALUE", "user1") + ); + } + + private void insertUser(String login, boolean active) { + db.executeInsert("users", ImmutableMap.of( + "LOGIN", login, + "ACTIVE", Boolean.toString(active) + )); + } + + private void insertProperty(String key, String value, long componentId) { + db.executeInsert("properties", ImmutableMap.of( + "PROP_KEY", key, + "RESOURCE_ID", Long.toString(componentId), + "TEXT_VALUE", value + )); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest/schema.sql new file mode 100644 index 00000000000..f8c90e90464 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v56/RemoveDefaultAssigneePropertiesOnDisabledUsersTest/schema.sql @@ -0,0 +1,25 @@ +CREATE TABLE "USERS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "LOGIN" VARCHAR(255), + "NAME" VARCHAR(200), + "EMAIL" VARCHAR(100), + "CRYPTED_PASSWORD" VARCHAR(40), + "SALT" VARCHAR(40), + "REMEMBER_TOKEN" VARCHAR(500), + "REMEMBER_TOKEN_EXPIRES_AT" TIMESTAMP, + "ACTIVE" BOOLEAN DEFAULT TRUE, + "SCM_ACCOUNTS" VARCHAR(4000), + "EXTERNAL_IDENTITY" VARCHAR(255), + "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100), + "USER_LOCAL" BOOLEAN, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); + +CREATE TABLE "PROPERTIES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROP_KEY" VARCHAR(512), + "RESOURCE_ID" INTEGER, + "TEXT_VALUE" CLOB(2147483647), + "USER_ID" INTEGER +); |