-
-
Notifications
You must be signed in to change notification settings - Fork 729
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
Fix RedundantPresenceValidationOnBelongs on two files #12428
Fix RedundantPresenceValidationOnBelongs on two files #12428
Conversation
- presence: true is redundant since Rails 5.0 BUT applies with new default config of belongs_to_required_by_default to true. Lots of files with belongs_to_required_by_default = false (backward compatibility). So: deleting this setting implies to adding optional: true - added 'NOT NULL' constraints so model constraints match with contraints on DB tables. - corresponding migration files to match AR Models & DB tables - rake tasks to check corrupt data (ie: NULL/nil in id fields) - updated the todo
3bb36e9
to
5e18038
Compare
Hello @sigmundpetersen or @drummer83 , |
belongs_to :stock_item, class_name: 'Spree::StockItem' | ||
belongs_to :originator, polymorphic: true | ||
belongs_to :originator, polymorphic: true, optional: true |
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 am not too familiar with the stock system but I feel like this shouldn't be optional @openfoodfoundation/core-devs ?
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.
Without the optional: true
, around 10 specs are in error, all related to originator.
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.
Maybe the specs need to set the originator? Is it just incomplete test data?
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.
Hello @mkllnk ,
I have no idea if the originator
nulleable is a good thing or not.
I have watched a bit one example and cannot make my mind.
I also have no data for stock movement
locally.
But, I have looked at the adjustment
model that belongs too to originator
(There are only 2)
(Btw for now with the self.belongs_to_required_by_default = false
)
There are various scope defined, amongst them this one:
scope :admin, -> { where(originator_type: nil) }
Which indicates that for the adjustment
model, the originator
may be null.
So my guess is that nulleable is a good choice.
May be someone can help us with the data model and what are the exact intentions of this particular model ?
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 looked at the production server, and we have some stock movement without originator :
sql=`select count(*) from spree_stock_movements where originator_id is null;'
ansible all_prod -u openfoodnetwork -a "psql -h localhost openfoodnetwork ofn_user -c '$sql'"
openfoodnetwork.org.au | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.org.nz | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.ca | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.org.uk | CHANGED | rc=0 >>
count
-------
3
(1 row)
openfoodnetwork.ie | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.net | CHANGED | rc=0 >>
count
-------
3
(1 row)
app.katuma.org | CHANGED | rc=0 >>
count
-------
26
(1 row)
csicsoka.org | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.de | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.in | CHANGED | rc=0 >>
count
-------
0
(1 row)
openfoodnetwork.be | CHANGED | rc=0 >>
count
-------
1
(1 row)
coopcircuits.fr | CHANGED | rc=0 >>
count
-------
16
(1 row)
So maybe we want to keep it as it is ? If I remember correctly we are trying to not change inventory, so that's another reason not to change this ?
I am not sure if an admin user can do manual stock movement ? but from what I can infer from the data, all stock movement orginator are Spree::Shipment
. In the case of an adjustment
an adjustment with no originator is manual adjustment, ie made by an admin. So it would be fair to assume the same here ?
@mkllnk what do you think ?
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.
This pull request is addressing style rules. So let's keep the logic as it is and go ahead with this.
I checked staging and prod for the updated column and we are good |
Let's test the checkout with stock controlled products just in case. |
Hey @cyrillefr @mkllnk , I've tested the scenario in which some items are added to the cart by the customer while in the meantime the stock is reduced to zero, by the admin - imitating a sort of race condition. This triggers the out-of-stock warning as usual: Also checked out an order with a variant which had only 2 on hand stock: in this case the UI reflected this limitation, allowing the customer to place only two line items in the cart. I think this looks good. Merging! |
- in relation to: Fix Rubocop Rails issues openfoodfoundation#11482 - and Require belongs_to associations by default openfoodfoundation#11297 - and openfoodfoundation#12407 openfoodfoundation#12428 etc.
What? Why?
There were a
presence: true
for theStockMovement
model and I have taken on me to add anot null
constraint at the DB level.What should we test?
After migration, all tests should pass and no rubocop offense should be raised.
Run the 2 rake files, to check it any corrupt data is present.
Release notes
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.
Dependencies
Documentation updates