// duplication_data
"m13.text_value, " +
- "m13.measure_data " +
+ "m13.measure_data, " +
+
+ // to detect multiple rows in snapshot_sources for the same snapshot
+ "s.id " +
"FROM snapshots s " +
"JOIN snapshot_sources ss " +
"f.enabled = ? " +
"AND f.scope = 'FIL' " +
"AND p.scope = 'PRJ' AND p.qualifier = 'TRK' " +
- "AND fs.file_uuid IS NULL";
+ "AND fs.file_uuid IS NULL " +
+ "ORDER BY s.id, ss.id desc";
public FeedFileSources(Database db, System2 system) {
super(db);
this.system = system;
}
-
+
private static final class FileSourceBuilder implements MassUpdate.Handler {
private final long now;
+ private long previousSnapshotId = -1;
public FileSourceBuilder(System2 system) {
now = system.now();
byte[] longOverallCovCond = row.getNullableBytes(28);
byte[] shortDuplicationData = row.getNullableBytes(29);
byte[] longDuplicationData = row.getNullableBytes(30);
+ long snapshotId = row.getLong(31);
+
+ if (snapshotId == previousSnapshotId) {
+ return false;
+ }
+ this.previousSnapshotId = snapshotId;
String[] sourceData = new FileSourceDto(source,
ofNullableBytes(shortRevisions, longRevisions),
ofNullableBytes(shortOverallHits, longOverallHits),
ofNullableBytes(shortOverallCond, longOverallCond),
ofNullableBytes(shortOverallCovCond, longOverallCovCond),
- ofNullableBytes(shortDuplicationData, longDuplicationData)
- ).getSourceData();
+ ofNullableBytes(shortDuplicationData, longDuplicationData)).getSourceData();
update.setString(1, projectUuid)
.setString(2, fileUuid)
return true;
}
-
+
private static String ofNullableBytes(@Nullable byte[] shortBytes, @Nullable byte[] longBytes) {
byte[] result;
if (shortBytes == null) {
migration.execute();
}
+ @Test
+ public void migrate_ignores_duplicate_multiple_rows_in_snapshot_source_for_the_same_snapshot() throws Exception {
+ db.prepareDbUnit(getClass(), "before.xml");
+ db.executeUpdateSql("insert into snapshot_sources " +
+ "(snapshot_id, data, updated_at) " +
+ "values " +
+ "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')");
+
+ db.executeUpdateSql("insert into snapshot_sources " +
+ "(snapshot_id, data, updated_at) " +
+ "values " +
+ "(6, 'class Bar {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')");
+
+ migration.execute();
+
+ List<Map<String, Object>> results = getFileSources();
+ assertThat(results).hasSize(2);
+
+ // the lastest inserted row is the one taken into account
+ assertThat(results.get(1).get("data")).isEqualTo(",,,,,,,,,,,,,,,class Bar {\r\n,,,,,,,,,,,,,,, // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n");
+ }
+
@Test
public void migrate_sources_with_no_scm_no_coverage() throws Exception {
db.prepareDbUnit(getClass(), "before.xml");
migration.execute();
- List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " +
- "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources");
+ List<Map<String, Object>> results = getFileSources();
assertThat(results).hasSize(2);
assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject");
migration.execute();
- List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " +
- "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources");
+ List<Map<String, Object>> results = getFileSources();
assertThat(results).hasSize(2);
assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject");
// db.assertDbUnit(getClass(), "after-with-invalid-duplication.xml", "file_sources");
- List<Map<String, Object>> results = db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " +
- "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources");
+ List<Map<String, Object>> results = getFileSources();
assertThat(results).hasSize(2);
assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject");
assertThat(results.get(1).get("createdAt")).isEqualTo(NOW);
}
+ private List<Map<String, Object>> getFileSources() {
+ return db.select("select project_uuid as \"projectUuid\", file_uuid as \"fileUuid\", created_at as \"createdAt\", " +
+ "updated_at as \"updatedAt\", data as \"data\", data as \"data\", line_hashes as \"lineHashes\", data_hash as \"dataHash\" from file_sources");
+ }
+
private String formatLongDate(long dateInMs) {
return DateUtils.formatDateTime(DateUtils.longToDate(dateInMs));
}