bwest87
bwest87

Reputation: 1233

Rails Data Migration Mystery

I have a BankAccount model and a Card model. Currently, the they both have a customer_id and a company_id. I'm trying to convert those to just be a polymorphic association to "owner". I added the proper relationships in the models, and then went to write a data migration for all the current customers and companies. That migration looks like this...

class AddOwnersToCardsAndBanks < ActiveRecord::Migration
  class Card < ActiveRecord::Base; end
  class BankAccount < ActiveRecord::Base; end

  def change
    # Card related changes
    add_column :cards, :owner_id, :integer
    add_column :cards, :owner_type, :string

    # BankAccount related changes
    add_column :bank_accounts, :owner_id, :integer
    add_column :bank_accounts, :owner_type, :string

    Card.all.each do |card|
      if card.try(:customer_id)
        card.update_attribute(:owner_id, card.patient_id)
        card.update_attribute(:owner_type, "Patient")
      end
    end

    BankAccount.all.each do |bank_acount|
      if bank_acount.try(:customer_id)
        bank_acount.update_attribute(:owner_id, bank_acount.customer_id)
        bank_acount.update_attribute(:owner_type, "Patient")
      end
      if bank_acount.try(:company_id)
        bank_acount.update_attribute(:owner_id, bank_acount.company_id)
        bank_acount.update_attribute(:owner_type, "Company")
      end
    end

    remove_column :cards, :customer_id, :integer
    remove_column :bank_accounts, :customer_id, :integer
    remove_column :bank_accounts, :company_id, :integer
  end
end

This works (so it seems) locally. I pushed this to a feature server, and even console logged what was happening in the migration. It appears that everything did what it should in the migration (which means I logged the bank account after it had been updated, and the attributes printed where what I expected them to be). However, when I do rails console from the feature server, and look up the changed data, then it only seemed to work properly for cards, but NOT for bank accounts.

Specifically, owner_id and owner_type are never actually set on the bank accounts IN THE DATABASE AFTER THE FACT. But when I log those attributes from the migration, they are there as expected. This is in contrast to cards, where everything gets set properly, and the database shows that. I'm totally confused.

It seems like maybe it's something to do with Heroku, or Circle, but I have no idea. Any thoughts on what could be the issue would be much appreciated.

Upvotes: 1

Views: 51

Answers (1)

EpiphanyMachine
EpiphanyMachine

Reputation: 126

Sometimes during a data migration you can't write to the columns that were just created. Try adding this to your migration script:

# BankAccount related changes
add_column :bank_accounts, :owner_id, :integer
add_column :bank_accounts, :owner_type, :string

Card.reset_column_information
BankAccount.reset_column_information

Card.all.each do |card|

More information can be found here: http://apidock.com/rails/ActiveRecord/Base/reset_column_information/class

Upvotes: 2

Related Questions