How to write conditional migrations in rails?

前端 未结 4 628
悲&欢浪女
悲&欢浪女 2020-12-30 01:14

I am looking for ways to write migrations in rails that can be executed against the database many times without failing.

For instance let say I have this migration:

4条回答
  •  情歌与酒
    2020-12-30 01:24

    Wrapping my migration in a conditional worked for me. Rails 4.X

    class AddUrlToProfile < ActiveRecord::Migration
      unless Profile.column_names.include?("url")
        def self.up
          add_column :profile, :url, :string
        end
    
        def self.down
          remove_column :profile, :url
        end
      end
    end 
    

提交回复
热议问题