From 596b193a1a2d64c50c728ff8051721f61cd79d0c Mon Sep 17 00:00:00 2001 From: "antoine.vinot" Date: Fri, 11 Oct 2024 15:54:52 +0200 Subject: [PATCH] SONAR-23098 Create Project Dependencies DB model --- .../java/org/sonar/db/version/SqTables.java | 1 + .../dependency/ProjectDependenciesDaoIT.java | 183 ++++++++++++++++++ .../src/main/java/org/sonar/db/DaoModule.java | 2 + .../src/main/java/org/sonar/db/DbClient.java | 7 + .../src/main/java/org/sonar/db/MyBatis.java | 4 + .../db/dependency/ProjectDependenciesDao.java | 52 +++++ .../dependency/ProjectDependenciesMapper.java | 36 ++++ .../dependency/ProjectDependenciesQuery.java | 57 ++++++ .../db/dependency/ProjectDependencyDto.java | 32 +++ .../dependency/ProjectDependenciesMapper.xml | 72 +++++++ server/sonar-db-dao/src/schema/schema-sq.ddl | 10 + .../java/org/sonar/db/DbTester.java | 7 + .../sonar/db/component/ComponentDbTester.java | 1 - .../ProjectDependenciesDbTester.java | 64 ++++++ .../src/docs/table_ownership.md | 1 + .../CreateProjectDependenciesTableIT.java | 69 +++++++ .../v108/CreateProjectDependenciesTable.java | 58 ++++++ .../migration/version/v108/DbVersion108.java | 2 +- 18 files changed, 656 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-dao/src/it/java/org/sonar/db/dependency/ProjectDependenciesDaoIT.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesDao.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesMapper.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesQuery.java create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependencyDto.java create mode 100644 server/sonar-db-dao/src/main/resources/org/sonar/db/dependency/ProjectDependenciesMapper.xml create mode 100644 server/sonar-db-dao/src/testFixtures/java/org/sonar/db/dependency/ProjectDependenciesDbTester.java create mode 100644 server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTableIT.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTable.java 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 3d5707f371f..0e5df66f68f 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 @@ -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 index 00000000000..5d0f7c8ca65 --- /dev/null +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/dependency/ProjectDependenciesDaoIT.java @@ -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> select = db.select(db.getSession(), "select * from project_dependencies"); + assertThat(select).hasSize(1); + Map 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> 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 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 page1Results = projectDependenciesDao.selectByQuery(db.getSession(), projectDependenciesQuery, Pagination.forPage(1).andSize(2)); + List 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 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 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> select = db.select(db.getSession(), "select * from project_dependencies"); + assertThat(select).hasSize(1); + Map 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 dtoPopulator) { + return db.projectDependencies().insertProjectDependency(PROJECT_BRANCH_UUID, suffix, dtoPopulator); + } +} 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 2913c653a97..9f3ed9ab4fe 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 @@ -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, 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 46cb3d67623..fc0faf5c348 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 @@ -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; + } } 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 6239cb6bde9..84223ad785c 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 @@ -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 index 00000000000..ae518d1217c --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesDao.java @@ -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 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 index 00000000000..e1d8a2b9bda --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesMapper.java @@ -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 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 index 00000000000..16f4c8201e5 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependenciesQuery.java @@ -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 index 00000000000..6c9b6550063 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/dependency/ProjectDependencyDto.java @@ -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 index 00000000000..e48e7973767 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/dependency/ProjectDependenciesMapper.xml @@ -0,0 +1,72 @@ + + + + + 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 + + + + 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} + ) + + + + delete from project_dependencies + where uuid = #{uuid,jdbcType=VARCHAR} + + + + + + + + from project_dependencies pd + inner join components c on pd.uuid = c.uuid + where c.branch_uuid = #{query.branchUuid,jdbcType=VARCHAR} + + AND ( + c.kee = #{query.query,jdbcType=VARCHAR} + OR lower(c.long_name) LIKE #{query.likeQuery} ESCAPE '/' + ) + + + + + 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} + + + diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index c18d6e54072..8d87b6f6d02 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -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, diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index 041cf9ffc84..9e6a46fecd5 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -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 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 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 implements BeforeEach return anticipatedTransitionDbTester; } + public ProjectDependenciesDbTester projectDependencies() { + return projectDependenciesDbTester; + } + @Override public void afterEach(ExtensionContext context) throws Exception { after(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index c4fd31374e0..7990f52e503 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -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 index 00000000000..edd64b6013e --- /dev/null +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/dependency/ProjectDependenciesDbTester.java @@ -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 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; + } +} diff --git a/server/sonar-db-migration/src/docs/table_ownership.md b/server/sonar-db-migration/src/docs/table_ownership.md index 99e27309c35..f988a75eb27 100644 --- a/server/sonar-db-migration/src/docs/table_ownership.md +++ b/server/sonar-db-migration/src/docs/table_ownership.md @@ -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 index 00000000000..5ae52c51f2a --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTableIT.java @@ -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 index 00000000000..db96b2023a0 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/CreateProjectDependenciesTable.java @@ -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()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java index 1c3340bdfce..aab86608e2f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/DbVersion108.java @@ -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); } } -- 2.39.5