import org.sonar.core.util.UuidFactory;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
import org.sonar.server.platform.db.migration.step.Select;
-import org.sonar.server.platform.db.migration.step.Upsert;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
public class RemoveOrphanRulesFromQualityProfiles extends DataChange {
@Override
protected void execute(Context context) throws SQLException {
- Select select = context.prepareSelect("select ar.uuid, ar.profile_uuid, ar.rule_uuid from rules_profiles rp " +
+ final String SELECT_QUERY = "select ar.uuid, ar.profile_uuid, ar.rule_uuid from rules_profiles rp " +
"inner join active_rules ar on ar.profile_uuid = rp.uuid " +
"inner join rules r on r.uuid = ar.rule_uuid " +
- "where rp.language != r.language");
- select.scroll(row -> process(context, row));
- }
+ "where rp.language != r.language";
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select(SELECT_QUERY);
+
+ final String UPDATE_QUERY = """
+ INSERT INTO qprofile_changes
+ (kee, rules_profile_uuid, change_type, created_at, user_uuid, change_data)
+ VALUES(?, ?, ?, ?, ?, ?)
+ """;
+ massUpdate.update(UPDATE_QUERY);
+ massUpdate.update("delete from active_rules where uuid = ?");
- private void process(Context context, Select.Row row) throws SQLException {
- Upsert insertQualityProfileChange = insertQualityProfileChange(context, row);
- Upsert deleteActiveRule = deleteActiveRule(context, row);
- insertQualityProfileChange.commit();
- deleteActiveRule.commit();
+ massUpdate.execute((row, update, index) -> {
+ if (index == 0) {
+ prepareUpdateForQProfileChanges(row, update);
+ }
+ if (index == 1) {
+ update.setString(1, row.getString(1));
+ }
+ return true;
+ });
}
- private Upsert insertQualityProfileChange(Context context, Select.Row row) throws SQLException {
- return context.prepareUpsert("INSERT INTO qprofile_changes " +
- "(kee, rules_profile_uuid, change_type, created_at, user_uuid, change_data) " +
- "VALUES(?, ?, ?, ?, ?, ?);")
- .setString(1, uuidFactory.create())
- .setString(2, row.getString(2))
+ private void prepareUpdateForQProfileChanges(Select.Row selectedRow, SqlStatement update) throws SQLException {
+ update.setString(1, uuidFactory.create())
+ .setString(2, selectedRow.getString(2))
.setString(3, "DEACTIVATED")
.setLong(4, system2.now())
.setString(5, null)
- .setString(6, "ruleUuid=" + row.getString(3))
- .execute();
- }
-
- private static Upsert deleteActiveRule(Context context, Select.Row row) throws SQLException {
- return context.prepareUpsert("delete from active_rules where uuid = ?")
- .setString(1, row.getString(1))
- .execute();
+ .setString(6, "ruleUuid=" + selectedRow.getString(3));
}
}