LoginSignup
18

More than 5 years have passed since last update.

posted at

updated at

Organization

railsで特定ファイルのmigrationを実行したいとき

rails db:migrateを使って特定のファイルのmigrationだけ実行する方法を探していたところ、railsコマンドを使うのではなくコンソールからやってしまう方法を見つけたのでメモ

$ rails console
require Rails.root + 'db/migrate/20150508000000_add_some_index.rb'
AddSomeIndex.new.migrate(:up)

特定のファイルのmigrationを取り消す場合は、下記のようにする。

AddSomeIndex.new.migrate(:down)

ちなみに前提としているmigrationファイルは例えばこんな感じ

migration_sample.rb
class AddSomeIndex < ActiveRecord::Migration
  def change
    add_index :some_models, :hoge_id, :unique => true
    add_index :some_models, :hoge_flag
  end
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
18