diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 325b65119c18..8f8551dc742f 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -115,7 +115,7 @@ public void test_real_start() throws IOException { ); assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize( CONTAINER_ITSELF - + 18 // MigrationConfigurationModule + + 19 // MigrationConfigurationModule + 17 // level 2 ); assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 4ba6c85a5d36..a1b83a1315dd 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -868,6 +868,7 @@ CREATE TABLE "ALM_APP_INSTALLS" ( "UUID" VARCHAR(40) NOT NULL, "ALM_ID" VARCHAR(40) NOT NULL, "OWNER_ID" VARCHAR(4000) NOT NULL, + "IS_OWNER_USER" BOOLEAN, "INSTALL_ID" VARCHAR(4000) NOT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java index a76b362d0e3c..2520f9434583 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java @@ -25,6 +25,10 @@ public enum ALM { BITBUCKETCLOUD, GITHUB; + public static ALM fromId(String almId) { + return ALM.valueOf(almId.toUpperCase(Locale.ENGLISH)); + } + public String getId() { return this.name().toLowerCase(Locale.ENGLISH); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java index 6deb486a5f43..60344b58569b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java @@ -19,6 +19,7 @@ */ package org.sonar.db.alm; +import java.util.List; import java.util.Objects; import java.util.Optional; import javax.annotation.Nullable; @@ -48,7 +49,7 @@ public AlmAppInstallDao(System2 system2, UuidFactory uuidFactory) { * @param ownerId ALM specific identifier of the owner of the app, like team or user uuid for Bitbucket Cloud or organization id for Github, can't be null * @param installId ALM specific identifier of the app installation, can't be null */ - public void insertOrUpdate(DbSession dbSession, ALM alm, String ownerId, String installId) { + public void insertOrUpdate(DbSession dbSession, ALM alm, String ownerId, @Nullable Boolean isOwnerUser, String installId) { checkAlm(alm); checkOwnerId(ownerId); checkArgument(isNotEmpty(installId), "installId can't be null nor empty"); @@ -56,17 +57,21 @@ public void insertOrUpdate(DbSession dbSession, ALM alm, String ownerId, String AlmAppInstallMapper mapper = getMapper(dbSession); long now = system2.now(); - if (mapper.update(alm.getId(), ownerId, installId, now) == 0) { - mapper.insert(uuidFactory.create(), alm.getId(), ownerId, installId, now); + if (mapper.update(alm.getId(), ownerId, isOwnerUser, installId, now) == 0) { + mapper.insert(uuidFactory.create(), alm.getId(), ownerId, isOwnerUser, installId, now); } } - public Optional getInstallId(DbSession dbSession, ALM alm, String ownerId) { + public List findAllWithNoOwnerType(DbSession dbSession) { + return getMapper(dbSession).selectAllWithNoOwnerType(); + } + + public Optional selectByOwner(DbSession dbSession, ALM alm, String ownerId) { checkAlm(alm); checkOwnerId(ownerId); AlmAppInstallMapper mapper = getMapper(dbSession); - return Optional.ofNullable(mapper.selectInstallId(alm.getId(), ownerId)); + return Optional.ofNullable(mapper.selectByOwner(alm.getId(), ownerId)); } public void delete(DbSession dbSession, ALM alm, String ownerId) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDto.java new file mode 100644 index 000000000000..2887472c4a5f --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDto.java @@ -0,0 +1,117 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.alm; + +import javax.annotation.Nullable; + +public class AlmAppInstallDto { + + /** + * Technical unique identifier, can't be null + */ + private String uuid; + /** + * alm_id, can't be null + */ + private String almId; + /** + * Owner id, can't be null + */ + private String ownerId; + /** + * Installation id, can't be null + */ + private String installId; + /** + * Is owner a user, can be null + */ + private Boolean isOwnerUser; + + private long createdAt; + private long updatedAt; + + public AlmAppInstallDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } + + public AlmAppInstallDto setAlmId(String almId) { + this.almId = almId; + return this; + } + + public AlmAppInstallDto setOwnerId(String ownerId) { + this.ownerId = ownerId; + return this; + } + + public AlmAppInstallDto setInstallId(String installId) { + this.installId = installId; + return this; + } + + public AlmAppInstallDto setIsOwnerUser(@Nullable Boolean isOwnerUser) { + this.isOwnerUser = isOwnerUser; + return this; + } + + AlmAppInstallDto setCreatedAt(long createdAt) { + this.createdAt = createdAt; + return this; + } + + AlmAppInstallDto setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + return this; + } + + public String getUuid() { + return uuid; + } + + public String getAlmId() { + return almId; + } + + public ALM getAlm() { + return ALM.fromId(almId); + } + + public String getOwnerId() { + return ownerId; + } + + public String getInstallId() { + return installId; + } + + @Nullable + public Boolean isOwnerUser() { + return isOwnerUser; + } + + public long getCreatedAt() { + return createdAt; + } + + public long getUpdatedAt() { + return updatedAt; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallMapper.java index 9a3f4eec3108..809a054ca26b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallMapper.java @@ -19,17 +19,23 @@ */ package org.sonar.db.alm; +import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; public interface AlmAppInstallMapper { @CheckForNull - String selectInstallId(@Param("almId") String almId, @Param("ownerId") String ownerId); + AlmAppInstallDto selectByOwner(@Param("almId") String almId, @Param("ownerId") String ownerId); - void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId, @Param("installId") String installId, @Param("now") long now); + List selectAllWithNoOwnerType(); - int update(@Param("almId") String almId, @Param("ownerId") String ownerId, @Param("installId") String installId, @Param("now") long now); + void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId, + @Nullable @Param("isOwnerUser") Boolean isOwnerUser, @Param("installId") String installId, @Param("now") long now); + + int update(@Param("almId") String almId, @Param("ownerId") String ownerId, + @Nullable @Param("isOwnerUser") Boolean isOwnerUser, @Param("installId") String installId, @Param("now") long now); void delete(@Param("almId") String almId, @Param("ownerId") String ownerId); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmAppInstallMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmAppInstallMapper.xml index 262ba4a8b765..2454efde08c0 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmAppInstallMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmAppInstallMapper.xml @@ -3,9 +3,18 @@ - + select from alm_app_installs where @@ -13,12 +22,21 @@ and owner_id = #{ownerId, jdbcType=VARCHAR} + + INSERT INTO alm_app_installs ( uuid, alm_id, owner_id, + is_owner_user, install_id, created_at, updated_at @@ -27,6 +45,7 @@ #{uuid, jdbcType=VARCHAR}, #{almId, jdbcType=VARCHAR}, #{ownerId, jdbcType=VARCHAR}, + #{isOwnerUser, jdbcType=BOOLEAN}, #{installId, jdbcType=VARCHAR}, #{now, jdbcType=BIGINT}, #{now, jdbcType=BIGINT} @@ -36,6 +55,7 @@ update alm_app_installs set install_id = #{installId, jdbcType=VARCHAR}, + is_owner_user = #{isOwnerUser, jdbcType=BOOLEAN}, updated_at = #{now, jdbcType=BIGINT} where alm_id = #{almId, jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java index aab2669fb067..8cd15f7f975e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java @@ -65,42 +65,42 @@ public class AlmAppInstallDaoTest { public void insert_throws_NPE_if_alm_is_null() { expectAlmNPE(); - underTest.insertOrUpdate(dbSession, null, A_OWNER, AN_INSTALL); + underTest.insertOrUpdate(dbSession, null, A_OWNER, true, AN_INSTALL); } @Test public void insert_throws_IAE_if_owner_id_is_null() { expectOwnerIdNullOrEmptyIAE(); - underTest.insertOrUpdate(dbSession, GITHUB, null, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, null, true, AN_INSTALL); } @Test public void insert_throws_IAE_if_owner_id_is_empty() { expectOwnerIdNullOrEmptyIAE(); - underTest.insertOrUpdate(dbSession, GITHUB, EMPTY_STRING, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, EMPTY_STRING, true, AN_INSTALL); } @Test public void insert_throws_IAE_if_install_id_is_null() { expectInstallIdNullOrEmptyIAE(); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, null); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, null); } @Test public void insert_throws_IAE_if_install_id_is_empty() { expectInstallIdNullOrEmptyIAE(); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, EMPTY_STRING); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, EMPTY_STRING); } @Test public void insert() { when(uuidFactory.create()).thenReturn(A_UUID); when(system2.now()).thenReturn(DATE); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL); assertThatAlmAppInstall(GITHUB, A_OWNER) .hasInstallId(AN_INSTALL) @@ -112,7 +112,7 @@ public void insert() { public void delete() { when(uuidFactory.create()).thenReturn(A_UUID); when(system2.now()).thenReturn(DATE); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL); assertThatAlmAppInstall(GITHUB, A_OWNER) .hasInstallId(AN_INSTALL) @@ -134,10 +134,10 @@ public void delete_doesn_t_fail() { public void update() { when(uuidFactory.create()).thenReturn(A_UUID); when(system2.now()).thenReturn(DATE); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL); when(system2.now()).thenReturn(DATE_LATER); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, OTHER_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER,true, OTHER_INSTALL); assertThatAlmAppInstall(GITHUB, A_OWNER) .hasInstallId(OTHER_INSTALL) @@ -151,16 +151,18 @@ public void putMultiple() { when(uuidFactory.create()) .thenReturn(A_UUID) .thenReturn(A_UUID_2); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL); - underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_OWNER, OTHER_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_OWNER, false, OTHER_INSTALL); assertThatAlmAppInstall(GITHUB, A_OWNER) .hasInstallId(AN_INSTALL) + .hasOwnerUser(true) .hasCreatedAt(DATE) .hasUpdatedAt(DATE); assertThatAlmAppInstall(GITHUB, ANOTHER_OWNER) .hasInstallId(OTHER_INSTALL) + .hasOwnerUser(false) .hasCreatedAt(DATE) .hasUpdatedAt(DATE); } @@ -169,34 +171,34 @@ public void putMultiple() { public void getInstallId_throws_NPE_when_alm_is_null() { expectAlmNPE(); - underTest.getInstallId(dbSession, null, A_OWNER); + underTest.selectByOwner(dbSession, null, A_OWNER); } @Test public void getInstallId_throws_IAE_when_owner_id_is_null() { expectOwnerIdNullOrEmptyIAE(); - underTest.getInstallId(dbSession, GITHUB, null); + underTest.selectByOwner(dbSession, GITHUB, null); } @Test public void getInstallId_throws_IAE_when_owner_id_is_empty() { expectOwnerIdNullOrEmptyIAE(); - underTest.getInstallId(dbSession, GITHUB, EMPTY_STRING); + underTest.selectByOwner(dbSession, GITHUB, EMPTY_STRING); } @Test public void getInstallId_returns_empty_optional_when_entry_does_not_exist_in_DB() { - assertThat(underTest.getInstallId(dbSession, GITHUB, A_OWNER)).isEmpty(); + assertThat(underTest.selectByOwner(dbSession, GITHUB, A_OWNER)).isEmpty(); } @Test public void getInstallId_returns_install_id_when_entry_exists() { when(uuidFactory.create()).thenReturn(A_UUID); - underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, AN_INSTALL); + underTest.insertOrUpdate(dbSession, GITHUB, A_OWNER, true, AN_INSTALL); - assertThat(underTest.getInstallId(dbSession, GITHUB, A_OWNER)).contains(AN_INSTALL); + assertThat(underTest.selectByOwner(dbSession, GITHUB, A_OWNER).map(AlmAppInstallDto::getInstallId)).contains(AN_INSTALL); } private void expectAlmNPE() { @@ -228,7 +230,7 @@ private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSess List> rows = dbTester.select( dbSession, "select" + - " install_id as \"installId\", created_at as \"createdAt\", updated_at as \"updatedAt\"" + + " install_id as \"installId\", is_owner_user as \"isOwnerUser\", created_at as \"createdAt\", updated_at as \"updatedAt\"" + " from alm_app_installs" + " where alm_id='" + alm.getId() + "' and owner_id='" + ownerId + "'"); if (rows.isEmpty()) { @@ -239,6 +241,7 @@ private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSess } return new AlmAppInstall( (String) rows.get(0).get("installId"), + (Boolean) rows.get(0).get("isOwnerUser"), (Long) rows.get(0).get("createdAt"), (Long) rows.get(0).get("updatedAt")); } @@ -251,7 +254,16 @@ public AlmAppInstallAssert hasInstallId(String expected) { isNotNull(); if (!Objects.equals(actual.getInstallId(), expected)) { - failWithMessage("Expected ALM App Install to have column INSTALL_ID to be <%s> but was <%s>", true, actual.getInstallId()); + failWithMessage("Expected ALM App Install to have column INSTALL_ID to be <%s> but was <%s>", expected, actual.getInstallId()); + } + return this; + } + + public AlmAppInstallAssert hasOwnerUser(boolean expected) { + isNotNull(); + + if (!Objects.equals(actual.isOwnerUser(), expected)) { + failWithMessage("Expected ALM App Install to have column IS_OWNER_USER to be <%s> but was <%s>", expected, actual.isOwnerUser()); } return this; } @@ -280,11 +292,13 @@ public AlmAppInstallAssert hasUpdatedAt(long expected) { private static final class AlmAppInstall { private final String installId; + private final Boolean isOwnerUser; private final Long createdAt; private final Long updatedAt; - public AlmAppInstall(@Nullable String installId, @Nullable Long createdAt, @Nullable Long updatedAt) { + public AlmAppInstall(@Nullable String installId, @Nullable Boolean isOwnerUser, @Nullable Long createdAt, @Nullable Long updatedAt) { this.installId = installId; + this.isOwnerUser = isOwnerUser; this.createdAt = createdAt; this.updatedAt = updatedAt; } @@ -303,5 +317,9 @@ public Long getCreatedAt() { public Long getUpdatedAt() { return updatedAt; } + + public Boolean isOwnerUser() { + return isOwnerUser; + } } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java index f5ffda01c3d6..a196b02f2bc2 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java @@ -38,6 +38,7 @@ import org.sonar.server.platform.db.migration.version.v72.DbVersion72; import org.sonar.server.platform.db.migration.version.v73.DbVersion73; import org.sonar.server.platform.db.migration.version.v74.DbVersion74; +import org.sonar.server.platform.db.migration.version.v75.DbVersion75; public class MigrationConfigurationModule extends Module { @Override @@ -59,6 +60,7 @@ protected void configureModule() { DbVersion72.class, DbVersion73.class, DbVersion74.class, + DbVersion75.class, // migration steps MigrationStepRegistryImpl.class, diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstall.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstall.java new file mode 100644 index 000000000000..e714398e39c4 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstall.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.v75; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.SupportsBlueGreen; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder; + +@SupportsBlueGreen +public class AddIsOwnerUserColumnInAlmAppInstall extends DdlChange { + + public AddIsOwnerUserColumnInAlmAppInstall(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), "alm_app_installs") + .addColumn(newBooleanColumnDefBuilder() + .setColumnName("is_owner_user") + .setIsNullable(true) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75.java new file mode 100644 index 000000000000..ec7c0d05c351 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/DbVersion75.java @@ -0,0 +1,33 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.v75; + +import org.sonar.server.platform.db.migration.step.MigrationStepRegistry; +import org.sonar.server.platform.db.migration.version.DbVersion; + +public class DbVersion75 implements DbVersion { + + @Override + public void addSteps(MigrationStepRegistry registry) { + registry + .add(2400, "Add column IS_OWNER_USER in ALM_APP_INSTALLS", AddIsOwnerUserColumnInAlmAppInstall.class) + ; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/package-info.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/package-info.java new file mode 100644 index 000000000000..5416ba01e6f1 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v75/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v75; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java index f0d5c74a350a..12e133442905 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/MigrationConfigurationModuleTest.java @@ -37,7 +37,7 @@ public void verify_component_count() { assertThat(container.getPicoContainer().getComponentAdapters()) .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER // DbVersion classes - + 15 + + 16 // Others + 3); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest.java new file mode 100644 index 000000000000..86bdff525145 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.v75; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BOOLEAN; + +public class AddIsOwnerUserColumnInAlmAppInstallTest { + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(AddIsOwnerUserColumnInAlmAppInstallTest.class, "almAppInstalls.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddIsOwnerUserColumnInAlmAppInstall underTest = new AddIsOwnerUserColumnInAlmAppInstall(db.database()); + + @Test + public void column_is_added_to_table() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("alm_app_installs", "is_owner_user", BOOLEAN, null, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } + +} \ No newline at end of file diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest/almAppInstalls.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest/almAppInstalls.sql new file mode 100644 index 000000000000..a266dd515ccb --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v75/AddIsOwnerUserColumnInAlmAppInstallTest/almAppInstalls.sql @@ -0,0 +1,12 @@ +CREATE TABLE "ALM_APP_INSTALLS" ( + "UUID" VARCHAR(40) NOT NULL, + "ALM_ID" VARCHAR(40) NOT NULL, + "OWNER_ID" VARCHAR(4000) NOT NULL, + "INSTALL_ID" VARCHAR(4000) NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + + CONSTRAINT "PK_ALM_APP_INSTALLS" PRIMARY KEY ("UUID") +); +CREATE UNIQUE INDEX "ALM_APP_INSTALLS_OWNER" ON "ALM_APP_INSTALLS" ("ALM_ID", "OWNER_ID"); +CREATE UNIQUE INDEX "ALM_APP_INSTALLS_INSTALL" ON "ALM_APP_INSTALLS" ("ALM_ID", "INSTALL_ID"); \ No newline at end of file