diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-10-12 17:50:21 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-10-13 12:13:11 +0200 |
commit | 0fda6a3c52bd398f8c7969a81ee649af1e62fa82 (patch) | |
tree | 4e548f3bea533f72a32d74000fb6c498a7c3ff77 /sonar-db/src | |
parent | 4fc17259a5634eb5f7ecd9859d9204c6a258f722 (diff) | |
download | sonarqube-0fda6a3c52bd398f8c7969a81ee649af1e62fa82.tar.gz sonarqube-0fda6a3c52bd398f8c7969a81ee649af1e62fa82.zip |
SONAR-8134 add column user_roles.organization_uuid
Diffstat (limited to 'sonar-db/src')
6 files changed, 369 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRoles.java b/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRoles.java new file mode 100644 index 00000000000..850a94695ca --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRoles.java @@ -0,0 +1,45 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.AddColumnsBuilder; +import org.sonar.db.version.DdlChange; +import org.sonar.db.version.VarcharColumnDef; + +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AddOrganizationUuidToUserRoles extends DdlChange { + + public AddOrganizationUuidToUserRoles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + VarcharColumnDef column = newVarcharColumnDefBuilder() + .setColumnName("organization_uuid") + .setIsNullable(true) + .setLimit(40) + .build(); + context.execute(new AddColumnsBuilder(getDialect(), "user_roles").addColumn(column).build()); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRoles.java b/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRoles.java new file mode 100644 index 00000000000..c35369408a5 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRoles.java @@ -0,0 +1,45 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.AlterColumnsBuilder; +import org.sonar.db.version.DdlChange; + +import static org.sonar.db.version.VarcharColumnDef.UUID_SIZE; +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class MakeOrganizationUuidNotNullOnUserRoles extends DdlChange { + + private static final String TABLE = "user_roles"; + + public MakeOrganizationUuidNotNullOnUserRoles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDatabase().getDialect(), TABLE) + .updateColumn(newVarcharColumnDefBuilder().setColumnName("organization_uuid").setLimit(UUID_SIZE).setIsNullable(false).build()) + .build()); + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRoles.java b/sonar-db/src/main/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRoles.java new file mode 100644 index 00000000000..de4aa74b83b --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRoles.java @@ -0,0 +1,61 @@ +/* + * 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.v62; + +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 static com.google.common.base.Preconditions.checkState; + +public class PopulateOrganizationUuidOfUserRoles extends BaseDataChange { + + private static final String INTERNAL_PROPERTY_DEFAULT_ORGANIZATION = "organization.default"; + + public PopulateOrganizationUuidOfUserRoles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + String organizationUuid = selectDefaultOrganizationUuid(context); + + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from user_roles where organization_uuid is null"); + massUpdate.update("update user_roles set organization_uuid=? where id=?"); + massUpdate.rowPluralName("user_roles"); + massUpdate.execute((row, update) -> { + long id = row.getLong(1); + update.setString(1, organizationUuid); + update.setLong(2, id); + return true; + }); + } + + private static String selectDefaultOrganizationUuid(Context context) throws SQLException { + Select select = context.prepareSelect("select text_value from internal_properties where kee=?"); + select.setString(1, INTERNAL_PROPERTY_DEFAULT_ORGANIZATION); + String uuid = select.get(row -> row.getString(1)); + checkState(uuid != null, "Default organization uuid is missing"); + return uuid; + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest.java new file mode 100644 index 00000000000..bd0a671a6ee --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest.java @@ -0,0 +1,57 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + + +public class AddOrganizationUuidToUserRolesTest { + + @Rule + public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, AddOrganizationUuidToUserRolesTest.class, "previous-user_roles.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddOrganizationUuidToUserRoles underTest = new AddOrganizationUuidToUserRoles(dbTester.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("user_roles", "organization_uuid", Types.VARCHAR, 40, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest.java new file mode 100644 index 00000000000..5ba5b9d0687 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest.java @@ -0,0 +1,85 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static java.lang.String.valueOf; + +public class MakeOrganizationUuidNotNullOnUserRolesTest { + + private static final String TABLE_USER_ROLES = "user_roles"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeOrganizationUuidNotNullOnUserRolesTest.class, + "in_progress_user_roles.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeOrganizationUuidNotNullOnUserRoles underTest = new MakeOrganizationUuidNotNullOnUserRoles(db.database()); + + @Test + public void migration_sets_uuid_column_not_nullable_on_empty_table() throws SQLException { + underTest.execute(); + + verifyColumnDefinition(); + } + + @Test + public void migration_sets_uuid_column_not_nullable_on_populated_table() throws SQLException { + insertUserRole(1, true); + insertUserRole(2, true); + + underTest.execute(); + + verifyColumnDefinition(); + } + + @Test + public void migration_fails_if_some_row_has_a_null_uuid() throws SQLException { + insertUserRole(1, false); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Fail to execute"); + + underTest.execute(); + } + + private void verifyColumnDefinition() { + db.assertColumnDefinition(TABLE_USER_ROLES, "organization_uuid", Types.VARCHAR, 40, false); + } + + private String insertUserRole(long id, boolean hasUuid) { + String uuid = "uuid_" + id; + db.executeInsert( + TABLE_USER_ROLES, + "ID", valueOf(id), + "USER_ID", id + 10, + "ROLE", valueOf(id + 100), + "ORGANIZATION_UUID", hasUuid ? "uuid_" + id : null); + return uuid; + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest.java new file mode 100644 index 00000000000..0c32fe89491 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest.java @@ -0,0 +1,76 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateOrganizationUuidOfUserRolesTest { + + private static final String TABLE_USER_ROLES = "user_roles"; + private static final String AN_ORG_UUID = "org1"; + + @Rule + public DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, PopulateOrganizationUuidOfUserRolesTest.class, "user_roles.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private PopulateOrganizationUuidOfUserRoles underTest = new PopulateOrganizationUuidOfUserRoles(dbTester.database()); + + @Test + public void execute_fails_with_ISE_if_default_organization_internal_property_is_not_set() throws SQLException { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Default organization uuid is missing"); + + underTest.execute(); + } + @Test + public void migration_populates_missing_organization_uuids() throws SQLException { + dbTester.executeInsert("user_roles", "user_id", "1", "role", "admin", "organization_uuid", null); + dbTester.executeInsert("user_roles", "user_id", "2", "role", "viewever", "organization_uuid", null); + dbTester.executeInsert("user_roles", "user_id", "3", "role", "viewever", "organization_uuid", AN_ORG_UUID); + insertDefaultOrganizationInternalProperty(AN_ORG_UUID); + + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_USER_ROLES)).isEqualTo(3); + assertThat(dbTester.countSql("select count(1) from user_roles where organization_uuid='" + AN_ORG_UUID + "'")).isEqualTo(3); + } + + @Test + public void migration_has_no_effect_on_empty_table() throws SQLException { + insertDefaultOrganizationInternalProperty(AN_ORG_UUID); + + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_USER_ROLES)).isEqualTo(0); + } + + private void insertDefaultOrganizationInternalProperty(String defaultOrganizationUuid) { + dbTester.executeInsert("internal_properties", "kee", "organization.default", "is_empty", false, "text_value", defaultOrganizationUuid); + } +} |