From: Julien Lancelot Date: Wed, 30 Aug 2017 13:08:48 +0000 (+0200) Subject: SONAR-9616 Migration populating main branch now set 'master' as name X-Git-Tag: 6.6-RC1~380^2~31 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8d5a5ef07e5eca76693ae1915d2dfee789ab9cef;p=sonarqube.git SONAR-9616 Migration populating main branch now set 'master' as name --- diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranches.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranches.java index 76d31d264f2..9d66de5abdc 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranches.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranches.java @@ -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); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranchesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranchesTest.java index e8ee9bba79f..29433a7dc19 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranchesTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateMainProjectBranchesTest.java @@ -20,69 +20,84 @@ 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 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); } }