# Existing players predate step tracking, so the funnel would start empty. # Their `progress` still says which stages they answered -- good enough to # reconstruct roughly how far they got. Players with no progress at all are # left alone: there is nothing to infer from. class BackfillPlayerFurthestStep < ActiveRecord::Migration[8.1] def up Player.where(furthest_step: nil).find_each do |player| stages = player.progress.keys.grep(/\A\d+\z/).map(&:to_i) next if stages.empty? && !player.progress.key?(Player::LAST_SAVE_KEY) # is_done is set on the final screen, not the results screen, so stop # there rather than claiming they saw the results. step = if player.is_done? "done" elsif player.progress.key?(Player::LAST_SAVE_KEY) "last_save" else "stage_#{stages.max}_result" end player.update_column(:furthest_step, step) end end def down # No-op: the reconstructed values are indistinguishable from live ones. end end