import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.dialect.MySql;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.sql.Connection;
public static interface InputLoader<S> {
String selectSql();
+ @CheckForNull
S load(ResultSet rs) throws SQLException;
}
int cursor = 0;
while (rs.next()) {
- if (converter.convert(inputLoader.load(rs), writeStatement)) {
+ S row = inputLoader.load(rs);
+ if (row == null) {
+ continue;
+ }
+ if (converter.convert(row, writeStatement)) {
writeStatement.addBatch();
writeStatement.clearParameters();
cursor++;
new MassUpdater.InputLoader<Row>() {
@Override
public String selectSql() {
- return "SELECT i.id, i.technical_debt FROM issues i WHERE i.technical_debt IS NOT NULL";
+ return "SELECT i.id, i.technical_debt FROM issues i";
}
@Override
public Row load(ResultSet rs) throws SQLException {
- Row row = new Row();
- row.id = SqlUtil.getLong(rs, 1);
- row.debt = SqlUtil.getLong(rs, 2);
- return row;
+ Long debt = SqlUtil.getLong(rs, 2);
+ if (!rs.wasNull() && debt != null) {
+ // See https://jira.codehaus.org/browse/SONAR-5394
+ // The SQL request should not set the filter on technical_debt is not null. There's no index
+ // on this column, so filtering is done programmatically.
+ Row row = new Row();
+ row.id = SqlUtil.getLong(rs, 1);
+ row.debt = debt;
+ return row;
+ }
+ return null;
}
},
new MassUpdater.InputConverter<Row>() {
return true;
}
}
- );
+ );
db.assertDbUnit(getClass(), "migrate_data_result.xml", "issues");
}
return true;
}
}
- );
+ );
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageException.class);
return true;
}
}
- );
+ );
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(MessageException.class);
}
}
+ @Test
+ public void ignore_null_rows() throws Exception {
+ db.prepareDbUnit(getClass(), "ignore_null_rows.xml");
+
+ new MassUpdater(db.database()).execute(
+ new MassUpdater.InputLoader<Row>() {
+ @Override
+ public String selectSql() {
+ return "SELECT i.id FROM issues i";
+ }
+
+ @Override
+ public Row load(ResultSet rs) throws SQLException {
+ return null;
+ }
+ },
+ new MassUpdater.InputConverter<Row>() {
+ @Override
+ public String updateSql() {
+ return "UPDATE issues SET severity=? WHERE id=?";
+ }
+
+ @Override
+ public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+ updateStatement.setString(1, "BLOCKER");
+ updateStatement.setLong(2, row.id);
+ return true;
+ }
+ }
+ );
+ // no changes, do not set severity to BLOCKER
+ db.assertDbUnit(getClass(), "ignore_null_rows.xml", "issues");
+ }
+
@Test
public void convert_select_sql() throws Exception {
Database db = mock(Database.class);
--- /dev/null
+<dataset>
+
+ <issues ID="1" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="1"
+ ACTION_PLAN_KEY="[null]" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+ EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+ ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+ RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+ TECHNICAL_DEBT="10"/>
+
+</dataset>