diff options
4 files changed, 65 insertions, 4 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java index 79d821297c5..0c2f6f522fc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java @@ -51,13 +51,17 @@ public class ConvertProfileMeasuresMigration implements DatabaseMigration { DbSession session = db.openSession(false); try { int i = 0; + Date now = new Date(); Migration44Mapper mapper = session.getMapper(Migration44Mapper.class); for (ProfileMeasure profileMeasure : mapper.selectProfileMeasures()) { - int version = mapper.selectProfileVersion(profileMeasure.getSnapshotId()); + Integer version = mapper.selectProfileVersion(profileMeasure.getSnapshotId()); QProfileDto44 profile = mapper.selectProfileById(profileMeasure.getProfileId()); if (profile != null) { - Date date = (Date)ObjectUtils.defaultIfNull(mapper.selectProfileVersionDate(profileMeasure.getProfileId(), version), - new Date()); + Date date = now; + if (version != null) { + date = (Date)ObjectUtils.defaultIfNull( + mapper.selectProfileVersionDate(profileMeasure.getProfileId(), version), now); + } // see format of JSON in org.sonar.batch.rule.UsedQProfiles StringWriter writer = new StringWriter(); JsonWriter json = JsonWriter.of(writer); diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java index 0d783725707..0614f58a36d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java @@ -64,4 +64,29 @@ public class ConvertProfileMeasuresMigrationTest { connection.close(); } } + + /** + * http://jira.codehaus.org/browse/SONAR-5515 + * Version of quality profile was introduced in SQ 2.9. Migration must not fail + * when there are still some projects which last analysis was done with SQ <= 2.8. + */ + @Test + public void missing_profile_version() throws Exception { + db.prepareDbUnit(getClass(), "missing_profile_version.xml"); + + migration.execute(); + + Connection connection = db.openConnection(); + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("select text_value from project_measures where id=2"); + try { + rs.next(); + // pb of comparison of timezones..., so using startsWith instead of equals + assertThat(rs.getString(1)).startsWith("[{\"key\":\"java-sonar-way\",\"language\":\"java\",\"name\":\"Sonar way\",\"rulesUpdatedAt\":"); + } finally { + rs.close(); + stmt.close(); + connection.close(); + } + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/missing_profile_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/missing_profile_version.xml new file mode 100644 index 00000000000..4366b94339d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/missing_profile_version.xml @@ -0,0 +1,31 @@ +<dataset> + <metrics id="1" name="ncloc" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="[true]" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0" hidden="[false]" delete_historical_data="[null]" /> + <metrics id="2" name="profile" VAL_TYPE="DATA" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="[true]" worst_value="0" optimized_best_value="[true]" best_value="100" direction="1" hidden="[false]" delete_historical_data="[null]"/> + + <!-- last snapshot --> + <snapshots purge_status="[null]" id="1001" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + scope="PRJ" qualifier="TRK" created_at="2009-11-01 13:58:00.00" build_date="2009-11-01 13:58:00.00" version="[null]" path="" + status="P" islast="[true]" depth="0" + period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" + period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" /> + + + <!-- last measures : ncloc, profile but no profile_version --> + <project_measures id="1" VALUE="60" METRIC_ID="1" SNAPSHOT_ID="1001" alert_text="[null]" RULES_CATEGORY_ID="[null]" + RULE_ID="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]" + alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" + variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/> + + <project_measures id="2" VALUE="20" text_value="Sonar way" METRIC_ID="2" SNAPSHOT_ID="1001" alert_text="[null]" RULES_CATEGORY_ID="[null]" + RULE_ID="[null]" tendency="[null]" measure_date="[null]" project_id="[null]" + alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" + variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/> + + + <!-- quality profiles --> + <rules_profiles id="20" name="Sonar way" language="java" kee="java-sonar-way" parent_kee="[null]"/> + <rules_profiles id="21" name="Other way" language="java" kee="java-other-way" parent_kee="[null]" /> + +</dataset> diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v44/Migration44Mapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v44/Migration44Mapper.java index bd6d139f3b1..19075d6e9db 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v44/Migration44Mapper.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v44/Migration44Mapper.java @@ -32,7 +32,8 @@ public interface Migration44Mapper { // migration of measures "profile" and "profile_version" List<ProfileMeasure> selectProfileMeasures(); - int selectProfileVersion(long snapshotId); + @CheckForNull + Integer selectProfileVersion(long snapshotId); @CheckForNull Date selectProfileVersionDate(@Param("profileId") int profileId, @Param("profileVersion") int profileVersion); |