diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-09 23:36:10 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-10 23:32:47 +0100 |
commit | 11416daf3e996fe08213faf52ebe376fb999a71f (patch) | |
tree | 6d6b4c0da5bbaa897a249f30bd7429223ba53aad | |
parent | 3779bf3b1e2bb79c614bb8a683d86a4fde788790 (diff) | |
download | sonarqube-11416daf3e996fe08213faf52ebe376fb999a71f.tar.gz sonarqube-11416daf3e996fe08213faf52ebe376fb999a71f.zip |
SONAR-8761 db migration to unset root flag of existing users
6 files changed, 156 insertions, 4 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java index a233e2f7e63..39cac92ce80 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java @@ -47,7 +47,7 @@ public class DbVersion63 implements DbVersion { .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class) .add(1513, "Make default organization guarded", MakeDefaultOrganizationGuarded.class) .add(1514, "Delete some entries in PROPERTIES", DeleteUselessProperties.class) - .add(1516, "Add ORGANIZATIONS.USER_ID", AddUserIdToOrganizations.class) - ; + .add(1515, "Unset user root flags", UnsetUserRootFlags.class) + .add(1516, "Add ORGANIZATIONS.USER_ID", AddUserIdToOrganizations.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlags.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlags.java new file mode 100644 index 00000000000..1c4834531b1 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlags.java @@ -0,0 +1,51 @@ +/* + * 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.server.platform.db.migration.version.v63; + +import java.sql.SQLException; +import org.sonar.api.utils.System2; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; + +/** + * Organizations and root users were not production-ready + * in versions prior to 6.3. For this reason they should not + * be enabled. This migration unsets the root flag from + * users. + */ +public class UnsetUserRootFlags extends DataChange { + + private final System2 system2; + + public UnsetUserRootFlags(Database db, System2 system2) { + super(db); + this.system2 = system2; + } + + @Override + protected void execute(Context context) throws SQLException { + context.prepareUpsert("update users set is_root=?, updated_at=? where is_root=?") + .setBoolean(1, false) + .setLong(2, system2.now()) + .setBoolean(3, true) + .execute() + .commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java index d66dde23fd2..8d6fbfc49dd 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java @@ -41,7 +41,7 @@ public class DbVersion63Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 16); + verifyMigrationCount(underTest, 17); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest.java new file mode 100644 index 00000000000..4b8679174c7 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest.java @@ -0,0 +1,82 @@ +/* + * 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.server.platform.db.migration.version.v63; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.internal.TestSystem2; +import org.sonar.db.DbTester; +import org.sonar.db.user.UserDto; +import org.sonar.db.user.UserTesting; + +public class UnsetUserRootFlagsTest { + + private static final long CREATED_AT = 1_500L; + private static final long FIXED_AT = 1_600L; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private TestSystem2 system = new TestSystem2().setNow(FIXED_AT); + + @Rule + public DbTester db = DbTester.createForSchema(system, UnsetUserRootFlagsTest.class, "in_progress_users.sql"); + + private UnsetUserRootFlags underTest = new UnsetUserRootFlags(db.database(), system); + + @Test + public void sets_USERS_IS_ROOT_to_false() throws SQLException { + UserDto root1 = db.users().makeRoot(createUser()); + UserDto user1 = createUser(); + UserDto root2 = db.users().makeRoot(createUser()); + UserDto user2 = createUser(); + + underTest.execute(); + + verifyNotRoot(CREATED_AT, user1, user2); + verifyNotRoot(FIXED_AT, root1, root2); + } + + @Test + public void migration_is_reentrant() throws SQLException { + UserDto root = db.users().makeRoot(createUser()); + + underTest.execute(); + verifyNotRoot(FIXED_AT, root); + + system.setNow(FIXED_AT + 100L); + underTest.execute(); + verifyNotRoot(FIXED_AT, root); + } + + private void verifyNotRoot(long updatedAt, UserDto... users) { + for (UserDto user : users) { + db.rootFlag().verify(user, false, updatedAt); + } + } + + private UserDto createUser() { + return db.users().insertUser(UserTesting.newUserDto() + .setCreatedAt(CREATED_AT) + .setUpdatedAt(CREATED_AT)); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest/in_progress_users.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest/in_progress_users.sql new file mode 100644 index 00000000000..d631feae68e --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/UnsetUserRootFlagsTest/in_progress_users.sql @@ -0,0 +1,18 @@ +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), + "ACTIVE" BOOLEAN DEFAULT TRUE, + "SCM_ACCOUNTS" VARCHAR(4000), + "EXTERNAL_IDENTITY" VARCHAR(255), + "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100), + "IS_ROOT" BOOLEAN NOT NULL, + "USER_LOCAL" BOOLEAN, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); +CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN"); +CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT"); 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 a6b2168a67b..a996126ea44 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 @@ -528,9 +528,10 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1511'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1512'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1513'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1514'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1515'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1516'); -INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482'); +INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', false, '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', true, '1474962596482', '1474962596482'); |