Created
November 2, 2016 14:24
-
-
Save n3bulous/9713b1086708486ac69f4a76b257df8e to your computer and use it in GitHub Desktop.
Idempotent Rails Migrations for Columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddEnvironmentToHost < ActiveRecord::Migration | |
def self.up | |
- add_column :hosts, :environment, :string | |
+ unless ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") | |
+ add_column :hosts, :environment, :string | |
+ end | |
end | |
def self.down | |
- remove_column :hosts, :environment | |
+ if ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") | |
+ remove_column :hosts, :environment | |
+ end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shamelessly purloined from https://projects.puppetlabs.com/issues/1959