aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2024-09-17 11:09:36 +0200
committersonartech <sonartech@sonarsource.com>2024-09-25 20:02:52 +0000
commitff3bc2b9e1d7b999c8ef73159884b7861287a8b5 (patch)
tree1c035bd4e40e37962b3b09da7211128c23eae1dc /server
parent91330c39e6093c175b74f1936c0e03b8f6c6d24e (diff)
downloadsonarqube-ff3bc2b9e1d7b999c8ef73159884b7861287a8b5.tar.gz
sonarqube-ff3bc2b9e1d7b999c8ef73159884b7861287a8b5.zip
SONAR-23064 Add ai_code_assurance column in projects table
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java17
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java10
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml3
-rw-r--r--server/sonar-db-dao/src/schema/schema-sq.ddl3
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTableIT.java80
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTable.java57
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java1
7 files changed, 170 insertions, 1 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java
index cf41906c23c..b11f64c0a94 100644
--- a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java
+++ b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java
@@ -145,6 +145,23 @@ class ProjectDaoIT {
}
@Test
+ void selectProjects_returnsAiCodeAssurance() {
+ ProjectDto dto1 = createProject("o1", "p1").setAiCodeAssurance(true);
+ ProjectDto dto2 = createProject("o1", "p2");
+
+ projectDao.insert(db.getSession(), dto1);
+ projectDao.insert(db.getSession(), dto2);
+
+ List<ProjectDto> projects = projectDao.selectProjects(db.getSession());
+ Map<String, Boolean> projectToAiCodeAssurance = projects.stream().collect(Collectors.toMap(EntityDto::getName,
+ ProjectDto::getAiCodeAssurance));
+ assertThat(projectToAiCodeAssurance)
+ .hasSize(2)
+ .containsEntry("projectName_p1", true)
+ .containsEntry("projectName_p2", false);
+ }
+
+ @Test
void select_all() {
ProjectDto dto1 = createProject("o1", "p1");
ProjectDto dto2 = createProject("o1", "p2");
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java
index b01511fed23..e7abfb11ece 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java
@@ -32,6 +32,7 @@ public class ProjectDto extends EntityDto {
private static final String TAGS_SEPARATOR = ",";
private String tags;
private CreationMethod creationMethod;
+ private boolean aiCodeAssurance;
private long createdAt;
private long updatedAt;
@@ -123,4 +124,13 @@ public class ProjectDto extends EntityDto {
this.creationMethod = creationMethod;
return this;
}
+
+ public boolean getAiCodeAssurance() {
+ return aiCodeAssurance;
+ }
+
+ public ProjectDto setAiCodeAssurance(boolean aiCodeAssurance) {
+ this.aiCodeAssurance = aiCodeAssurance;
+ return this;
+ }
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
index c1594315c72..47e09da2ec7 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
@@ -11,6 +11,7 @@
p.tags as tagsString,
p.private as isPrivate,
p.creation_method as creationMethod,
+ p.ai_code_assurance as aiCodeAssurance,
p.created_at as createdAt,
p.updated_at as updatedAt
</sql>
@@ -174,6 +175,7 @@
private,
tags,
creation_method,
+ ai_code_assurance,
created_at,
updated_at
)
@@ -186,6 +188,7 @@
#{isPrivate,jdbcType=BOOLEAN},
#{tagsString, jdbcType=VARCHAR},
#{creationMethod, jdbcType=VARCHAR},
+ #{aiCodeAssurance, jdbcType=BOOLEAN},
#{createdAt,jdbcType=BIGINT},
#{updatedAt,jdbcType=BIGINT}
)
diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl
index e37c2c8ade4..ea869a1910b 100644
--- a/server/sonar-db-dao/src/schema/schema-sq.ddl
+++ b/server/sonar-db-dao/src/schema/schema-sq.ddl
@@ -792,7 +792,8 @@ CREATE TABLE "PROJECTS"(
"CREATED_AT" BIGINT,
"UPDATED_AT" BIGINT NOT NULL,
"NCLOC" BIGINT,
- "CREATION_METHOD" CHARACTER VARYING(50) NOT NULL
+ "CREATION_METHOD" CHARACTER VARYING(50) NOT NULL,
+ "AI_CODE_ASSURANCE" BOOLEAN DEFAULT FALSE NOT NULL
);
ALTER TABLE "PROJECTS" ADD CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
CREATE UNIQUE NULLS NOT DISTINCT INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE" NULLS FIRST);
diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTableIT.java
new file mode 100644
index 00000000000..e1ceef55747
--- /dev/null
+++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTableIT.java
@@ -0,0 +1,80 @@
+/*
+ * 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.v107;
+
+import java.sql.SQLException;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.db.MigrationDbTester;
+
+import static java.sql.Types.BOOLEAN;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.sonar.server.platform.db.migration.version.v107.AddAiCodeAssuranceColumnInProjectsTable.AI_CODE_ASSURANCE;
+import static org.sonar.server.platform.db.migration.version.v107.AddAiCodeAssuranceColumnInProjectsTable.DEFAULT_COLUMN_VALUE;
+import static org.sonar.server.platform.db.migration.version.v107.AddAiCodeAssuranceColumnInProjectsTable.PROJECTS_TABLE_NAME;
+
+class AddAiCodeAssuranceColumnInProjectsTableIT {
+
+ @RegisterExtension
+ public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddAiCodeAssuranceColumnInProjectsTable.class);
+
+ private final AddAiCodeAssuranceColumnInProjectsTable underTest = new AddAiCodeAssuranceColumnInProjectsTable(db.database());
+
+ @Test
+ void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException {
+ db.assertColumnDoesNotExist(PROJECTS_TABLE_NAME, AI_CODE_ASSURANCE);
+ underTest.execute();
+ assertColumnExists();
+ }
+
+ @Test
+ void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException {
+ underTest.execute();
+ assertColumnExists();
+ assertThatCode(underTest::execute).doesNotThrowAnyException();
+ }
+
+ @Test
+ void execute_whenDataAlreadyExists_shouldCreateColumnWithDefaultValue() throws SQLException {
+ db.executeInsert(PROJECTS_TABLE_NAME,
+ "UUID", "uuid",
+ "KEE", "uuid",
+ "QUALIFIER", "TRK",
+ "PRIVATE", true,
+ "UPDATED_AT", 1,
+ "CREATION_METHOD", "UI");
+
+ underTest.execute();
+ assertAiCodeAssuranceColumnSetToDefault();
+ assertColumnExists();
+ }
+
+ private void assertAiCodeAssuranceColumnSetToDefault() {
+ Map<String, Object> selectResult = db.selectFirst("select ai_code_assurance from projects where UUID = 'uuid'");
+ assertThat(selectResult).containsEntry(AI_CODE_ASSURANCE, DEFAULT_COLUMN_VALUE);
+ }
+
+ private void assertColumnExists() {
+ db.assertColumnDefinition(PROJECTS_TABLE_NAME, AI_CODE_ASSURANCE, BOOLEAN, null, false);
+ }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTable.java
new file mode 100644
index 00000000000..b3c2b05b8b1
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/AddAiCodeAssuranceColumnInProjectsTable.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.server.platform.db.migration.version.v107;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.def.BooleanColumnDef;
+import org.sonar.server.platform.db.migration.def.ColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class AddAiCodeAssuranceColumnInProjectsTable extends DdlChange {
+ static final String PROJECTS_TABLE_NAME = "projects";
+ static final String AI_CODE_ASSURANCE = "ai_code_assurance";
+ @VisibleForTesting
+ static final Boolean DEFAULT_COLUMN_VALUE = false;
+
+ public AddAiCodeAssuranceColumnInProjectsTable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ try (Connection connection = getDatabase().getDataSource().getConnection()) {
+ if (!DatabaseUtils.tableColumnExists(connection, PROJECTS_TABLE_NAME, AI_CODE_ASSURANCE)) {
+ ColumnDef columnDef = BooleanColumnDef.newBooleanColumnDefBuilder()
+ .setColumnName(AI_CODE_ASSURANCE)
+ .setIsNullable(false)
+ .setDefaultValue(DEFAULT_COLUMN_VALUE)
+ .build();
+ context.execute(new AddColumnsBuilder(getDialect(), PROJECTS_TABLE_NAME)
+ .addColumn(columnDef)
+ .build());
+ }
+ }
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
index 2ca360a3244..6735d32f0ee 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v107/DbVersion107.java
@@ -56,6 +56,7 @@ public class DbVersion107 implements DbVersion {
.add(10_7_012, "Create 'cves' table", CreateCvesTable.class)
.add(10_7_013, "Create 'cve_cwe' table", CreateCveCweTable.class)
.add(10_7_014, "Create 'issues_dependency' table", CreateIssuesDependencyTable.class)
+ .add(10_7_015, "Add 'ai_code_assurance' column to 'projects' table", AddAiCodeAssuranceColumnInProjectsTable.class)
;
}