Skip to content

Instantly share code, notes, and snippets.

@n3bulous
Created November 2, 2016 14:24
Show Gist options
  • Save n3bulous/9713b1086708486ac69f4a76b257df8e to your computer and use it in GitHub Desktop.
Save n3bulous/9713b1086708486ac69f4a76b257df8e to your computer and use it in GitHub Desktop.
Idempotent Rails Migrations for Columns
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
@n3bulous
Copy link
Author

n3bulous commented Nov 2, 2016

Shamelessly purloined from https://projects.puppetlabs.com/issues/1959

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment