Skip to content

Commit

Permalink
SONAR-7786 merge PROJECTS table clean migrations and do them earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Jul 11, 2016
1 parent a8be32d commit 50d0c1c
Show file tree
Hide file tree
Showing 27 changed files with 92 additions and 118 deletions.
Expand Up @@ -21,9 +21,10 @@
#
# SonarQube 6.0
#
class FixProjectUuidOfDeveloperProjects < ActiveRecord::Migration
class CleanSomeDataInTableProjects < ActiveRecord::Migration

def self.up
execute_java_migration('org.sonar.db.version.v60.FixProjectUuidOfDeveloperProjects')
execute_java_migration('org.sonar.db.version.v60.CleanUsurperRootComponents')
end
end

This file was deleted.

Expand Up @@ -73,7 +73,7 @@ private static void cleanUsurperRootComponents(Context context) throws SQLExcept
" p.scope = 'PRJ'" +
" and p.qualifier in ('TRK', 'VW', 'DEV')" +
" )");
massUpdate.update("delete from duplications_index where component_uuid=?");
massUpdate.update("delete from duplications_index where snapshot_id in (select id from snapshots where component_uuid=?)");
massUpdate.update("delete from project_measures where component_uuid=?");
massUpdate.update("delete from ce_activity where component_uuid=?");
massUpdate.update("delete from events where component_uuid=?");
Expand Down Expand Up @@ -122,7 +122,7 @@ private static void cleanUsurperRootComponents(Context context) throws SQLExcept
private void cleanSnapshotWithIncorrectRoot(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("select" +
" sn.uuid,sn.id" +
" sn.id" +
" from " +
" projects p, snapshots sn" +
" where" +
Expand All @@ -131,22 +131,19 @@ private void cleanSnapshotWithIncorrectRoot(Context context) throws SQLException
" p.scope = 'PRJ'" +
" and p.qualifier in ('TRK', 'VW', 'DEV')" +
" )");
massUpdate.update("DELETE from duplications_index WHERE analysis_uuid=?");
massUpdate.update("DELETE from project_measures WHERE analysis_uuid=?");
massUpdate.update("DELETE from ce_activity WHERE analysis_uuid=?");
massUpdate.update("DELETE from events WHERE analysis_uuid=?");
massUpdate.update("DELETE from ce_activity WHERE snapshot_id=?");
massUpdate.update("DELETE from events WHERE snapshot_id=?");
massUpdate.update("DELETE from project_measures WHERE snapshot_id=?");
massUpdate.update("DELETE from duplications_index WHERE project_snapshot_id=?");
massUpdate.update("DELETE from snapshots WHERE id=?");
massUpdate.rowPluralName("snapshots with incorrect root");
massUpdate.execute((row, update, updateIndex) -> {
String analysisUuid = row.getString(1);
long snapshotId = row.getLong(2);
long snapshotId = row.getLong(1);
switch (updateIndex) {
case 0:
case 1:
case 2:
case 3:
update.setString(1, analysisUuid);
return true;
case 4:
update.setLong(1, snapshotId);
return true;
Expand Down
Expand Up @@ -35,15 +35,18 @@ public FixProjectUuidOfDeveloperProjects(Database db) {
@Override
public void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("select distinct developer_uuid from projects where qualifier = 'DEV_PRJ' and project_uuid != developer_uuid");
massUpdate.update("update projects set project_uuid = developer_uuid where developer_uuid = ? and qualifier = 'DEV_PRJ' and project_uuid != developer_uuid");
massUpdate.rowPluralName("developers in project");
massUpdate.select("select distinct p.person_id,d.uuid from projects p, projects d where p.qualifier = 'DEV_PRJ' and p.project_uuid != d.uuid and d.id = p.person_id");
massUpdate.update("update projects set project_uuid = ? where person_id = ? and qualifier = 'DEV_PRJ' and project_uuid != ?");
massUpdate.rowPluralName("developers with incorrect project_uuid");
massUpdate.execute(FixProjectUuidOfDeveloperProjects::handleComponent);
}

private static boolean handleComponent(Select.Row row, SqlStatement update) throws SQLException {
String developerUuid = row.getString(1);
long personId = row.getLong(1);
String developerUuid = row.getString(2);
update.setString(1, developerUuid);
update.setLong(2, personId);
update.setString(3, developerUuid);

return true;
}
Expand Down
Expand Up @@ -62,7 +62,7 @@ public void execute_fixes_scope_and_qualifier_of_snapshot_inconsistent_with_comp
insertComponent("sc3", "qu3"),
insertComponent("sc4", "qu4")
};
String[] snapshotUuids = {
Long[] snapshotIds = {
insertSnapshot(componentUuids[0], "sc1", "qu1"),
insertSnapshot(componentUuids[1], "sc2", "quW"),
insertSnapshot(componentUuids[2], "scX", "qu3"),
Expand All @@ -71,10 +71,10 @@ public void execute_fixes_scope_and_qualifier_of_snapshot_inconsistent_with_comp

underTest.execute();

assertSnapshot(snapshotUuids[0], "sc1", "qu1");
assertSnapshot(snapshotUuids[1], "sc2", "qu2");
assertSnapshot(snapshotUuids[2], "sc3", "qu3");
assertSnapshot(snapshotUuids[3], "sc4", "qu4");
assertSnapshot(snapshotIds[0], "sc1", "qu1");
assertSnapshot(snapshotIds[1], "sc2", "qu2");
assertSnapshot(snapshotIds[2], "sc3", "qu3");
assertSnapshot(snapshotIds[3], "sc4", "qu4");
}

@Test
Expand All @@ -101,13 +101,13 @@ public void executes_deletes_data_in_all_children_tables_of_component_for_usurpe
long usurperId = 12L;
String usurperUuid = "usurper_uuid";
insertComponent(Scopes.PROJECT, Qualifiers.MODULE, usurperId, usurperUuid, usurperUuid);
insertDuplicationsIndex(usurperUuid);
insertProjectMeasures(usurperUuid, dontCare());
insertCeActivity(usurperUuid, dontCare());
insertEvent(usurperUuid, dontCare());
Long snapshotId = insertSnapshot(usurperUuid, usurperUuid);
insertDuplicationsIndex(snapshotId);
insertProjectMeasures(usurperUuid, dontCareLong());
insertCeActivity(usurperUuid, dontCareLong());
insertEvent(usurperUuid, dontCareLong());
insertSnapshot(usurperUuid, dontCare());
insertSnapshot(dontCare(), usurperUuid);
insertSnapshot(usurperUuid, usurperUuid);
insertProjectLinks(usurperUuid);
insertIssue(usurperUuid, null);
insertIssue(null, usurperUuid);
Expand Down Expand Up @@ -142,7 +142,7 @@ public void execute_deletes_snapshots_which_root_is_not_root() throws SQLExcepti
insertComponent(Scopes.PROJECT, "DEV"),
insertComponent(Scopes.PROJECT, "DEV_PRJ"),
};
String[] snapshotUuids = {
Long[] snapshotIds = {
insertSnapshot(dontCare(), componentUuids[0]),
insertSnapshot(dontCare(), componentUuids[1]),
insertSnapshot(dontCare(), componentUuids[2]),
Expand All @@ -156,16 +156,16 @@ public void execute_deletes_snapshots_which_root_is_not_root() throws SQLExcepti

underTest.execute();

assertUuidsInTableProjects("snapshots", snapshotUuids[0], snapshotUuids[4], snapshotUuids[7]);
assertIdsInTableProjects("snapshots", snapshotIds[0], snapshotIds[4], snapshotIds[7]);
}

@Test
public void execute_deletes_children_tables_of_snapshots_when_root_of_snapshot_is_not_root() throws SQLException {
String componentUuid = insertComponent(Scopes.FILE, Scopes.FILE);
String snapshotUuid = insertSnapshot(dontCare(), componentUuid);
insertProjectMeasures(dontCare(), snapshotUuid);
insertCeActivity(componentUuid, snapshotUuid);
insertEvent(componentUuid, snapshotUuid);
Long snapshotId = insertSnapshot(dontCare(), componentUuid);
insertProjectMeasures(dontCare(), snapshotId);
insertCeActivity(componentUuid, snapshotId);
insertEvent(componentUuid, snapshotId);

underTest.execute();

Expand All @@ -174,34 +174,34 @@ public void execute_deletes_children_tables_of_snapshots_when_root_of_snapshot_i
.forEach(s -> assertThat(db.countRowsOfTable(s)).describedAs("table " + s).isEqualTo(0));
}

private void insertDuplicationsIndex(String componentUuid) {
private void insertDuplicationsIndex(Long snapshotId) {
db.executeInsert(
"duplications_index",
"COMPONENT_UUID", componentUuid,
"ANALYSIS_UUID", dontCare(),
"PROJECT_SNAPSHOT_ID", valueOf(dontCareLong()),
"SNAPSHOT_ID", valueOf(snapshotId),
"HASH", dontCare(),
"INDEX_IN_FILE", valueOf(0),
"START_LINE", valueOf(0),
"END_LINE", valueOf(0));
db.commit();
}

private void insertProjectMeasures(String componentUuid, String analysisUuid) {
private void insertProjectMeasures(String componentUuid, Long snapshotId) {
db.executeInsert(
"project_measures",
"METRIC_ID", valueOf(123L),
"COMPONENT_UUID", componentUuid,
"ANALYSIS_UUID", analysisUuid);
"SNAPSHOT_ID", valueOf(snapshotId));
db.commit();
}

private void insertCeActivity(String componentUuid, String analysisUuid) {
private void insertCeActivity(String componentUuid, Long snapshotId) {
db.executeInsert(
"ce_activity",
"UUID", dontCare(),
"TASK_TYPE", dontCare(),
"COMPONENT_UUID", componentUuid,
"ANALYSIS_UUID", analysisUuid,
"SNAPSHOT_ID", valueOf(snapshotId),
"STATUS", dontCare(),
"IS_LAST", "true",
"IS_LAST_KEY", dontCare(),
Expand All @@ -211,25 +211,25 @@ private void insertCeActivity(String componentUuid, String analysisUuid) {
db.commit();
}

private void insertEvent(String componentUuid, String analysisUuid) {
private void insertEvent(String componentUuid, Long snapshotId) {
db.executeInsert(
"events",
"ANALYSIS_UUID", analysisUuid,
"SNAPSHOT_ID", valueOf(snapshotId),
"COMPONENT_UUID", componentUuid,
"CREATED_AT", valueOf(122L),
"EVENT_DATE", valueOf(123L));
db.commit();
}

private String insertSnapshot(String componentUuid, String rootComponentUuid) {
String uuid = "uuid_" + idGenerator++;
private Long insertSnapshot(String componentUuid, String rootComponentUuid) {
Long id = idGenerator++;
db.executeInsert(
"snapshots",
"UUID", uuid,
"ID", valueOf(id),
"COMPONENT_UUID", componentUuid,
"ROOT_COMPONENT_UUID", rootComponentUuid);
db.commit();
return uuid;
return id;
}

private void insertProjectLinks(String componentUuid) {
Expand Down Expand Up @@ -311,39 +311,42 @@ private String insertComponent(String scope, String qualifier, long id, String u
"projects",
"ID", valueOf(id),
"UUID", uuid,
"ROOT_UUID", dontCare(),
"PROJECT_UUID", projectUuid,
"UUID_PATH", dontCare(),
"SCOPE", scope,
"QUALIFIER", qualifier);
db.commit();
return uuid;
}

private String insertSnapshot(String componentUuid, String scope, String qualifier) {
private Long insertSnapshot(String componentUuid, String scope, String qualifier) {
long id = idGenerator++;
String uuid = "uuid_" + id;

db.executeInsert(
"snapshots",
"ID", valueOf(id),
"UUID", uuid,
"id", valueOf(id),
"component_uuid", componentUuid,
"root_component_uuid", dontCare(),
"scope", scope,
"qualifier", qualifier);
db.commit();
return uuid;
return id;
}

private void assertSnapshot(String snapshotUuid, String scope, String qualifier) {
List<Map<String, Object>> rows = db.select("select SCOPE, QUALIFIER from snapshots where UUID='" + snapshotUuid + "'");
private void assertSnapshot(Long snapshotId, String scope, String qualifier) {
List<Map<String, Object>> rows = db.select("select SCOPE, QUALIFIER from snapshots where ID=" + snapshotId);
assertThat(rows).hasSize(1);
Map<String, Object> row = rows.get(0);
assertThat(row.get("SCOPE")).isEqualTo(scope);
assertThat(row.get("QUALIFIER")).isEqualTo(qualifier);
}

private void assertIdsInTableProjects(String tableName, Long... expected) {
assertThat(db.select("select id from " + tableName)
.stream()
.map(stringObjectMap -> (Long) stringObjectMap.entrySet().iterator().next().getValue()))
.containsOnly(expected);
}

private void assertUuidsInTableProjects(String tableName, String... expected) {
assertThat(db.select("select uuid from " + tableName)
.stream()
Expand All @@ -356,4 +359,8 @@ private void assertUuidsInTableProjects(String tableName, String... expected) {
private String dontCare() {
return "DC_" + dontCareGenerator++;
}

private Long dontCareLong() {
return dontCareGenerator++;
}
}
Expand Up @@ -27,6 +27,7 @@
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;

import static java.lang.String.valueOf;
import static org.assertj.core.api.Assertions.assertThat;

public class FixProjectUuidOfDeveloperProjectsTest {
Expand All @@ -40,7 +41,7 @@ public class FixProjectUuidOfDeveloperProjectsTest {

@Rule
public DbTester db = DbTester.createForSchema(System2.INSTANCE, FixProjectUuidOfDeveloperProjectsTest.class,
"in_progress_projects.sql");
"projects_5.6.sql");

private FixProjectUuidOfDeveloperProjects underTest = new FixProjectUuidOfDeveloperProjects(db.database());

Expand Down Expand Up @@ -84,9 +85,9 @@ private void insertComponents() {
insert(PROJECT_UUID, "TRK", null, PROJECT_UUID);
insert(FILE_UUID, "FIL", null, PROJECT_UUID);
// developer
insert(DEVELOPER_UUID, "DEV", DEVELOPER_UUID, DEVELOPER_UUID);
insert(DEV1_IN_PROJECT_UUID, "DEV_PRJ", DEVELOPER_UUID, /* not correct */PROJECT_UUID);
insert(DEV2_IN_PROJECT_UUID, "DEV_PRJ", DEVELOPER_UUID, /* not correct */PROJECT_UUID);
Long personId = insert(DEVELOPER_UUID, "DEV", null, DEVELOPER_UUID);
insert(DEV1_IN_PROJECT_UUID, "DEV_PRJ", personId, /* not correct */PROJECT_UUID);
insert(DEV2_IN_PROJECT_UUID, "DEV_PRJ", personId, /* not correct */PROJECT_UUID);
db.commit();
}

Expand All @@ -95,14 +96,17 @@ private void verifyProjectUuid(String uuid, @Nullable String expectedProjectUuid
assertThat(rows.get("projectUuid")).isEqualTo(expectedProjectUuid);
}

private String insert(String uuid, String qualifier, @Nullable String developerUuid, String projectUuid) {
private Long insert(String uuid, String qualifier, @Nullable Long personId, String projectUuid) {
db.executeInsert(
TABLE_PROJECTS,
"UUID", uuid,
"DEVELOPER_UUID", developerUuid,
"PERSON_ID", personId == null ? null : valueOf(personId),
"PROJECT_UUID", projectUuid,
"ROOT_UUID", "NOT_USED",
"QUALIFIER", qualifier);
return uuid;
db.commit();
return db.select("select ID from projects where UUID='" + uuid + "'").stream()
.findFirst()
.map(f -> (Long) f.get("ID"))
.orElseThrow(() -> new IllegalStateException("NO ID??"));
}
}

0 comments on commit 50d0c1c

Please sign in to comment.