I have the migration file db\migrate\20100905201547_create_blocks.rb.
How can I specifically rollback that migration file?
rake db:rollback STEP=1
Is a way to do this, if the migration you want to rollback is the last one applied. You can substitute 1 for however many migrations you want to go back.
For example:
rake db:rollback STEP=5
Will also rollback all the migration that happened later (4, 3, 2 and also 1).
To roll back all migrations back to (and including) a target migration, use: (This corrected command was added after all the comments pointing out the error in the original post)
rake db:migrate VERSION=20100905201547
In order to rollback only one specific migration (out of order) use:
rake db:migrate:down VERSION=20100905201547
Note that this will NOT rollback any interceding migrations -- only the one listed. If that is not what you intended, you can safely run rake db:migrate and it will re-run only that one, skipping any others that were not previously rolled back.
And if you ever want to migrate a single migration out of order, there is also its inverse db:migrate:up:
rake db:migrate:up VERSION=20100905201547
rake db:migrate:down VERSION=20100905201547
will roll back the specific file.
To find the version of all migrations, you can use this command:
rake db:migrate:status
Or, simply the prefix of the migration's file name is the version you need to rollback.
See the Ruby on Rails guide entry on migrations.
To rollback the last migration you can do:
rake db:rollback
If you want to rollback a specific migration with a version, you should do:
rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION
For example, if the version is 20141201122027, you will do
rake db:migrate:down VERSION=20141201122027
to rollback that specific migration.
You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements.
If you want to rollback just the last migration, then you can use either
rake db:rollback
or
rake db:rollback STEP=1
If you want rollback number of migrations at once, then you simply pass an argument:
rake db:rollback STEP=n
where n is number of migrations to rollback, counting from latest migration.
If you want to rollback to a specific migration, then you should pass the version of the migration in the following:
rake db:migrate:down VERSION=xxxxx
where xxxxx is the version number of the migration.
Use:
rake db:migrate:down VERSION=your_migrations's_version_number_here
The version is the numerical prefix on the migration's file name.
How to find the version:
Your migration files are stored in your rails_root/db/migrate directory. Find the appropriate file up to which you want to rollback and copy the prefix number.
For example:
File name: 20140208031131_create_roles.rb
Then the version is 20140208031131.
Rolling back the last migration:
# rails < 5.0
rake db:rollback
# rails >= 5.0
rake db:rollback
# or
rails db:rollback
Rolling back the last n number of migrations
# rails < 5.0
rake db:rollback STEP=2
# rails >= 5.0
rake db:rollback STEP=2
# or
rails db:rollback STEP=2
Rolling back a specific migration
# rails < 5.0
rake db:migrate:down VERSION=20100905201547
# rails >= 5.0
rake db:migrate:down VERSION=20100905201547
# or
rails db:migrate:down VERSION=20100905201547
To rollback the last migration you can do:
rake db:rollback
If you want to rollback a specific migration with a version you should do:
rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION
If the migration file you want to rollback was called db/migrate/20141201122027_create_some_table.rb, then the VERSION for that migration is 20141201122027, which is the timestamp of when that migration was created, and the command to roll back that migration would be:
rake db:migrate:down VERSION=20141201122027
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: Migrate or revert only some migrations
If it is a reversible migration and the last one which has been executed, then run rake db:rollback. And you can always use the version.
For example, if the migration file is 20140716084539_create_customer_stats.rb, the rollback command will be:
rake db:migrate:down VERSION=20140716084539
If you are using Ruby on Rails 3
Step: 1 (check the last migration)
bundle exec rake db:migrate:status
Step: 2 (roll back the last migration)
bundle exec rake db:rollback
Now, you can revert the migration with safety one by one.
For a specific migration
rails d migration <migration_name>
For reverting multiple migrations
bundle exec rake db:rollback STEP=n
where n is how many migrations you want to rollback.
Example: bundle exec rake db:rollback STEP=5
A migration file looks like this,
20221213051020_my_migrations
In this case, the model name should be MyMigration. The migration ends with a plural word, so it ends with migrations.
To roll back this particular migration, you have to understand that the first part of the migration name (number in front of the migration name) is the migration number.
To roll back this migration, just open the terminal and write,
rake db:migrate:down VERSION=migration_number
So finally, you have to actually type in the terminal to roll back this particular migration,
Write the below command on terminal to rollback a particular migration, upper command is just to explain you
rake db:migrate:down VERSION=20221213051020
Just remember that each migration has a different migration number, so watch carefully and copy paste or type manually.
Migrations change the state of the database using the command
bundle exec rake db:migrate
We can undo a single migration step using
bundle exec rake db:rollback
To go all the way back to the beginning, we can use
bundle exec rake db:migrate VERSION=0
As you might guess, substituting any other number for 0 migrates to that version number, where the version numbers come from listing the migrations sequentially.
Well, in rails 5 it's quite easy
rake db:migrate:status
or
rails db:migrate:status
It was modified to handle both the same way.
Then just pick which version you want to roll back
and then run
rake db:migrate VERSION=2013424230423
Make sure VERSION is all capital letters.
If you have a problem with any step of the migration or stuck in the middle simply, go to the migration file and comment out the lines that were already migrated.
If you want to rollback and migrate you can run:
rake db:migrate:redo
That's the same as:
rake db:rollback
rake db:migrate
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.
For a 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 roll back the last migration. You can make usage of the STEPS attribute here, as usual.
rails db:migrate:down:primary VERSION=your_migration_timestamp, to revert only the provided migration version. Here primary is the name of the database too.
In addition:
When a migration you deployed long ago does not let you migrate a new one.
I work in a larger Ruby on Rails application 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, and 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 database. The only solution that worked for me is:
I scped the file to the server
I opened the rails console
I required the file in the IRB session
then AddNewMyNewFieldToAccounts.new.down
And then I could run the deploy build again.
You can run down migration command to rollback the migration like below:
rake db:migrate:down VERSION=20100905201547
If you want to revert from the last migration, use the rake db:rollback command. It's working fine for me!
Related
I have recently in my project needed to erase my databases and using rails g migrate DropTable. This has worked in making me deleting the schema, but now when I run rails db:migrate the schema does not repopulate with the db/migrate information. I am kind of stuck here. How should I go about filling up my schema.
Try to the following rake or rails, make sure your file exists under the db/migrate then take TIMESTAMP number from file name then run like below
rake db:migrate:up VERSION=20171212082249 #=> 20171212082249 that mean TIMESTAMP number from file name under the db/migrate
or
rake db:migrate:redo VERSION=20171212082249 # that will run the down and then the up step
You can see this article for more explanation.
Additional
rake db:rollback STEP=1
This will roll back lost one migration, if you need to rollback specific one from many then try this
rake db:migrate:down VERSION=TIMESTAMP #=> TIMESTAMP mean 20171212082249 this number from your migrated file name
ok so I've done a rake db:migrate but I need to add another attribute. So I was thinking I'll just add it to the migration file and then rake db:migrate again. However I noticed that it just takes me back to the command line. If I submit my form it throws an error unknown attribute. Am I missing something here?
When you ran rake db:migrate first time, the table was created in the database because of which the migration file that you ran has a status up now. You can check the migration status by firing rake db:migrate:status command.
Now, after adding the new field to the same migration, when you run the rake db:migrate, your migration file would not be picked up by the rake task. Only the migrations with status down are executed with rake db:migrate command.
What you need to do here is,
Option 1: Rollback the changes
If you don't have any important data in your migrated table and it's ok to drop the table and recreate it then you can just rollback the particular migration. rollback the changes from the db by running:
rake db:rollback version = version_number
where replace version_number with the prefix of your migration file. For example, if you are trying to rollback changes of file 20140125190622_create_users.rb then run
rake db:rollback version = 20140125190622
After this, add the new field in your migration file and run rake db:migrate.
Option 2: Incremental Migration
Create a new migration to add the new field in the table. Run the following command:
rails generate migration AddFieldNameToTableName field_name:data_type
For example: To add username field in users table
rails generate migration AddUsernameToUsers username:string
after this run rake db:migrate and the new field would be added to your table.
I'm writing my first Rails app. I've run a few rails generate model ... and rake db:migrate commands, but I now want to change my data model and so need to undo a few migrations.
The docs say I can undo a migration with rake db:rollback, but this isn't working. When I run this in the console the computer thinks for a few seconds but doesn't make any changes to db/migrate/ or db/migrate/schema.rb. No output is printed to the console.
Is this behavior correct? Shouldn't db:rollback be changing my schema? If so, can anyone think why it might not be working?
I'm on Rails v. 3.2.6.
EDIT
At the moment rake db:migrate:status gives
database: db/development.sqlite3
Status Migration ID Migration Name
--------------------------------------------------
up 20120617191211 Create irs
up 20120701154357 Create paths
up 20120701154421 Create nodes
up 20120702151447 ********** NO FILE **********
down 20120702155140 Create venues
down 20120703233833 Remove path from venues
Solution (see my comment): run
rake db:migrate:status
and correct problems you find there. In this case (per #MarkThomas' followup), you might want to check all files you need are in place.
This is what worked for me. Combine the steps given in this answer and comment by dB.
run rake db:migrate:status
If you have a ****NO FILE**** entry, just note the version number as noFileVersion. Note the version of the entry just above no file entry(stable_version).
created a "dummy" file with name noFileVersion_create_nothing.rb, and body
class CreateNothing < ActiveRecord::Migration[6.0]
def change
end
end
run rake db:migrate VERSION=stable_version
remove the noFileVersion_create_nothing.rb manually.
run rake db:migrate.
run rake db:migrate:status again to check if No file entry has disappeared.
The following worked for me:
rake db:migrate:down VERSION=20190304092208
Version number can be obtained by the following command:
rake db:migrate:status
This was the last version number to rollback one last migration
NOTE: Use the suggestions with caution, it is very dangerous for non-dev environments.
If
rake db:migrate:status
gives you a migration that says
up 20120702151447 ********** NO FILE **********
Then the best thing to do would be to do a (note that the following command will drop the database):
rake db:reset
to redo all migrations. If the last migration is the one missing, then schema.rb will have the last migration that rake db:migrate will look for:
ActiveRecord::Schema.define(:version => 20120702151447) do
Change that number to the last one in your migrate folder.
After adding migration files in the db/migrate folder and running rake db:migrate, I want get back to the previous step, I think using VERSION=n is the right way to do that, but I don't know the correct value of n to use. Is there any command to check the current n value?
It would be great if anyone could provide full instructions on how to use rake db:migrate.
For starters
rake db:rollback will get you back one step
then
rake db:rollback STEP=n
Will roll you back n migrations where n is the number of recent migrations you want to rollback.
More references here.
Roll back the most recent migration:
rake db:rollback
Roll back the n most recent migrations:
rake db:rollback STEP=n
You can find full instructions on the use of Rails migration tasks for rake on the Rails Guide for running migrations.
Here's some more:
rake db:migrate - Run all migrations that haven't been run already
rake db:migrate VERSION=20080906120000 - Run all necessary migrations (up or down) to get to the given version
rake db:migrate RAILS_ENV=test - Run migrations in the given environment
rake db:migrate:redo - Roll back one migration and run it again
rake db:migrate:redo STEP=n - Roll back the last n migrations and run them again
rake db:migrate:up VERSION=20080906120000 - Run the up method for the given migration
rake db:migrate:down VERSION=20080906120000 - Run the down method for the given migration
And to answer your question about where you get a migration's version number from:
The version is the numerical prefix on the migration's filename. For
example, to migrate to version 20080906120000 run
$ rake db:migrate VERSION=20080906120000
(From Running Migrations in the Rails Guides)
Best way is running Particular migration again by using down or up(in rails 4. It's change)
rails db:migrate:up VERSION=timestamp
Now how you get the timestamp.
Go to this path
/db/migrate
Identify migration file you want to revert.pick the timestamp from that file name.
If the version is 20150616132425, then use:
rails db:migrate:down VERSION=20150616132425
Other people have already answered you how to rollback, but you also asked how you could identify the version number of a migration.
rake db:migrate:status gives a list of your migrations version, name and status (up or down)
Your can also find the migration file, which contain a timestamp in the filename, that is the version number. Migrations are located in folder: /db/migrate
try {
$result=DB::table('users')->whereExists(function ($Query){
$Query->where('id','<','14162756');
$Query->whereBetween('password',[14162756,48384486]);
$Query->whereIn('id',[3,8,12]);
});
}catch (\Exception $error){
Log::error($error);
DB::rollBack(1);
return redirect()->route('bye');
}
If I have two migrations, mig1 and mig2, I run rake db:migrate, then I go back to mig1 and change the default value of a column, will this change be reflected when I run rake db:migrate again? Or do I have to make a new migration just for that column to make the change?
You can redo a given VERSION by running the following:
rake db:migrate:down VERSION=___________
rake db:migrate:up VERSION=____________
You should either make a new migration or use the rake db:rollback task to move back to the version of your database before the migration in question was ran. Changes to migration scripts won't be automatically picked up.
The current version of your schema is tracked and applied to migrations, so running rake db:migrate will not rerun old migrations. It is for this reason that you are able to use the rollback feature, as long as you provided correct self.down methods on your migration. Rolling back executes these down methods, undoing the migrations as it goes.
You can then edit the migration and re-migrate.
rake db:migrate:redo VERSION=____