diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-06-15 11:45:29 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-06-15 12:18:58 +0200 |
commit | 6430646c2e4c83ce157223e7a61327ba2f11d5e5 (patch) | |
tree | c6d25d3e81fa9a7c2e20bcd6bcd46d4d78710041 | |
parent | f07b43ff19634878dc520304d9b36f3e89c2649b (diff) | |
download | sonarqube-6430646c2e4c83ce157223e7a61327ba2f11d5e5.tar.gz sonarqube-6430646c2e4c83ce157223e7a61327ba2f11d5e5.zip |
SONAR-7688 fix migration from 4.5
Column PROJECT_MEASURES.PROJECT_ID is NULL when
upgrading from 4.5
3 files changed, 51 insertions, 65 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasures.java b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasures.java index 7dd23cc8e07..4452552dc1c 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasures.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasures.java @@ -20,8 +20,6 @@ package org.sonar.db.version.v60; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; import org.sonar.db.Database; import org.sonar.db.version.BaseDataChange; import org.sonar.db.version.MassUpdate; @@ -36,40 +34,17 @@ public class PopulateComponentUuidOfMeasures extends BaseDataChange { @Override public void execute(Context context) throws SQLException { - Map<Long, String> componentUuidById = buildComponentUuidMap(context); - if (componentUuidById.isEmpty()) { - return; - } - - populateUuidColumns(context, componentUuidById); - } - - private Map<Long, String> buildComponentUuidMap(Context context) throws SQLException { - Map<Long, String> componentUuidById = new HashMap<>(); - context.prepareSelect("select distinct p.id, p.uuid from projects p" + - " join project_measures pm on pm.project_id = p.id and pm.component_uuid is null") - .scroll(row -> componentUuidById.put(row.getLong(1), row.getString(2))); - return componentUuidById; - } - - private void populateUuidColumns(Context context, Map<Long, String> componentUuidById) throws SQLException { MassUpdate massUpdate = context.prepareMassUpdate(); - massUpdate.select("SELECT pm.id, pm.project_id from project_measures pm where pm.component_uuid is null"); + massUpdate.select("select pm.id, s.component_uuid from project_measures pm inner join snapshots s on s.id=pm.snapshot_id where pm.component_uuid is null"); massUpdate.update("UPDATE project_measures SET component_uuid=? WHERE id=?"); massUpdate.rowPluralName("measures"); - massUpdate.execute((row, update) -> this.handle(componentUuidById, row, update)); + massUpdate.execute(this::handle); } - public boolean handle(Map<Long, String> componentUuidById, Select.Row row, SqlStatement update) throws SQLException { + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { long id = row.getLong(1); - long componentId = row.getLong(2); - - String componentUuid = componentUuidById.get(componentId); - - if (componentUuid == null) { - return false; - } + String componentUuid = row.getString(2); update.setString(1, componentUuid); update.setLong(2, id); diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest.java index 5a95ac55957..2eb47532e3e 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest.java @@ -44,15 +44,15 @@ public class PopulateComponentUuidOfMeasuresTest { underTest.execute(); assertThat(db.countRowsOfTable("project_measures")).isEqualTo(0); - assertThat(db.countRowsOfTable("projects")).isEqualTo(0); + assertThat(db.countRowsOfTable("snapshots")).isEqualTo(0); } @Test - public void migration_updates_component_uuid_with_values_from_table_projects_when_they_exist() throws SQLException { - String uuid1 = insertComponent(40); - String uuid2 = insertComponent(50); - String uuid3 = insertComponent(60); - String uuid4 = insertComponent(70); + public void migration_updates_component_uuid_with_values_from_table_snapshots_when_they_exist() throws SQLException { + String uuid1 = insertSnapshot(40); + String uuid2 = insertSnapshot(50); + String uuid3 = insertSnapshot(60); + String uuid4 = insertSnapshot(70); insertMeasure(1, 40); insertMeasure(2, 60); @@ -70,8 +70,8 @@ public class PopulateComponentUuidOfMeasuresTest { @Test public void migration_is_reentrant() throws SQLException { - String uuid1 = insertComponent(40); - String uuid2 = insertComponent(50); + String uuid1 = insertSnapshot(40); + String uuid2 = insertSnapshot(50); insertMeasure(1, 40); underTest.execute(); @@ -82,31 +82,32 @@ public class PopulateComponentUuidOfMeasuresTest { } - private void verifyMeasure(long id, long componentId, @Nullable String componentUuid) { - List<Map<String, Object>> rows = db.select("select PROJECT_ID, COMPONENT_UUID from project_measures where ID=" + id); + private void verifyMeasure(long id, long snapshotId, @Nullable String componentUuid) { + List<Map<String, Object>> rows = db.select("select SNAPSHOT_ID, COMPONENT_UUID from project_measures where ID=" + id); assertThat(rows).hasSize(1); Map<String, Object> row = rows.get(0); - assertThat(row.get("PROJECT_ID")).isEqualTo(componentId); + assertThat(row.get("SNAPSHOT_ID")).isEqualTo(snapshotId); assertThat(row.get("COMPONENT_UUID")).isEqualTo(componentUuid); } - private String insertComponent(long id) { + private String insertSnapshot(long id) { String uuid = "uuid_" + id; db.executeInsert( - "projects", - "ID", valueOf(id), - "UUID", uuid); + "snapshots", + "id", valueOf(id), + "component_uuid", uuid, + "root_component_uuid", valueOf(id + 100)); return uuid; } - private void insertMeasure(long id, long componentId) { + private void insertMeasure(long id, long snapshotId) { db.executeInsert( "project_measures", "ID", valueOf(id), "METRIC_ID", valueOf(id + 10), - "SNAPSHOT_ID", valueOf(id + 100), + "SNAPSHOT_ID", valueOf(snapshotId), "VALUE", valueOf(id + 1000), - "PROJECT_ID", valueOf(componentId)); + "PROJECT_ID", valueOf(snapshotId + 1000)); } } diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest/in_progress_measures_with_projects.sql b/sonar-db/src/test/resources/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest/in_progress_measures_with_projects.sql index c21cb13f205..94d9362daaa 100644 --- a/sonar-db/src/test/resources/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest/in_progress_measures_with_projects.sql +++ b/sonar-db/src/test/resources/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest/in_progress_measures_with_projects.sql @@ -31,25 +31,35 @@ CREATE INDEX "MEASURES_SID_METRIC" ON "PROJECT_MEASURES" ("SNAPSHOT_ID", "METRIC CREATE INDEX "MEASURES_PERSON" ON "PROJECT_MEASURES" ("PERSON_ID"); -CREATE TABLE "PROJECTS" ( +CREATE TABLE "SNAPSHOTS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400), - "ROOT_ID" INTEGER, - "UUID" VARCHAR(50), - "PROJECT_UUID" VARCHAR(50), - "MODULE_UUID" VARCHAR(50), - "MODULE_UUID_PATH" VARCHAR(4000), - "NAME" VARCHAR(2000), - "DESCRIPTION" VARCHAR(2000), - "ENABLED" BOOLEAN NOT NULL DEFAULT TRUE, + "CREATED_AT" BIGINT, + "BUILD_DATE" BIGINT, + "COMPONENT_UUID" VARCHAR(50) NOT NULL, + "PARENT_SNAPSHOT_ID" INTEGER, + "STATUS" VARCHAR(4) NOT NULL DEFAULT 'U', + "PURGE_STATUS" INTEGER, + "ISLAST" BOOLEAN NOT NULL DEFAULT FALSE, "SCOPE" VARCHAR(3), "QUALIFIER" VARCHAR(10), - "DEPRECATED_KEE" VARCHAR(400), - "PATH" VARCHAR(2000), - "LANGUAGE" VARCHAR(20), - "COPY_RESOURCE_ID" INTEGER, - "LONG_NAME" VARCHAR(2000), - "PERSON_ID" INTEGER, - "CREATED_AT" TIMESTAMP, - "AUTHORIZATION_UPDATED_AT" BIGINT + "ROOT_SNAPSHOT_ID" INTEGER, + "VERSION" VARCHAR(500), + "PATH" VARCHAR(500), + "DEPTH" INTEGER, + "ROOT_COMPONENT_UUID" VARCHAR(50) NOT NULL, + "PERIOD1_MODE" VARCHAR(100), + "PERIOD1_PARAM" VARCHAR(100), + "PERIOD1_DATE" BIGINT, + "PERIOD2_MODE" VARCHAR(100), + "PERIOD2_PARAM" VARCHAR(100), + "PERIOD2_DATE" BIGINT, + "PERIOD3_MODE" VARCHAR(100), + "PERIOD3_PARAM" VARCHAR(100), + "PERIOD3_DATE" BIGINT, + "PERIOD4_MODE" VARCHAR(100), + "PERIOD4_PARAM" VARCHAR(100), + "PERIOD4_DATE" BIGINT, + "PERIOD5_MODE" VARCHAR(100), + "PERIOD5_PARAM" VARCHAR(100), + "PERIOD5_DATE" BIGINT ); |