Skip to content

Commit 4aba881

Browse files
Fix schema for word root (#279)
* Add root_id into Words & rake task to Migrate WordRoots to words.root_id * Minor code fix
1 parent 926822c commit 4aba881

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddRootIdToWords < ActiveRecord::Migration[7.0]
2+
def change
3+
c = Word.connection
4+
c.add_column :words, :root_id, :integer
5+
c.add_index :words, :root_id
6+
end
7+
end

lib/tasks/migrate_word_roots.rake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace :db do
2+
desc "Migrate WordRoots to words.root_id"
3+
task migrate_roots: :environment do
4+
updated_count = 0
5+
root_skipped = []
6+
7+
WordRoot.eager_load(:word).find_each do |word_root|
8+
word = word_root.word
9+
root = Root.find_by(id: word_root.root_id)
10+
11+
if word.nil? || word.root_id.present?
12+
root_skipped << word_root.id
13+
next
14+
end
15+
16+
if root
17+
word.update_column(:root_id, root.id)
18+
updated_count += 1
19+
end
20+
end
21+
22+
puts "Data migration complete! #{updated_count} words updated with root_id."
23+
puts "Skipped WordRoot IDs: #{root_skipped.join(', ')}"
24+
end
25+
end

0 commit comments

Comments
 (0)