@Override
protected void execute(Context context) throws SQLException {
+ populateUuids(context);
+ removeOrphans(context);
+ }
+
+ private void populateUuids(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();
massUpdate.select("select rp.rule_id, ru.uuid " +
return true;
});
}
+
+ private void removeOrphans(DataChange.Context context) throws SQLException {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+
+ massUpdate.select("select uuid from rules_parameters where rule_uuid is null");
+ massUpdate.update("delete from rules_parameters where uuid = ?");
+
+ massUpdate.execute((row, update) -> {
+ update.setString(1, row.getString(1));
+ return true;
+ });
+ }
}
tuple("uuid-rp-3", 2L, "uuid-rule-2"));
}
+ @Test
+ public void remove_orphans() throws SQLException {
+ insertRulesParametersEntry("uuid-rp-4", 666L);
+
+ underTest.execute();
+
+ assertThat(db.countSql("select count(*) from rules_parameters"))
+ .isEqualTo(3);
+ assertThat(db.select("select uuid, rule_id, rule_uuid from rules_parameters"))
+ .extracting(m -> m.get("UUID"), m -> m.get("RULE_ID"), m -> m.get("RULE_UUID"))
+ .containsExactlyInAnyOrder(
+ tuple("uuid-rp-1", 1L, "uuid-rule-1"),
+ tuple("uuid-rp-2", 1L, "uuid-rule-1"),
+ tuple("uuid-rp-3", 2L, "uuid-rule-2"));
+ }
+
private void insertRule(long id, String uuid) {
db.executeInsert("rules",
"id", id,