ChatGPT解决这个技术问题 Extra ChatGPT

How to rollback just one step using rake db:migrate

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.


r
rwilliams

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.


A
Ajedi32

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)


When rake db:rollback wasn't working to rollback the most recent migration, I had to use rake db:migrate VERSION= and it worked fine
H
Hemali

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.


While this doesn't answer the question, it is indeed the best way. Rolling back by steps can cause mistakes, especially when working on software in a group.
G
Grant Miller

If the version is 20150616132425, then use:

rails db:migrate:down VERSION=20150616132425

j
jollierMisbrandedKeener

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


By the way if you run the status command, you might see lines like: ********** NO FILE ********** Those migrations is not existing on your current branch if you use git.
N
Nicholas Alaeddin
  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');
    }

This question already contains multiple answers and an accepted answer. Can you explain (by editing your answer) where your answer differs from the other answers? Also know that Code-only answers are not useful in the long run.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now