I have a unique integer column called ordinality. I wrote a function so that when someone wants to edit one row, it will automatically adjust the other ordinality values.
For items (A, B, C, D) with ordinality (1, 2, 3, 4), if we desire D to have ordinality 2, this set the ordinalities to (1, 3, 4, 2).
create rule update_ordinality as (
on update to
test_table
do instead
update
test_table
set ordinality = case
when ordinality = old.ordinality then new.ordinality
when ordinality > old.ordinality and ordinality <= new.ordinality then ordinality - 1
when ordinality >= new.ordinality and ordinality < old.ordinality then ordinality + 1
else ordinality
end
);
This function I think only makes sense when updating a singular row. Is there a way to make it so that if it is a bulk update, it proceeds as usual?