class LegacyBank < ActiveRecord::Base
  set_table_name 'banks'
end

class BankNowInheritsFromPerson < ActiveRecord::Migration
  def up
    add_column :people, :swift, :string
    add_column :people, :clearing, :string

    for bank in LegacyBank.all
      vcard = Vcard.where(:object_type => 'Bank', :object_id => bank.id).first
      person = Bank.create!(
        :vcard    => vcard,
        :swift    => bank.swift,
        :clearing => bank.clearing
      )

      if vcard
        vcard.object = person
        vcard.save!
      else
        person.build_vcard.save
      end
    end

    drop_table :banks
  end
end

class LegacyBank < ActiveRecord::Base
  set_table_name 'banks'
end

class BankNowInheritsFromPerson < ActiveRecord::Migration
  def up
    add_column :people, :swift, :string
    add_column :people, :clearing, :string

    for bank in LegacyBank.all
      vcard = Vcard.where(:object_type => 'Bank', :object_id => bank.id).first
      person = Bank.create!(
        :vcard    => vcard,
        :swift    => bank.swift,
        :clearing => bank.clearing
      )

      if vcard
        vcard.object = person
        vcard.save!
      else
        person.build_vcard.save
      end

      # Accounts
      BankAccount.where(:bank_id => bank.id).update_all(:bank_id => person.id)
    end

    drop_table :banks
  end
end

Migrate associated accounts when changing Bank model to inherit from Person.
Migrate associated accounts when changing Bank model to inherit from Person.
