diff options
23 files changed, 606 insertions, 17 deletions
diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1411_add_organization_uuid_to_permission_templates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1411_add_organization_uuid_to_permission_templates.rb new file mode 100644 index 00000000000..01d94e19572 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1411_add_organization_uuid_to_permission_templates.rb @@ -0,0 +1,29 @@ +# +# 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. +# + +# +# SonarQube 6.2 +# +class AddOrganizationUuidToPermissionTemplates < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v62.AddOrganizationUuidToPermissionTemplates') + end +end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1412_populate_organization_uuid_of_permission_templates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1412_populate_organization_uuid_of_permission_templates.rb new file mode 100644 index 00000000000..d855a08cfa0 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1412_populate_organization_uuid_of_permission_templates.rb @@ -0,0 +1,29 @@ +# +# 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. +# + +# +# SonarQube 6.2 +# +class PopulateOrganizationUuidOfPermissionTemplates < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v62.PopulateOrganizationUuidOfPermissionTemplates') + end +end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1413_make_organization_uuid_not_null_on_permission_templates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1413_make_organization_uuid_not_null_on_permission_templates.rb new file mode 100644 index 00000000000..8258665b4a1 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1413_make_organization_uuid_not_null_on_permission_templates.rb @@ -0,0 +1,29 @@ +# +# 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. +# + +# +# SonarQube 6.2 +# +class MakeOrganizationUuidNotNullOnPermissionTemplates < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnPermissionTemplates') + end +end 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 a31226775f2..c883ed34a12 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 @@ -30,7 +30,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1_411; + public static final int LAST_VERSION = 1_413; /** * 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 1de7f2384ab..36e9c61eedd 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 @@ -159,16 +159,21 @@ import org.sonar.db.version.v61.DropIsGlobalFromDashboards; import org.sonar.db.version.v61.PopulateTableProperties2; import org.sonar.db.version.v61.RemoveViewsDefinitionFromProperties; import org.sonar.db.version.v61.ShrinkModuleUuidPathOfProjects; -<<<<<<< HEAD import org.sonar.db.version.v62.AddIsRootColumnOnTableUsers; -======= import org.sonar.db.version.v62.AddOrganizationUuidToGroups; ->>>>>>> SONAR-8134 add db column groups.organization_uuid +import org.sonar.db.version.v62.AddOrganizationUuidToPermissionTemplates; +import org.sonar.db.version.v62.AddOrganizationUuidToUserRoles; import org.sonar.db.version.v62.CreateDefaultOrganization; import org.sonar.db.version.v62.CreateTableOrganizations; import org.sonar.db.version.v62.DeletePermissionShareDashboard; +import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnGroups; +import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnPermissionTemplates; +import org.sonar.db.version.v62.MakeOrganizationUuidNotNullOnUserRoles; import org.sonar.db.version.v62.MakeRootColumnNotNullOnTableUsers; import org.sonar.db.version.v62.PopulateIsRootColumnOnTableUsers; +import org.sonar.db.version.v62.PopulateOrganizationUuidOfGroups; +import org.sonar.db.version.v62.PopulateOrganizationUuidOfPermissionTemplates; +import org.sonar.db.version.v62.PopulateOrganizationUuidOfUserRoles; public class MigrationStepModule extends Module { @Override @@ -352,13 +357,17 @@ public class MigrationStepModule extends Module { CreateTableOrganizations.class, CreateDefaultOrganization.class, DeletePermissionShareDashboard.class, -<<<<<<< HEAD AddIsRootColumnOnTableUsers.class, PopulateIsRootColumnOnTableUsers.class, - MakeRootColumnNotNullOnTableUsers.class); -======= - AddOrganizationUuidToGroups.class - ); ->>>>>>> SONAR-8134 add db column groups.organization_uuid + MakeRootColumnNotNullOnTableUsers.class, + AddOrganizationUuidToGroups.class, + PopulateOrganizationUuidOfGroups.class, + MakeOrganizationUuidNotNullOnGroups.class, + AddOrganizationUuidToUserRoles.class, + AddOrganizationUuidToPermissionTemplates.class, + PopulateOrganizationUuidOfUserRoles.class, + MakeOrganizationUuidNotNullOnUserRoles.class, + PopulateOrganizationUuidOfPermissionTemplates.class, + MakeOrganizationUuidNotNullOnPermissionTemplates.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplates.java b/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplates.java new file mode 100644 index 00000000000..0398af94033 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplates.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 AddOrganizationUuidToPermissionTemplates extends DdlChange { + + public AddOrganizationUuidToPermissionTemplates(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(), "permission_templates").addColumn(column).build()); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplates.java b/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplates.java new file mode 100644 index 00000000000..93a2ef586e9 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplates.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 MakeOrganizationUuidNotNullOnPermissionTemplates extends DdlChange { + + private static final String TABLE = "permission_templates"; + + public MakeOrganizationUuidNotNullOnPermissionTemplates(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/PopulateOrganizationUuidOfPermissionTemplates.java b/sonar-db/src/main/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplates.java new file mode 100644 index 00000000000..a3c894e6700 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplates.java @@ -0,0 +1,67 @@ +/* + * 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.Date; +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 static com.google.common.base.Preconditions.checkState; + +public class PopulateOrganizationUuidOfPermissionTemplates extends BaseDataChange { + + private static final String INTERNAL_PROPERTY_DEFAULT_ORGANIZATION = "organization.default"; + + private final System2 system2; + + public PopulateOrganizationUuidOfPermissionTemplates(Database db, System2 system2) { + super(db); + this.system2 = system2; + } + + @Override + public void execute(Context context) throws SQLException { + String organizationUuid = selectDefaultOrganizationUuid(context); + + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from permission_templates where organization_uuid is null"); + massUpdate.update("update permission_templates set organization_uuid=?, updated_at=? where id=?"); + massUpdate.rowPluralName("permission_templates"); + massUpdate.execute((row, update) -> { + long groupId = row.getLong(1); + update.setString(1, organizationUuid); + update.setDate(2, new Date(system2.now())); + update.setLong(3, groupId); + 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/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 856ba90d52c..93c0362b9c9 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 @@ -500,6 +500,8 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1408'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1409'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1410'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1411'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1412'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1413'); 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'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/test/java/org/sonar/db/DbTester.java b/sonar-db/src/test/java/org/sonar/db/DbTester.java index e861d64d77b..2261e47e749 100644 --- a/sonar-db/src/test/java/org/sonar/db/DbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/DbTester.java @@ -20,7 +20,6 @@ package org.sonar.db; import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -65,10 +64,13 @@ import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.SequenceUuidFactory; +import org.sonar.db.component.ComponentDbTester; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.organization.OrganizationTesting; +import org.sonar.db.user.UserDbTester; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static java.sql.ResultSetMetaData.columnNoNulls; @@ -89,11 +91,17 @@ public class DbTester extends ExternalResource { private DbSession session = null; private boolean disableDefaultOrganization = false; private boolean started = false; + private OrganizationDto defaultOrganization; + + private final UserDbTester userTester; + private final ComponentDbTester componentTester; private DbTester(System2 system2, @Nullable String schemaPath) { this.system2 = system2; this.db = TestDb.create(schemaPath); initDbClient(); + this.userTester = new UserDbTester(this); + this.componentTester = new ComponentDbTester(this); } public static DbTester create(System2 system2) { @@ -119,7 +127,7 @@ public class DbTester extends ExternalResource { } public DbTester setDisableDefaultOrganization(boolean b) { - Preconditions.checkState(!started, "DbTester is already started"); + checkState(!started, "DbTester is already started"); this.disableDefaultOrganization = b; return this; } @@ -136,14 +144,27 @@ public class DbTester extends ExternalResource { } private void insertDefaultOrganization() { - OrganizationDto org = OrganizationTesting.newOrganizationDto(); + defaultOrganization = OrganizationTesting.newOrganizationDto(); try (DbSession dbSession = db.getMyBatis().openSession(false)) { - client.organizationDao().insert(dbSession, org); - client.internalPropertiesDao().save(dbSession, "organization.default", org.getUuid()); + client.organizationDao().insert(dbSession, defaultOrganization); + client.internalPropertiesDao().save(dbSession, "organization.default", defaultOrganization.getUuid()); dbSession.commit(); } } + public OrganizationDto getDefaultOrganization() { + checkState(defaultOrganization != null, "Default organization has not been created"); + return defaultOrganization; + } + + public UserDbTester users() { + return userTester; + } + + public ComponentDbTester components() { + return componentTester; + } + @Override protected void after() { if (session != null) { diff --git a/sonar-db/src/test/java/org/sonar/db/organization/OrganizationTesting.java b/sonar-db/src/test/java/org/sonar/db/organization/OrganizationTesting.java index d3776e7462a..bc8dc321aae 100644 --- a/sonar-db/src/test/java/org/sonar/db/organization/OrganizationTesting.java +++ b/sonar-db/src/test/java/org/sonar/db/organization/OrganizationTesting.java @@ -33,10 +33,11 @@ public class OrganizationTesting { /** * Insert an {@link OrganizationDto} and commit the session */ - public static void insert(DbTester dbTester, OrganizationDto dto) { + public static OrganizationDto insert(DbTester dbTester, OrganizationDto dto) { DbSession dbSession = dbTester.getSession(); dbTester.getDbClient().organizationDao().insert(dbSession, dto); dbSession.commit(); + return dto; } /** 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 37645642589..689122fe613 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(147); + assertThat(container.size()).isEqualTo(153); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest.java new file mode 100644 index 00000000000..72ce123d756 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest.java @@ -0,0 +1,56 @@ +/* + * 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 AddOrganizationUuidToPermissionTemplatesTest { + + @Rule + public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, AddOrganizationUuidToPermissionTemplatesTest.class, "previous-permission_templates.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddOrganizationUuidToPermissionTemplates underTest = new AddOrganizationUuidToPermissionTemplates(dbTester.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("permission_templates", "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/MakeOrganizationUuidNotNullOnPermissionTemplatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplatesTest.java new file mode 100644 index 00000000000..9afc8f36028 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplatesTest.java @@ -0,0 +1,86 @@ +/* + * 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 MakeOrganizationUuidNotNullOnPermissionTemplatesTest { + + private static final String TABLE_PERMISSION_TEMPLATES = "permission_templates"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MakeOrganizationUuidNotNullOnPermissionTemplatesTest.class, + "in_progress_permission_templates.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MakeOrganizationUuidNotNullOnPermissionTemplates underTest = new MakeOrganizationUuidNotNullOnPermissionTemplates(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 { + insertTemplate(1, true); + insertTemplate(2, true); + + underTest.execute(); + + verifyColumnDefinition(); + } + + @Test + public void migration_fails_if_some_row_has_a_null_uuid() throws SQLException { + insertTemplate(1, false); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Fail to execute"); + + underTest.execute(); + } + + private void verifyColumnDefinition() { + db.assertColumnDefinition(TABLE_PERMISSION_TEMPLATES, "organization_uuid", Types.VARCHAR, 40, false); + } + + private String insertTemplate(long id, boolean hasUuid) { + String uuid = "uuid_" + id; + db.executeInsert( + TABLE_PERMISSION_TEMPLATES, + "ID", valueOf(id), + "NAME", valueOf(id + 10), + "KEE", valueOf(id + 100), + "ORGANIZATION_UUID", hasUuid ? "uuid_" + id : null); + return uuid; + } +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest.java new file mode 100644 index 00000000000..b7ffbb7356b --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest.java @@ -0,0 +1,77 @@ +/* + * 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 PopulateOrganizationUuidOfPermissionTemplatesTest { + + private static final String TABLE_PERMISSION_TEMPLATES = "permission_templates"; + private static final String AN_ORG_UUID = "org1"; + + @Rule + public DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, PopulateOrganizationUuidOfPermissionTemplatesTest.class, "permission_templates.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private PopulateOrganizationUuidOfPermissionTemplates underTest = new PopulateOrganizationUuidOfPermissionTemplates(dbTester.database(), System2.INSTANCE); + + @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(TABLE_PERMISSION_TEMPLATES, "kee", "t1", "name", "T1", "organization_uuid", null); + dbTester.executeInsert(TABLE_PERMISSION_TEMPLATES, "kee", "t2", "name", "T2", "organization_uuid", null); + dbTester.executeInsert(TABLE_PERMISSION_TEMPLATES, "kee", "t3", "name", "T3", "organization_uuid", AN_ORG_UUID); + insertDefaultOrganizationInternalProperty(AN_ORG_UUID); + + underTest.execute(); + + assertThat(dbTester.countRowsOfTable(TABLE_PERMISSION_TEMPLATES)).isEqualTo(3); + assertThat(dbTester.countSql("select count(1) from permission_templates 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_PERMISSION_TEMPLATES)).isEqualTo(0); + } + + private void insertDefaultOrganizationInternalProperty(String defaultOrganizationUuid) { + dbTester.executeInsert("internal_properties", "kee", "organization.default", "is_empty", false, "text_value", defaultOrganizationUuid); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest/previous-permission_templates.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest/previous-permission_templates.sql new file mode 100644 index 00000000000..a690318b027 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToPermissionTemplatesTest/previous-permission_templates.sql @@ -0,0 +1,9 @@ +CREATE TABLE "PERMISSION_TEMPLATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "KEE" VARCHAR(100) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "KEY_PATTERN" VARCHAR(500), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest/previous-user_roles.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest/previous-user_roles.sql new file mode 100644 index 00000000000..75957baf6e7 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/AddOrganizationUuidToUserRolesTest/previous-user_roles.sql @@ -0,0 +1,10 @@ +CREATE TABLE "USER_ROLES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "USER_ID" INTEGER, + "RESOURCE_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL +); + +CREATE INDEX "USER_ROLES_RESOURCE" ON "USER_ROLES" ("RESOURCE_ID"); + +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES" ("USER_ID"); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnGroupsTest/in_progress_groups.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnGroupsTest/in_progress_groups.sql new file mode 100644 index 00000000000..4a6fd1f49b9 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnGroupsTest/in_progress_groups.sql @@ -0,0 +1,8 @@ +CREATE TABLE "GROUPS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "NAME" VARCHAR(500), + "DESCRIPTION" VARCHAR(200), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplatesTest/in_progress_permission_templates.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplatesTest/in_progress_permission_templates.sql new file mode 100644 index 00000000000..163e5ecb021 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnPermissionTemplatesTest/in_progress_permission_templates.sql @@ -0,0 +1,10 @@ +CREATE TABLE "PERMISSION_TEMPLATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "KEE" VARCHAR(100) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "KEY_PATTERN" VARCHAR(500), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest/in_progress_user_roles.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest/in_progress_user_roles.sql new file mode 100644 index 00000000000..e4d1b024417 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/MakeOrganizationUuidNotNullOnUserRolesTest/in_progress_user_roles.sql @@ -0,0 +1,7 @@ +CREATE TABLE "USER_ROLES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "USER_ID" INTEGER, + "RESOURCE_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfGroupsTest/user_groups.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfGroupsTest/user_groups.sql new file mode 100644 index 00000000000..0ee6f37fe25 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfGroupsTest/user_groups.sql @@ -0,0 +1,16 @@ +CREATE TABLE "GROUPS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "NAME" VARCHAR(500), + "DESCRIPTION" VARCHAR(200), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); + +CREATE TABLE "INTERNAL_PROPERTIES" ( + "KEE" VARCHAR(50) NOT NULL PRIMARY KEY, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest/permission_templates.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest/permission_templates.sql new file mode 100644 index 00000000000..cc29ea296da --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfPermissionTemplatesTest/permission_templates.sql @@ -0,0 +1,18 @@ +CREATE TABLE "PERMISSION_TEMPLATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "KEE" VARCHAR(100) NOT NULL, + "DESCRIPTION" VARCHAR(4000), + "KEY_PATTERN" VARCHAR(500), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); + +CREATE TABLE "INTERNAL_PROPERTIES" ( + "KEE" VARCHAR(50) NOT NULL PRIMARY KEY, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest/user_roles.sql b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest/user_roles.sql new file mode 100644 index 00000000000..8448b6e6002 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v62/PopulateOrganizationUuidOfUserRolesTest/user_roles.sql @@ -0,0 +1,15 @@ +CREATE TABLE "USER_ROLES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ORGANIZATION_UUID" VARCHAR(40), + "USER_ID" INTEGER, + "RESOURCE_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL +); + +CREATE TABLE "INTERNAL_PROPERTIES" ( + "KEE" VARCHAR(50) NOT NULL PRIMARY KEY, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); |