class AddUnreadTrackingColumns < ActiveRecord::Migration def up add_column :user_stats, :first_topic_unread_at, :datetime, null: false, default: "epoch" add_column :topics, :last_unread_at, :datetime, null: false, default: "epoch" execute < 1 AND last_read_post_number < CASE WHEN moderator OR admin THEN topics.highest_staff_post_number ELSE topics.highest_post_number END AND topics.deleted_at IS NULL AND topics.archetype <> 'private_message' ), 'epoch') SQL add_index :topics, [:last_unread_at] # we need this function for performance reasons execute <