Ruby-on-rails 运行单个迁移文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/753919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:08:30  来源:igfitidea点击:

Run a single migration file

ruby-on-railsrubymigration

提问by nan

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.

有没有一种简单的方法来运行单个迁移?我不想迁移到某个版本,我只想运行一个特定的版本。

采纳答案by Orion Edwards

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

您可以直接从 ruby​​ 文件中运行代码:

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

Note: newer versions of rails may require AddFoos.new.uprather than AddFoos.up.

注意:较新版本的 rails 可能需要AddFoos.new.up而不是AddFoos.up.

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

另一种方法(没有 IRB)依赖于 require 返回类名数组的事实:

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

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

请注意,如果您这样做,它可能不会更新schema_migrations表,但无论如何这似乎是您想要的。

回答by gtd

Assuming fairly recent version of Rails you can always run:

假设您始终可以运行最新版本的 Rails:

rake db:migrate:up VERSION=20090408054532

Where version is the timestamp in the filename of the migration.

其中 version 是迁移文件名中的时间戳。

Edit: At some point over the last 8 years (I'm not sure what version) Rails added checks that prevent this from running if it has already been run. This is indicated by an entry in the schema_migrationstable. To re-run it, simply execute rake db:migrate:redo VERSION=20090408054532instead.

编辑:在过去 8 年的某个时间点(我不确定是哪个版本)Rails 添加了检查,如果它已经运行,则阻止它运行。这由表中的条目指示schema_migrations。要重新运行它,只需执行即可rake db:migrate:redo VERSION=20090408054532

回答by Benjamin Crouzier

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 singlemigration multipletimes, 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

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:

编辑 2:您也可以直接删除数据库中的迁移条目。例如:

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

rake db:migratewill then rerun the upmethod of the nuked migrations.

rake db:migrate然后将重新运行upnuked 迁移的方法。

回答by chibicode

If you've implemented a changemethod like this:

如果你已经实现了change这样的方法:

class AddPartNumberToProducts < ActiveRecord::Migration
  def change
    add_column :products, :part_number, :string
  end
end

You can create an instance of the migration and run migrate(:up)or migrate(:down)on an instance, like this:

您可以创建迁移实例并在实例上运行migrate(:up)或运行migrate(:down),如下所示:

$ rails console
>> require "db/migrate/20090408054532_add_part_number_to_products.rb"
>> AddPartNumberToProducts.new.migrate(:down)

回答by rolph dzounga

This are the steps to run again this migration file "20150927161307_create_users.rb"

这是再次运行此迁移文件“20150927161307_create_users.rb”的步骤

  1. Run the console mode. (rails c)
  2. Copy and past the class which is in that file to the console.

    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          t.string :name
          t.string :email
          t.timestamps null: false   end
        end
      end
    end
    
  3. Create an instance of the class CreateUsers: c1 = CreateUsers.new

  4. Execute the method changeof that instance: c1.change
  1. 运行控制台模式。(导轨 c)
  2. 将该文件中的类复制并粘贴到控制台。

    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          t.string :name
          t.string :email
          t.timestamps null: false   end
        end
      end
    end
    
  3. 创建一个类的实例CreateUsersc1 = CreateUsers.new

  4. 执行该change实例的方法:c1.change

回答by Deepak Mahakale

As of rails 5you can also use railsinstead of rake

至于rails 5你也可以使用rails代替rake

Rails 3 - 4

导轨 3 - 4

# < rails-5.0
rake db:migrate:up VERSION=20160920130051

Rails 5

导轨 5

# >= rails-5.0
rake db:migrate:up VERSION=20160920130051

# or

rails db:migrate:up VERSION=20160920130051

回答by Dejan Cancarevic

If you're having trouble with paths you can use

如果您遇到路径问题,可以使用

require Rails.root + 'db/migrate/20090408054532_add_foos.rb'

回答by ramya

Method 1 :

方法一:

rake db:migrate:up VERSION=20080906120000

Method 2:

方法二:

In Rails Console 1. Copy paste the migration class in console (say add_name_to_user.rb) 2. Then in console, type the following

在 Rails 控制台中 1. 在控制台中复制粘贴迁移类(比如 add_name_to_user.rb) 2. 然后在控制台中,键入以下内容

Sharding.run_on_all_shards{AddNameToUser.up}

It is done!!

完成了!!

回答by viniciusnz

Please notice that instead of script/runner, you may have to use rails runneron new rails environments.

请注意script/runner,您可能必须rails runner在新的 rails 环境中使用,而不是。

回答by Tasos Anesiadis

If you want to run it from console, this is what you are looking for:

如果你想从控制台运行它,这就是你要找的:

$ rails console
irb(main)> require "#{Rails.root.to_s}/db/migrate/XXXXX_my_migration.rb"
irb(main)> AddFoo.migrate(:up)

I tried the other answers, but requiring without Rails.rootdidnt work for me.

我尝试了其他答案,但要求Rails.root没有对我有用。

Also, .migrate(:up)part forces the migration to rerun regardless if it has already run or not. This is useful for when you already ran a migration, have kinda undone it by messing around with the db and want a quick solution to have it up again.

此外,.migrate(:up)无论是否已经运行,part 都会强制迁移重新运行。当您已经运行迁移时,这很有用,通过弄乱数据库来撤消它并希望快速解决方案重新启动它。