]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12364 - IT fixes, add uniq new code period table constraint
authorJacek <jacek.poreda@sonarsource.com>
Tue, 6 Aug 2019 13:57:25 +0000 (15:57 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 24 Sep 2019 18:21:13 +0000 (20:21 +0200)
server/sonar-db-dao/src/main/resources/org/sonar/db/newcodeperiod/NewCodePeriodMapper.xml
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/CreateNewCodePeriodTable.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/CreateNewCodePeriodTableTest.java
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest/v79.sql
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java

index fbd85860878f9a0bf8d46f5682bbfabf2ebd96ed..17371e37d2217669975a15481dd5b4059d08f915 100644 (file)
     updated_at=#{updatedAt, jdbcType=TIMESTAMP}
     WHERE
     uuid=#{uuid, jdbcType=VARCHAR}
+    OR
+    (project_uuid=#{projectUuid, jdbcType=VARCHAR}
+    AND
+    branch_uuid=#{branchUuid, jdbcType=VARCHAR})
   </update>
 
   <select id="selectByProject" parameterType="map" resultType="org.sonar.db.newcodeperiod.NewCodePeriodDto">
index f2b7a1c1cd3d6b717b6f9882addb125f3bac5ab4..106e1292d66b6cf66034af3ee1df92486eb43a10 100644 (file)
@@ -25,6 +25,7 @@ import org.sonar.db.Database;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef;
 import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
 import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
 import org.sonar.server.platform.db.migration.step.DdlChange;
 
@@ -102,6 +103,14 @@ public class CreateNewCodePeriodTable extends DdlChange {
       .addColumn(UPDATED_AT)
       .addColumn(CREATED_AT)
       .build());
+
+    context.execute(new CreateIndexBuilder()
+      .setTable(TABLE_NAME)
+      .setName("uniq_new_code_periods")
+      .setUnique(true)
+      .addColumn(PROJECT_UUID_COLUMN)
+      .addColumn(BRANCH_UUID_COLUMN)
+      .build());
   }
 
   private boolean tableExists() throws SQLException {
index dfee7c48dea79971f92b146f03ccb2f99b7369a5..a74fa6384e5a877f87056b408a43a15e66353723 100644 (file)
@@ -45,6 +45,7 @@ public class CreateNewCodePeriodTableTest {
 
     dbTester.assertTableExists(TABLE_NAME);
     dbTester.assertPrimaryKey(TABLE_NAME, "pk_new_code_periods", "uuid");
+    dbTester.assertUniqueIndex(TABLE_NAME, "uniq_new_code_periods", "project_uuid", "branch_uuid");
 
     dbTester.assertColumnDefinition(TABLE_NAME, "uuid", VARCHAR, 40, false);
     dbTester.assertColumnDefinition(TABLE_NAME, "project_uuid", VARCHAR, 40, true);
index 55efd8003164d0900ed38fee079982ac56e58a71..158480582ab24ecd451dcdd7566cc91ec683d609 100644 (file)
@@ -873,6 +873,8 @@ CREATE TABLE "NEW_CODE_PERIODS" (
   CONSTRAINT "PK_NEW_CODE_PERIOD" PRIMARY KEY ("UUID")
 );
 
+CREATE UNIQUE INDEX "UNIQ_NEW_CODE_PERIODS" ON "NEW_CODE_PERIODS" ("PROJECT_UUID", "BRANCH_UUID");
+
 CREATE TABLE "WEBHOOKS" (
   "UUID" VARCHAR(40) NOT NULL,
   "NAME" VARCHAR(100) NOT NULL,
index db6c6062ada6c61b5518e1dbde08950ee0c37d4d..9d483a507e0a77cb59c8beb9748e89f09f71207d 100644 (file)
@@ -105,7 +105,7 @@ public class SetBaselineAction implements ProjectAnalysesWsAction {
 
       dbClient.newCodePeriodDao().upsert(dbSession, new NewCodePeriodDto()
         .setProjectUuid(projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid())
-        .setBranchUuid(branchKey != null ? projectBranch.uuid() : null)
+        .setBranchUuid(projectBranch.uuid())
         .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
         .setValue(analysisUuid)
       );
index 6af027736a736187825fe07834007c6cfd835fb1..b6931e90213cebc6d23fddd8126c7686f15e82d3 100644 (file)
@@ -76,7 +76,7 @@ public class SetBaselineActionTest {
 
   @Test
   @UseDataProvider("nullOrEmpty")
-  public void set_baseline_on_project(@Nullable String branchName) {
+  public void set_baseline_on_main_branch(@Nullable String branchName) {
     ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
     BranchDto branch = new BranchDto()
       .setBranchType(BranchType.LONG)
@@ -90,7 +90,7 @@ public class SetBaselineActionTest {
 
     call(project.getKey(), branchName, analysis.getUuid());
 
-    NewCodePeriodDto loaded = dbClient.newCodePeriodDao().selectByProject(dbSession, project.uuid()).get();
+    NewCodePeriodDto loaded = dbClient.newCodePeriodDao().selectByBranch(dbSession, project.uuid(), branch.getUuid()).get();
     assertThat(loaded.getValue()).isEqualTo(analysis.getUuid());
     assertThat(loaded.getType()).isEqualTo(SPECIFIC_ANALYSIS);
   }