]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7688 fix migration from 4.5
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 15 Jun 2016 09:45:29 +0000 (11:45 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 15 Jun 2016 10:18:58 +0000 (12:18 +0200)
Column PROJECT_MEASURES.PROJECT_ID is NULL when
upgrading from 4.5

sonar-db/src/main/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasures.java
sonar-db/src/test/java/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest.java
sonar-db/src/test/resources/org/sonar/db/version/v60/PopulateComponentUuidOfMeasuresTest/in_progress_measures_with_projects.sql

index 7dd23cc8e072e1d6a5623589f17ff5ec359aa73c..4452552dc1c4a8c716ac70b2aed578be82f3d2ab 100644 (file)
@@ -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);
 
index 5a95ac559575407e91a732a9f8695c211d3ccc28..2eb47532e3e2ad4dc9d24a84f0a9cfc906865ab7 100644 (file)
@@ -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));
 
   }
 }
index c21cb13f20556b85ba96aeee3beaa74863caa4fa..94d9362daaafbca1a16e9122799007d2c04b3052 100644 (file)
@@ -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
 );