micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

Automatic checking of row counts for update* when only <= 1 row is expected to be affected

Open mikehearn opened this issue 5 months ago • 1 comments

Feature description

Safe/sanity checked usage of update methods currently requires code like this:

var rowCount = repository.updateFooById(id);
if (rowCount != 1) {
    throw IllegalStateException("Update of FOO by id %s should have affected 1 row but affected %d".formatted(id, rowCount));
} 

If you don't check it's possible an update silently touches more or less data than you expected, which isn't ideal.

It'd be good if Micronaut Data could do this sort of check for you. I'm thinking something like @MustUpdateOne and @MaybeUpdatesOne annotations on update methods. The former would ensure rowcount == 1, the latter that it's <= 1 and I don't think there's much point in having more flexibility than that.

mikehearn avatar Jul 18 '25 08:07 mikehearn

An alternative to annotations would be a new prefix, e.g. maybeUpdateFooById (<= 1) or alwaysUpdateFooById for (==1). But that might be less clear, or harder to implement, or create a lot more code churn if people want to refactor to use the feature. So an annotation may be better?

mikehearn avatar Jul 18 '25 08:07 mikehearn