I have a column called :active in one of my database tables (I'm using postgres) that is currently a boolean value, and is defaulted to true. I'd like to change this column's type from boolean to integer, so that I can use an enum going forward with this project. I've checked the documentation and some stack overflow answers, but haven't found a solid answer yet. How do I do make this change using migrations? I am using Rails 5, if that makes a difference. Thanks!
Your migration file should look like this
def change
change_column :table_name, :active, :integer
end
Also you need to rewrite all the previous records to an integer value which can be done in the same migration file.
You can create a new migration with:
rails g migration change_data_type_for_active
Then edit the migration to use change_column:
class ChangeDataTypeForActive < ActiveRecord::Migration
def self.up
change_column :table_name, :active, :integer
end
def self.down
change_column :table_name, :active, :boolean
end
end
Then run the migration:
rake db:migrate
The reason to define up and down methods is that you can return you database to the previos state with:
rake db:rollback