Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How can I revert a migration with rake db:migrate?

After installing devise MODEL User i got this.

class DeviseCreateUsers < ActiveRecord::Migration   def self.up     create_table(:users) do |t|       t.database_authenticatable :null => false       t.recoverable       t.rememberable       t.trackable        # t.encryptable       # t.confirmable       # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both       # t.token_authenticatable         t.timestamps     end      add_index :users, :email,                :unique => true     add_index :users, :reset_password_token, :unique => true     # add_index :users, :confirmation_token,   :unique => true     # add_index :users, :unlock_token,         :unique => true     # add_index :users, :authentication_token, :unique => true   end    def self.down     drop_table :users   end end 

Now if i do rake db:migrate the users table will be created.

How can i revert this migration, i.e. how can I delete the users table using rake again ?

like image 389
shibly Avatar asked Oct 08 '11 02:10

shibly


People also ask

How do I revert a database migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How do I undo a rake database?

just use rake db:reset , that will drop your database (same as undoing all migrations) and reset to the last schema.


1 Answers

Run the following command

rake db:migrate:down VERSION=<version> 

where <version> is the version number of your migration file you want to revert.

eg. if you want to revert a migration with file name 3846656238_create_users.rb

rake db:migrate:down VERSION=3846656238

like image 110
Mahesh Avatar answered Sep 28 '22 10:09

Mahesh