I have the following migration file db\migrate\20100905201547_create_blocks.rb
How can I specifically rollback that migration file?
I have the following migration file db\migrate\20100905201547_create_blocks.rb
How can I specifically rollback that migration file?
To roll back all migrations up to a particular version (e.g. 20181002222222), use:
rake db:migrate VERSION=20181002222222
(Note that this uses db:migrate -- not db:migrate:down as in other answers to this question.)
Assuming the specified migration version is older than the current version, this will roll back all migrations up to, but not including, the specified version.
For example, if rake db:migrate:status initially displays:
(... some older migrations ...)
up 20181001002039 Some migration description
up 20181002222222 Some migration description
up 20181003171932 Some migration description
up 20181004211151 Some migration description
up 20181005151403 Some migration description
Running:
rake db:migrate VERSION=20181002222222
Will result in:
(... some older migrations ...)
up 20181001002039 Some migration description
up 20181002222222 Some migration description
down 20181003171932 Some migration description
down 20181004211151 Some migration description
down 20181005151403 Some migration description
Reference: https://makandracards.com/makandra/845-migrate-or-revert-only-some-migrations
Step: 1 (Check the last migration)
bundle exec rake db:migrate:status
Step: 2 (Rollback Last Migration)
bundle exec rake db:rollback
Now, you can revert migration with safety One by One.
rails d migration <migration_name>
bundle exec rake db:rollback STEP=n
n = how many migrations do you want to rollback
Example: bundle exec rake db:rollback STEP=5
i found these steps most useful.
To check for status, run rails db:migrate:status. Then you'll have a good view of the migrations you want to remove.
Then, run rails db:rollback to revert the changes one by one. After doing so, you can check the status again to be fully confident.
Next, if you want to remove or delete. Run rails d migration <migration_name>. This would clean up the versions you created.
After that's done, you can proceed to making new changes.
In Addition
What happened is, I work in a larger Rails app with more than a thousand of migration files. And, it takes a month for us to ship a medium-sized feature. I was working on a feature and I had deployed a migration a month ago then in the review process the structure of migration and filename changed, now I try to deploy my new code, the build failed saying
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "my_new_field" of relation "accounts" already exists
none of the above-mentioned solutions worked for me because the old migration file was missing and the field I intended to create in my new migration file already existed in the DB. The only solution that worked for me is:
scped the file to the serverrails consoleAddNewMyNewFieldToAccounts.new.downthen I could run the deploy build again.
Hope it helps you too.
For multiple databases configurations (RoR >= v6), you must append the database name in the command, like:
rails db:rollback:primary, where primary is the name of the database in your config/databases.yml file, to rollback the last migration. You can make usage of the STEPS attribute here, as usual.rails db:rollback:primary VERSION=your_migration_timestamp, to rollback only the provided migration version. Here primary is the name of the database too.If you Want to Revert from last Migration Use rake db:rollback command It's Work Fine for Me!