Skip to content

Commit

Permalink
Only remove spree_credit_card fields if they exist
Browse files Browse the repository at this point in the history
I encountered a situation today on a database where these fields did not already exist. Best to have some kind of catch in place to not make the migration bomb out if the fields do not exist.
  • Loading branch information
radar committed Oct 8, 2013
1 parent f66caa3 commit 9741b8f
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class RemoveUnusedCreditCardFields < ActiveRecord::Migration
def up
remove_column :spree_credit_cards, :start_month
remove_column :spree_credit_cards, :start_year
remove_column :spree_credit_cards, :issue_number
remove_column :spree_credit_cards, :start_month if column_exists?(:spree_credit_cards, :start_month)
remove_column :spree_credit_cards, :start_year if column_exists?(:spree_credit_cards, :start_year)
remove_column :spree_credit_cards, :issue_number if column_exists?(:spree_credit_cards, :issue_number)
end
def down
add_column :spree_credit_cards, :start_month, :string
add_column :spree_credit_cards, :start_year, :string
add_column :spree_credit_cards, :issue_number, :string
end

def column_exists?(table, column)
ActiveRecord::Base.connection.column_exists?(table, column)
end
end

0 comments on commit 9741b8f

Please sign in to comment.