]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23098 Create Project Dependencies DB model
authorantoine.vinot <antoine.vinot@sonarsource.com>
Fri, 11 Oct 2024 13:54:52 +0000 (15:54 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 21 Oct 2024 20:03:59 +0000 (20:03 +0000)
18 files changed:
server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
server/sonar-db-dao/src/it/java/org/sonar/db/dependency/ProjectDependenciesDaoIT.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesDao.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesMapper.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesQuery.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependencyDto.java [new file with mode: 0644]
server/sonar-db-dao/src/main/resources/org/sonar/db/dependency/ProjectDependenciesMapper.xml [new file with mode: 0644]
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/dependency/ProjectDependenciesDbTester.java [new file with mode: 0644]
server/sonar-db-migration/src/docs/table_ownership.md
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTableIT.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTable.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java

index 3d5707f371f130034da137a7dff5eb184effd452..0e5df66f68f3910057efd28436f4f8afaaf275b3 100644 (file)
@@ -85,6 +85,7 @@ public final class SqTables {
     "project_alm_settings",
     "project_badge_token",
     "project_branches",
+    "project_dependencies",
     "project_links",
     "project_measures",
     "project_qprofiles",
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/dependency/ProjectDependenciesDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/dependency/ProjectDependenciesDaoIT.java
new file mode 100644 (file)
index 0000000..5d0f7c8
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import javax.annotation.Nullable;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.Pagination;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ProjectDependenciesDaoIT {
+
+  private static final String PROJECT_BRANCH_UUID = "branchUuid";
+
+  @RegisterExtension
+  private final DbTester db = DbTester.create(System2.INSTANCE);
+
+  private final ProjectDependenciesDao projectDependenciesDao = db.getDbClient().projectDependenciesDao();
+
+  @Test
+  void insert_shouldPersistProjectDependencies() {
+    var projectDependencyDto = new ProjectDependencyDto("projectUuid", "version", "includePaths", "packageManager", 1L, 2L);
+
+    projectDependenciesDao.insert(db.getSession(), projectDependencyDto);
+
+    List<Map<String, Object>> select = db.select(db.getSession(), "select * from project_dependencies");
+    assertThat(select).hasSize(1);
+    Map<String, Object> stringObjectMap = select.get(0);
+    assertThat(stringObjectMap).containsExactlyInAnyOrderEntriesOf(
+      Map.of(
+        "uuid", projectDependencyDto.uuid(),
+        "version", projectDependencyDto.version(),
+        "include_paths", projectDependencyDto.includePaths(),
+        "package_manager", projectDependencyDto.packageManager(),
+        "created_at", projectDependencyDto.createdAt(),
+        "updated_at", projectDependencyDto.updatedAt())
+    );
+  }
+
+  @Test
+  void deleteByUuid_shoudDeleteProjectDependencies() {
+    var projectDependencyDto = new ProjectDependencyDto("projectUuid", "version", "includePaths", "packageManager", 1L, 2L);
+    projectDependenciesDao.insert(db.getSession(), projectDependencyDto);
+
+    projectDependenciesDao.deleteByUuid(db.getSession(), projectDependencyDto.uuid());
+
+    List<Map<String, Object>> select = db.select(db.getSession(), "select * from project_dependencies");
+    assertThat(select).isEmpty();
+  }
+
+  @Test
+  void selectByQuery_shouldReturnProjectDependencies_whenQueryByBranchUuid() {
+    ProjectData projectData = db.components().insertPublicProject();
+    var projectDependencyDto = new ProjectDependencyDto(projectData.getMainBranchComponent().uuid(), "version", "includePaths", "packageManager", 1L, 2L);
+    projectDependenciesDao.insert(db.getSession(), projectDependencyDto);
+
+    ProjectDependenciesQuery projectDependenciesQuery = new ProjectDependenciesQuery(projectData.mainBranchUuid(), null);
+    List<ProjectDependencyDto> results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.all());
+
+    assertThat(results).hasSize(1);
+    assertThat(results.get(0)).usingRecursiveComparison().isEqualTo(projectDependencyDto);
+  }
+
+  @Test
+  void selectByQuery_shouldReturnPaginatedProjectDependencies() {
+    ProjectDependencyDto projectDependencyDto1 = insertProjectDependency("1");
+    ProjectDependencyDto projectDependencyDto2 = insertProjectDependency("2");
+    ProjectDependencyDto projectDependencyDto3 = insertProjectDependency("3");
+    ProjectDependencyDto projectDependencyDto4 = insertProjectDependency("4");
+
+    ProjectDependenciesQuery projectDependenciesQuery = new ProjectDependenciesQuery(PROJECT_BRANCH_UUID, null);
+    List<ProjectDependencyDto> page1Results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.forPage(1).andSize(2));
+    List<ProjectDependencyDto> page2Results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.forPage(2).andSize(2));
+
+    assertThat(page1Results).hasSize(2);
+    assertThat(page1Results.get(0)).usingRecursiveComparison().isEqualTo(projectDependencyDto1);
+    assertThat(page1Results.get(1)).usingRecursiveComparison().isEqualTo(projectDependencyDto2);
+    assertThat(page2Results).hasSize(2);
+    assertThat(page2Results.get(0)).usingRecursiveComparison().isEqualTo(projectDependencyDto3);
+    assertThat(page2Results.get(1)).usingRecursiveComparison().isEqualTo(projectDependencyDto4);
+  }
+
+  @Test
+  void selectByQuery_shouldPartiallyMatchLongName_whenQueriedByText() {
+    ProjectDependencyDto projectDepSearched = insertProjectDependency("sEArched");
+    insertProjectDependency("notWanted");
+    ProjectDependencyDto projectDepSearchAsWell = insertProjectDependency("sEArchedAsWell");
+    insertProjectDependency("notwantedeither");
+
+    ProjectDependenciesQuery projectDependenciesQuery = new ProjectDependenciesQuery(PROJECT_BRANCH_UUID, "long_nameSearCHed");
+    List<ProjectDependencyDto> results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.all());
+
+    assertThat(results).hasSize(2);
+    assertThat(results.get(0)).usingRecursiveComparison().isEqualTo(projectDepSearched);
+    assertThat(results.get(1)).usingRecursiveComparison().isEqualTo(projectDepSearchAsWell);
+  }
+
+  @Test
+  void selectByQuery_shouldExactlyMatchKee_whenQueriedByText() {
+    ProjectDependencyDto projectDepSearched = insertProjectDependency("1", dto -> dto.setKey("keySearched"));
+    insertProjectDependency("2", dto -> dto.setKey("KEySearCHed"));
+    insertProjectDependency("3", dto -> dto.setKey("some_keySearched"));
+
+    ProjectDependenciesQuery projectDependenciesQuery = new ProjectDependenciesQuery(PROJECT_BRANCH_UUID, "keySearched");
+    List<ProjectDependencyDto> results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.all());
+
+    assertThat(results).hasSize(1);
+    assertThat(results.get(0)).usingRecursiveComparison().isEqualTo(projectDepSearched);
+  }
+
+  @Test
+  void update_shouldUpdateProjectDependency() {
+    ProjectDependencyDto projectDependencyDto = insertProjectDependency();
+    ProjectDependencyDto updatedProjectDependency =
+      new ProjectDependencyDto(projectDependencyDto.uuid(), "updatedVersion", "updatedIncludePaths", "updatedPackageManager", 2L, 3L);
+
+    projectDependenciesDao.update(db.getSession(), updatedProjectDependency);
+
+    List<Map<String, Object>> select = db.select(db.getSession(), "select * from project_dependencies");
+    assertThat(select).hasSize(1);
+    Map<String, Object> stringObjectMap = select.get(0);
+    assertThat(stringObjectMap).containsExactlyInAnyOrderEntriesOf(
+      Map.of(
+        "uuid", updatedProjectDependency.uuid(),
+        "version", updatedProjectDependency.version(),
+        "include_paths", updatedProjectDependency.includePaths(),
+        "package_manager", updatedProjectDependency.packageManager(),
+        "created_at", projectDependencyDto.createdAt(),
+        "updated_at", updatedProjectDependency.updatedAt())
+    );
+  }
+
+  @Test
+  void countByQuery_shouldReturnTheTotalOfDependencies() {
+    insertProjectDependency("sEArched");
+    insertProjectDependency("notWanted");
+    insertProjectDependency("sEArchedAsWell");
+    db.projectDependencies().insertProjectDependency("another_branch_uuid", "searched");
+
+    ProjectDependenciesQuery projectDependenciesQuery = new ProjectDependenciesQuery(PROJECT_BRANCH_UUID, "long_nameSearCHed");
+
+    assertThat(projectDependenciesDao.countByQuery(db.getSession(), projectDependenciesQuery)).isEqualTo(2);
+    assertThat(projectDependenciesDao.countByQuery(db.getSession(), new ProjectDependenciesQuery(PROJECT_BRANCH_UUID, null))).isEqualTo(3);
+    assertThat(projectDependenciesDao.countByQuery(db.getSession(), new ProjectDependenciesQuery("another_branch_uuid", null))).isEqualTo(1);
+  }
+
+  private ProjectDependencyDto insertProjectDependency() {
+    return db.projectDependencies().insertProjectDependency(PROJECT_BRANCH_UUID);
+  }
+  
+  private ProjectDependencyDto insertProjectDependency(String suffix) {
+    return insertProjectDependency(suffix, null);
+  }
+
+  private ProjectDependencyDto insertProjectDependency(String suffix, @Nullable Consumer<ComponentDto> dtoPopulator) {
+    return db.projectDependencies().insertProjectDependency(PROJECT_BRANCH_UUID, suffix, dtoPopulator);
+  }
+}
index 2913c653a979eaee404c4c0e5df0d63f5f1077ba..9f3ed9ab4fee7b8a1b6b93b73417377b92361af9 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.component.SnapshotDao;
 import org.sonar.db.dependency.CveCweDao;
 import org.sonar.db.dependency.CveDao;
 import org.sonar.db.dependency.IssuesDependencyDao;
+import org.sonar.db.dependency.ProjectDependenciesDao;
 import org.sonar.db.duplication.DuplicationDao;
 import org.sonar.db.entity.EntityDao;
 import org.sonar.db.es.EsQueueDao;
@@ -163,6 +164,7 @@ public class DaoModule extends Module {
     PluginDao.class,
     ProjectDao.class,
     ProjectBadgeTokenDao.class,
+    ProjectDependenciesDao.class,
     ProjectExportDao.class,
     PortfolioDao.class,
     ProjectLinkDao.class,
index 46cb3d676239d50bfe6c7106b3e15688a3923f50..fc0faf5c348813b69c2d9243b0ecb9164162d388 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.component.SnapshotDao;
 import org.sonar.db.dependency.CveCweDao;
 import org.sonar.db.dependency.CveDao;
 import org.sonar.db.dependency.IssuesDependencyDao;
+import org.sonar.db.dependency.ProjectDependenciesDao;
 import org.sonar.db.duplication.DuplicationDao;
 import org.sonar.db.entity.EntityDao;
 import org.sonar.db.es.EsQueueDao;
@@ -202,6 +203,7 @@ public class DbClient {
   private final CveDao cveDao;
   private final CveCweDao cveCweDao;
   private final IssuesDependencyDao issuesDependencyDao;
+  private final ProjectDependenciesDao projectDependenciesDao;
 
   public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) {
     this.database = database;
@@ -299,6 +301,7 @@ public class DbClient {
     cveDao = getDao(map, CveDao.class);
     cveCweDao = getDao(map, CveCweDao.class);
     issuesDependencyDao = getDao(map, IssuesDependencyDao.class);
+    projectDependenciesDao = getDao(map, ProjectDependenciesDao.class);
   }
 
   public DbSession openSession(boolean batch) {
@@ -666,4 +669,8 @@ public class DbClient {
   public IssuesDependencyDao issuesDependencyDao() {
     return issuesDependencyDao;
   }
+
+  public ProjectDependenciesDao projectDependenciesDao() {
+    return projectDependenciesDao;
+  }
 }
index 6239cb6bde902d2234dd2b9f002c5bbcd8c86c0b..84223ad785cb024fcb8ea585f3737068f4db2c93 100644 (file)
@@ -69,6 +69,8 @@ import org.sonar.db.dependency.CveDto;
 import org.sonar.db.dependency.CveMapper;
 import org.sonar.db.dependency.IssuesDependencyDto;
 import org.sonar.db.dependency.IssuesDependencyMapper;
+import org.sonar.db.dependency.ProjectDependenciesMapper;
+import org.sonar.db.dependency.ProjectDependencyDto;
 import org.sonar.db.duplication.DuplicationMapper;
 import org.sonar.db.duplication.DuplicationUnitDto;
 import org.sonar.db.entity.EntityDto;
@@ -250,6 +252,7 @@ public class MyBatis {
     confBuilder.loadAlias("ProjectQgateAssociation", ProjectQgateAssociationDto.class);
     confBuilder.loadAlias("Project", ProjectDto.class);
     confBuilder.loadAlias("ProjectBadgeToken", ProjectBadgeTokenDto.class);
+    confBuilder.loadAlias("ProjectDependency", ProjectDependencyDto.class);
     confBuilder.loadAlias("AnalysisPropertyValuePerProject", AnalysisPropertyValuePerProject.class);
     confBuilder.loadAlias("ProjectAlmKeyAndProject", ProjectAlmKeyAndProject.class);
     confBuilder.loadAlias("PrAndBranchCountByProjectDto", PrBranchAnalyzedLanguageCountByProjectDto.class);
@@ -325,6 +328,7 @@ public class MyBatis {
       PluginMapper.class,
       PortfolioMapper.class,
       ProjectAlmSettingMapper.class,
+      ProjectDependenciesMapper.class,
       ProjectLinkMapper.class,
       ProjectMapper.class,
       ProjectBadgeTokenMapper.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesDao.java
new file mode 100644 (file)
index 0000000..ae518d1
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import java.util.List;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
+
+public class ProjectDependenciesDao implements Dao {
+
+  private static ProjectDependenciesMapper mapper(DbSession session) {
+    return session.getMapper(ProjectDependenciesMapper.class);
+  }
+
+  public void insert(DbSession session, ProjectDependencyDto projectDependencyDto) {
+    mapper(session).insert(projectDependencyDto);
+  }
+
+  public void deleteByUuid(DbSession session, String uuid) {
+    mapper(session).deleteByUuid(uuid);
+  }
+
+  public List<ProjectDependencyDto> selectByQuery(DbSession session, ProjectDependenciesQuery projectDependenciesQuery, Pagination pagination) {
+    return mapper(session).selectByQuery(projectDependenciesQuery, pagination);
+  }
+
+  public int countByQuery(DbSession session, ProjectDependenciesQuery projectDependenciesQuery) {
+    return mapper(session).countByQuery(projectDependenciesQuery);
+  }
+
+  public void update(DbSession session, ProjectDependencyDto projectDependencyDto) {
+    mapper(session).update(projectDependencyDto);
+  }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesMapper.java
new file mode 100644 (file)
index 0000000..e1d8a2b
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import java.util.List;
+import org.apache.ibatis.annotations.Param;
+import org.sonar.db.Pagination;
+
+public interface ProjectDependenciesMapper {
+  void insert(ProjectDependencyDto dto);
+
+  void deleteByUuid(String uuid);
+
+  List<ProjectDependencyDto> selectByQuery(@Param("query") ProjectDependenciesQuery query, @Param("pagination") Pagination pagination);
+
+  void update(ProjectDependencyDto dto);
+
+  int countByQuery(@Param("query") ProjectDependenciesQuery query);
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesQuery.java
new file mode 100644 (file)
index 0000000..16f4c82
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import java.util.Locale;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import static org.sonar.db.DaoUtils.buildLikeValue;
+import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
+
+public final class ProjectDependenciesQuery {
+  private final String branchUuid;
+  @Nullable
+  private final String query;
+
+  public ProjectDependenciesQuery(String branchUuid, @Nullable String query) {
+    this.branchUuid = branchUuid;
+    this.query = query;
+  }
+
+  /**
+   * Used by MyBatis mapper
+   */
+  @CheckForNull
+  public String getLikeQuery() {
+    return query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER).toLowerCase(Locale.ENGLISH);
+  }
+
+  public String branchUuid() {
+    return branchUuid;
+  }
+
+  @Nullable
+  public String query() {
+    return query;
+  }
+
+
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependencyDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependencyDto.java
new file mode 100644 (file)
index 0000000..6c9b655
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import javax.annotation.Nullable;
+
+public record ProjectDependencyDto(
+  String uuid,
+  @Nullable String version,
+  @Nullable String includePaths,
+  @Nullable String packageManager,
+  Long createdAt,
+  Long updatedAt
+  ) {
+}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/dependency/ProjectDependenciesMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/dependency/ProjectDependenciesMapper.xml
new file mode 100644 (file)
index 0000000..e48e797
--- /dev/null
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
+<mapper namespace="org.sonar.db.dependency.ProjectDependenciesMapper">
+  <sql id="projectDependenciesColumns">
+    pd.uuid as uuid,
+    pd.version as version,
+    pd.include_paths as includePaths,
+    pd.package_manager as packageManager,
+    pd.created_at as createdAt,
+    pd.updated_at as updatedAt
+  </sql>
+
+  <insert id="insert" parameterType="ProjectDependency" useGeneratedKeys="false">
+    insert into project_dependencies (
+      uuid,
+      version,
+      include_paths,
+      package_manager,
+      created_at,
+      updated_at
+    ) values (
+      #{uuid,jdbcType=VARCHAR},
+      #{version,jdbcType=CLOB},
+      #{includePaths,jdbcType=CLOB},
+      #{packageManager,jdbcType=VARCHAR},
+      #{createdAt,jdbcType=BIGINT},
+      #{updatedAt,jdbcType=BIGINT}
+    )
+  </insert>
+
+  <delete id="deleteByUuid" parameterType="string">
+    delete from project_dependencies
+    where uuid = #{uuid,jdbcType=VARCHAR}
+  </delete>
+
+  <select id="selectByQuery" parameterType="map" resultType="ProjectDependency">
+    select <include refid="projectDependenciesColumns"/>
+    <include refid="sqlSelectByQuery" />
+    ORDER BY c.kee ASC
+    <include refid="org.sonar.db.common.Common.pagination"/>
+  </select>
+
+  <select id="countByQuery" resultType="int">
+    select count(pd.uuid)
+    <include refid="sqlSelectByQuery" />
+  </select>
+
+  <sql id="sqlSelectByQuery">
+    from project_dependencies pd
+    inner join components c on pd.uuid = c.uuid
+    where c.branch_uuid = #{query.branchUuid,jdbcType=VARCHAR}
+    <if test="query.query() != null">
+      AND (
+        c.kee = #{query.query,jdbcType=VARCHAR}
+        OR lower(c.long_name) LIKE #{query.likeQuery} ESCAPE '/'
+      )
+    </if>
+  </sql>
+
+  <update id="update" parameterType="ProjectDependency" useGeneratedKeys="false">
+    update project_dependencies
+    set
+      uuid = #{uuid, jdbcType=VARCHAR},
+      version = #{version, jdbcType=CLOB},
+      include_paths = #{includePaths, jdbcType=CLOB},
+      package_manager = #{packageManager, jdbcType=VARCHAR},
+      updated_at = #{updatedAt, jdbcType=BIGINT}
+    where
+      uuid = #{uuid, jdbcType=VARCHAR}
+  </update>
+
+</mapper>
index c18d6e54072d6139fdd64c7cfd962a12df624478..8d87b6f6d0233dcf2fe0955d8d7896ed442b3a73 100644 (file)
@@ -732,6 +732,16 @@ ALTER TABLE "PROJECT_BRANCHES" ADD CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY(
 CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_PROJECT_BRANCHES" ON "PROJECT_BRANCHES"("BRANCH_TYPE" NULLS FIRST, "PROJECT_UUID" NULLS FIRST, "KEE" NULLS FIRST);
 CREATE INDEX "PROJECT_BRANCHES_PROJECT_UUID" ON "PROJECT_BRANCHES"("PROJECT_UUID" NULLS FIRST);
 
+CREATE TABLE "PROJECT_DEPENDENCIES"(
+    "UUID" CHARACTER VARYING(40) NOT NULL,
+    "VERSION" CHARACTER LARGE OBJECT,
+    "INCLUDE_PATHS" CHARACTER LARGE OBJECT,
+    "PACKAGE_MANAGER" CHARACTER VARYING(50),
+    "CREATED_AT" BIGINT NOT NULL,
+    "UPDATED_AT" BIGINT NOT NULL
+);
+ALTER TABLE "PROJECT_DEPENDENCIES" ADD CONSTRAINT "PK_PROJECT_DEPENDENCIES" PRIMARY KEY("UUID");
+
 CREATE TABLE "PROJECT_LINKS"(
     "UUID" CHARACTER VARYING(40) NOT NULL,
     "PROJECT_UUID" CHARACTER VARYING(40) NOT NULL,
index 041cf9ffc84a37756982ed814e28345a28621b27..9e6a46fecd51d4915f5169a7e0b12d4c8515dc6a 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.db.audit.AuditPersister;
 import org.sonar.db.audit.NoOpAuditPersister;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ProjectLinkDbTester;
+import org.sonar.db.dependency.ProjectDependenciesDbTester;
 import org.sonar.db.event.EventDbTester;
 import org.sonar.db.favorite.FavoriteDbTester;
 import org.sonar.db.issue.IssueDbTester;
@@ -94,6 +95,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> implements BeforeEach
   private final AlmPatsDbTester almPatsDbtester;
   private final AuditDbTester auditDbTester;
   private final AnticipatedTransitionDbTester anticipatedTransitionDbTester;
+  private final ProjectDependenciesDbTester projectDependenciesDbTester;
 
   private DbTester(UuidFactory uuidFactory, System2 system2, @Nullable String schemaPath, AuditPersister auditPersister, MyBatisConfExtension... confExtensions) {
     super(TestDbImpl.create(schemaPath, confExtensions));
@@ -125,6 +127,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> implements BeforeEach
     this.almPatsDbtester = new AlmPatsDbTester(this);
     this.auditDbTester = new AuditDbTester(this);
     this.anticipatedTransitionDbTester = new AnticipatedTransitionDbTester(this);
+    this.projectDependenciesDbTester = new ProjectDependenciesDbTester(this);
   }
 
   public static DbTester create() {
@@ -268,6 +271,10 @@ public class DbTester extends AbstractDbTester<TestDbImpl> implements BeforeEach
     return anticipatedTransitionDbTester;
   }
 
+  public ProjectDependenciesDbTester projectDependencies() {
+    return projectDependenciesDbTester;
+  }
+
   @Override
   public void afterEach(ExtensionContext context) throws Exception {
     after();
index c4fd31374e0aa4e80bbee1d59399ec9127f5afe3..7990f52e503221aca44d78be19a37319294e9d77 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.entity.EntityDto;
 import org.sonar.db.portfolio.PortfolioDto;
diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/dependency/ProjectDependenciesDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/dependency/ProjectDependenciesDbTester.java
new file mode 100644 (file)
index 0000000..edd64b6
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.dependency;
+
+import java.util.function.Consumer;
+import javax.annotation.Nullable;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
+
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+
+public class ProjectDependenciesDbTester {
+  private final DbTester db;
+  private final DbClient dbClient;
+
+  public ProjectDependenciesDbTester(DbTester db) {
+    this.db = db;
+    this.dbClient = db.getDbClient();
+  }
+
+  public ProjectDependencyDto insertProjectDependency(String branchUuid) {
+    return insertProjectDependency(branchUuid, EMPTY, null);
+  }
+
+  public ProjectDependencyDto insertProjectDependency(String branchUuid, String suffix) {
+    return insertProjectDependency(branchUuid, suffix, null);
+  }
+
+  public ProjectDependencyDto insertProjectDependency(String branchUuid, String suffix, @Nullable Consumer<ComponentDto> dtoPopulator) {
+    ComponentDto componentDto = new ComponentDto().setUuid("uuid" + suffix)
+      .setKey("key" + suffix)
+      .setUuidPath("uuidPath" + suffix)
+      .setName("name" + suffix)
+      .setLongName("long_name" + suffix)
+      .setBranchUuid(branchUuid);
+
+    if (dtoPopulator != null) {
+      dtoPopulator.accept(componentDto);
+    }
+
+    db.components().insertComponent(componentDto);
+    var projectDependencyDto = new ProjectDependencyDto(componentDto.uuid(), "version" + suffix, "includePaths" + suffix, "packageManager" + suffix, 1L, 2L);
+    dbClient.projectDependenciesDao().insert(db.getSession(), projectDependencyDto);
+    return projectDependencyDto;
+  }
+}
index 99e27309c35b4deaebfa0ee9285b9cf324014704..f988a75eb278984dc6847f2a74211108086d91f1 100644 (file)
@@ -67,6 +67,7 @@ Important read: [Data Ownership Principles](https://xtranet-sonarsource.atlassia
 | project_alm_settings | Integration Squad |
 | project_badge_token | Dev and Team Workflow Squad |
 | project_branches |  |
+| project_dependencies | Analysis Experience Squad |
 | project_links | Dev and Team Workflow Squad |
 | project_measures | Analysis Experience Squad |
 | project_qgates | Dev and Team Workflow Squad |
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTableIT.java
new file mode 100644 (file)
index 0000000..5ae52c5
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v108;
+
+import java.sql.SQLException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.db.MigrationDbTester;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static java.sql.Types.BIGINT;
+import static java.sql.Types.CLOB;
+import static java.sql.Types.VARCHAR;
+import static org.sonar.db.MigrationDbTester.createForMigrationStep;
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
+
+class CreateProjectDependenciesTableIT {
+
+  private static final String TABLE_NAME = "project_dependencies";
+
+  @RegisterExtension
+  public final MigrationDbTester db = createForMigrationStep(CreateProjectDependenciesTable.class);
+
+  private final DdlChange underTest = new CreateProjectDependenciesTable(db.database());
+
+  @Test
+  void execute_shouldCreateTable() throws SQLException {
+    db.assertTableDoesNotExist(TABLE_NAME);
+
+    underTest.execute();
+
+    db.assertTableExists(TABLE_NAME);
+    db.assertPrimaryKey(TABLE_NAME, "pk_project_dependencies", "uuid");
+    db.assertColumnDefinition(TABLE_NAME, "uuid", VARCHAR, UUID_SIZE, false);
+    db.assertColumnDefinition(TABLE_NAME, "version", CLOB, null, true);
+    db.assertColumnDefinition(TABLE_NAME, "include_paths", CLOB, null, true);
+    db.assertColumnDefinition(TABLE_NAME, "package_manager", VARCHAR, 50, true);
+    db.assertColumnDefinition(TABLE_NAME, "created_at", BIGINT, null, false);
+    db.assertColumnDefinition(TABLE_NAME, "updated_at", BIGINT, null, false);
+  }
+
+  @Test
+  void execute_shouldBeReentrant() throws SQLException {
+    db.assertTableDoesNotExist(TABLE_NAME);
+    underTest.execute();
+
+    underTest.execute();
+
+    db.assertTableExists(TABLE_NAME);
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTable.java
new file mode 100644 (file)
index 0000000..db96b20
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.v108;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.CreateTableChange;
+
+import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.server.platform.db.migration.def.ClobColumnDef.newClobColumnDefBuilder;
+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 CreateProjectDependenciesTable extends CreateTableChange {
+
+  private static final String TABLE_NAME = "project_dependencies";
+  private static final String COLUMN_UUID_NAME = "uuid";
+  private static final String COLUMN_VERSION_NAME = "version";
+  private static final String COLUMN_INCLUDE_PATHS_NAME = "include_paths";
+  private static final String COLUMN_PACKAGE_MANAGER_NAME = "package_manager";
+  private static final int COLUMN_PACKAGE_MANAGER_SIZE = 50;
+  private static final String COLUMN_CREATED_AT_NAME = "created_at";
+  private static final String COLUMN_UPDATED_AT_NAME = "updated_at";
+
+  protected CreateProjectDependenciesTable(Database db) {
+    super(db, TABLE_NAME);
+  }
+
+  @Override
+  public void execute(Context context, String tableName) throws SQLException {
+    context.execute(new CreateTableBuilder(getDialect(), tableName)
+      .addPkColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_UUID_NAME).setIsNullable(false).setLimit(UUID_SIZE).build())
+      .addColumn(newClobColumnDefBuilder().setColumnName(COLUMN_VERSION_NAME).setIsNullable(true).build())
+      .addColumn(newClobColumnDefBuilder().setColumnName(COLUMN_INCLUDE_PATHS_NAME).setIsNullable(true).build())
+      .addColumn(newVarcharColumnDefBuilder().setColumnName(COLUMN_PACKAGE_MANAGER_NAME).setIsNullable(true).setLimit(COLUMN_PACKAGE_MANAGER_SIZE).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_CREATED_AT_NAME).setIsNullable(false).build())
+      .addColumn(newBigIntegerColumnDefBuilder().setColumnName(COLUMN_UPDATED_AT_NAME).setIsNullable(false).build())
+      .build());
+  }
+}
index 1c3340bdfce0f70378dd8cf2c8a74584981d62db..aab86608e2fa2fb1661955c701254cee1cb5a1bd 100644 (file)
@@ -56,7 +56,7 @@ public class DbVersion108 implements DbVersion {
       .add(10_8_013, "Drop index on 'project_branches.measures_migrated'", DropIndexOnProjectBranchesMeasuresMigrated.class)
       .add(10_8_014, "Drop 'measures_migrated' column on 'project_branches' table", DropMeasuresMigratedColumnInProjectBranchesTable.class)
       .add(10_8_015, "Add column 'impacts' in 'active_rules' table", AddImpactsColumnInActiveRulesTable.class)
-    ;
+      .add(10_8_016, "Create 'project_dependencies' table", CreateProjectDependenciesTable.class);
   }
 
 }