Skip to content
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

Conversation

cyrillefr
Copy link
Contributor

What? Why?

There were a presence: true for the StockMovement model and I have taken on me to add a not 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):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

- 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
@cyrillefr cyrillefr force-pushed the RedundantPresenceValidationOnBelongs_part_III branch from 3bb36e9 to 5e18038 Compare May 1, 2024 13:43
@cyrillefr
Copy link
Contributor Author

Hello @sigmundpetersen or @drummer83 ,
Some failed front-end task that runs ok locally.
Could it be possible to re-run the checking ?

@rioug rioug added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label May 6, 2024
belongs_to :stock_item, class_name: 'Spree::StockItem'
belongs_to :originator, polymorphic: true
belongs_to :originator, polymorphic: true, optional: true
Copy link
Collaborator

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 ?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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 ?

Copy link
Collaborator

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 ?

Copy link
Member

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.

@rioug
Copy link
Collaborator

rioug commented May 6, 2024

I checked staging and prod for the updated column and we are good

@mkllnk
Copy link
Member

mkllnk commented May 8, 2024

Let's test the checkout with stock controlled products just in case.

@filipefurtad0 filipefurtad0 self-assigned this May 15, 2024
@filipefurtad0 filipefurtad0 added pr-staged-fr staging.coopcircuits.fr and removed pr-staged-fr staging.coopcircuits.fr labels May 15, 2024
@filipefurtad0
Copy link
Contributor

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:

image

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!

@filipefurtad0 filipefurtad0 merged commit b3ac6be into openfoodfoundation:master May 15, 2024
54 checks passed
cyrillefr added a commit to cyrillefr/openfoodnetwork that referenced this pull request May 23, 2024
 - in relation to: Fix Rubocop Rails issues openfoodfoundation#11482
 - and  Require belongs_to associations by default openfoodfoundation#11297
 - and openfoodfoundation#12407 openfoodfoundation#12428 etc.
@cyrillefr cyrillefr deleted the RedundantPresenceValidationOnBelongs_part_III branch June 11, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants