How to write conditional migrations in rails?

前端 未结 4 623
悲&欢浪女
悲&欢浪女 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:28

    This should work

    def self.table_exists?(name)
      ActiveRecord::Base.connection.tables.include?(name)
    end
    
    if table_exists?(:profile) && !Profile.column_names.include?("url")
      add_column :profile, :url, :string
    end
    

提交回复
热议问题