ruby - Rescue block did not run in rails migration script -
i have generated following migration:
class addmarketstatusidtoproducts < activerecord::migration def change add_column :products, :market_status_id, :integer end end
but started raising following error @ heroku:
20150816131733 addmarketstatusidtoproducts: migrating ===================== -- add_column(:products, :market_status_id, :integer) (1.7ms) alter table "products" add "market_status_id" integer pg::undefinedtable: error: relation "products" not exist : alter table "products" add "market_status_id" integer (1.0ms) rollback rake aborted! standarderror: error has occurred, , later migrations canceled:
error message quite clear, used following migration script instead:
class addmarketstatusidtoproducts < activerecord::migration def change begin add_column :products, :market_status_id, :integer rescue pg::undefinedtable create_table :products |t| t.float :price t.timestamps null: false end end end end
but still getting same error message!! don't understand, why rescue code did not run?
i don't know why rescue block not running think error 1 standarderror: error has occurred, , later migrations canceled:
standarderror
can handle may be: rescue standarderror => error
. guess don't know this.
but correct way above is:
class addmarketstatusidtoproducts < activerecord::migration def change if table_exists?("products") add_column :products, :market_status_id, :integer else create_table :products |t| t.float :price t.integer :market_status_id t.timestamps null: false end end end end
rails has provided methods table_exists?
, column_exists?
this. have never used know can done. please check syntax once not 100% sure syntax.
hope helps.
Comments
Post a Comment