diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-03-30 10:13:33 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-16 20:20:45 +0200 |
commit | 5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c (patch) | |
tree | 418dbeb6b1c0b512b9d06ab542dec9887431c05f /server/sonar-db-dao/src/main/java/org/sonar/db | |
parent | ac3c3089d82cc78390f761776a848be77306538d (diff) | |
download | sonarqube-5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c.tar.gz sonarqube-5073eb0f98e99ab0efbdd3f34b07a97f11e1d26c.zip |
SONAR-10517 Create table project_mappings
* Migration
* Add DAO
* Purge on project deletion
Diffstat (limited to 'server/sonar-db-dao/src/main/java/org/sonar/db')
9 files changed, 200 insertions, 0 deletions
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 a7aeef31af0..ff37086215a 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 @@ -40,6 +40,7 @@ import org.sonar.db.event.EventDao; import org.sonar.db.issue.IssueChangeDao; import org.sonar.db.issue.IssueDao; import org.sonar.db.alm.AlmAppInstallDao; +import org.sonar.db.mapping.ProjectMappingsDao; import org.sonar.db.measure.LiveMeasureDao; import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.custom.CustomMeasureDao; @@ -117,6 +118,7 @@ public class DaoModule extends Module { PermissionTemplateDao.class, PluginDao.class, ProjectLinkDao.class, + ProjectMappingsDao.class, ProjectQgateAssociationDao.class, PropertiesDao.class, PurgeDao.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 99732f30f36..31d0465dcd4 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 @@ -38,6 +38,7 @@ import org.sonar.db.event.EventDao; import org.sonar.db.issue.IssueChangeDao; import org.sonar.db.issue.IssueDao; import org.sonar.db.alm.AlmAppInstallDao; +import org.sonar.db.mapping.ProjectMappingsDao; import org.sonar.db.measure.LiveMeasureDao; import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.custom.CustomMeasureDao; @@ -136,6 +137,7 @@ public class DbClient { private final LiveMeasureDao liveMeasureDao; private final WebhookDao webhookDao; private final WebhookDeliveryDao webhookDeliveryDao; + private final ProjectMappingsDao projectMappingsDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -200,6 +202,7 @@ public class DbClient { liveMeasureDao = getDao(map, LiveMeasureDao.class); webhookDao = getDao(map, WebhookDao.class); webhookDeliveryDao = getDao(map, WebhookDeliveryDao.class); + projectMappingsDao = getDao(map, ProjectMappingsDao.class); } public DbSession openSession(boolean batch) { @@ -435,4 +438,8 @@ public class DbClient { public WebhookDeliveryDao webhookDeliveryDao() { return webhookDeliveryDao; } + + public ProjectMappingsDao projectMappingsDao() { + return projectMappingsDao; + } } 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 bbd97e3fcfa..82967d9b259 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 @@ -63,6 +63,8 @@ import org.sonar.db.issue.IssueChangeMapper; import org.sonar.db.issue.IssueDto; import org.sonar.db.issue.IssueMapper; import org.sonar.db.issue.ShortBranchIssueDto; +import org.sonar.db.mapping.ProjectMappingDto; +import org.sonar.db.mapping.ProjectMappingsMapper; import org.sonar.db.measure.LiveMeasureMapper; import org.sonar.db.measure.MeasureDto; import org.sonar.db.measure.MeasureMapper; @@ -176,6 +178,7 @@ public class MyBatis implements Startable { confBuilder.loadAlias("PermissionTemplateUser", PermissionTemplateUserDto.class); confBuilder.loadAlias("Plugin", PluginDto.class); confBuilder.loadAlias("ProjectQgateAssociation", ProjectQgateAssociationDto.class); + confBuilder.loadAlias("ProjectMapping", ProjectMappingDto.class); confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class); confBuilder.loadAlias("QualityGateCondition", QualityGateConditionDto.class); confBuilder.loadAlias("QualityGate", QualityGateDto.class); @@ -232,6 +235,7 @@ public class MyBatis implements Startable { PermissionTemplateCharacteristicMapper.class, PermissionTemplateMapper.class, PluginMapper.class, + ProjectMappingsMapper.class, ProjectQgateAssociationMapper.class, PropertiesMapper.class, PurgeMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingDto.java new file mode 100644 index 00000000000..7c1bd12c4c2 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingDto.java @@ -0,0 +1,65 @@ +/* + * 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.mapping; + +/** + * Generic purpose way to attach data to a project. For example to store the identifier + * of the project in a remote system (ALM). + * The pair (keyType, key) is unique. + * Data is automatically purged when project is deleted. + */ +public final class ProjectMappingDto { + private String uuid; + private String keyType; + private String key; + private String projectUuid; + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getKeyType() { + return keyType; + } + + public void setKeyType(String keyType) { + this.keyType = keyType; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getProjectUuid() { + return projectUuid; + } + + public void setProjectUuid(String projectUuid) { + this.projectUuid = projectUuid; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsDao.java new file mode 100644 index 00000000000..bf9a728fd1b --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsDao.java @@ -0,0 +1,79 @@ +/* + * 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.mapping; + +import java.util.Optional; +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; + +public class ProjectMappingsDao implements Dao { + + private final System2 system2; + private final UuidFactory uuidFactory; + + public ProjectMappingsDao(System2 system2, UuidFactory uuidFactory) { + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + public void put(DbSession dbSession, String keyType, String key, String projectUuid) { + checkKeyType(keyType); + checkKey(key); + checkArgument(isNotEmpty(projectUuid), "projectUuid can't be null nor empty"); + + ProjectMappingsMapper mapper = getMapper(dbSession); + mapper.deleteByKey(keyType, key); + long now = system2.now(); + mapper.put(uuidFactory.create(), keyType, key, projectUuid, now); + } + + public Optional<ProjectMappingDto> get(DbSession dbSession, String keyType, String key) { + checkKeyType(keyType); + checkKey(key); + + ProjectMappingsMapper mapper = getMapper(dbSession); + return Optional.ofNullable(mapper.selectByKey(keyType, key)); + } + + public void clear(DbSession dbSession, String keyType, String key) { + checkKeyType(keyType); + checkKey(key); + ProjectMappingsMapper mapper = getMapper(dbSession); + mapper.deleteByKey(keyType, key); + } + + private static void checkKeyType(@Nullable String keyType) { + checkArgument(keyType != null && !keyType.isEmpty(), "key type can't be null nor empty"); + } + + private static void checkKey(@Nullable String key) { + checkArgument(key != null && !key.isEmpty(), "key can't be null nor empty"); + } + + private static ProjectMappingsMapper getMapper(DbSession dbSession) { + return dbSession.getMapper(ProjectMappingsMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsMapper.java new file mode 100644 index 00000000000..c59db398b8d --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/mapping/ProjectMappingsMapper.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.db.mapping; + +import javax.annotation.CheckForNull; +import org.apache.ibatis.annotations.Param; + +public interface ProjectMappingsMapper { + + @CheckForNull + ProjectMappingDto selectByKey(@Param("keyType") String keyType, @Param("key") String key); + + void put(@Param("uuid") String uuid, @Param("keyType") String keyType, @Param("key") String key, @Param("projectUuid") String projectUuid, @Param("createdAt") long createdAt); + + void deleteByKey(@Param("keyType") String keyType, @Param("key") String key); +} 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 4e346364ee9..bab365e9f53 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 @@ -291,6 +291,13 @@ class PurgeCommands { profiler.stop(); } + void deleteProjectMappings(String rootUuid) { + profiler.start("deleteProjectMappings (project_mappings)"); + purgeMapper.deleteProjectMappingsByProjectUuid(rootUuid); + session.commit(); + profiler.stop(); + } + void deleteBranch(String rootUuid) { profiler.start("deleteBranch (project_branches)"); purgeMapper.deleteBranchByUuid(rootUuid); 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 0d6a2d09d91..1270cdde241 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 @@ -202,6 +202,7 @@ public class PurgeDao implements Dao { commands.deleteCeActivity(rootUuid); commands.deleteCeQueue(rootUuid); commands.deleteWebhookDeliveries(rootUuid); + commands.deleteProjectMappings(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 a9dec7bb5b2..bbf0be4b2d2 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 @@ -108,6 +108,8 @@ public interface PurgeMapper { void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid); + void deleteProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid); + void deleteBranchByUuid(@Param("uuid") String uuid); void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid); |