]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12626 Migrate existing branches to 'BRANCH' type
authorMichal Duda <michal.duda@sonarsource.com>
Tue, 5 Nov 2019 13:08:25 +0000 (14:08 +0100)
committerSonarTech <sonartech@sonarsource.com>
Mon, 9 Dec 2019 19:46:16 +0000 (20:46 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonType.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasks.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest/schema.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest/schema.sql [new file with mode: 0644]

index d6ee2292a571e514d75bfd25c36d1a78fb2dd170..531c0a318960df9b190cb648c06b5b29028baea2 100644 (file)
@@ -39,6 +39,9 @@ public class DbVersion81 implements DbVersion {
       .add(3109, "Populate EXCLUDE_FROM_PURGE column", PopulateExcludeBranchFromPurgeColumn.class)
       .add(3110, "Remove 'sonar.branch.longLivedBranches.regex'", RemoveLLBRegexSetting.class)
       .add(3111, "Rename 'sonar.dbcleaner.daysBeforeDeletingInactiveShortLivingBranches' setting",
-        RenameDaysBeforeDeletingInactiveSLBSetting.class);
+        RenameDaysBeforeDeletingInactiveSLBSetting.class)
+      .add(3112, "Migrate short and long living branches types to common BRANCH type", MigrateSlbsAndLlbsToCommonType.class)
+      .add(3113, "Migrate short and long living branches types to common BRANCH type in ce tasks table",
+        MigrateSlbsAndLlbsToCommonTypeInCeTasks.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonType.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonType.java
new file mode 100644 (file)
index 0000000..e21c8b0
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v81;
+
+import java.sql.SQLException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.SupportsBlueGreen;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+@SupportsBlueGreen
+public class MigrateSlbsAndLlbsToCommonType extends DataChange {
+  private final System2 system;
+
+  public MigrateSlbsAndLlbsToCommonType(Database db, System2 system) {
+    super(db);
+    this.system = system;
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    long now = system.now();
+
+    context.prepareUpsert("update project_branches set branch_type = 'BRANCH', updated_at=? where branch_type = 'SHORT' or branch_type = 'LONG'")
+      .setLong(1, now)
+      .execute()
+      .commit();
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasks.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasks.java
new file mode 100644 (file)
index 0000000..dcedbf0
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v81;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.SupportsBlueGreen;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+@SupportsBlueGreen
+public class MigrateSlbsAndLlbsToCommonTypeInCeTasks extends DataChange {
+  static final String TABLE = "ce_task_characteristics";
+
+  public MigrateSlbsAndLlbsToCommonTypeInCeTasks(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(Context context) throws SQLException {
+    context.prepareUpsert("update " + TABLE + " set text_value = 'BRANCH' where kee = 'branchType' and text_value in ('LONG', 'SHORT')")
+      .execute()
+      .commit();
+  }
+}
index 9d37c435f777cabee31f3bdec1274996a977d5d1..3ad0ca6cdd8761e5fb93d78beee47ee3a2ccdcc3 100644 (file)
@@ -37,7 +37,7 @@ public class DbVersion81Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 12);
+    verifyMigrationCount(underTest, 14);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest.java
new file mode 100644 (file)
index 0000000..cd9caf9
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v81;
+
+import java.sql.SQLException;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.platform.db.migration.version.v81.MigrateSlbsAndLlbsToCommonTypeInCeTasks.TABLE;
+
+public class MigrateSlbsAndLlbsToCommonTypeInCeTasksTest {
+
+  @Rule
+  public CoreDbTester dbTester = CoreDbTester.createForSchema(MigrateSlbsAndLlbsToCommonTypeInCeTasksTest.class, "schema.sql");
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MigrateSlbsAndLlbsToCommonTypeInCeTasks underTest = new MigrateSlbsAndLlbsToCommonTypeInCeTasks(dbTester.database());
+
+  @Before
+  public void setup() {
+    insertCeTaskCharacteristic("char-uuid-1", "task-uuid-1", "branchType", "LONG");
+    insertCeTaskCharacteristic("char-uuid-2", "task-uuid-1", "branch", "foobar");
+    insertCeTaskCharacteristic("char-uuid-3", "task-uuid-1", "someKey", "someValue");
+
+    insertCeTaskCharacteristic("char-uuid-21", "task-uuid-2", "branchType", "SHORT");
+    insertCeTaskCharacteristic("char-uuid-22", "task-uuid-2", "branch", "feature/1");
+    insertCeTaskCharacteristic("char-uuid-23", "task-uuid-2", "someKey", "anotherValue");
+
+    insertCeTaskCharacteristic("char-uuid-31", "task-uuid-3", "pullRequest", "1");
+    insertCeTaskCharacteristic("char-uuid-32", "task-uuid-3", "someKey", "yetAnotherValue");
+
+    assertThat(dbTester.countRowsOfTable(TABLE)).isEqualTo(8);
+  }
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    verifyMigrationResult();
+  }
+
+  @Test
+  public void migration_is_re_entrant() throws SQLException {
+    underTest.execute();
+    underTest.execute();
+
+    verifyMigrationResult();
+  }
+
+  private void verifyMigrationResult() {
+    assertThat(dbTester.countRowsOfTable(TABLE)).isEqualTo(8);
+    assertThat(dbTester.countSql("select count(*) from " + TABLE + " where kee = 'branchType' and text_value in ('LONG', 'SHORT')")).isEqualTo(0);
+    assertThat(dbTester.select("select uuid from " + TABLE + " where kee = 'branchType' and text_value = 'BRANCH'")
+      .stream()
+      .map(e -> e.get("UUID"))
+      .collect(Collectors.toSet())).containsExactlyInAnyOrder("char-uuid-1", "char-uuid-21");
+  }
+
+  private void insertCeTaskCharacteristic(String uuid, String taskUuid, String key, String value) {
+    dbTester.executeInsert(
+      TABLE,
+      "UUID", uuid,
+      "TASK_UUID", taskUuid,
+      "KEE", key,
+      "TEXT_VALUE", value);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest.java
new file mode 100644 (file)
index 0000000..5663d3c
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.v81;
+
+import java.sql.SQLException;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MigrateSlbsAndLlbsToCommonTypeTest {
+
+  private static final String BRANCHES_TABLE = "project_branches";
+  private static final String MAIN_BRANCH_1 = "825644c3-06b2-44b5-833e-13169a5379ad";
+  private static final String MAIN_BRANCH_2 = "47a53ace-0297-4a4e-bcab-07a65e3eeac4";
+  private static final String PR_1 = "ad85f2ee-7271-407a-b9f7-b0a80343e001";
+  private static final String LLB_1 = "08905714-2bfd-48a6-b19d-7801bc4d1ca4";
+  private static final String SLB_1 = "a2020607-4134-45f9-87ea-2cdaa84bf386";
+  private static final String PR_2 = "834eba05-e4f7-4214-bfec-c7823e101487";
+  private static final String LLB_2 = "fea036fe-e830-4d87-85ee-7aaa21729019";
+  private static final String SLB_2 = "20166755-953e-4b8e-8c1d-67d812e42418";
+
+  @Rule
+  public CoreDbTester dbTester = CoreDbTester.createForSchema(MigrateSlbsAndLlbsToCommonTypeTest.class, "schema.sql");
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MigrateSlbsAndLlbsToCommonType underTest = new MigrateSlbsAndLlbsToCommonType(dbTester.database(), System2.INSTANCE);
+
+  @Before
+  public void setup() {
+    insertMainBranch(MAIN_BRANCH_1);
+    insertBranch(MAIN_BRANCH_1, PR_1, "PULL_REQUEST");
+    insertBranch(MAIN_BRANCH_1, LLB_1, "LONG");
+    insertBranch(MAIN_BRANCH_1, SLB_1, "SHORT");
+
+    insertMainBranch(MAIN_BRANCH_2);
+    insertBranch(MAIN_BRANCH_1, PR_2, "PULL_REQUEST");
+    insertBranch(MAIN_BRANCH_1, LLB_2, "LONG");
+    insertBranch(MAIN_BRANCH_1, SLB_2, "SHORT");
+  }
+
+  @Test
+  public void execute() throws SQLException {
+    underTest.execute();
+
+    verifyMigrationResult();
+  }
+
+  @Test
+  public void migration_is_re_entrant() throws SQLException {
+    underTest.execute();
+    underTest.execute();
+
+    verifyMigrationResult();
+  }
+
+  private void verifyMigrationResult() {
+    assertThat(dbTester.countRowsOfTable(BRANCHES_TABLE)).isEqualTo(8);
+    assertThat(dbTester.countSql("select count(*) from project_branches where branch_type = 'LONG' or branch_type = 'SHORT'")).isEqualTo(0);
+    assertThat(dbTester.select("select uuid from " + BRANCHES_TABLE + " where branch_type = 'BRANCH'")
+      .stream()
+      .map(e -> e.get("UUID"))
+      .collect(Collectors.toSet())).containsExactlyInAnyOrder(MAIN_BRANCH_1, MAIN_BRANCH_2, SLB_1, SLB_2, LLB_1, LLB_2);
+    assertThat(dbTester.select("select uuid from " + BRANCHES_TABLE + " where branch_type = 'PULL_REQUEST'")
+      .stream()
+      .map(e -> e.get("UUID"))
+      .collect(Collectors.toSet())).containsExactlyInAnyOrder(PR_1, PR_2);
+  }
+
+  private void insertBranch(String mainBranchUuid, String uuid, String type) {
+    dbTester.executeInsert(
+      BRANCHES_TABLE,
+      "UUID", uuid,
+      "PROJECT_UUID", mainBranchUuid,
+      "KEE", "pb-key-" + uuid,
+      "KEY_TYPE", "TSR",
+      "BRANCH_TYPE", type,
+      "MERGE_BRANCH_UUID", "mb-uuid-" + mainBranchUuid,
+      "MANUAL_BASELINE_ANALYSIS_UUID", null,
+      "CREATED_AT", System2.INSTANCE.now(),
+      "UPDATED_AT", System2.INSTANCE.now());
+  }
+
+  private void insertMainBranch(String mainBranchUuid) {
+    insertBranch(mainBranchUuid, mainBranchUuid, "LONG");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeInCeTasksTest/schema.sql
new file mode 100644 (file)
index 0000000..9297473
--- /dev/null
@@ -0,0 +1,9 @@
+CREATE TABLE "CE_TASK_CHARACTERISTICS" (
+  "UUID" VARCHAR(40) NOT NULL,
+  "TASK_UUID" VARCHAR(40) NOT NULL,
+  "KEE" VARCHAR(50) NOT NULL,
+  "TEXT_VALUE" VARCHAR(4000),
+
+  CONSTRAINT "PK_CE_TASK_CHARACTERISTICS" PRIMARY KEY ("UUID")
+);
+CREATE INDEX "CE_TASK_CHARACTERISTICS_TASK_UUID" ON "CE_TASK_CHARACTERISTICS" ("TASK_UUID");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/MigrateSlbsAndLlbsToCommonTypeTest/schema.sql
new file mode 100644 (file)
index 0000000..479c9fc
--- /dev/null
@@ -0,0 +1,15 @@
+CREATE TABLE "PROJECT_BRANCHES" (
+  "UUID" VARCHAR(50) NOT NULL,
+  "PROJECT_UUID" VARCHAR(50) NOT NULL,
+  "KEE" VARCHAR(255) NOT NULL,
+  "KEY_TYPE" VARCHAR(12) NOT NULL,
+  "BRANCH_TYPE" VARCHAR(12),
+  "MERGE_BRANCH_UUID" VARCHAR(50),
+  "PULL_REQUEST_BINARY" BLOB,
+  "MANUAL_BASELINE_ANALYSIS_UUID" VARCHAR(40),
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL,
+
+  CONSTRAINT "PK_PROJECT_BRANCHES" PRIMARY KEY ("UUID")
+);
+CREATE UNIQUE INDEX "PROJECT_BRANCHES_KEE_KEY_TYPE" ON "PROJECT_BRANCHES" ("PROJECT_UUID", "KEE", "KEY_TYPE");