]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Migration populating main branch now set 'master' as name
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Aug 2017 13:08:48 +0000 (15:08 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:55 +0000 (11:34 +0200)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranches.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranchesTest.java

index 76d31d264f2fa9309cb923c9fad78db528eedae1..9d66de5abdca307411527e526a72e59b355cc4b0 100644 (file)
@@ -25,13 +25,9 @@ import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
 import org.sonar.server.platform.db.migration.step.MassUpdate;
 
-import static org.apache.commons.lang.StringUtils.repeat;
-
 public class PopulateMainProjectBranches extends DataChange {
-  private static final int KEE_MAX_LENGTH = 255;
-  static final String NULL_KEY = repeat("_", KEE_MAX_LENGTH);
-  private static final String INSERT_MAIN_PROJECT_BRANCHES = "INSERT INTO project_branches (uuid, project_uuid, kee_type, kee, branch_type, "
-    + "merge_branch_uuid, pull_request_title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
+
+  private static final String MAIN_BRANCH_NAME = "master";
 
   private final System2 system2;
 
@@ -47,14 +43,15 @@ public class PopulateMainProjectBranches extends DataChange {
     massUpdate.select("SELECT uuid FROM projects p "
       + "WHERE p.scope='PRJ' AND p.qualifier='TRK' AND p.main_branch_project_uuid IS NULL "
       + "AND NOT EXISTS (SELECT uuid FROM project_branches b WHERE b.uuid = p.uuid)");
-    massUpdate.update(INSERT_MAIN_PROJECT_BRANCHES);
+    massUpdate.update("INSERT INTO project_branches (uuid, project_uuid, kee_type, kee, branch_type, "
+      + "merge_branch_uuid, pull_request_title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
     massUpdate.rowPluralName("projects");
     massUpdate.execute((row, update) -> {
       String uuid = row.getString(1);
       update.setString(1, uuid);
       update.setString(2, uuid);
       update.setString(3, "BRANCH");
-      update.setString(4, NULL_KEY);
+      update.setString(4, MAIN_BRANCH_NAME);
       update.setString(5, "LONG");
       update.setString(6, null);
       update.setString(7, null);
index e8ee9bba79fa379989467586ae8ce82be046f85c..29433a7dc19d18095ab49660c8da7d6e1b7cda95 100644 (file)
 package org.sonar.server.platform.db.migration.version.v66;
 
 import java.sql.SQLException;
-import java.util.Map;
+import java.util.stream.Collectors;
 import javax.annotation.Nullable;
+import org.assertj.core.groups.Tuple;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
-import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.core.util.Uuids;
 import org.sonar.db.CoreDbTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
 
 public class PopulateMainProjectBranchesTest {
+
+  private final static long PAST = 10_000_000_000L;
+  private final static long NOW = 50_000_000_000L;
+
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
   @Rule
   public CoreDbTester db = CoreDbTester.createForSchema(PopulateMainProjectBranchesTest.class, "initial.sql");
 
-  private System2 system2 = new AlwaysIncreasingSystem2();
+  private System2 system2 = new TestSystem2().setNow(NOW);
   private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
 
   @Test
-  public void populate_with_existing_project() throws SQLException {
-    insertProject("project1");
-    insertProject("project2", "project1", "PRJ");
-    insertProject("project3", null, "XXX");
+  public void migrate() throws SQLException {
+    String project = insertProject();
 
     underTest.execute();
 
-    assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
+    assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", NOW, NOW));
+  }
+
+  @Test
+  public void does_nothing_on_non_projects() throws SQLException {
+    insertProject(null, "BRC");
+    insertProject(null, "VW");
 
-    Map<String, Object> row = db.selectFirst("select * from project_branches");
-    assertThat(row.get("UUID")).isEqualTo("project1");
-    assertThat(row.get("PROJECT_UUID")).isEqualTo("project1");
-    assertThat(row.get("KEE")).isEqualTo(PopulateMainProjectBranches.NULL_KEY);
-    assertThat(row.get("BRANCH_TYPE")).isEqualTo("LONG");
+    underTest.execute();
 
+    assertThat(db.countRowsOfTable("project_branches")).isZero();
   }
 
   @Test
-  public void do_nothing_if_no_project() throws SQLException {
-    insertProject("project2", "project1", "PRJ");
-    insertProject("project3", null, "XXX");
-
+  public void does_nothing_on_empty_table() throws SQLException {
     underTest.execute();
-    assertThat(db.countRowsOfTable("project_branches")).isEqualTo(0);
+
+    assertThat(db.countRowsOfTable("project_branches")).isZero();
   }
 
   @Test
-  public void do_not_populate_if_already_exists() throws SQLException {
-    insertProject("project1");
-    insertBranch("project1");
+  public void does_nothing_if_already_migrated() throws SQLException {
+    String project = insertProject();
+    insertMainBranch(project);
 
-    assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
     underTest.execute();
-    assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
+
+    assertProjectBranches(tuple("master", project, project, "BRANCH", "LONG", PAST, PAST));
+  }
+
+  private void assertProjectBranches(Tuple... expectedTuples) {
+    assertThat(db.select("SELECT KEE, UUID, PROJECT_UUID, KEE_TYPE, BRANCH_TYPE, CREATED_AT, UPDATED_AT FROM PROJECT_BRANCHES")
+      .stream()
+      .map(map -> new Tuple(map.get("KEE"), map.get("UUID"), map.get("PROJECT_UUID"), map.get("KEE_TYPE"), map.get("BRANCH_TYPE"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
+      .collect(Collectors.toList()))
+        .containsExactlyInAnyOrder(expectedTuples);
   }
 
-  private void insertProject(String uuid) {
-    insertProject(uuid, null, "PRJ");
+  private String insertProject() {
+    return insertProject(null, "PRJ");
   }
 
-  private void insertProject(String uuid, @Nullable String mainBranchUuid, String scope) {
+  private String insertProject(@Nullable String mainBranchUuid, String scope) {
+    String uuid = Uuids.createFast();
     db.executeInsert("PROJECTS",
       "ORGANIZATION_UUID", "default-org",
       "KEE", uuid + "-key",
@@ -94,16 +109,17 @@ public class PopulateMainProjectBranchesTest {
       "PRIVATE", "true",
       "qualifier", "TRK",
       "scope", scope);
+    return uuid;
   }
 
-  private void insertBranch(String uuid) {
+  private void insertMainBranch(String uuid) {
     db.executeInsert("PROJECT_BRANCHES",
       "uuid", uuid,
       "project_uuid", uuid,
       "kee_type", "BRANCH",
-      "kee", PopulateMainProjectBranches.NULL_KEY,
+      "kee", "master",
       "branch_type", "LONG",
-      "created_at", 0,
-      "updated_at", 0);
+      "created_at", PAST,
+      "updated_at", PAST);
   }
 }