Run migrations from rails console
In the console:
ActiveRecord::Migration.remove_column :table_name, :column_name
To update your schema.rb
file after running migrations from the console, you must run rails db:migrate
Rails <= 4
This will allow you to migrate without reloading the whole rails environment:
ActiveRecord::Migrator.migrate "db/migrate"
and rollback:
# 3 is the number of migration to rollback, optional, defaults to 1
ActiveRecord::Migrator.rollback "db/migrate", 3
Rails >= 5 (thanks to @gssbzn, his answer is below)
Migrate :
ActiveRecord::MigrationContext.new("db/migrate").migrate
And rollback :
# 3 is the number of migration to rollback, optional, defaults to 1
ActiveRecord::MigrationContext.new("db/migrate").rollback 3
Another way that I find neater to just run some migration command from console is this:
ActiveRecord::Schema.define do
create_table :foo do |t|
t.string :bar
t.timestamps
end
end
This has the advantage that the contents inside the block is compatible with just copy and pasting random contents from a real migration file / schema.rb
.
For rails 5.2 the accepted answer has been removed and replaced with
ActiveRecord::MigrationContext.new("db/migrate").migrate
Please be aware as this may also change for future versions of rails as they work to add multiple database connections