[TAN-6099] Rake task for consolidating area fields (and updating docile custom field options)#12956
Conversation
|
| @@ -0,0 +1,273 @@ | |||
| # frozen_string_literal: true | |||
There was a problem hiding this comment.
Again, flagging use of AI here as Claude wrote the majority of this code.
I've been over the code & tweaked some things, and then I ran the rake task locally with success, but should definitely have a BE developer go over it well too :)
Let me know if anything needs to be changed, or you catch any incorrect behaviour!
sebastienhoorens
left a comment
There was a problem hiding this comment.
This should work :) It just feels like a lot of code and I wonder if it could have been simpler or if it could have been made more readable by extracting helper methods.
| expect(area_graauw.title_multiloc['fr-FR']).to eq('Graauw') | ||
| end | ||
|
|
||
| it 'uses a database transaction that rolls back on error' do |
There was a problem hiding this comment.
I'm not sure this test is truly testing the rollback. I think it would still pass without rollback happening.
back/engines/commercial/multi_tenancy/spec/tasks/remap_areas_and_custom_field_options_spec.rb
Show resolved
Hide resolved
back/lib/tasks/single_use/remap_areas_and_custom_field_options.rake
Outdated
Show resolved
Hide resolved
jamesspeake
left a comment
There was a problem hiding this comment.
Wow. This is hugely complex (and potentially) destructive, but the tests give me confidence it's doing the right thing. However I would add a caution warning in the header of this rake task that it should always be run against a sandbox copy of the platform locally first (as you have done). It would scare me to run it otherwise.
A couple of other comments to look at.
| User.where("custom_field_values->>'domicile' = ?", area_to_merge.id).each do |user| | ||
| user.custom_field_values['domicile'] = area_to_keep.id | ||
| user.save(validate: false) | ||
| end |
There was a problem hiding this comment.
One thing I can also think of here. We probably also need to catch the example where user custom fields are stored in an idea:
Idea.where("custom_field_values->>'u_domicile' = ?", area_to_merge.id)
| def update_projects_associations(area_to_keep, area_to_merge) | ||
| AreasProject.where(area_id: area_to_merge.id).each do |areas_project| | ||
| existing = AreasProject.find_by(area_id: area_to_keep.id, project_id: areas_project.project_id) | ||
| existing ? areas_project.destroy : areas_project.update!(area_id: area_to_keep.id) | ||
| end | ||
| end | ||
|
|
||
| def update_followers(area_to_keep, area_to_merge) | ||
| Follower.where(followable_type: 'Area', followable_id: area_to_merge.id).each do |follower| | ||
| existing = Follower.find_by(followable_type: 'Area', followable_id: area_to_keep.id, user_id: follower.user_id) | ||
| existing ? follower.destroy : follower.update!(followable_id: area_to_keep.id) | ||
| end | ||
| end | ||
|
|
||
| def update_static_pages(area_to_keep, area_to_merge) | ||
| AreasStaticPage.where(area_id: area_to_merge.id).each do |areas_static_page| | ||
| existing = AreasStaticPage.find_by(area_id: area_to_keep.id, static_page_id: areas_static_page.static_page_id) | ||
| existing ? areas_static_page.destroy : areas_static_page.update!(area_id: area_to_keep.id) | ||
| end | ||
| end | ||
|
|
||
| def update_user_domicile(area_to_keep, area_to_merge, domicile_field) |
There was a problem hiding this comment.
It would also be useful I think to see how many updates happen in each of these methods in the stats
AI-Assisted
Rake task and spec were both written primarily with Claude & then further tweaked. If there are certain BE patterns used we don't typically employ, just let me know :)
Tested
I tested the rake task locally on an actual cloned platform and it worked as expected, merging areas & updating the user domicile area, Project area, etc 👍
Description
Adds reusable rake task to remap and merge areas from CSV file.
Adds a new rake task single_use:remap_areas_and_custom_field_options that allows remapping/consolidating platform areas based on a CSV mapping file.
Features:
Changelog
Technical