diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-07-19 09:29:17 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-07-19 09:29:17 +0200 |
commit | 6938a551fa3d0f80d45a9c4e99075150ba365677 (patch) | |
tree | 417b01968d80c4b6a1aeebb2ea5c1754d9b0be92 /sonar-db | |
parent | 226c898c0e4284b1cba76b7830652281ce92fe72 (diff) | |
parent | aa2ad15260a5551cd45339211442cd1f572c1501 (diff) | |
download | sonarqube-6938a551fa3d0f80d45a9c4e99075150ba365677.tar.gz sonarqube-6938a551fa3d0f80d45a9c4e99075150ba365677.zip |
Merge remote-tracking branch 'origin/branch-5.6'
Diffstat (limited to 'sonar-db')
6 files changed, 194 insertions, 1 deletions
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 2c3be5b7f11..2e3b5f8bfcb 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 @@ -83,6 +83,7 @@ import org.sonar.db.version.v55.FeedRulesLongDateColumns; import org.sonar.db.version.v55.FeedRulesTypes; import org.sonar.db.version.v56.FixLengthOfIssuesMessageOnOracle; import org.sonar.db.version.v56.FixTypeOfRuleTypeOnMysql; +import org.sonar.db.version.v56.UpdateUsersExternalIdentityWhenEmpty; import org.sonar.db.version.v60.AddAnalysisUuidColumnToCeActivity; import org.sonar.db.version.v60.AddAnalysisUuidColumnToEvents; import org.sonar.db.version.v60.AddAnalysisUuidColumnToMeasures; @@ -226,6 +227,7 @@ public class MigrationStepModule extends Module { // 5.6 FixTypeOfRuleTypeOnMysql.class, FixLengthOfIssuesMessageOnOracle.class, + UpdateUsersExternalIdentityWhenEmpty.class, // 6.0 AddUuidColumnsToResourceIndex.class, diff --git a/sonar-db/src/main/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmpty.java b/sonar-db/src/main/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmpty.java new file mode 100644 index 00000000000..68d5a5046d9 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmpty.java @@ -0,0 +1,69 @@ +/* + * 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.api.utils.System2; +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; + +/** + * Update USERS.EXTERNAL_IDENTITY_PROVIDER to 'sonarqube' and USERS.EXTERNAL_IDENTITY to user's login when one of this 2 columns is null + */ +public class UpdateUsersExternalIdentityWhenEmpty extends BaseDataChange { + + private final System2 system2; + + public UpdateUsersExternalIdentityWhenEmpty(Database db, System2 system2) { + super(db); + this.system2 = system2; + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT u.id, u.login FROM users u WHERE external_identity_provider IS NULL OR external_identity IS NULL"); + massUpdate.update("UPDATE users SET external_identity_provider=?, external_identity=?, updated_at=? WHERE id=?"); + massUpdate.rowPluralName("users"); + massUpdate.execute(new MigrationHandler(system2.now())); + } + + private static class MigrationHandler implements MassUpdate.Handler { + + private final long now; + + public MigrationHandler(long now) { + this.now = now; + } + + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + update.setString(1, "sonarqube"); + update.setString(2, row.getString(2)); + update.setLong(3, now); + update.setLong(4, row.getLong(1)); + return true; + } + } + +} 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 5e0c15f1de8..fff413c49dd 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 @@ -405,6 +405,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1125'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1150'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1151'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1152'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1153'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1200'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1201'); 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 b10eba51a6f..579506d38b1 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(125); + assertThat(container.size()).isEqualTo(126); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest.java b/sonar-db/src/test/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest.java new file mode 100644 index 00000000000..7f0abdcc7db --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest.java @@ -0,0 +1,104 @@ +/* + * 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.Map; +import javax.annotation.Nullable; +import org.junit.Before; +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 com.google.common.collect.Maps.newHashMap; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class UpdateUsersExternalIdentityWhenEmptyTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, UpdateUsersExternalIdentityWhenEmptyTest.class, "schema.sql"); + + static final long PAST = 1_000_000_000_000L; + static final long NOW = 1_500_000_000_000L; + + System2 system = mock(System2.class); + + MigrationStep underTest = new UpdateUsersExternalIdentityWhenEmpty(db.database(), system); + + @Before + public void setUp() throws Exception { + when(system.now()).thenReturn(NOW); + } + + @Test + public void migrate_users() throws Exception { + insertUser("user-without-eternal-identity", null, null, PAST); + insertUser("user-with-only-eternal-identity-provider", "github", null, PAST); + insertUser("user-with-only-eternal-identity", null, "login1", PAST); + insertUser("user-with-both-eternal-identity", "github", "login2", PAST); + + underTest.execute(); + + checkUserIsUpdated("user-without-eternal-identity"); + checkUserIsUpdated("user-with-only-eternal-identity-provider"); + checkUserIsUpdated("user-with-only-eternal-identity"); + + checkUserIsNotUpdated("user-with-both-eternal-identity"); + } + + @Test + public void doest_not_fail_when_no_user() throws Exception { + underTest.execute(); + } + + private void insertUser(String login, @Nullable String externalIdentity, @Nullable String externalIdentityProvider, long updatedAt) { + Map<String, String> params = newHashMap(ImmutableMap.of( + "LOGIN", login, + "CREATED_AT", Long.toString(PAST), + "UPDATED_AT", Long.toString(updatedAt))); + if (externalIdentity != null) { + params.put("EXTERNAL_IDENTITY", externalIdentity); + } + if (externalIdentityProvider != null) { + params.put("EXTERNAL_IDENTITY_PROVIDER", externalIdentityProvider); + } + + db.executeInsert("users", params); + } + + private void checkUserIsUpdated(String login) { + Map<String, Object> row = db.selectFirst("select EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, UPDATED_AT from users where LOGIN='" + login + "'"); + assertThat((String) row.get("EXTERNAL_IDENTITY_PROVIDER")).isEqualTo("sonarqube"); + assertThat((String) row.get("EXTERNAL_IDENTITY")).isEqualTo(login); + assertThat(row.get("UPDATED_AT")).isEqualTo(NOW); + } + + private void checkUserIsNotUpdated(String login) { + Map<String, Object> row = db.selectFirst("select EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, UPDATED_AT from users where LOGIN='" + login + "'"); + assertThat((String) row.get("EXTERNAL_IDENTITY_PROVIDER")).isNotEmpty(); + assertThat((String) row.get("EXTERNAL_IDENTITY")).isNotEmpty(); + assertThat(row.get("UPDATED_AT")).isEqualTo(PAST); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest/schema.sql new file mode 100644 index 00000000000..127fc346a9f --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v56/UpdateUsersExternalIdentityWhenEmptyTest/schema.sql @@ -0,0 +1,17 @@ +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 +); |