Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Created December 18, 2018 16:56
Show Gist options
  • Save matthewrudy/f7510c924c7e1c0a91515d6c4929eef7 to your computer and use it in GitHub Desktop.
Save matthewrudy/f7510c924c7e1c0a91515d6c4929eef7 to your computer and use it in GitHub Desktop.
An example of adding a column as nullable, setting the default, then making it not null.
class AddTimestampsToRegions < ActiveRecord::Migration[4.2]
def up
add_timestamps :regions, null: true
execute "UPDATE regions SET created_at = NOW(), updated_at = NOW()"
change_column_null :regions, :created_at, false
change_column_null :regions, :updated_at, false
end
def down
remove_column :regions, :created_at
remove_column :regions, :updated_at
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment