cirandas.net

ref: master

db/migrate/20150625234824_add_user_id_to_session.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class AddUserIdToSession < ActiveRecord::Migration

  def change
    add_column :sessions, :user_id, :integer
    add_index :sessions, :user_id
  end

  def up
    Session.reset_column_information

    # cleanup data: {}
    Session.where(data: "BAh7AA==\n").delete_all
    # cleanup data with lang key only
    Session.where("data ~ 'BAh7BjoJbGFuZyIH.{3,3}=\n'").delete_all

    # very slow migration, only do for the last month
    Session.where('updated_at > ?', 1.month.ago).find_each batch_size: 50 do |session|
      begin
        # this calls Session#copy_to_columns
        session.save!
      rescue ArgumentError
        # old ActionController::Flash::FlashHash from rails 2.3
        session.destroy
      end

      # limit limitless allocations
      GC.start
    end
  end

end