about summary refs log tree commit diff stats
path: root/db/migrate/20250512181245_add_latest_post_at_to_stream.rb
blob: 9d753e3d4ead82a5d73e0f718593342520a19e69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class AddLatestPostAtToStream < ActiveRecord::Migration[7.1]
  def up
    add_column :streams, :latest_post_at, :datetime
    
    Stream.all.each do |stream|
      if stream.updates.empty?
        stream.latest_post_at = stream.created_at
      else
        stream.latest_post_at = stream.updates.order(created_at: :desc).first.created_at
      end

      stream.save!
    end

    change_column_null :streams, :latest_post_at, false
  end

  def down
    remove_column :streams, :latest_post_at, :datetime
  end
end