Run a single migration file

后端 未结 11 749
说谎
说谎 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:51

    You can just run the code directly out of the ruby file:

    rails console
    >> require "db/migrate/20090408054532_add_foos.rb"
    >> AddFoos.new.up
    

    Note: Very old versions of rails may require AddFoos.up rather than AddFoos.new.up.

    An alternative way (without IRB) which relies on the fact that require returns an array of class names:

    script/runner 'require("db/migrate/20090408054532_add_foos.rb").first.constantize.up'
    

    Note that if you do this, it won't update the schema_migrations table, but it seems like that's what you want anyway.

    Additionally, if it can't find the file you may need to use require("./db/..." or try require_relative depending on your working directory

提交回复
热议问题