Fix Scheduling Spec Goals Delete (for develop) #1436
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Sister PR of #1435.
These PRs fix two bugs with our scheduling specifications: 1) The migration for Versioning Scheduling Goals did not update the `when` condition of the `on update` trigger for scheduling specifications, leading to the stack depth unexpectedly exploding when trying to update a spec 38+ goals (the threshold may be lower, but I did not look into this). 2) The function used for `on delete` did not properly handle bulk deletes (as in a delete statement that deletes multiple rows). This function and its trigger have been updated to operate "per-statement" and go through all the rows deleted in reverse priority order. This ensures thatPR Description from 1435:
Independently of these bugs, I also put a depth check on the
increment spec update when the goals subtable is updated
trigger, as there's no reason for us to update the revision when a trigger changes a goal's priority.A simple example of the second bug follows:
Let's say we have the following specification. Specification are being expressed as an array ordered by priority and "tailing" priorities will be expressed as
null
in order to avoid confusion caused by the array changing size mid-walkthrough.[ Goal 1, Goal 2, Goal 3, Goal 4, Goal 5, Goal 6 ]
Now let us delete Goal 1, Goal 2, and Goal 5. The expected output for this action is
[Goal 3, Goal 4, Goal 6, null, null, null]
Under the old logic, the following would happen:
[null, null, Goal 3, Goal 4, null, Goal 6]
after delete
trigger for Goal 1 is processed. As Goal 3, Goal 4, and Goal 6 have a strictly higher priority than Goal 1, their priorities are reduced by 1:[null, Goal 3, Goal 4, null, Goal 6, null]
after delete
trigger for Goal 2 is processed. As Goal 4 and Goal 6 have a strictly higher priority than Goal 2, their priorities are reduced by 1:[null, Goal 3 and Goal 4, null, Goal 6, null, null]
after delete
trigger for Goal 5 is processed. As no Goals have a strictly higher priority than Goal 5, no rows are updated:[null, Goal 3 and Goal 4, null, Goal 6, null, null]
Note that preserving the old per-row function but instead reducing goals with a priority greater than or equal to the current goal's would not have been sufficient, as it would lead to gaps in priorities (as well as conflicting priorities in different setups). For example, in the above example,
Goal 6
has been placed at priority 4 before we processGoal 5
's deletion, meaning it will not be moved to priority 3, even though that's where we would expect it to be at the end of the transaction, leaving a gap at priority 3.Under the new logic, the following occurs:
[null, null, Goal 3, Goal 4, null, Goal 6]
after delete
for step 1 begins.Goal 5
, as the deleted Goal with the highest priority, is processed first:[null, null, Goal 3, Goal 4, Goal 6, null]
[null, Goal 3, Goal 4, Goal 6, null, null]
[Goal 3, Goal 4, Goal 6, null, null, null]
after delete
for step 1 completes.The output in this case is exactly as expected, as by processing in reverse priority order, we guarantee that all goals with a strictly higher priority than that of the deleted goal we're processing is still at a strictly higher priority than that deleted goal, even if that priority has changed.
Verification
DB Tests were added to test bulk deleting from the both types of specs in three different scenarios: a chunk out of the middle, only the middle left, and many alternating rows. Against
develop
, these tests are expected to fail.Documentation
As said in 1435, we need to document that PGCMP does not catch
when
conditions on triggers so that viewers are aware to keep their eye out for it.Future work
As said in 1435, looking into extending PGCMP to implement the
when
condition trigger match.