-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add move method to automation rule #5998
Conversation
This is needed to re-organize rules without explicit changing all priorities or changing one by one
Oops, I use the manager introduced in #5995, that PR should be merged first |
What does it mean to "move an automation rule N steps"? |
@humitos if you have this rules
The automation rules are run in that order (according to their priority). User can rearrange the rules to change the order they are run. Like
Users shouldn't need to update each priority version one by one. Think of it like a drag and drop thing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this implementation is too complex and does not manage moving one step up (like steps=-1
, at least in the tests).
I think we can implement this in a simpler way:
- query all the Automation Rules sorted by priority
- get the index of the one that we want to move from that list
- move that Automation Rule where it should be in the list (steps)
- set the index of each Automation Rule in the list as
priority
So, we will end up always with 0, 1, 2, 3 as priority
if we have 4 rules, instead of dealing with weird priority numbers.
It does, tests are still missing. It's on the description.
We can't save the values like that, it breaks the constraint of unique (project, priority)
Current implementation always keeps the range, it doesn't add weird numbers |
Why it will break that constraint if we will have priorities like 0, 1, 2, 3, 4 tied to one project?
This is the weird number that I was reffering to: https://github.com/readthedocs/readthedocs.org/pull/5998/files#diff-763de362f34a86b264903b372e7dd27fR1012 |
That's is temporary, to avoid the unique constraint
In the final result not, but in the intermediate steps, when you try to save or update an object, other object will have the same priority. That's why I can't use just one |
This is ready for review. This is on top of #5995 (that should be merged first) |
I also overrode the delete method to keep the priority numbers the same after delete |
# avoid hitting the unique constraint (project, priority). | ||
for rule in rules: | ||
rule.priority = expression | ||
rule.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we hit an exception here? The rule will be stuck with a broken priority. We likely need this to happen in some kind of DB transaction to avoid bad states here.
This feels really complicated and brittle. I don't have a better suggestion currently, but I'm wondering if this concept of priority is something we need to enforce so strongly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have transactions per request
ATOMIC_REQUESTS = True |
So, we need to have a unique priority in all rules of a project, that way they get sorted how the user wants.
We can remove the unique constraint as Manuel suggested, and maybe check for inconsistencies on save?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I guess we can keep this for now, but I'm guessing it will cause issues at some point :)
Co-Authored-By: Eric Holscher <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not happy with the implementation, but feel free to merge if you want. @ericholscher already approved it 😄. Although, ...
I think that having the constrain at the db level for this just makes our lives harder and does not gives us too much benefit, since the order/priority will always set programmatically (not manual).
I feel it's easier to maintain all this code (with a simple implementation) if we just remove the constrain from the db.
I like having the DB restriction though. I don't trust code not to mess it up, and then we'd end up with weird bugs. This will lead to different weird bugs, but at least these weird bugs will be understandable :) |
This is needed to re-organize rules
without explicit changing all priorities
or changing one by one
I just need to add tests for negative steps, and add a comment why just doing an
.update(F('priority') + 1)
doesn't work