Ruby on Rails: How can I return a migration using rake db: migrate?

After installing the MODEL User utility, 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: a user table migration will be created.

How can I cancel this migration, i.e. How can I delete the user table using rake again?

+78
ruby-on-rails migration database-migration rake
Oct 08 2018-11-11T00:
source share
8 answers

run

 rake db:migrate:down VERSION=<version> 

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

eg. if you want to return the migration with the file name 3846656238_create_users.rb

rake db: migrate: down VERSION = 3846656238

+127
Oct 08 2018-11-11T00:
source share

Just run this command:

 rake db:rollback 
+106
Oct 08 2018-11-11T00:
source share

I believe that there are three options for returning migration (they also overlap):

  • Reset the most recent migration:

    rake db:migrate:down # Rails 2 only.

  • Reset the number (n) of recent migrations:

    rake db:rollback STEP=n

  • Go to the previous, specific version:

    $ rake db:migrate:down VERSION=nnn # Rails 3 (also indicate the version number).

The version number means SHA (Secure Hash Algorithm) for commit, which is a long hexadecimal number that looks something like this: 886af3194768917c78e ... You can see this by doing git log

You can see these commands (and others) with their descriptions using rake -T db: which for rails 3.2 includes:

 rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false) rake db:migrate:status # Display status of migrations rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n) 
+55
Oct 08 2018-11-11T00:
source share

You can roll back and specify how many recent migrations will be canceled, for example.

 rake db:rollback STEP=3 

for the last three migrations.

+12
08 Oct '11 at 8:27
source share

As a new programmer (or other new programmers)

rake db:rollback runs about half the time. I start there.

If not, rake db:migrate:down VERSION=3846656238

connect VERSION for the version number of your migration file that you want to return.

+8
Feb 10 '15 at 0:06
source share
 rake db:migrate:redo 

Undo and reapply the last migration.

+7
Jun 06 '14 at 19:38
source share

For rails 5 we can use rails command instead of rake

 rails db:migrate:down VERSION=<version> 

Example

rails db: migrate: down VERSION = 20170330090327

+1
Mar 31 '17 at 5:55
source share

Run this command in terminal:

 rake db:migrate:status 

or

 bundle exec rake db:migrate:status 

Shows the status, migration identifiers, the name of the migration for all previously transferred movements. select your migration id (i.e. version number) and put it in the next command after version = ,, and press enter

 bundle exec rake db:migrate:down VERSION= 
0
Aug 09 '16 at 10:39 on
source share



All Articles