Run a single migration file

后端 未结 11 751
说谎
说谎 2020-11-30 16:01

Is there an easy way to run a single migration? I don\'t want to migrate to a certain version I just want to run a specific one.

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 16:47

    If you want to run a specific migration, do

    $ rake db:migrate:up VERSION=20080906120000
    

    If you want to run migrations multiple times, do

    # use the STEP parameter if you need to go more than one version back
    $ rake db:migrate:redo STEP=3
    

    If you want to run a single migration multiple times, do

    # this is super useful
    $ rake db:migrate:redo VERSION=20080906120000
    

    (you can find the version number in the filename of your migration)


    Edit: You can also simply rename your migration file, Eg:

    20151013131830_my_migration.rb -> 20151013131831_my_migration.rb

    Then migrate normally, this will treat the migration as a new one (usefull if you want to migrate on a remote environment (such as staging) on which you have less control.

    Edit 2: You can also just nuke the migration entry in the database. Eg:

    rails_c> q = "delete from schema_migrations where version = '20151013131830'"
    rails_c> ActiveRecord::Base.connection.execute(q)
    

    rake db:migrate will then rerun the up method of the nuked migrations.

提交回复
热议问题