Ruby on Rails: How can I revert a migration with rake db:migrate?
Solution 1
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
Solution 2
Just run this command:
rake db:rollback
Solution 3
I believe there are three options available for reverting migrations (they also overlap):
Roll down the most recent migration:
rake db:migrate:down
# Rails 2 only.Roll down a number(n) of recent migrations:
rake db:rollback STEP=n
Roll down to a previous, specific version:
$ rake db:migrate:down VERSION=nnn
# Rails 3 (provide version number also).
Version Number means the SHA(Secure Hash Algorithm) for the commit which is a long hexadecimal number which looks something like 886af3194768917c78e... You can see it by doing git log
You can see these commands (and others) with their descriptions by 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)
Solution 4
You can do rollback and specify how many last migrations will be rollbacked, e.g.
rake db:rollback STEP=3
for 3 last migrations.
Solution 5
As an new programmer (or to other new programmers)
rake db:rollback
works about half the time. I start there.
If not, rake db:migrate:down VERSION=3846656238
plug in VERSION for the version number of your migration file you want to revert.
shibly
Updated on August 02, 2020Comments
-
shibly almost 4 years
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 ?
-
Ajedi32 almost 11 yearsRoll down to a specific version:
rake db:migrate VERSION=<version number>
-
martin almost 11 yearsAt least for rails 3.0.20, the first command is wrong. A single
rake db:migrate:down
aborts with the error message "VERSION is required". The recommendedrake db:rollback
however works. -
Michael Durrant almost 10 yearsAs detailed in the answer, Rails 2 ONLY for the first commmand.
-
Kostas Rousis almost 10 yearsEnvironment variables are case sensitive so it should be
STEP
andVERSION
-
tf.rz about 8 yearsI'm getting an
UnknownMigrationVersionError
but I figured out it's because my migrations are insidedb/migrate/main
, does anybody know a workaround for this to havedb:migrate:down
look inside that specific directory or the migrate subdirectories? -
johnml almost 8 yearsTo clarify,
$ rake db:migrate:down VERSION=nnn
doesn't roll down to a version, it migrates down the version specified. -
Pre-alpha about 7 yearsthis is a more quicker and easier way, instead of looking up version numbers if you want to undo the last few migrations
-
morhook over 5 yearsThis is just for rails 3 onwards. My life is in rails 2. So sad
-
Mahesh about 5 years@morhook This works for rails 3 also. Check the docs here guides.rubyonrails.org/v3.2/migrations.html
-
morhook about 5 yearsYou are right! It works for both rails 2 and rails 3. Thanks @Mahesh for your input!