From: Julien HENRY Date: Fri, 27 Jul 2018 08:56:05 +0000 (+0200) Subject: SONAR-11038 Rename table alm_project_mappings -> project_alm_bindings X-Git-Tag: 7.5~627 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5d2dc236eb5ed578a124c38b12734c780397a9c9;p=sonarqube.git SONAR-11038 Rename table alm_project_mappings -> project_alm_bindings --- diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java index 9effb770606..18b68506d10 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java @@ -53,7 +53,6 @@ public final class SqTables { "active_rules", "active_rule_parameters", "alm_app_installs", - "alm_project_mappings", "analysis_properties", "ce_activity", "ce_queue", @@ -86,6 +85,7 @@ public final class SqTables { "perm_tpl_characteristics", "plugins", "projects", + "project_alm_bindings", "project_branches", "project_links", "project_mappings", 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 1a36753f7d7..4e43f0178e4 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 @@ -853,7 +853,7 @@ CREATE TABLE "ALM_APP_INSTALLS" ( 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"); -CREATE TABLE "ALM_PROJECT_MAPPINGS" ( +CREATE TABLE "PROJECT_ALM_BINDINGS" ( "UUID" VARCHAR(40) NOT NULL, "ALM_ID" VARCHAR(40) NOT NULL, "REPO_ID" VARCHAR(256) NOT NULL, @@ -862,10 +862,10 @@ CREATE TABLE "ALM_PROJECT_MAPPINGS" ( "URL" VARCHAR(2000) NOT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL, - CONSTRAINT "PK_ALM_PROJECT_MAPPINGS" PRIMARY KEY ("UUID") + CONSTRAINT "PK_PROJECT_ALM_BINDINGS" PRIMARY KEY ("UUID") ); -CREATE UNIQUE INDEX "ALM_PROJECT_MAPPINGS_ALM_REPO" ON "ALM_PROJECT_MAPPINGS" ("ALM_ID", "REPO_ID"); -CREATE UNIQUE INDEX "ALM_PROJECT_MAPPINGS_PROJECT" ON "ALM_PROJECT_MAPPINGS" ("PROJECT_UUID"); +CREATE UNIQUE INDEX "PROJECT_ALM_BINDINGS_ALM_REPO" ON "PROJECT_ALM_BINDINGS" ("ALM_ID", "REPO_ID"); +CREATE UNIQUE INDEX "PROJECT_ALM_BINDINGS_PROJECT" ON "PROJECT_ALM_BINDINGS" ("PROJECT_UUID"); CREATE TABLE "PROJECT_MAPPINGS" ( "UUID" VARCHAR(40) NOT NULL, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index b6908ba0cb0..f16b9e9e588 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -23,7 +23,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import org.sonar.core.platform.Module; -import org.sonar.db.alm.AlmProjectMappingsDao; +import org.sonar.db.alm.ProjectAlmBindingsDao; import org.sonar.db.ce.CeActivityDao; import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeScannerContextDao; @@ -106,7 +106,7 @@ public class DaoModule extends Module { GroupMembershipDao.class, GroupPermissionDao.class, AlmAppInstallDao.class, - AlmProjectMappingsDao.class, + ProjectAlmBindingsDao.class, InternalPropertiesDao.class, IssueChangeDao.class, IssueDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 2965acf71db..a6775767e91 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -21,7 +21,7 @@ package org.sonar.db; import java.util.IdentityHashMap; import java.util.Map; -import org.sonar.db.alm.AlmProjectMappingsDao; +import org.sonar.db.alm.ProjectAlmBindingsDao; import org.sonar.db.ce.CeActivityDao; import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeScannerContextDao; @@ -91,7 +91,7 @@ public class DbClient { private final QualityProfileDao qualityProfileDao; private final PropertiesDao propertiesDao; private final AlmAppInstallDao almAppInstallDao; - private final AlmProjectMappingsDao almProjectMappingsDao; + private final ProjectAlmBindingsDao projectAlmBindingsDao; private final InternalPropertiesDao internalPropertiesDao; private final SnapshotDao snapshotDao; private final ComponentDao componentDao; @@ -151,7 +151,7 @@ public class DbClient { map.put(dao.getClass(), dao); } almAppInstallDao = getDao(map, AlmAppInstallDao.class); - almProjectMappingsDao = getDao(map, AlmProjectMappingsDao.class); + projectAlmBindingsDao = getDao(map, ProjectAlmBindingsDao.class); schemaMigrationDao = getDao(map, SchemaMigrationDao.class); authorizationDao = getDao(map, AuthorizationDao.class); organizationDao = getDao(map, OrganizationDao.class); @@ -220,8 +220,8 @@ public class DbClient { return almAppInstallDao; } - public AlmProjectMappingsDao almProjectMappingsDao() { - return almProjectMappingsDao; + public ProjectAlmBindingsDao projectAlmBindingsDao() { + return projectAlmBindingsDao; } public SchemaMigrationDao schemaMigrationDao() { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 74a37144ac9..9743df75701 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -31,8 +31,8 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.session.TransactionIsolationLevel; import org.sonar.api.Startable; import org.sonar.db.alm.AlmAppInstallMapper; -import org.sonar.db.alm.AlmProjectMappingDto; -import org.sonar.db.alm.AlmProjectMappingsMapper; +import org.sonar.db.alm.ProjectAlmBindingDto; +import org.sonar.db.alm.ProjectAlmBindingsMapper; import org.sonar.db.ce.CeActivityMapper; import org.sonar.db.ce.CeQueueMapper; import org.sonar.db.ce.CeScannerContextMapper; @@ -152,7 +152,6 @@ public class MyBatis implements Startable { // DTO aliases, keep them sorted alphabetically confBuilder.loadAlias("ActiveRule", ActiveRuleDto.class); confBuilder.loadAlias("ActiveRuleParam", ActiveRuleParamDto.class); - confBuilder.loadAlias("AlmProjectMapping", AlmProjectMappingDto.class); confBuilder.loadAlias("CeTaskCharacteristic", CeTaskCharacteristicDto.class); confBuilder.loadAlias("Component", ComponentDto.class); confBuilder.loadAlias("ComponentWithSnapshot", ComponentDtoWithSnapshotId.class); @@ -179,6 +178,7 @@ public class MyBatis implements Startable { confBuilder.loadAlias("PermissionTemplate", PermissionTemplateDto.class); confBuilder.loadAlias("PermissionTemplateUser", PermissionTemplateUserDto.class); confBuilder.loadAlias("Plugin", PluginDto.class); + confBuilder.loadAlias("ProjectAlmBinding", ProjectAlmBindingDto.class); confBuilder.loadAlias("ProjectQgateAssociation", ProjectQgateAssociationDto.class); confBuilder.loadAlias("ProjectMapping", ProjectMappingDto.class); confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class); @@ -202,9 +202,8 @@ public class MyBatis implements Startable { // keep them sorted alphabetically Class[] mappers = { ActiveRuleMapper.class, - AnalysisPropertiesMapper.class, AlmAppInstallMapper.class, - AlmProjectMappingsMapper.class, + AnalysisPropertiesMapper.class, AuthorizationMapper.class, BranchMapper.class, CeActivityMapper.class, @@ -213,7 +212,6 @@ public class MyBatis implements Startable { CeTaskInputMapper.class, CeTaskCharacteristicMapper.class, ComponentKeyUpdaterMapper.class, - ProjectLinkMapper.class, ComponentMapper.class, LiveMeasureMapper.class, CustomMeasureMapper.class, @@ -237,6 +235,8 @@ public class MyBatis implements Startable { PermissionTemplateCharacteristicMapper.class, PermissionTemplateMapper.class, PluginMapper.class, + ProjectAlmBindingsMapper.class, + ProjectLinkMapper.class, ProjectMappingsMapper.class, ProjectQgateAssociationMapper.class, PropertiesMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingDto.java deleted file mode 100644 index 7c2fcf46798..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingDto.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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; - -public class AlmProjectMappingDto { - private String uuid; - private String almId; - private String repoId; - private String projectUuid; - private String githubSlug; - private String url; - - public String getAlmId() { - return almId; - } - - public AlmProjectMappingDto setAlmId(String almId) { - this.almId = almId; - return this; - } - - public String getRepoId() { - return repoId; - } - - public AlmProjectMappingDto setRepoId(String repoId) { - this.repoId = repoId; - return this; - } - - public String getProjectUuid() { - return projectUuid; - } - - public AlmProjectMappingDto setProjectUuid(String projectUuid) { - this.projectUuid = projectUuid; - return this; - } - - public String getGithubSlug() { - return githubSlug; - } - - public AlmProjectMappingDto setGithubSlug(String githubSlug) { - this.githubSlug = githubSlug; - return this; - } - - public String getUrl() { - return url; - } - - public AlmProjectMappingDto setUrl(String url) { - this.url = url; - return this; - } - - public String getUuid() { - return uuid; - } - - public AlmProjectMappingDto setUuid(String uuid) { - this.uuid = uuid; - return this; - } -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java deleted file mode 100644 index 6e7a3b2639c..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 java.util.Collection; -import java.util.List; -import java.util.Objects; -import javax.annotation.Nullable; -import org.sonar.api.utils.System2; -import org.sonar.core.util.UuidFactory; -import org.sonar.db.Dao; -import org.sonar.db.DbSession; - -import static com.google.common.base.Preconditions.checkArgument; -import static org.apache.commons.lang.StringUtils.isNotEmpty; -import static org.sonar.db.DatabaseUtils.executeLargeInputs; - -public class AlmProjectMappingsDao implements Dao { - - private final System2 system2; - private final UuidFactory uuidFactory; - - public AlmProjectMappingsDao(System2 system2, UuidFactory uuidFactory) { - this.system2 = system2; - this.uuidFactory = uuidFactory; - } - - public void insertOrUpdate(DbSession dbSession, ALM alm, String repoId, String projectUuid, @Nullable String githubSlug, String url) { - checkAlm(alm); - checkRepoId(repoId); - checkArgument(isNotEmpty(projectUuid), "projectUuid can't be null nor empty"); - checkArgument(isNotEmpty(url), "url can't be null nor empty"); - - AlmProjectMappingsMapper mapper = getMapper(dbSession); - long now = system2.now(); - - if (mapper.update(alm.getId(), repoId, projectUuid, githubSlug, url, now) == 0) { - mapper.insert(uuidFactory.create(), alm.getId(), repoId, projectUuid, githubSlug, url, now); - } - } - - public boolean mappingExists(DbSession dbSession, ALM alm, String repoId) { - checkAlm(alm); - checkRepoId(repoId); - - return getMapper(dbSession).mappingCount(alm.getId(), repoId) == 1; - } - - /** - * Gets a list or mappings by their repo_id. The result does NOT contain {@code null} values for mappings not found, so - * the size of result may be less than the number of ids. - *

Results may be in a different order as input ids.

- */ - public List selectByRepoIds(final DbSession session, ALM alm, Collection repoIds) { - return executeLargeInputs(repoIds, partionnedIds -> getMapper(session).selectByRepoIds(alm.getId(), partionnedIds)); - } - - private static void checkAlm(@Nullable ALM alm) { - Objects.requireNonNull(alm, "alm can't be null"); - } - - private static void checkRepoId(@Nullable String repoId) { - checkArgument(isNotEmpty(repoId), "repoId can't be null nor empty"); - } - - private static AlmProjectMappingsMapper getMapper(DbSession dbSession) { - return dbSession.getMapper(AlmProjectMappingsMapper.class); - } -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java deleted file mode 100644 index 5baeafc91e2..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 java.util.List; -import javax.annotation.Nullable; -import org.apache.ibatis.annotations.Param; - -public interface AlmProjectMappingsMapper { - - int mappingCount(@Param("almId") String almId, @Param("repoId") String repoId); - - void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid, - @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now); - - int update(@Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid, - @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now); - - List selectByRepoIds(@Param("almId") String almId, @Param("repoIds") List repoIds); -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingDto.java new file mode 100644 index 00000000000..db42d29e607 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingDto.java @@ -0,0 +1,83 @@ +/* + * 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; + +public class ProjectAlmBindingDto { + private String uuid; + private String almId; + private String repoId; + private String projectUuid; + private String githubSlug; + private String url; + + public String getAlmId() { + return almId; + } + + public ProjectAlmBindingDto setAlmId(String almId) { + this.almId = almId; + return this; + } + + public String getRepoId() { + return repoId; + } + + public ProjectAlmBindingDto setRepoId(String repoId) { + this.repoId = repoId; + return this; + } + + public String getProjectUuid() { + return projectUuid; + } + + public ProjectAlmBindingDto setProjectUuid(String projectUuid) { + this.projectUuid = projectUuid; + return this; + } + + public String getGithubSlug() { + return githubSlug; + } + + public ProjectAlmBindingDto setGithubSlug(String githubSlug) { + this.githubSlug = githubSlug; + return this; + } + + public String getUrl() { + return url; + } + + public ProjectAlmBindingDto setUrl(String url) { + this.url = url; + return this; + } + + public String getUuid() { + return uuid; + } + + public ProjectAlmBindingDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsDao.java new file mode 100644 index 00000000000..f2d6cc18428 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsDao.java @@ -0,0 +1,86 @@ +/* + * 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 java.util.Collection; +import java.util.List; +import java.util.Objects; +import javax.annotation.Nullable; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isNotEmpty; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class ProjectAlmBindingsDao implements Dao { + + private final System2 system2; + private final UuidFactory uuidFactory; + + public ProjectAlmBindingsDao(System2 system2, UuidFactory uuidFactory) { + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + public void insertOrUpdate(DbSession dbSession, ALM alm, String repoId, String projectUuid, @Nullable String githubSlug, String url) { + checkAlm(alm); + checkRepoId(repoId); + checkArgument(isNotEmpty(projectUuid), "projectUuid can't be null nor empty"); + checkArgument(isNotEmpty(url), "url can't be null nor empty"); + + ProjectAlmBindingsMapper mapper = getMapper(dbSession); + long now = system2.now(); + + if (mapper.update(alm.getId(), repoId, projectUuid, githubSlug, url, now) == 0) { + mapper.insert(uuidFactory.create(), alm.getId(), repoId, projectUuid, githubSlug, url, now); + } + } + + public boolean bindingExists(DbSession dbSession, ALM alm, String repoId) { + checkAlm(alm); + checkRepoId(repoId); + + return getMapper(dbSession).bindingCount(alm.getId(), repoId) == 1; + } + + /** + * Gets a list of bindings by their repo_id. The result does NOT contain {@code null} values for bindings not found, so + * the size of result may be less than the number of ids. + *

Results may be in a different order as input ids.

+ */ + public List selectByRepoIds(final DbSession session, ALM alm, Collection repoIds) { + return executeLargeInputs(repoIds, partionnedIds -> getMapper(session).selectByRepoIds(alm.getId(), partionnedIds)); + } + + private static void checkAlm(@Nullable ALM alm) { + Objects.requireNonNull(alm, "alm can't be null"); + } + + private static void checkRepoId(@Nullable String repoId) { + checkArgument(isNotEmpty(repoId), "repoId can't be null nor empty"); + } + + private static ProjectAlmBindingsMapper getMapper(DbSession dbSession) { + return dbSession.getMapper(ProjectAlmBindingsMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsMapper.java new file mode 100644 index 00000000000..9fa3f788f39 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsMapper.java @@ -0,0 +1,37 @@ +/* + * 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 java.util.List; +import javax.annotation.Nullable; +import org.apache.ibatis.annotations.Param; + +public interface ProjectAlmBindingsMapper { + + int bindingCount(@Param("almId") String almId, @Param("repoId") String repoId); + + void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid, + @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now); + + int update(@Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid, + @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now); + + List selectByRepoIds(@Param("almId") String almId, @Param("repoIds") List repoIds); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java index ec3fe0d866f..80634fd8fa3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -298,9 +298,9 @@ class PurgeCommands { profiler.stop(); } - void deleteAlmProjectMappings(String rootUuid) { - profiler.start("deleteAlmProjectMappings (alm_project_mappings)"); - purgeMapper.deleteAlmProjectMappingsByProjectUuid(rootUuid); + void deleteProjectAlmBindings(String rootUuid) { + profiler.start("deleteProjectAlmBindings (project_alm_bindings)"); + purgeMapper.deleteProjectAlmBindingsByProjectUuid(rootUuid); session.commit(); profiler.stop(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java index 17060408739..c62dcbd14f2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -203,7 +203,7 @@ public class PurgeDao implements Dao { commands.deleteCeQueue(rootUuid); commands.deleteWebhookDeliveries(rootUuid); commands.deleteProjectMappings(rootUuid); - commands.deleteAlmProjectMappings(rootUuid); + commands.deleteProjectAlmBindings(rootUuid); commands.deleteBranch(rootUuid); commands.deleteLiveMeasures(rootUuid); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java index 6d5d5e0d7d6..de1b8f47bfd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -110,7 +110,7 @@ public interface PurgeMapper { void deleteProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid); - void deleteAlmProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid); + void deleteProjectAlmBindingsByProjectUuid(@Param("projectUuid") String projectUuid); void deleteBranchByUuid(@Param("uuid") String uuid); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml deleted file mode 100644 index 6797a87ec56..00000000000 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - INSERT INTO alm_project_mappings - ( - uuid, - alm_id, - repo_id, - project_uuid, - github_slug, - url, - created_at, - updated_at - ) - VALUES ( - #{uuid, jdbcType=VARCHAR}, - #{almId, jdbcType=VARCHAR}, - #{repoId, jdbcType=VARCHAR}, - #{projectUuid, jdbcType=VARCHAR}, - #{githubSlug, jdbcType=VARCHAR}, - #{url, jdbcType=VARCHAR}, - #{now, jdbcType=BIGINT}, - #{now, jdbcType=BIGINT} - ) - - - - update alm_project_mappings set - project_uuid = #{projectUuid, jdbcType=VARCHAR}, - github_slug = #{githubSlug, jdbcType=VARCHAR}, - url = #{url, jdbcType=VARCHAR}, - updated_at = #{now, jdbcType=BIGINT} - where - alm_id = #{almId, jdbcType=VARCHAR} - and repo_id = #{repoId, jdbcType=VARCHAR} - - - - - diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/ProjectAlmBindingsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/ProjectAlmBindingsMapper.xml new file mode 100644 index 00000000000..b31e45e6481 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/ProjectAlmBindingsMapper.xml @@ -0,0 +1,68 @@ + + + + + + + + + INSERT INTO project_alm_bindings + ( + uuid, + alm_id, + repo_id, + project_uuid, + github_slug, + url, + created_at, + updated_at + ) + VALUES ( + #{uuid, jdbcType=VARCHAR}, + #{almId, jdbcType=VARCHAR}, + #{repoId, jdbcType=VARCHAR}, + #{projectUuid, jdbcType=VARCHAR}, + #{githubSlug, jdbcType=VARCHAR}, + #{url, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT}, + #{now, jdbcType=BIGINT} + ) + + + + update project_alm_bindings set + project_uuid = #{projectUuid, jdbcType=VARCHAR}, + github_slug = #{githubSlug, jdbcType=VARCHAR}, + url = #{url, jdbcType=VARCHAR}, + updated_at = #{now, jdbcType=BIGINT} + where + alm_id = #{almId, jdbcType=VARCHAR} + and repo_id = #{repoId, jdbcType=VARCHAR} + + + + + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index a248eba7024..5e8535cd978 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -364,8 +364,8 @@ delete from project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR} - - delete from alm_project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR} + + delete from project_alm_bindings where project_uuid=#{projectUuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmProjectMappingsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmProjectMappingsDaoTest.java deleted file mode 100644 index e8fba5bc38a..00000000000 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmProjectMappingsDaoTest.java +++ /dev/null @@ -1,348 +0,0 @@ -/* - * 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 java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import javax.annotation.Nullable; -import org.assertj.core.api.AbstractAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.System2; -import org.sonar.core.util.UuidFactory; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.tuple; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.db.alm.ALM.BITBUCKETCLOUD; -import static org.sonar.db.alm.ALM.GITHUB; - -public class AlmProjectMappingsDaoTest { - - private static final String A_UUID = "abcde1234"; - private static final String ANOTHER_UUID = "xyz789"; - private static final String EMPTY_STRING = ""; - - private static final String A_REPO = "my_repo"; - private static final String ANOTHER_REPO = "another_repo"; - - private static final String A_GITHUB_SLUG = null; - private static final String ANOTHER_GITHUB_SLUG = "example/foo"; - - private static final String A_URL = "foo url"; - private static final String ANOTHER_URL = "bar url"; - - private static final long DATE = 1_600_000_000_000L; - private static final long DATE_LATER = 1_700_000_000_000L; - - private System2 system2 = mock(System2.class); - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule - public DbTester dbTester = DbTester.create(system2); - - private DbSession dbSession = dbTester.getSession(); - private UuidFactory uuidFactory = mock(UuidFactory.class); - private AlmProjectMappingsDao underTest = new AlmProjectMappingsDao(system2, uuidFactory); - - @Test - public void insert_throws_NPE_if_alm_is_null() { - expectAlmNPE(); - - underTest.insertOrUpdate(dbSession, null, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - } - - @Test - public void insert_throws_IAE_if_repo_id_is_null() { - expectRepoIdNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, null, A_UUID, A_GITHUB_SLUG, A_URL); - } - - @Test - public void insert_throws_IAE_if_repo_id_is_empty() { - expectRepoIdNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, EMPTY_STRING, A_UUID, A_GITHUB_SLUG, A_URL); - } - - @Test - public void insert_throws_IAE_if_project_uuid_is_null() { - expectProjectUuidNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, null, A_GITHUB_SLUG, A_URL); - } - - @Test - public void insert_throws_IAE_if_project_uuid_is_empty() { - expectProjectUuidNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, EMPTY_STRING, A_GITHUB_SLUG, A_URL); - } - - @Test - public void insert_throws_IAE_if_url_is_null() { - expectUrlNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, null); - } - - @Test - public void insert_throws_IAE_if_url_is_empty() { - expectUrlNullOrEmptyIAE(); - - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, EMPTY_STRING); - } - - @Test - public void insert() { - when(uuidFactory.create()).thenReturn(A_UUID); - when(system2.now()).thenReturn(DATE); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - - assertThatAlmProjectMapping(GITHUB, A_REPO) - .hasProjectUuid(A_UUID) - .hasGithubSlug(A_GITHUB_SLUG) - .hasUrl(A_URL) - .hasCreatedAt(DATE) - .hasUpdatedAt(DATE); - } - - @Test - public void update() { - when(uuidFactory.create()).thenReturn(A_UUID); - when(system2.now()).thenReturn(DATE); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - - when(system2.now()).thenReturn(DATE_LATER); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, ANOTHER_UUID, ANOTHER_GITHUB_SLUG, ANOTHER_URL); - - assertThatAlmProjectMapping(GITHUB, A_REPO) - .hasProjectUuid(ANOTHER_UUID) - .hasGithubSlug(ANOTHER_GITHUB_SLUG) - .hasUrl(ANOTHER_URL) - .hasCreatedAt(DATE) - .hasUpdatedAt(DATE_LATER); - } - - @Test - public void insert_multiple() { - when(system2.now()).thenReturn(DATE); - when(uuidFactory.create()) - .thenReturn(A_UUID) - .thenReturn(ANOTHER_UUID); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, ANOTHER_GITHUB_SLUG, ANOTHER_URL); - - assertThatAlmProjectMapping(GITHUB, A_REPO) - .hasProjectUuid(A_UUID) - .hasGithubSlug(A_GITHUB_SLUG) - .hasUrl(A_URL) - .hasCreatedAt(DATE) - .hasUpdatedAt(DATE); - - assertThatAlmProjectMapping(GITHUB, ANOTHER_REPO) - .hasProjectUuid(ANOTHER_UUID) - .hasGithubSlug(ANOTHER_GITHUB_SLUG) - .hasUrl(ANOTHER_URL) - .hasCreatedAt(DATE) - .hasUpdatedAt(DATE); - } - - @Test - public void mappingExists_throws_NPE_when_alm_is_null() { - expectAlmNPE(); - - underTest.mappingExists(dbSession, null, A_REPO); - } - - @Test - public void mappingExists_throws_IAE_when_repo_id_is_null() { - expectRepoIdNullOrEmptyIAE(); - - underTest.mappingExists(dbSession, GITHUB, null); - } - - @Test - public void mappingExists_throws_IAE_when_repo_id_is_empty() { - expectRepoIdNullOrEmptyIAE(); - - underTest.mappingExists(dbSession, GITHUB, EMPTY_STRING); - } - - @Test - public void mappingExists_returns_false_when_entry_does_not_exist_in_DB() { - assertThat(underTest.mappingExists(dbSession, GITHUB, A_REPO)).isFalse(); - } - - @Test - public void mappingExists_returns_true_when_entry_exists() { - when(uuidFactory.create()).thenReturn(A_UUID); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - - assertThat(underTest.mappingExists(dbSession, GITHUB, A_REPO)).isTrue(); - } - - @Test - public void select_by_repo_ids() { - when(system2.now()).thenReturn(DATE); - when(uuidFactory.create()) - .thenReturn("uuid1") - .thenReturn("uuid2") - .thenReturn("uuid3"); - underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); - underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL); - underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo"); - - assertThat(underTest.selectByRepoIds(dbSession, GITHUB, Arrays.asList(A_REPO, ANOTHER_REPO, "foo"))) - .extracting(AlmProjectMappingDto::getUuid, AlmProjectMappingDto::getAlmId, AlmProjectMappingDto::getRepoId, AlmProjectMappingDto::getProjectUuid, AlmProjectMappingDto::getUrl, AlmProjectMappingDto::getGithubSlug) - .containsExactlyInAnyOrder( - tuple("uuid1", GITHUB.getId(), A_REPO, A_UUID, A_URL, A_GITHUB_SLUG), - tuple("uuid2", GITHUB.getId(), ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null)); - } - - private void expectAlmNPE() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("alm can't be null"); - } - - private void expectRepoIdNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("repoId can't be null nor empty"); - } - - private void expectProjectUuidNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("projectUuid can't be null nor empty"); - } - - private void expectUrlNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("url can't be null nor empty"); - } - - private AlmAppInstallAssert assertThatAlmProjectMapping(ALM alm, String repoId) { - return new AlmAppInstallAssert(dbTester, dbSession, alm, repoId); - } - - private static class AlmAppInstallAssert extends AbstractAssert { - - private AlmAppInstallAssert(DbTester dbTester, DbSession dbSession, ALM alm, String repoId) { - super(asAlmProjectMapping(dbTester, dbSession, alm, repoId), AlmAppInstallAssert.class); - } - - private static AlmProjectMapping asAlmProjectMapping(DbTester dbTester, DbSession dbSession, ALM alm, String repoId) { - List> rows = dbTester.select( - dbSession, - "select" + - " project_uuid as \"projectUuid\", github_slug as \"githubSlug\", url as \"url\", " + - " created_at as \"createdAt\", updated_at as \"updatedAt\"" + - " from alm_project_mappings" + - " where alm_id='" + alm.getId() + "' and repo_id='" + repoId + "'"); - if (rows.isEmpty()) { - return null; - } - if (rows.size() > 1) { - throw new IllegalStateException("Unique index violation"); - } - return new AlmProjectMapping( - (String) rows.get(0).get("projectUuid"), - (String) rows.get(0).get("githubSlug"), - (String) rows.get(0).get("url"), - (Long) rows.get(0).get("createdAt"), - (Long) rows.get(0).get("updatedAt")); - } - - public void doesNotExist() { - isNull(); - } - - AlmAppInstallAssert hasProjectUuid(String expected) { - isNotNull(); - - if (!Objects.equals(actual.projectUuid, expected)) { - failWithMessage("Expected ALM Project Mapping to have column PROJECT_UUID to be <%s> but was <%s>", expected, actual.projectUuid); - } - return this; - } - - AlmAppInstallAssert hasGithubSlug(String expected) { - isNotNull(); - - if (!Objects.equals(actual.githubSlug, expected)) { - failWithMessage("Expected ALM Project Mapping to have column GITHUB_SLUG to be <%s> but was <%s>", expected, actual.githubSlug); - } - return this; - } - - AlmAppInstallAssert hasUrl(String expected) { - isNotNull(); - - if (!Objects.equals(actual.url, expected)) { - failWithMessage("Expected ALM Project Mapping to have column URL to be <%s> but was <%s>", expected, actual.url); - } - return this; - } - - AlmAppInstallAssert hasCreatedAt(long expected) { - isNotNull(); - - if (!Objects.equals(actual.createdAt, expected)) { - failWithMessage("Expected ALM Project Mapping to have column CREATED_AT to be <%s> but was <%s>", expected, actual.createdAt); - } - - return this; - } - - AlmAppInstallAssert hasUpdatedAt(long expected) { - isNotNull(); - - if (!Objects.equals(actual.updatedAt, expected)) { - failWithMessage("Expected ALM Project Mapping to have column UPDATED_AT to be <%s> but was <%s>", expected, actual.updatedAt); - } - - return this; - } - - } - - private static final class AlmProjectMapping { - private final String projectUuid; - private final String githubSlug; - private final String url; - private final Long createdAt; - private final Long updatedAt; - - AlmProjectMapping(@Nullable String projectUuid, @Nullable String githubSlug, @Nullable String url, @Nullable Long createdAt, @Nullable Long updatedAt) { - this.projectUuid = projectUuid; - this.githubSlug = githubSlug; - this.url = url; - this.createdAt = createdAt; - this.updatedAt = updatedAt; - } - } -} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/ProjectAlmBindingsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/ProjectAlmBindingsDaoTest.java new file mode 100644 index 00000000000..c0b5150d0b6 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/ProjectAlmBindingsDaoTest.java @@ -0,0 +1,348 @@ +/* + * 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 java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.annotation.Nullable; +import org.assertj.core.api.AbstractAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.db.alm.ALM.BITBUCKETCLOUD; +import static org.sonar.db.alm.ALM.GITHUB; + +public class ProjectAlmBindingsDaoTest { + + private static final String A_UUID = "abcde1234"; + private static final String ANOTHER_UUID = "xyz789"; + private static final String EMPTY_STRING = ""; + + private static final String A_REPO = "my_repo"; + private static final String ANOTHER_REPO = "another_repo"; + + private static final String A_GITHUB_SLUG = null; + private static final String ANOTHER_GITHUB_SLUG = "example/foo"; + + private static final String A_URL = "foo url"; + private static final String ANOTHER_URL = "bar url"; + + private static final long DATE = 1_600_000_000_000L; + private static final long DATE_LATER = 1_700_000_000_000L; + + private System2 system2 = mock(System2.class); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public DbTester dbTester = DbTester.create(system2); + + private DbSession dbSession = dbTester.getSession(); + private UuidFactory uuidFactory = mock(UuidFactory.class); + private ProjectAlmBindingsDao underTest = new ProjectAlmBindingsDao(system2, uuidFactory); + + @Test + public void insert_throws_NPE_if_alm_is_null() { + expectAlmNPE(); + + underTest.insertOrUpdate(dbSession, null, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + } + + @Test + public void insert_throws_IAE_if_repo_id_is_null() { + expectRepoIdNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, null, A_UUID, A_GITHUB_SLUG, A_URL); + } + + @Test + public void insert_throws_IAE_if_repo_id_is_empty() { + expectRepoIdNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, EMPTY_STRING, A_UUID, A_GITHUB_SLUG, A_URL); + } + + @Test + public void insert_throws_IAE_if_project_uuid_is_null() { + expectProjectUuidNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, null, A_GITHUB_SLUG, A_URL); + } + + @Test + public void insert_throws_IAE_if_project_uuid_is_empty() { + expectProjectUuidNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, EMPTY_STRING, A_GITHUB_SLUG, A_URL); + } + + @Test + public void insert_throws_IAE_if_url_is_null() { + expectUrlNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, null); + } + + @Test + public void insert_throws_IAE_if_url_is_empty() { + expectUrlNullOrEmptyIAE(); + + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, EMPTY_STRING); + } + + @Test + public void insert() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(DATE); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + + assertThatProjectAlmBinding(GITHUB, A_REPO) + .hasProjectUuid(A_UUID) + .hasGithubSlug(A_GITHUB_SLUG) + .hasUrl(A_URL) + .hasCreatedAt(DATE) + .hasUpdatedAt(DATE); + } + + @Test + public void update() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(DATE); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + + when(system2.now()).thenReturn(DATE_LATER); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, ANOTHER_UUID, ANOTHER_GITHUB_SLUG, ANOTHER_URL); + + assertThatProjectAlmBinding(GITHUB, A_REPO) + .hasProjectUuid(ANOTHER_UUID) + .hasGithubSlug(ANOTHER_GITHUB_SLUG) + .hasUrl(ANOTHER_URL) + .hasCreatedAt(DATE) + .hasUpdatedAt(DATE_LATER); + } + + @Test + public void insert_multiple() { + when(system2.now()).thenReturn(DATE); + when(uuidFactory.create()) + .thenReturn(A_UUID) + .thenReturn(ANOTHER_UUID); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, ANOTHER_GITHUB_SLUG, ANOTHER_URL); + + assertThatProjectAlmBinding(GITHUB, A_REPO) + .hasProjectUuid(A_UUID) + .hasGithubSlug(A_GITHUB_SLUG) + .hasUrl(A_URL) + .hasCreatedAt(DATE) + .hasUpdatedAt(DATE); + + assertThatProjectAlmBinding(GITHUB, ANOTHER_REPO) + .hasProjectUuid(ANOTHER_UUID) + .hasGithubSlug(ANOTHER_GITHUB_SLUG) + .hasUrl(ANOTHER_URL) + .hasCreatedAt(DATE) + .hasUpdatedAt(DATE); + } + + @Test + public void mappingExists_throws_NPE_when_alm_is_null() { + expectAlmNPE(); + + underTest.bindingExists(dbSession, null, A_REPO); + } + + @Test + public void mappingExists_throws_IAE_when_repo_id_is_null() { + expectRepoIdNullOrEmptyIAE(); + + underTest.bindingExists(dbSession, GITHUB, null); + } + + @Test + public void mappingExists_throws_IAE_when_repo_id_is_empty() { + expectRepoIdNullOrEmptyIAE(); + + underTest.bindingExists(dbSession, GITHUB, EMPTY_STRING); + } + + @Test + public void mappingExists_returns_false_when_entry_does_not_exist_in_DB() { + assertThat(underTest.bindingExists(dbSession, GITHUB, A_REPO)).isFalse(); + } + + @Test + public void mappingExists_returns_true_when_entry_exists() { + when(uuidFactory.create()).thenReturn(A_UUID); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + + assertThat(underTest.bindingExists(dbSession, GITHUB, A_REPO)).isTrue(); + } + + @Test + public void select_by_repo_ids() { + when(system2.now()).thenReturn(DATE); + when(uuidFactory.create()) + .thenReturn("uuid1") + .thenReturn("uuid2") + .thenReturn("uuid3"); + underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL); + underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL); + underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo"); + + assertThat(underTest.selectByRepoIds(dbSession, GITHUB, Arrays.asList(A_REPO, ANOTHER_REPO, "foo"))) + .extracting(ProjectAlmBindingDto::getUuid, ProjectAlmBindingDto::getAlmId, ProjectAlmBindingDto::getRepoId, ProjectAlmBindingDto::getProjectUuid, ProjectAlmBindingDto::getUrl, ProjectAlmBindingDto::getGithubSlug) + .containsExactlyInAnyOrder( + tuple("uuid1", GITHUB.getId(), A_REPO, A_UUID, A_URL, A_GITHUB_SLUG), + tuple("uuid2", GITHUB.getId(), ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null)); + } + + private void expectAlmNPE() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("alm can't be null"); + } + + private void expectRepoIdNullOrEmptyIAE() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("repoId can't be null nor empty"); + } + + private void expectProjectUuidNullOrEmptyIAE() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("projectUuid can't be null nor empty"); + } + + private void expectUrlNullOrEmptyIAE() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("url can't be null nor empty"); + } + + private ProjectAlmBindingAssert assertThatProjectAlmBinding(ALM alm, String repoId) { + return new ProjectAlmBindingAssert(dbTester, dbSession, alm, repoId); + } + + private static class ProjectAlmBindingAssert extends AbstractAssert { + + private ProjectAlmBindingAssert(DbTester dbTester, DbSession dbSession, ALM alm, String repoId) { + super(asProjectAlmBinding(dbTester, dbSession, alm, repoId), ProjectAlmBindingAssert.class); + } + + private static ProjectAlmBinding asProjectAlmBinding(DbTester dbTester, DbSession dbSession, ALM alm, String repoId) { + List> rows = dbTester.select( + dbSession, + "select" + + " project_uuid as \"projectUuid\", github_slug as \"githubSlug\", url as \"url\", " + + " created_at as \"createdAt\", updated_at as \"updatedAt\"" + + " from project_alm_bindings" + + " where alm_id='" + alm.getId() + "' and repo_id='" + repoId + "'"); + if (rows.isEmpty()) { + return null; + } + if (rows.size() > 1) { + throw new IllegalStateException("Unique index violation"); + } + return new ProjectAlmBinding( + (String) rows.get(0).get("projectUuid"), + (String) rows.get(0).get("githubSlug"), + (String) rows.get(0).get("url"), + (Long) rows.get(0).get("createdAt"), + (Long) rows.get(0).get("updatedAt")); + } + + public void doesNotExist() { + isNull(); + } + + ProjectAlmBindingAssert hasProjectUuid(String expected) { + isNotNull(); + + if (!Objects.equals(actual.projectUuid, expected)) { + failWithMessage("Expected Project ALM Binding to have column PROJECT_UUID to be <%s> but was <%s>", expected, actual.projectUuid); + } + return this; + } + + ProjectAlmBindingAssert hasGithubSlug(String expected) { + isNotNull(); + + if (!Objects.equals(actual.githubSlug, expected)) { + failWithMessage("Expected Project ALM Binding to have column GITHUB_SLUG to be <%s> but was <%s>", expected, actual.githubSlug); + } + return this; + } + + ProjectAlmBindingAssert hasUrl(String expected) { + isNotNull(); + + if (!Objects.equals(actual.url, expected)) { + failWithMessage("Expected Project ALM Binding to have column URL to be <%s> but was <%s>", expected, actual.url); + } + return this; + } + + ProjectAlmBindingAssert hasCreatedAt(long expected) { + isNotNull(); + + if (!Objects.equals(actual.createdAt, expected)) { + failWithMessage("Expected Project ALM Binding to have column CREATED_AT to be <%s> but was <%s>", expected, actual.createdAt); + } + + return this; + } + + ProjectAlmBindingAssert hasUpdatedAt(long expected) { + isNotNull(); + + if (!Objects.equals(actual.updatedAt, expected)) { + failWithMessage("Expected Project ALM Binding to have column UPDATED_AT to be <%s> but was <%s>", expected, actual.updatedAt); + } + + return this; + } + + } + + private static final class ProjectAlmBinding { + private final String projectUuid; + private final String githubSlug; + private final String url; + private final Long createdAt; + private final Long updatedAt; + + ProjectAlmBinding(@Nullable String projectUuid, @Nullable String githubSlug, @Nullable String url, @Nullable Long createdAt, @Nullable Long updatedAt) { + this.projectUuid = projectUuid; + this.githubSlug = githubSlug; + this.url = url; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + } +} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index 36f75d46862..73579845ada 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -622,19 +622,19 @@ public class PurgeDaoTest { } @Test - public void deleteProject_deletes_alm_project_mappings() { + public void deleteProject_deletes_project_alm_bindings() { ALM alm = ALM.GITHUB; String repoId = "123"; String otherRepoId = repoId + "-foo"; ComponentDto project = dbTester.components().insertPublicProject(); - dbClient.almProjectMappingsDao().insertOrUpdate(dbSession, alm, repoId, project.uuid(), null, "foo"); - dbClient.almProjectMappingsDao().insertOrUpdate(dbSession, alm, otherRepoId, "D2", null, "bar"); + dbClient.projectAlmBindingsDao().insertOrUpdate(dbSession, alm, repoId, project.uuid(), null, "foo"); + dbClient.projectAlmBindingsDao().insertOrUpdate(dbSession, alm, otherRepoId, "D2", null, "bar"); underTest.deleteProject(dbSession, project.uuid()); - assertThat(dbClient.almProjectMappingsDao().mappingExists(dbSession, alm, repoId)).isFalse(); - assertThat(dbClient.almProjectMappingsDao().mappingExists(dbSession, alm, otherRepoId)).isTrue(); + assertThat(dbClient.projectAlmBindingsDao().bindingExists(dbSession, alm, repoId)).isFalse(); + assertThat(dbClient.projectAlmBindingsDao().bindingExists(dbSession, alm, otherRepoId)).isTrue(); } @Test diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java deleted file mode 100644 index 6264cef7962..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTable.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.v73; - -import java.sql.Connection; -import java.sql.SQLException; -import org.sonar.db.Database; -import org.sonar.db.DatabaseUtils; -import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef; -import org.sonar.server.platform.db.migration.def.VarcharColumnDef; -import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; -import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; -import org.sonar.server.platform.db.migration.step.DdlChange; - -import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.MAX_SIZE; -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class CreateAlmProjectMappingsTable extends DdlChange { - - private static final String TABLE_NAME = "alm_project_mappings"; - - private static final VarcharColumnDef UUID = newVarcharColumnDefBuilder() - .setColumnName("uuid") - .setLimit(UUID_SIZE) - .setIsNullable(false) - .build(); - private static final VarcharColumnDef ALM_ID_COLUMN = newVarcharColumnDefBuilder() - .setColumnName("alm_id") - .setIsNullable(false) - .setLimit(40) - .build(); - private static final VarcharColumnDef REPO_ID_COLUMN = newVarcharColumnDefBuilder() - .setColumnName("repo_id") - .setIsNullable(false) - .setLimit(256) - .build(); - private static final VarcharColumnDef PROJECT_UUID_COLUMN = newVarcharColumnDefBuilder() - .setColumnName("project_uuid") - .setIsNullable(false) - .setLimit(40) - .build(); - private static final VarcharColumnDef GITHUB_SLUG_COLUMN = newVarcharColumnDefBuilder() - .setColumnName("github_slug") - .setIsNullable(true) - .setLimit(256) - .build(); - private static final VarcharColumnDef URL_COLUMN = newVarcharColumnDefBuilder() - .setColumnName("url") - .setIsNullable(false) - .setLimit(2000) - .build(); - private static final BigIntegerColumnDef CREATED_AT_COLUMN = newBigIntegerColumnDefBuilder() - .setColumnName("created_at") - .setIsNullable(false) - .build(); - private static final BigIntegerColumnDef UPDATED_AT_COLUMN = newBigIntegerColumnDefBuilder() - .setColumnName("updated_at") - .setIsNullable(false) - .build(); - - public CreateAlmProjectMappingsTable(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - - if (!tableExists()) { - context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) - .addPkColumn(UUID) - .addColumn(ALM_ID_COLUMN) - .addColumn(REPO_ID_COLUMN) - .addColumn(PROJECT_UUID_COLUMN) - .addColumn(GITHUB_SLUG_COLUMN) - .addColumn(URL_COLUMN) - .addColumn(CREATED_AT_COLUMN) - .addColumn(UPDATED_AT_COLUMN) - .build()); - - context.execute(new CreateIndexBuilder(getDialect()) - .addColumn(ALM_ID_COLUMN) - .addColumn(REPO_ID_COLUMN) - .setUnique(true) - .setTable(TABLE_NAME) - .setName(TABLE_NAME + "_alm_repo") - .build()); - context.execute(new CreateIndexBuilder(getDialect()) - .addColumn(PROJECT_UUID_COLUMN) - .setUnique(true) - .setTable(TABLE_NAME) - .setName(TABLE_NAME + "_project") - .build()); - } - } - - private boolean tableExists() throws SQLException { - try (Connection connection = getDatabase().getDataSource().getConnection()) { - return DatabaseUtils.tableExists(TABLE_NAME, connection); - } - } -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTable.java new file mode 100644 index 00000000000..282504a2569 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTable.java @@ -0,0 +1,119 @@ +/* + * 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.v73; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateProjectAlmBindingsTable extends DdlChange { + + private static final String TABLE_NAME = "project_alm_bindings"; + + private static final VarcharColumnDef UUID = newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setLimit(UUID_SIZE) + .setIsNullable(false) + .build(); + private static final VarcharColumnDef ALM_ID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("alm_id") + .setIsNullable(false) + .setLimit(40) + .build(); + private static final VarcharColumnDef REPO_ID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("repo_id") + .setIsNullable(false) + .setLimit(256) + .build(); + private static final VarcharColumnDef PROJECT_UUID_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("project_uuid") + .setIsNullable(false) + .setLimit(40) + .build(); + private static final VarcharColumnDef GITHUB_SLUG_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("github_slug") + .setIsNullable(true) + .setLimit(256) + .build(); + private static final VarcharColumnDef URL_COLUMN = newVarcharColumnDefBuilder() + .setColumnName("url") + .setIsNullable(false) + .setLimit(2000) + .build(); + private static final BigIntegerColumnDef CREATED_AT_COLUMN = newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build(); + private static final BigIntegerColumnDef UPDATED_AT_COLUMN = newBigIntegerColumnDefBuilder() + .setColumnName("updated_at") + .setIsNullable(false) + .build(); + + public CreateProjectAlmBindingsTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + + if (!tableExists()) { + context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(UUID) + .addColumn(ALM_ID_COLUMN) + .addColumn(REPO_ID_COLUMN) + .addColumn(PROJECT_UUID_COLUMN) + .addColumn(GITHUB_SLUG_COLUMN) + .addColumn(URL_COLUMN) + .addColumn(CREATED_AT_COLUMN) + .addColumn(UPDATED_AT_COLUMN) + .build()); + + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(ALM_ID_COLUMN) + .addColumn(REPO_ID_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName(TABLE_NAME + "_alm_repo") + .build()); + context.execute(new CreateIndexBuilder(getDialect()) + .addColumn(PROJECT_UUID_COLUMN) + .setUnique(true) + .setTable(TABLE_NAME) + .setName(TABLE_NAME + "_project") + .build()); + } + } + + private boolean tableExists() throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(TABLE_NAME, connection); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java index b996ea12a5c..7549bf7bc59 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73.java @@ -40,7 +40,7 @@ public class DbVersion73 implements DbVersion { .add(2210, "Add 'securityhotspotadmin' permission to templates characteristics already having 'issueadmin'", PopulateHotspotAdminPermissionOnTemplatesCharacteristics.class) .add(2211, "Set SUBSCRIPTION not nullable in ORGANIZATIONS", SetSubscriptionOnOrganizationsNotNullable.class) .add(2212, "Add index on ORGANIZATION_MEMBERS", AddIndexOnOrganizationMembers.class) - .add(2213, "Create table to store alm project mappings", CreateAlmProjectMappingsTable.class) + .add(2213, "Create table to store project ALM bindings", CreateProjectAlmBindingsTable.class) ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java deleted file mode 100644 index 79557d02099..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.v73; - -import java.sql.SQLException; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.db.CoreDbTester; - -import static java.sql.Types.BIGINT; -import static java.sql.Types.VARCHAR; -import static org.assertj.core.api.Assertions.assertThat; - -public class CreateAlmProjectMappingsTableTest { - - private static final String TABLE = "alm_project_mappings"; - - @Rule - public final CoreDbTester db = CoreDbTester.createForSchema(CreateAlmProjectMappingsTableTest.class, "empty.sql"); - - private CreateAlmProjectMappingsTable underTest = new CreateAlmProjectMappingsTable(db.database()); - - @Test - public void creates_table_on_empty_db() throws SQLException { - underTest.execute(); - - checkTable(); - } - - @Test - public void migration_is_reentrant() throws SQLException { - underTest.execute(); - underTest.execute(); - - checkTable(); - } - - private void checkTable() { - assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); - - db.assertColumnDefinition(TABLE, "uuid", VARCHAR, 40, false); - db.assertPrimaryKey(TABLE, "pk_" + TABLE, "uuid"); - db.assertColumnDefinition(TABLE, "alm_id", VARCHAR, 40, false); - db.assertColumnDefinition(TABLE, "repo_id", VARCHAR, 256, false); - db.assertColumnDefinition(TABLE, "project_uuid", VARCHAR, 40, false); - db.assertColumnDefinition(TABLE, "github_slug", VARCHAR, 256, true); - db.assertColumnDefinition(TABLE, "url", VARCHAR, 2000, false); - db.assertColumnDefinition(TABLE, "created_at", BIGINT, null, false); - db.assertColumnDefinition(TABLE, "updated_at", BIGINT, null, false); - - db.assertUniqueIndex(TABLE, TABLE + "_alm_repo", "alm_id", "repo_id"); - db.assertUniqueIndex(TABLE, TABLE + "_project", "project_uuid"); - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTableTest.java new file mode 100644 index 00000000000..3c294c47921 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTableTest.java @@ -0,0 +1,71 @@ +/* + * 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.v73; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static java.sql.Types.BIGINT; +import static java.sql.Types.VARCHAR; +import static org.assertj.core.api.Assertions.assertThat; + +public class CreateProjectAlmBindingsTableTest { + + private static final String TABLE = "project_alm_bindings"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateProjectAlmBindingsTableTest.class, "empty.sql"); + + private CreateProjectAlmBindingsTable underTest = new CreateProjectAlmBindingsTable(db.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + checkTable(); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + checkTable(); + } + + private void checkTable() { + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + + db.assertColumnDefinition(TABLE, "uuid", VARCHAR, 40, false); + db.assertPrimaryKey(TABLE, "pk_" + TABLE, "uuid"); + db.assertColumnDefinition(TABLE, "alm_id", VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "repo_id", VARCHAR, 256, false); + db.assertColumnDefinition(TABLE, "project_uuid", VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "github_slug", VARCHAR, 256, true); + db.assertColumnDefinition(TABLE, "url", VARCHAR, 2000, false); + db.assertColumnDefinition(TABLE, "created_at", BIGINT, null, false); + db.assertColumnDefinition(TABLE, "updated_at", BIGINT, null, false); + + db.assertUniqueIndex(TABLE, TABLE + "_alm_repo", "alm_id", "repo_id"); + db.assertUniqueIndex(TABLE, TABLE + "_project", "project_uuid"); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73Test.java index fbd6a8fd21e..9f36bc3591f 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v73/DbVersion73Test.java @@ -35,6 +35,6 @@ public class DbVersion73Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 13); + verifyMigrationCount(underTest, 14); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateAlmProjectMappingsTableTest/empty.sql deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTableTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v73/CreateProjectAlmBindingsTableTest/empty.sql new file mode 100644 index 00000000000..e69de29bb2d