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;
@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);
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);
@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();
}
- 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));
}
}
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
);