17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-05-08

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
17
18
0

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
  3. You can use dark theme
What you can do with signing up
17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?