Emi Matchu
470c805880
In impress-2020, we do a big slow query to figure out which users have
been active in trades recently. Now, we cache that timestamp on the
User model.
This won't have any immediate effect; it's to clear the way for Classic
DTI to receive the better trade ratios feature people like from 2020.
I also added some unit testing infra because I finally wanted it! for
all the ways you can trigger this timestamp lol
Note too that this is a bit of an unusually complex migration, but my
hope is that the batching and query structure and such helps it run
surprisingly fast! 🤞
36 lines
1.2 KiB
Ruby
36 lines
1.2 KiB
Ruby
class AddLastTradeActivityAtToUsers < ActiveRecord::Migration[7.1]
|
|
def change
|
|
add_column :users, :last_trade_activity_at, :timestamp
|
|
|
|
reversible do |direction|
|
|
direction.up do
|
|
User.find_in_batches do |users|
|
|
# Find the last ClosetList/ClosetHanger updated_at timestamp for each
|
|
# user, for trading lists/hangers only.
|
|
max_closet_list_updated_at_by_user_id = ClosetList.
|
|
trading.
|
|
group(:user_id).
|
|
where(user_id: users.map(&:id)).
|
|
maximum(:updated_at)
|
|
max_closet_hanger_updated_at_by_user_id = ClosetHanger.
|
|
trading.
|
|
group(:user_id).
|
|
where(user_id: users.map(&:id)).
|
|
maximum(:updated_at)
|
|
|
|
# Set `last_trade_activity_at` to the largest such `updated_at` for
|
|
# that user, or nil if there's none.
|
|
User.transaction do
|
|
users.each do |user|
|
|
user.last_trade_activity_at = [
|
|
max_closet_list_updated_at_by_user_id[user.id],
|
|
max_closet_hanger_updated_at_by_user_id[user.id],
|
|
].filter(&:present?).max
|
|
user.save!
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|