]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20240605134916_add_notes_and_diary_comments_counter_caches.rb
Merge pull request #5932 from tomhughes/frozen-strings
[rails.git] / db / migrate / 20240605134916_add_notes_and_diary_comments_counter_caches.rb
1 # frozen_string_literal: true
2
3 class AddNotesAndDiaryCommentsCounterCaches < ActiveRecord::Migration[7.1]
4   def self.up
5     add_column :users, :diary_comments_count, :integer, :default => 0
6     add_column :users, :note_comments_count, :integer, :default => 0
7
8     users_with_diary_comments = DiaryComment.distinct.pluck(:user_id)
9     users_with_diary_comments.each do |user_id|
10       User.reset_counters(user_id, :diary_comments)
11     end
12
13     users_with_note_comments = NoteComment.where.not(:author_id => nil).distinct.pluck(:author_id)
14     users_with_note_comments.each do |author_id|
15       User.reset_counters(author_id, :note_comments)
16     end
17   end
18
19   def self.down
20     remove_column :users, :diary_comments_count
21     remove_column :users, :note_comments_count
22   end
23 end