your programing

특정 마이그레이션을 롤백하는 방법은 무엇입니까?

lovepro 2020. 9. 29. 08:16
반응형

특정 마이그레이션을 롤백하는 방법은 무엇입니까?


다음 마이그레이션 파일이 있습니다. db\migrate\20100905201547_create_blocks.rb

마이그레이션 파일을 구체적으로 롤백하려면 어떻게해야합니까?


rake db:rollback STEP=1

롤백하려는 마이그레이션이 마지막으로 적용된 경우이를 수행하는 방법입니다. 되돌리려는 마이그레이션 수를 1로 대체 할 수 있습니다.

예를 들면 :

rake db:rollback STEP=5

또한 나중에 발생한 모든 마이그레이션 (4, 3, 2 및 1)도 롤백합니다.

모든 마이그레이션을 대상 마이그레이션 (포함)으로 롤백하려면 다음을 사용하십시오. (이 수정 된 명령은 원본 게시물에서 오류를 지적한 모든 주석 후에 추가되었습니다)

rake db:migrate VERSION=20100905201547

하나의 특정 마이그레이션 만 롤백하려면 (무 순서) 다음을 사용하십시오.

rake db:migrate:down VERSION=20100905201547

이렇게하면 중간 마이그레이션이 롤백되지 않으며 나열된 마이그레이션 만 롤백됩니다. 이것이 의도 한 것이 아닌 경우 안전하게 실행할 수 rake db:migrate있으며 이전에 롤백되지 않은 다른 항목은 건너 뛰고 해당 항목 만 다시 실행합니다.


rake db:migrate:down VERSION=20100905201547

특정 파일을 롤백합니다.


모든 마이그레이션의 버전을 찾으려면 다음 명령을 사용할 수 있습니다.

rake db:migrate:status

또는 단순히 마이그레이션 파일 이름의 접두사가 롤백해야하는 버전입니다.


마이그레이션 에 대한 Ruby on Rails 가이드 항목참조하세요 .


마지막 마이그레이션을 롤백하려면 다음을 수행하십시오.

rake db:rollback

특정 마이그레이션을 특정 버전으로 롤백하려면 다음을 수행해야합니다.

rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION

예를 들어 버전이 20141201122027이면 다음을 수행합니다.

rake db:migrate:down VERSION=20141201122027

특정 마이그레이션을 롤백합니다.


rake db:rollback다른 옵션 을 사용하여 마이그레이션을 롤백 할 수 있습니다. 구문은 요구 사항에 따라 다릅니다.

마지막 마이그레이션 만 롤백하려는 경우 다음 중 하나를 사용할 수 있습니다.

rake db:rollback

또는

rake db:rollback STEP=1

한 번에 여러 마이그레이션을 롤백하려면 인수를 전달하면됩니다.

rake db:rollback STEP=n

여기서 n롤백 할 마이그레이션 수는 최신 마이그레이션에서 계산됩니다.

특정 마이그레이션으로 롤백하려면 다음에서 마이그레이션 버전을 전달해야합니다.

rake db:migrate:down VERSION=xxxxx

여기서 xxxxx는 마이그레이션의 버전 번호입니다.


rake db:migrate:down VERSION=your_migrations's_version_number_here

버전은 마이그레이션 파일 이름의 숫자 접두사입니다.

버전을 찾는 방법 :

마이그레이션 파일은 rails_root/db/migrate디렉토리에 저장됩니다 . 롤백 할 적절한 파일을 찾아 접두사 번호를 복사합니다.

예를 들면

file name: 20140208031131_create_roles.rb then the version is 20140208031131


Rolling back last migration:

# rails < 5.0
rake db:rollback

# rails >= 5.0
rake db:rollback
# or
rails db:rollback

Rolling back 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

If it is a reversible migration and the last one which has been executed, then run rake db:rollback. And you can always use version. e.g

the migration file is 20140716084539_create_customer_stats.rb,so the rollback command will be, rake db:migrate:down VERSION=20140716084539


From Rails Guide

Reverting Previous Migrations

You can use Active Record's ability to rollback migrations using the revert method:

require_relative '20100905201547_create_blocks'

class FixupCreateBlock < ActiveRecord::Migration
  def change
    revert CreateBlock

    create_table(:apples) do |t|
      t.string :variety
    end
  end
end

The revert method also accepts a block of instructions to reverse. This could be useful to revert selected parts of previous migrations. For example, let's imagine that CreateBlock is committed and it is later decided it would be best to use Active Record validations, in place of the CHECK constraint, to verify the zipcode.

    class DontUseConstraintForZipcodeValidationMigration < ActiveRecord::Migration
      def change
        revert do
          # copy-pasted code from CreateBlock
          reversible do |dir|
            dir.up do
              # add a CHECK constraint
              execute <<-SQL
                ALTER TABLE distributors
                  ADD CONSTRAINT zipchk
                    CHECK (char_length(zipcode) = 5);
              SQL
            end
            dir.down do
              execute <<-SQL
                ALTER TABLE distributors
                  DROP CONSTRAINT zipchk
              SQL
            end
          end

          # The rest of the migration was ok
        end
      end
    end

The same migration could also have been written without using revert but this would have involved a few more steps: reversing the order of create_table and reversible, replacing create_table by drop_table, and finally replacing up by down and vice-versa. This is all taken care of by revert.


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.

Hope that helps


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


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

참고URL : https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration

반응형