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

Y24-225 fix location count #819

Merged
merged 4 commits into from
Aug 28, 2024
Merged

Y24-225 fix location count #819

merged 4 commits into from
Aug 28, 2024

Conversation

seenanair
Copy link
Contributor

@seenanair seenanair commented Aug 22, 2024

Closes #813

Changes proposed in this pull request

  • Creates a rake task to update children_count column to the correct value

Instructions for Reviewers

[All PRs] - Confirm PR template filled
[Feature Branches] - Review code
[Production Merges to main]
    - Check story numbers included
    - Check for debug code
    - Check version

@seenanair seenanair requested a review from BenTopping August 22, 2024 08:23
Location.find_each do |location|
correct_count = location.children.count
if location.children_count != correct_count
location.update_column(:children_count, correct_count) # rubocop:disable Rails/SkipsModelValidations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't want to skip model validations as rubocop is warning us here. It might be that update_column is trying to bypass them. An alternative way of writing this would be

location.children_count = correct_count
location.save

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Ben. Sure, will change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thoughts im not sure skipping validations is a bad thing here as we are editing a column that we don't validate or control. Is there documentation in the ancestry gem on how to update this? Do the use a similar rake task with update_column?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought the same. As we were not quite sure how the ancestry gem internally works and what exactly causes the bug, I was not sure calling a save method would make ancestry to update the column as we expected. So, I thought the best way is to override this column that is externally managed by ancestry gem

@grandchild = create(:location, parent: @child1)

# Manually set children_count to 0 for all locations
# rubocop:disable Rails/SkipsModelValidations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set the children_count on creation instead?

@parent = create(:location, children_count: 0)
@child1 = create(:location, parent: @parent, children_count: 0)
@child2 = create(:location, parent: @parent, children_count: 0)
@grandchild = create(:location, parent: @child1, children_count: 0)

This might not work as ancestry might override it each time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be worth having a children_count that is set to 100 (or some other non zero value) and confirm that also gets corrected.


it 'fixes the children_count for all locations and outputs the correct message' do
expect do
Rake::Task['location:fix_children_count'].invoke
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add expectations to confirm that children_count is 0 before running the rake tests as otherwise we can't know for sure that children_count is actually updated.

@seenanair seenanair requested a review from BenTopping August 27, 2024 10:46
@seenanair seenanair merged commit 978a5d2 into develop Aug 28, 2024
4 checks passed
@seenanair seenanair deleted the y24-225-fix-location-count branch August 28, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Y24-225 - [BUG] LabWhere location child count not updating correctly
2 participants