How we can remove column from rails console? Just simple run:
ActiveRecord::Migration.remove_column :table_name, :column_name
If you already have a migration file such as db/migrate/201807081703000_remove_name.rb
, you can do this:
>> require "db/migrate/201807081703000_remove_name.rb"
>> RemoveName.up
If you just want to do rake db:migrate, try this:
>> ActiveRecord::Migrator.migrate "db/migrate"
If you want to get all Schema Migrations from DB, you can create simple class “on the fly”:
irb(main):008:0> class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end
=> :version
irb(main):009:0> versions = SchemaMigration.all
SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations" LIMIT $1 [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<SchemaMigration version: "20180708170300">, #<SchemaMigration version: "20180708170338">, #<SchemaMigration version: "20180708172001">, #<SchemaMigration version: "20180708172212">, #<SchemaMigration version: "20180708172418">, #<SchemaMigration version: "20180708185012">, #<SchemaMigration version: "20180709084924">, #<SchemaMigration version: "20180709125051">, #<SchemaMigration version: "20180709213101">, #<SchemaMigration version: "20180709225124">, ...]>
Let’s map each version from relation:
irb(main):010:0> versions.map { |x| x.version }
SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
=> ["20180708170300", "20180708170338", "20180708172001", "20180708172212", "20180708172418", "20180708185012", "20180709084924", "20180709125051", "20180709213101", "20180709225124", "20180709225459", "20180709225643", "20180715093113", "20180715093115", "20180715110050", "20180715125007", "20180715133243", "20180719011711", "20180719015046", "20180719022850", "20180719023143", "20180720200907", "20180720220547", "20180721172835", "20180805155915", "20180805172558", "20180805172559", "20180805172560", "20180805172561", "20180805172562", "20180805172563", "20180806180420", "20180811160538", "20180811171759", "20180811173035", "20180811173056", "20180811173854", "20180812141029", "20180827172646", "20180829113050", "20180829125222", "20180829125223", "20180829125224", "20180829125225", "20180829133904", "20180902121500", "20180907083601", "20180907090321", "20180921182936", "20181005222446", "20181005225831"]
Another way, just use ActiveRecord::SchemaMigration
class:
irb(main):012:0> ActiveRecord::SchemaMigration.all
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" LIMIT $1 [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<ActiveRecord::SchemaMigration version: "20180708170300">, #<ActiveRecord::SchemaMigration version: "20180708170338">, #<ActiveRecord::SchemaMigration version: "20180708172001">, #<ActiveRecord::SchemaMigration version: "20180708172212">, #<ActiveRecord::SchemaMigration version: "20180708172418">, #<ActiveRecord::SchemaMigration version: "20180708185012">, #<ActiveRecord::SchemaMigration version: "20180709084924">, #<ActiveRecord::SchemaMigration version: "20180709125051">, #<ActiveRecord::SchemaMigration version: "20180709213101">, #<ActiveRecord::SchemaMigration version: "20180709225124">, ...]>
Let’s get current version of schema migration:
irb(main):007:0> ActiveRecord::Migrator.current_version
(1.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
=> 20181005225831