Skip to content

Comments

[TAN-6099] Rake task for consolidating area fields (and updating docile custom field options)#12956

Merged
jamesspeake merged 27 commits intomasterfrom
TAN-6099-consolidate-area-field-rake-task
Feb 20, 2026
Merged

[TAN-6099] Rake task for consolidating area fields (and updating docile custom field options)#12956
jamesspeake merged 27 commits intomasterfrom
TAN-6099-consolidate-area-field-rake-task

Conversation

@amanda-anderson
Copy link
Contributor

@amanda-anderson amanda-anderson commented Jan 22, 2026

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:

  • Maps OLD area names to NEW area names from CSV input (2 columns)
  • Merges multiple areas into one when multiple OLD values map to same NEW value
  • Transfers all associations: projects, followers, static pages, user domicile values
  • Automatically syncs custom field options via existing Area model callbacks
  • Handles edge cases: existing targets, duplicates, missing areas, case-insensitive matching
  • Transaction-wrapped with detailed progress output

Changelog

Technical

  • [TAN-6099] Added rake task for consolidating area fields (and updating docile custom field options).

@notion-workspace
Copy link

@cl-dev-bot
Copy link
Collaborator

cl-dev-bot commented Jan 22, 2026

Messages
📖 Changelog provided 🎉
📖 Notion issue: TAN-6099
📖

Run the e2e tests

📖 Check translation progress

Generated by 🚫 dangerJS against a98698c

@@ -0,0 +1,273 @@
# frozen_string_literal: true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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!

Copy link
Contributor

@sebastienhoorens sebastienhoorens left a comment

Choose a reason for hiding this comment

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this test is truly testing the rollback. I think it would still pass without rollback happening.

Copy link
Contributor

@jamesspeake jamesspeake left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines 213 to 216
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
Copy link
Contributor

Choose a reason for hiding this comment

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

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)

Comment on lines 189 to 210
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

It would also be useful I think to see how many updates happen in each of these methods in the stats

@jamesspeake jamesspeake merged commit 64877bd into master Feb 20, 2026
16 checks passed
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.

4 participants